VarInt_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "memory/MallocAllocator.h"
  16. #include "crypto/random/Random.h"
  17. #include "util/VarInt.h"
  18. #include "util/Assert.h"
  19. #include "util/Bits.h"
  20. #include <stdio.h>
  21. #define BUF_SZ 1024
  22. static void fidelityTest()
  23. {
  24. struct Allocator* alloc = MallocAllocator_new(1<<20);
  25. struct Random* rand = Random_new(alloc, NULL, NULL);
  26. uint64_t* buf = Allocator_malloc(alloc, BUF_SZ*8);
  27. Random_bytes(rand, (uint8_t*) buf, BUF_SZ*8);
  28. // make a mix of different size numbers
  29. for (int i = 0; i < BUF_SZ; i++) {
  30. uint64_t x = buf[i];
  31. switch (x & 3) {
  32. case 3: break;
  33. case 2: buf[i] = x >> 32; break;
  34. case 1: buf[i] = x >> 48; break;
  35. case 0: buf[i] = x >> 56; break;
  36. }
  37. }
  38. uint8_t* buf2 = Allocator_malloc(alloc, BUF_SZ*8);
  39. struct VarInt_Iter iter;
  40. VarInt_mk(&iter, buf2, BUF_SZ*8);
  41. VarInt_toEnd(&iter);
  42. for (int i = 0; i < BUF_SZ; i++) { Assert_true(!VarInt_push(&iter, buf[i])); }
  43. Assert_true(iter.ptr > buf2);
  44. for (int i = BUF_SZ - 1; i >= 0; i--) {
  45. uint64_t x = ~0;
  46. Assert_true(!VarInt_pop(&iter, &x));
  47. Assert_true(buf[i] == x);
  48. }
  49. Assert_true(iter.ptr == iter.end);
  50. Allocator_free(alloc);
  51. }
  52. static inline void test(uint8_t* bytes, int len, uint64_t num, uint8_t* buf)
  53. {
  54. struct VarInt_Iter iter = { .ptr = bytes, .end = &bytes[len], .start = bytes };
  55. uint64_t out = ~0;
  56. Assert_true(!VarInt_pop(&iter, &out));
  57. Assert_true(out == num);
  58. Assert_true(iter.ptr == iter.end);
  59. buf[0] = buf[len+1] = 0xee;
  60. struct VarInt_Iter iter2 = { .ptr = &buf[len+1], .end = &buf[len+1], .start = &buf[1] };
  61. if (len < 9) { Assert_true(VarInt_push(&iter2, 0xf000000000000000ull)); }
  62. if (len < 5) { Assert_true(VarInt_push(&iter2, 0xf0000000)); }
  63. if (len < 3) { Assert_true(VarInt_push(&iter2, 0xf000)); }
  64. Assert_true(!VarInt_push(&iter2, num));
  65. Assert_true(iter2.ptr == iter2.start);
  66. Assert_true(buf[0] == 0xee);
  67. Assert_true(buf[len+1] == 0xee);
  68. }
  69. #define TEST(bytes, num) do { \
  70. uint8_t data[sizeof(bytes)+1] = {0}; \
  71. test(bytes, sizeof(bytes)-1, num, data); \
  72. } while (0)
  73. // CHECKFILES_IGNORE expecting a ;
  74. static void simpleTest()
  75. {
  76. TEST("\x00", 0x00);
  77. TEST("\xAC", 0xAC);
  78. TEST("\xFD\xAB\xCD", 0xABCD);
  79. TEST("\xFE\xAB\xCD\xEF\x01", 0xABCDEF01);
  80. TEST("\xFF\xAB\xCD\xEF\x01\x23\x45\x67\x89", 0xABCDEF0123456789ull);
  81. }
  82. int main()
  83. {
  84. simpleTest();
  85. fidelityTest();
  86. return 0;
  87. }