summaryrefslogtreecommitdiff
path: root/tests/pb
diff options
context:
space:
mode:
authorJosh Haberman <jhaberman@gmail.com>2015-05-18 10:55:20 -0700
committerJosh Haberman <jhaberman@gmail.com>2015-06-02 15:55:45 -0700
commit919fea438a5ac5366684cfa26d2bb3d17519cb60 (patch)
tree6a2d282c3c7910263241e03f41be23c6a6cda710 /tests/pb
parent6650b3c6527c17965adf7239850857a10d56ba62 (diff)
Ported upb to C89, for greater portability.
A large part of this change contains surface-level porting, like moving variable declarations to the top of the block. However there are a few more substantial things too: - moved internal-only struct definitions to a separate file (structdefs.int.h), for greater encapsulation and ABI compatibility. - removed the UPB_UPCAST macro, since it requires access to the internal-only struct definitions. Replaced uses with calls to inline, type-safe casting functions. - removed the UPB_DEFINE_CLASS/UPB_DEFINE_STRUCT macros. Class and struct definitions are now more explicit -- you get to see the actual class/struct keywords in the source. The casting convenience functions have been moved into UPB_DECLARE_DERIVED_TYPE() and UPB_DECLARE_DERIVED_TYPE2(). - the new way that we duplicate base methods in derived types is also more convenient and requires less duplication. It is also less greppable, but hopefully that is not too big a problem. Compiler flags (-std=c89 -pedantic) should help to rigorously enforce that the code is free of C99-isms. A few functions are not available in C89 (strtoll). There are temporary, hacky solutions in place.
Diffstat (limited to 'tests/pb')
-rw-r--r--tests/pb/test_decoder.cc4
-rw-r--r--tests/pb/test_varint.c49
2 files changed, 28 insertions, 25 deletions
diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc
index 98926a6..c615a3c 100644
--- a/tests/pb/test_decoder.cc
+++ b/tests/pb/test_decoder.cc
@@ -111,7 +111,7 @@ using std::string;
void vappendf(string* str, const char *format, va_list args) {
va_list copy;
- va_copy(copy, args);
+ __va_copy(copy, args);
int count = vsnprintf(NULL, 0, format, args);
if (count >= 0)
@@ -577,7 +577,7 @@ void run_decoder(const string& proto, const string* expected_output) {
fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash);
fprintf(stderr, "JIT on: %s\n",
global_method->is_native() ? "true" : "false");
- fprintf(stderr, "Input (len=%zu): ", proto.size());
+ fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size());
PrintBinary(proto);
fprintf(stderr, "\n");
if (expected_output) {
diff --git a/tests/pb/test_varint.c b/tests/pb/test_varint.c
index 84ff831..c80f127 100644
--- a/tests/pb/test_varint.c
+++ b/tests/pb/test_varint.c
@@ -8,25 +8,30 @@
#include "upb/pb/varint.int.h"
#include "tests/upb_test.h"
-// Test that we can round-trip from int->varint->int.
+/* 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];
+ size_t bytes;
+ upb_decoderet r;
+
memset(buf, 0xff, sizeof(buf));
- size_t bytes = upb_vencode64(num, buf);
+ bytes = upb_vencode64(num, buf);
if (num <= UINT32_MAX) {
+ uint64_t encoded = upb_vencode32(num);
char buf2[16];
+ upb_decoderet r;
+
memset(buf2, 0, sizeof(buf2));
- uint64_t encoded = upb_vencode32(num);
memcpy(&buf2, &encoded, 8);
- upb_decoderet r = decoder(buf2);
+ 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);
+ r = decoder(buf);
ASSERT(r.val == num);
ASSERT(r.p == buf + bytes);
ASSERT(upb_zzenc_64(upb_zzdec_64(num)) == num);
@@ -36,13 +41,23 @@ static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
#define TEST(bytes, expected_val) {\
size_t n = sizeof(bytes) - 1; /* for NULL */ \
char buf[UPB_PB_VARINT_MAX_LEN]; \
+ upb_decoderet r; \
memset(buf, 0xff, sizeof(buf)); \
memcpy(buf, bytes, n); \
- upb_decoderet r = decoder(buf); \
+ r = decoder(buf); \
ASSERT(r.val == expected_val); \
ASSERT(r.p == buf + n); \
}
+ uint64_t num;
+
+ char twelvebyte[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1};
+ 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);
+
TEST("\x00", 0ULL);
TEST("\x01", 1ULL);
TEST("\x81\x14", 0xa01ULL);
@@ -57,16 +72,7 @@ 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};
- 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 < UINT64_MAX; num *= 1.5) {
+ for (num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) {
test_varint_for_num(decoder, num);
}
test_varint_for_num(decoder, 0);
@@ -80,16 +86,13 @@ static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) {
return upb_vdecode_ ## decoder(p); \
} \
void test_ ## decoder() { \
- printf("Testing varint decoder: " #decoder "..."); \
- fflush(stdout); \
test_varint_decoder(&_upb_vdecode_ ## decoder); \
- printf("ok.\n"); \
} \
-TEST_VARINT_DECODER(check2_branch32);
-TEST_VARINT_DECODER(check2_branch64);
-TEST_VARINT_DECODER(check2_wright);
-TEST_VARINT_DECODER(check2_massimino);
+TEST_VARINT_DECODER(check2_branch32)
+TEST_VARINT_DECODER(check2_branch64)
+TEST_VARINT_DECODER(check2_wright)
+TEST_VARINT_DECODER(check2_massimino)
int run_tests(int argc, char *argv[]) {
UPB_UNUSED(argc);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback