1
0

NumberCompress_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #define NumberCompress_OLD_CODE
  16. #include "switch/NumberCompress.h"
  17. #include "switch/EncodingScheme.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Assert.h"
  20. #include <stdio.h>
  21. #include <inttypes.h>
  22. static void numberCompressions_generic(
  23. uint32_t nInterfaces,
  24. uint32_t (*bitsUsedForLabel)(const uint64_t label),
  25. uint32_t (*bitsUsedForNumber)(const uint32_t number),
  26. uint64_t (*getCompressed)(const uint32_t number, const uint32_t bitsUsed),
  27. uint32_t (*getDecompressed)(const uint64_t label, const uint32_t bitsUsed),
  28. struct EncodingScheme* (* defineScheme)(struct Allocator* alloc) )
  29. {
  30. uint8_t bitWidths[64] = { 0 };
  31. for (uint32_t i = 0; i < nInterfaces; ++i) {
  32. bitWidths[bitsUsedForNumber(i)] = 1;
  33. }
  34. for (uint32_t bits = 0; bits < 64; ++bits) {
  35. if (!bitWidths[bits]) {
  36. continue;
  37. }
  38. for (uint32_t i = 0; i < nInterfaces; ++i) {
  39. /* only check for greater-or-equal bit widths */
  40. if (bits < bitsUsedForNumber(i)) {
  41. continue;
  42. }
  43. uint64_t label = getCompressed(i, bits);
  44. if (1 == i) {
  45. Assert_true(1 == label);
  46. continue;
  47. }
  48. Assert_true(bits == bitsUsedForLabel(label));
  49. Assert_true(i == getDecompressed(label, bits));
  50. }
  51. }
  52. for (uint64_t label = 0; label < 0x10000u; ++label) {
  53. uint32_t bits = bitsUsedForLabel(label);
  54. Assert_true(1 == bitWidths[bits]);
  55. if (1 == (label & Bits_maxBits64(bits))) {
  56. //Assert_true(4 == bits);
  57. Assert_true(1 == getDecompressed(label, bits));
  58. } else {
  59. uint32_t i = getDecompressed(label, bits);
  60. Assert_true(i < nInterfaces);
  61. }
  62. }
  63. struct Allocator* alloc = Allocator_new(20000);
  64. struct EncodingScheme* scheme = defineScheme(alloc);
  65. for (uint32_t i = 0; i < nInterfaces; i++) {
  66. for (int j = 0; j < scheme->count; j++) {
  67. int bits = EncodingScheme_formSize(&scheme->forms[j]);
  68. if ((int)bitsUsedForNumber(i) > bits) { continue; }
  69. uint64_t label = getCompressed(i, bits);
  70. for (int k = j; k < scheme->count; k++) {
  71. uint64_t labelB = EncodingScheme_convertLabel(scheme, label, k);
  72. if (1 == i && k != 0) {
  73. Assert_true(1 == label);
  74. Assert_true(EncodingScheme_convertLabel_INVALID == labelB);
  75. continue;
  76. }
  77. int bitsB = bitsUsedForLabel(labelB);
  78. Assert_true(bitsB == scheme->forms[k].prefixLen + scheme->forms[k].bitCount
  79. || (i == 1 && bitsB == 4));
  80. //printf("%u == %u (%lu / %d)\n", i, getDecompressed(labelB, bitsB), labelB, bitsB);
  81. Assert_true(i == getDecompressed(labelB, bitsB));
  82. uint64_t labelC = EncodingScheme_convertLabel(scheme, labelB, j);
  83. Assert_true(labelC == label);
  84. }
  85. }
  86. }
  87. Allocator_free(alloc);
  88. }
  89. #define TEST(impl) \
  90. numberCompressions_generic( \
  91. GLUE(impl, INTERFACES), \
  92. GLUE(impl, bitsUsedForLabel), \
  93. GLUE(impl, bitsUsedForNumber), \
  94. GLUE(impl, getCompressed), \
  95. GLUE(impl, getDecompressed), \
  96. GLUE(impl, defineScheme) \
  97. )
  98. #define GLUE(a,b) GLUE2(a,b)
  99. #define GLUE2(a,b) NumberCompress_ ## a ## _ ## b
  100. int main()
  101. {
  102. TEST(f4);
  103. TEST(f8);
  104. TEST(v3x5x8);
  105. TEST(v4x8);
  106. return 0;
  107. }