test_varint.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdio.h>
  2. #include "upb/pb/varint.int.h"
  3. #include "tests/upb_test.h"
  4. #include "upb/port_def.inc"
  5. /* Test that we can round-trip from int->varint->int. */
  6. static void test_varint_for_num(upb_decoderet (*decoder)(const char*),
  7. uint64_t num) {
  8. char buf[16];
  9. size_t bytes;
  10. upb_decoderet r;
  11. memset(buf, 0xff, sizeof(buf));
  12. bytes = upb_vencode64(num, buf);
  13. if (num <= UINT32_MAX) {
  14. uint64_t encoded = upb_vencode32(num);
  15. char buf2[16];
  16. upb_decoderet r;
  17. memset(buf2, 0, sizeof(buf2));
  18. memcpy(&buf2, &encoded, 8);
  19. #ifdef UPB_BIG_ENDIAN
  20. char swap[8];
  21. swap[0] = buf2[7];
  22. swap[1] = buf2[6];
  23. swap[2] = buf2[5];
  24. swap[3] = buf2[4];
  25. swap[4] = buf2[3];
  26. swap[5] = buf2[2];
  27. swap[6] = buf2[1];
  28. swap[7] = buf2[0];
  29. buf2[0] = swap[0];
  30. buf2[1] = swap[1];
  31. buf2[2] = swap[2];
  32. buf2[3] = swap[3];
  33. buf2[4] = swap[4];
  34. buf2[5] = swap[5];
  35. buf2[6] = swap[6];
  36. buf2[7] = swap[7];
  37. #endif
  38. r = decoder(buf2);
  39. ASSERT(r.val == num);
  40. ASSERT(r.p == buf2 + upb_value_size(encoded));
  41. ASSERT(upb_zzenc_32(upb_zzdec_32(num)) == num);
  42. }
  43. r = decoder(buf);
  44. ASSERT(r.val == num);
  45. ASSERT(r.p == buf + bytes);
  46. ASSERT(upb_zzenc_64(upb_zzdec_64(num)) == num);
  47. }
  48. static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
  49. #define TEST(bytes, expected_val) {\
  50. size_t n = sizeof(bytes) - 1; /* for NULL */ \
  51. char buf[UPB_PB_VARINT_MAX_LEN]; \
  52. upb_decoderet r; \
  53. memset(buf, 0xff, sizeof(buf)); \
  54. memcpy(buf, bytes, n); \
  55. r = decoder(buf); \
  56. ASSERT(r.val == expected_val); \
  57. ASSERT(r.p == buf + n); \
  58. }
  59. uint64_t num;
  60. char twelvebyte[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1};
  61. const char *twelvebyte_buf = twelvebyte;
  62. /* A varint that terminates before hitting the end of the provided buffer,
  63. * but in too many bytes (11 instead of 10). */
  64. upb_decoderet r = decoder(twelvebyte_buf);
  65. ASSERT(r.p == NULL);
  66. TEST("\x00", 0ULL);
  67. TEST("\x01", 1ULL);
  68. TEST("\x81\x14", 0xa01ULL);
  69. TEST("\x81\x03", 0x181ULL);
  70. TEST("\x81\x83\x07", 0x1c181ULL);
  71. TEST("\x81\x83\x87\x0f", 0x1e1c181ULL);
  72. TEST("\x81\x83\x87\x8f\x1f", 0x1f1e1c181ULL);
  73. TEST("\x81\x83\x87\x8f\x9f\x3f", 0x1f9f1e1c181ULL);
  74. TEST("\x81\x83\x87\x8f\x9f\xbf\x7f", 0x1fdf9f1e1c181ULL);
  75. TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x01", 0x3fdf9f1e1c181ULL);
  76. TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x03", 0x303fdf9f1e1c181ULL);
  77. TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x83\x07", 0x8303fdf9f1e1c181ULL);
  78. #undef TEST
  79. for (num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) {
  80. test_varint_for_num(decoder, num);
  81. }
  82. test_varint_for_num(decoder, 0);
  83. }
  84. #define TEST_VARINT_DECODER(decoder) \
  85. /* Create non-inline versions for convenient inspection of assembly language \
  86. * output. */ \
  87. upb_decoderet _upb_vdecode_ ## decoder(const char *p) { \
  88. return upb_vdecode_ ## decoder(p); \
  89. } \
  90. void test_ ## decoder(void) { \
  91. test_varint_decoder(&_upb_vdecode_ ## decoder); \
  92. } \
  93. TEST_VARINT_DECODER(check2_branch32)
  94. TEST_VARINT_DECODER(check2_branch64)
  95. int run_tests(int argc, char *argv[]) {
  96. UPB_UNUSED(argc);
  97. UPB_UNUSED(argv);
  98. test_check2_branch32();
  99. test_check2_branch64();
  100. return 0;
  101. }