NumberCompress_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "switch/NumberCompress.h"
  16. #include "switch/EncodingScheme.h"
  17. #include "memory/MallocAllocator.h"
  18. #include "util/Assert.h"
  19. #include <stdio.h>
  20. #include <inttypes.h>
  21. static void numberCompressions_generic(
  22. uint32_t nInterfaces,
  23. uint32_t (*bitsUsedForLabel)(const uint64_t label),
  24. uint32_t (*bitsUsedForNumber)(const uint32_t number),
  25. uint64_t (*getCompressed)(const uint32_t number, const uint32_t bitsUsed),
  26. uint32_t (*getDecompressed)(const uint64_t label, const uint32_t bitsUsed),
  27. struct EncodingScheme* (* defineScheme)(struct Allocator* alloc) )
  28. {
  29. uint8_t bitWidths[64] = { 0 };
  30. for (uint32_t i = 0; i < nInterfaces; ++i) {
  31. bitWidths[bitsUsedForNumber(i)] = 1;
  32. }
  33. for (uint32_t bits = 0; bits < 64; ++bits) {
  34. if (!bitWidths[bits]) {
  35. continue;
  36. }
  37. for (uint32_t i = 0; i < nInterfaces; ++i) {
  38. /* only check for greater-or-equal bit widths */
  39. if (bits < bitsUsedForNumber(i)) {
  40. continue;
  41. }
  42. uint64_t label = getCompressed(i, bits);
  43. if (1 == i) {
  44. Assert_true(1 == label);
  45. continue;
  46. }
  47. Assert_true(bits == bitsUsedForLabel(label));
  48. Assert_true(i == getDecompressed(label, bits));
  49. }
  50. }
  51. for (uint64_t label = 0; label < 0x10000u; ++label) {
  52. uint32_t bits = bitsUsedForLabel(label);
  53. Assert_true(1 == bitWidths[bits]);
  54. if (1 == (label & Bits_maxBits64(bits))) {
  55. //Assert_true(4 == bits);
  56. Assert_true(1 == getDecompressed(label, bits));
  57. } else {
  58. uint32_t i = getDecompressed(label, bits);
  59. Assert_true(i < nInterfaces);
  60. }
  61. }
  62. struct Allocator* alloc = MallocAllocator_new(20000);
  63. struct EncodingScheme* scheme = defineScheme(alloc);
  64. for (uint32_t i = 0; i < nInterfaces; i++) {
  65. for (int j = 0; j < scheme->count; j++) {
  66. int bits = scheme->forms[j].prefixLen + scheme->forms[j].bitCount;
  67. if ((int)bitsUsedForNumber(i) > bits) { continue; }
  68. uint64_t label = getCompressed(i, bits);
  69. for (int k = j; k < scheme->count; k++) {
  70. uint64_t labelB = EncodingScheme_convertLabel(scheme, label, k);
  71. if (1 == i && k != 0) {
  72. Assert_true(1 == label);
  73. Assert_true(EncodingScheme_convertLabel_INVALID == labelB);
  74. continue;
  75. }
  76. int bitsB = bitsUsedForLabel(labelB);
  77. Assert_true(bitsB == scheme->forms[k].prefixLen + scheme->forms[k].bitCount
  78. || (i == 1 && bitsB == 4));
  79. Assert_true(i == getDecompressed(labelB, bitsB));
  80. uint64_t labelC = EncodingScheme_convertLabel(scheme, labelB, j);
  81. Assert_true(labelC == label);
  82. }
  83. }
  84. }
  85. Allocator_free(alloc);
  86. }
  87. #define TEST(impl) \
  88. numberCompressions_generic( \
  89. GLUE(impl, INTERFACES), \
  90. GLUE(impl, bitsUsedForLabel), \
  91. GLUE(impl, bitsUsedForNumber), \
  92. GLUE(impl, getCompressed), \
  93. GLUE(impl, getDecompressed), \
  94. GLUE(impl, defineScheme) \
  95. )
  96. #define GLUE(a,b) GLUE2(a,b)
  97. #define GLUE2(a,b) NumberCompress_ ## a ## _ ## b
  98. int main()
  99. {
  100. TEST(f4);
  101. TEST(f8);
  102. TEST(v3x5x8);
  103. TEST(v4x8);
  104. return 0;
  105. }