summaryrefslogtreecommitdiff
path: root/tests/test_varint.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_varint.c')
-rw-r--r--tests/test_varint.c40
1 files changed, 37 insertions, 3 deletions
diff --git a/tests/test_varint.c b/tests/test_varint.c
index 4c076b3..0fc93f0 100644
--- a/tests/test_varint.c
+++ b/tests/test_varint.c
@@ -8,12 +8,39 @@
#include "upb/pb/varint.h"
#include "upb_test.h"
+// Test that we can round-trip from int->varint->int.
+static void test_varint_for_num(upb_decoderet (*decoder)(const char*),
+ uint64_t num) {
+ char buf[16];
+ memset(buf, 0xff, sizeof(buf));
+ size_t bytes = upb_vencode64(num, buf);
+
+ if (num <= UINT32_MAX) {
+ char buf2[16];
+ memset(buf2, 0, sizeof(buf2));
+ uint64_t encoded = upb_vencode32(num);
+ memcpy(&buf2, &encoded, 8);
+ upb_decoderet r = decoder(buf2);
+ ASSERT(r.val == num);
+ ASSERT(r.p == buf2 + upb_value_size(encoded));
+ ASSERT(upb_zzenc_32(upb_zzdec_32(num)) == num);
+ }
+
+ upb_decoderet r = decoder(buf);
+ ASSERT(r.val == num);
+ ASSERT(r.p == buf + bytes);
+ ASSERT(upb_zzenc_64(upb_zzdec_64(num)) == num);
+}
+
static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
#define TEST(bytes, expected_val) {\
- const char buf[] = bytes "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" ; \
+ size_t n = sizeof(bytes) - 1; /* for NULL */ \
+ char buf[UPB_PB_VARINT_MAX_LEN]; \
+ memset(buf, 0xff, sizeof(buf)); \
+ memcpy(buf, bytes, n); \
upb_decoderet r = decoder(buf); \
ASSERT(r.val == expected_val); \
- ASSERT(r.p == buf + sizeof(buf) - 16); /* - 1 for NULL */ \
+ ASSERT(r.p == buf + n); \
}
TEST("\x00", 0ULL);
@@ -30,12 +57,19 @@ static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
TEST("\x81\x83\x87\x8f\x9f\xbf\xff\x81\x83\x07", 0x8303fdf9f1e1c181ULL);
#undef TEST
- char twelvebyte[16] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01, 0x01};
+ char twelvebyte[16] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
+ 0x80, 0x01, 0x01};
const char *twelvebyte_buf = twelvebyte;
// A varint that terminates before hitting the end of the provided buffer,
// but in too many bytes (11 instead of 10).
upb_decoderet r = decoder(twelvebyte_buf);
ASSERT(r.p == NULL);
+
+
+ for (uint64_t num = 5; num * 1.5 > num; num *= 1.5) {
+ test_varint_for_num(decoder, num);
+ }
+ test_varint_for_num(decoder, 0);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback