diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_cpp.cc | 48 | ||||
-rw-r--r-- | tests/test_decoder.cc | 213 | ||||
-rw-r--r-- | tests/test_def.c | 30 | ||||
-rw-r--r-- | tests/test_table.cc | 22 | ||||
-rw-r--r-- | tests/test_varint.c | 4 | ||||
-rw-r--r-- | tests/test_vs_proto2.cc | 22 |
6 files changed, 257 insertions, 82 deletions
diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index db2337e..2b122a3 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -10,6 +10,7 @@ #include <stdio.h> #include <string.h> #include <iostream> +#include <set> #include "upb/def.h" #include "upb/descriptor/reader.h" #include "upb/handlers.h" @@ -18,6 +19,36 @@ #include "upb_test.h" #include "upb/upb.h" +template <class T> +void AssertInsert(T* const container, const typename T::value_type& val) { + bool inserted = container->insert(val).second; + ASSERT(inserted); +} + +static void TestCasts1() { + const upb::MessageDef* md = upb::MessageDef::New(&md); + const upb::Def* def = md->Upcast(); + const upb::MessageDef* md2 = upb::down_cast<const upb::MessageDef*>(def); + const upb::MessageDef* md3 = upb::dyn_cast<const upb::MessageDef*>(def); + + ASSERT(md == md2); + ASSERT(md == md3); + + const upb::EnumDef* ed = upb::dyn_cast<const upb::EnumDef*>(def); + ASSERT(!ed); + + md->Unref(&md); +} + +static void TestCasts2() { + // Test non-const -> const cast. + upb::MessageDef* md = upb::MessageDef::New(&md); + upb::Def* def = md->Upcast(); + const upb::MessageDef* const_md = upb::down_cast<const upb::MessageDef*>(def); + ASSERT(const_md == md); + md->Unref(&md); +} + static void TestSymbolTable(const char *descriptor_file) { upb::SymbolTable *s = upb::SymbolTable::New(&s); upb::Status status; @@ -25,9 +56,22 @@ static void TestSymbolTable(const char *descriptor_file) { std::cerr << "Couldn't load descriptor: " << status.GetString(); exit(1); } - const upb::MessageDef *md = s->LookupMessage("A", &md); + const upb::MessageDef* md = s->LookupMessage("C", &md); ASSERT(md); + // We want a def that satisfies this to test iteration. + ASSERT(md->field_count() > 1); + +#ifdef UPB_CXX11 + // Test range-based for. + std::set<const upb::FieldDef*> fielddefs; + for (const upb::FieldDef* f : *md) { + AssertInsert(&fielddefs, f); + ASSERT(f->containing_type() == md); + } + ASSERT(fielddefs.size() == md->field_count()); +#endif + s->Unref(&s); md->Unref(&md); } @@ -40,6 +84,8 @@ int run_tests(int argc, char *argv[]) { return 1; } TestSymbolTable(argv[1]); + TestCasts1(); + TestCasts2(); return 0; } diff --git a/tests/test_decoder.cc b/tests/test_decoder.cc index 9d54173..9782013 100644 --- a/tests/test_decoder.cc +++ b/tests/test_decoder.cc @@ -20,6 +20,10 @@ * pointers overflow (this might be difficult). * - a few "kitchen sink" examples (one proto that uses all types, lots * of submsg/sequences, etc. + * - test different handlers at every level and whether handlers fire at + * the correct field path. + * - test skips that extend past the end of current buffer (where decoder + * returns value greater than the size param). */ #ifndef __STDC_FORMAT_MACROS @@ -34,12 +38,28 @@ #include "upb/bytestream.h" #include "upb/handlers.h" #include "upb/pb/decoder.h" -#include "upb/pb/varint.h" +#include "upb/pb/varint.int.h" #include "upb_test.h" #include "upb/upb.h" #include "third_party/upb/tests/test_decoder_schema.upb.h" +#undef PRINT_FAILURE +#define PRINT_FAILURE(expr) \ + fprintf(stderr, "Assertion failed: %s:%d\n", __FILE__, __LINE__); \ + fprintf(stderr, "expr: %s\n", #expr); \ + if (testhash) { \ + fprintf(stderr, "assertion failed running test %x. " \ + "Run with the arg %x to run only this test.\n", \ + testhash, testhash); \ + fprintf(stderr, "Failed at %02.2f%% through tests.\n", \ + (float)completed * 100 / total); \ + } + uint32_t filter_hash = 0; +double completed; +double total; +double *count; +bool count_only; // Copied from decoder.c, since this is not a public interface. typedef struct { @@ -134,13 +154,15 @@ const buffer empty; buffer cat(const buffer& a, const buffer& b, const buffer& c = empty, const buffer& d = empty, - const buffer& e = empty) { + const buffer& e = empty, + const buffer& f = empty) { buffer ret; ret.append(a); ret.append(b); ret.append(c); ret.append(d); ret.append(e); + ret.append(f); return ret; } @@ -179,7 +201,7 @@ buffer submsg(uint32_t fn, const buffer& buf) { // using the closure depth to test that the stack of closures is properly // handled. -int closures[UPB_MAX_NESTING]; +int closures[UPB_DECODER_MAX_NESTING]; buffer output; void indentbuf(buffer *buf, int depth) { @@ -363,12 +385,13 @@ void reghandlers(upb_handlers *h) { const upb::Handlers *handlers; const upb::Handlers *plan; -uint32_t Hash(const buffer& proto, const buffer* expected_output) { +uint32_t Hash(const buffer& proto, const buffer* expected_output, size_t seam1, + size_t seam2) { uint32_t hash = MurmurHash2(proto.buf(), proto.len(), 0); if (expected_output) hash = MurmurHash2(expected_output->buf(), expected_output->len(), hash); - bool hasjit = upb::pb::HasJitCode(plan); - hash = MurmurHash2(&hasjit, 1, hash); + hash = MurmurHash2(&seam1, sizeof(seam1), hash); + hash = MurmurHash2(&seam2, sizeof(seam2), hash); return hash; } @@ -380,6 +403,8 @@ bool parse( size_t parsed = s->PutStringBuffer(UPB_BYTESTREAM_BYTES_STRING, buf + start, len); if (s->pipeline()->status().ok() != (parsed >= len)) { + fprintf(stderr, "Status: %s, parsed=%zu, len=%zu\n", + s->pipeline()->status().GetString(), parsed, len); ASSERT(false); } if (!s->pipeline()->status().ok()) @@ -391,8 +416,6 @@ bool parse( #define LINE(x) x "\n" void run_decoder(const buffer& proto, const buffer* expected_output) { - testhash = Hash(proto, expected_output); - if (filter_hash && testhash != filter_hash) return; upb::Pipeline pipeline(NULL, 0, upb_realloc, NULL); upb::Sink* sink = pipeline.NewSink(handlers); upb::Sink* decoder_sink = pipeline.NewSink(plan); @@ -400,37 +423,42 @@ void run_decoder(const buffer& proto, const buffer* expected_output) { upb::pb::ResetDecoderSink(d, sink); for (size_t i = 0; i < proto.len(); i++) { for (size_t j = i; j < UPB_MIN(proto.len(), i + 5); j++) { - pipeline.Reset(); - output.clear(); - sink->Reset(&closures[0]); - size_t ofs = 0; - bool ok = - decoder_sink->StartMessage() && - decoder_sink->StartString( - UPB_BYTESTREAM_BYTES_STARTSTR, proto.len()) && - parse(decoder_sink, proto.buf(), 0, i, &ofs) && - parse(decoder_sink, proto.buf(), i, j, &ofs) && - parse(decoder_sink, proto.buf(), j, proto.len(), &ofs) && - ofs == proto.len() && - decoder_sink->EndString(UPB_BYTESTREAM_BYTES_ENDSTR); - if (ok) decoder_sink->EndMessage(); - if (expected_output) { - if (!output.eql(*expected_output)) { - fprintf(stderr, "Text mismatch: '%s' vs '%s'\n", - output.buf(), expected_output->buf()); - } - if (!ok) { - fprintf(stderr, "Failed: %s\n", pipeline.status().GetString()); - } - ASSERT(ok); - ASSERT(output.eql(*expected_output)); - } else { - if (ok) { - fprintf(stderr, "Didn't expect ok result, but got output: '%s'\n", - output.buf()); + testhash = Hash(proto, expected_output, i, j); + if (filter_hash && testhash != filter_hash) continue; + if (!count_only) { + pipeline.Reset(); + output.clear(); + sink->Reset(&closures[0]); + size_t ofs = 0; + bool ok = + decoder_sink->StartMessage() && + decoder_sink->StartString( + UPB_BYTESTREAM_BYTES_STARTSTR, proto.len()) && + parse(decoder_sink, proto.buf(), 0, i, &ofs) && + parse(decoder_sink, proto.buf(), i, j, &ofs) && + parse(decoder_sink, proto.buf(), j, proto.len(), &ofs) && + ofs == proto.len() && + decoder_sink->EndString(UPB_BYTESTREAM_BYTES_ENDSTR); + if (ok) decoder_sink->EndMessage(); + if (expected_output) { + if (!output.eql(*expected_output)) { + fprintf(stderr, "Text mismatch: '%s' vs '%s'\n", + output.buf(), expected_output->buf()); + } + if (!ok) { + fprintf(stderr, "Failed: %s\n", pipeline.status().GetString()); + } + ASSERT(ok); + ASSERT(output.eql(*expected_output)); + } else { + if (ok) { + fprintf(stderr, "Didn't expect ok result, but got output: '%s'\n", + output.buf()); + } + ASSERT(!ok); } - ASSERT(!ok); } + (*count)++; } } testhash = 0; @@ -446,7 +474,7 @@ void assert_successful_parse(const buffer& proto, va_start(args, expected_fmt); expected_text.vappendf(expected_fmt, args); va_end(args); - // The JIT is only used for data >=20 bytes from end-of-buffer, so + // To test both middle-of-buffer and end-of-buffer code paths, // repeat once with no-op padding data at the end of buffer. run_decoder(proto, &expected_text); run_decoder(cat( proto, thirty_byte_nop ), &expected_text); @@ -457,8 +485,7 @@ void assert_does_not_parse_at_eof(const buffer& proto) { } void assert_does_not_parse(const buffer& proto) { - // The JIT is only used for data >=20 bytes from end-of-buffer, so - // repeat once with no-op padding data at the end of buffer. + // Test that the error is caught both at end-of-buffer and middle-of-buffer. assert_does_not_parse_at_eof(proto); assert_does_not_parse_at_eof(cat( proto, thirty_byte_nop )); } @@ -641,10 +668,12 @@ void test_invalid() { assert_does_not_parse_at_eof( buffer("\x80") ); // EOF inside a known group. - assert_does_not_parse_at_eof( tag(4, UPB_WIRE_TYPE_START_GROUP) ); + // TODO(haberman): add group to decoder test schema. + //assert_does_not_parse_at_eof( tag(4, UPB_WIRE_TYPE_START_GROUP) ); // EOF inside an unknown group. - assert_does_not_parse_at_eof( tag(UNKNOWN_FIELD, UPB_WIRE_TYPE_START_GROUP) ); + // TODO(haberman): unknown groups not supported yet. + //assert_does_not_parse_at_eof( tag(UNKNOWN_FIELD, UPB_WIRE_TYPE_START_GROUP) ); // End group that we are not currently in. assert_does_not_parse( tag(4, UPB_WIRE_TYPE_END_GROUP) ); @@ -658,15 +687,24 @@ void test_invalid() { cat( tag(UPB_MAX_FIELDNUMBER + 1, UPB_WIRE_TYPE_DELIMITED), varint(0) )); + // Known group inside a submessage has ENDGROUP tag AFTER submessage end. + assert_does_not_parse( + cat ( submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, + tag(UPB_DESCRIPTOR_TYPE_GROUP, UPB_WIRE_TYPE_START_GROUP)), + tag(UPB_DESCRIPTOR_TYPE_GROUP, UPB_WIRE_TYPE_END_GROUP))); + // Test exceeding the resource limit of stack depth. buffer buf; - for (int i = 0; i <= UPB_MAX_NESTING; i++) { + for (int i = 0; i <= UPB_DECODER_MAX_NESTING; i++) { buf.assign(submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, buf)); } assert_does_not_parse(buf); } void test_valid() { + // Empty protobuf. + assert_successful_parse(buffer(""), "<\n>\n"); + test_valid_data_for_signed_type(UPB_DESCRIPTOR_TYPE_DOUBLE, dbl(33), dbl(-66)); @@ -698,6 +736,49 @@ void test_valid() { test_valid_data_for_type(UPB_DESCRIPTOR_TYPE_FIXED64, uint64(33), uint64(66)); test_valid_data_for_type(UPB_DESCRIPTOR_TYPE_FIXED32, uint32(33), uint32(66)); + // Unknown fields. + int int32_type = UPB_DESCRIPTOR_TYPE_INT32; + int msg_type = UPB_DESCRIPTOR_TYPE_MESSAGE; + assert_successful_parse( + cat( tag(12345, UPB_WIRE_TYPE_VARINT), varint(2345678) ), + "<\n>\n"); + assert_successful_parse( + cat( tag(12345, UPB_WIRE_TYPE_32BIT), uint32(2345678) ), + "<\n>\n"); + assert_successful_parse( + cat( tag(12345, UPB_WIRE_TYPE_64BIT), uint64(2345678) ), + "<\n>\n"); + assert_successful_parse( + submsg(12345, buffer(" ")), + "<\n>\n"); + + assert_successful_parse( + cat( + submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, + submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, + cat( tag(int32_type, UPB_WIRE_TYPE_VARINT), varint(2345678), + tag(12345, UPB_WIRE_TYPE_VARINT), varint(2345678) ))), + tag(int32_type, UPB_WIRE_TYPE_VARINT), varint(22222)), + LINE("<") + LINE("%u:{") + LINE(" <") + LINE(" %u:{") + LINE(" <") + LINE(" %u:2345678") + LINE(" >") + LINE(" }") + LINE(" >") + LINE("}") + LINE("%u:22222") + LINE(">"), msg_type, msg_type, int32_type, int32_type); + + assert_successful_parse( + cat( tag(UPB_DESCRIPTOR_TYPE_INT32, UPB_WIRE_TYPE_VARINT), varint(1), + tag(12345, UPB_WIRE_TYPE_VARINT), varint(2345678) ), + LINE("<") + LINE("%u:1") + LINE(">"), UPB_DESCRIPTOR_TYPE_INT32); + // Test implicit startseq/endseq. uint32_t repfl_fn = rep_fn(UPB_DESCRIPTOR_TYPE_FLOAT); uint32_t repdb_fn = rep_fn(UPB_DESCRIPTOR_TYPE_DOUBLE); @@ -753,7 +834,7 @@ void test_valid() { // Staying within the stack limit should work properly. buffer buf; buffer textbuf; - int total = UPB_MAX_NESTING - 1; + int total = UPB_DECODER_MAX_NESTING - 1; for (int i = 0; i < total; i++) { buf.assign(submsg(UPB_DESCRIPTOR_TYPE_MESSAGE, buf)); indentbuf(&textbuf, i); @@ -779,42 +860,62 @@ void run_tests() { test_valid(); } +void test_emptyhandlers(bool allowjit) { + // Create an empty handlers to make sure that the decoder can handle empty + // messages. + upb::Handlers* h = upb_handlers_new(UPB_TEST_DECODER_EMPTYMESSAGE, NULL, &h); + bool ok = upb::Handlers::Freeze(&h, 1, NULL); + ASSERT(ok); + const upb::Handlers* plan = upb::pb::GetDecoderHandlers(h, allowjit, &plan); + h->Unref(&h); + plan->Unref(&plan); +} + extern "C" { int run_tests(int argc, char *argv[]) { if (argc > 1) filter_hash = strtol(argv[1], NULL, 16); - for (int i = 0; i < UPB_MAX_NESTING; i++) { + for (int i = 0; i < UPB_DECODER_MAX_NESTING; i++) { closures[i] = i; } - // Create an empty handlers to make sure that the decoder can handle empty - // messages. - upb::Handlers *h = upb_handlers_new(UPB_TEST_DECODER_EMPTYMESSAGE, NULL, &h); - bool ok = upb::Handlers::Freeze(&h, 1, NULL); - ASSERT(ok); - plan = upb::pb::GetDecoderHandlers(h, true, &plan); - h->Unref(&h); - plan->Unref(&plan); - // Construct decoder plan. - h = upb::Handlers::New(UPB_TEST_DECODER_DECODERTEST, NULL, &handlers); + upb::Handlers* h = + upb::Handlers::New(UPB_TEST_DECODER_DECODERTEST, NULL, &handlers); reghandlers(h); - ok = upb::Handlers::Freeze(&h, 1, NULL); + bool ok = upb::Handlers::Freeze(&h, 1, NULL); + ASSERT(ok); handlers = h; + // Count tests. + plan = upb::pb::GetDecoderHandlers(handlers, false, &plan); + count_only = true; + count = &total; + total = 0; + run_tests(); + count_only = false; + count = &completed; + plan->Unref(&plan); + // Test without JIT. plan = upb::pb::GetDecoderHandlers(handlers, false, &plan); ASSERT(!upb::pb::HasJitCode(plan)); + completed = 0; run_tests(); plan->Unref(&plan); + test_emptyhandlers(false); + #ifdef UPB_USE_JIT_X64 // Test JIT. plan = upb::pb::GetDecoderHandlers(handlers, true, &plan); ASSERT(upb::pb::HasJitCode(plan)); + completed = 0; run_tests(); plan->Unref(&plan); + + test_emptyhandlers(true); #endif plan = NULL; diff --git a/tests/test_def.c b/tests/test_def.c index 7c6e6cc..9d3805c 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -75,7 +75,7 @@ static void test_fielddef_unref() { upb_symtab_unref(s, &s); upb_msgdef_unref(md, &md); // Check that md is still alive. - ASSERT(strcmp(upb_def_fullname(upb_upcast(md)), "A") == 0); + ASSERT(strcmp(upb_msgdef_fullname(md), "A") == 0); // Check that unref of fielddef frees the whole remaining graph. upb_fielddef_unref(f, &f); @@ -123,13 +123,13 @@ static upb_fielddef *newfield( static upb_msgdef *upb_msgdef_newnamed(const char *name, void *owner) { upb_msgdef *m = upb_msgdef_new(owner); - upb_def_setfullname(upb_upcast(m), name, NULL); + upb_msgdef_setfullname(m, name, NULL); return m; } static upb_enumdef *upb_enumdef_newnamed(const char *name, void *owner) { upb_enumdef *e = upb_enumdef_new(owner); - upb_def_setfullname(upb_upcast(e), name, NULL); + upb_enumdef_setfullname(e, name, NULL); return e; } @@ -143,15 +143,15 @@ static void test_replacement() { upb_msgdef *m2 = upb_msgdef_newnamed("MyMessage2", &s); upb_enumdef *e = upb_enumdef_newnamed("MyEnum", &s); - upb_def *newdefs[] = {upb_upcast(m), upb_upcast(m2), upb_upcast(e)}; + upb_def *newdefs[] = {UPB_UPCAST(m), UPB_UPCAST(m2), UPB_UPCAST(e)}; upb_status status = UPB_STATUS_INIT; ASSERT_STATUS(upb_symtab_add(s, newdefs, 3, &s, &status), &status); // Try adding a new definition of MyEnum, MyMessage should get replaced with // a new version. upb_enumdef *e2 = upb_enumdef_new(&s); - upb_def_setfullname(upb_upcast(e2), "MyEnum", NULL); - upb_def *newdefs2[] = {upb_upcast(e2)}; + upb_enumdef_setfullname(e2, "MyEnum", NULL); + upb_def *newdefs2[] = {UPB_UPCAST(e2)}; ASSERT_STATUS(upb_symtab_add(s, newdefs2, 1, &s, &status), &status); const upb_msgdef *m3 = upb_symtab_lookupmsg(s, "MyMessage", &m3); @@ -184,7 +184,7 @@ static void test_freeze_free() { upb_fielddef_settype(f, UPB_TYPE_MESSAGE); ASSERT(upb_fielddef_setnumber(f, 1, NULL)); ASSERT(upb_fielddef_setname(f, "foo", NULL)); - ASSERT(upb_fielddef_setsubdef(f, upb_upcast(m4), NULL)); + ASSERT(upb_fielddef_setmsgsubdef(f, m4, NULL)); ASSERT(upb_msgdef_addfield(m1, f, &f, NULL)); @@ -197,9 +197,9 @@ static void test_freeze_free() { ASSERT(upb_fielddef_setnumber(f, 1, NULL)); ASSERT(upb_fielddef_setname(f, "foo", NULL)); - ASSERT(upb_fielddef_setsubdef(f, upb_upcast(m1), NULL)); - ASSERT(upb_fielddef_setsubdef(f, upb_upcast(m2), NULL)); - ASSERT(upb_fielddef_setsubdef(f, upb_upcast(m3), NULL)); + ASSERT(upb_fielddef_setmsgsubdef(f, m1, NULL)); + ASSERT(upb_fielddef_setmsgsubdef(f, m2, NULL)); + ASSERT(upb_fielddef_setmsgsubdef(f, m3, NULL)); // Make M3 cyclic with itself. ASSERT(upb_msgdef_addfield(m3, f, &f, NULL)); @@ -211,8 +211,8 @@ static void test_freeze_free() { upb_msgdef_unref(m2, &m2); // Test that they are still alive (NOT allowed by the API). - ASSERT(strcmp("M1", upb_def_fullname(upb_upcast(m1))) == 0); - ASSERT(strcmp("M2", upb_def_fullname(upb_upcast(m2))) == 0); + ASSERT(strcmp("M1", upb_msgdef_fullname(m1)) == 0); + ASSERT(strcmp("M2", upb_msgdef_fullname(m2)) == 0); // Freeze M3. If the test leaked no memory, then freeing m1 and m2 was // successful. @@ -232,20 +232,20 @@ static void test_partial_freeze() { upb_fielddef_settype(f1, UPB_TYPE_MESSAGE); ASSERT(upb_fielddef_setnumber(f1, 1, NULL)); ASSERT(upb_fielddef_setname(f1, "f1", NULL)); - ASSERT(upb_fielddef_setsubdef(f1, upb_upcast(m1), NULL)); + ASSERT(upb_fielddef_setmsgsubdef(f1, m1, NULL)); upb_fielddef *f2 = upb_fielddef_new(&f2); upb_fielddef_settype(f2, UPB_TYPE_MESSAGE); ASSERT(upb_fielddef_setnumber(f2, 2, NULL)); ASSERT(upb_fielddef_setname(f2, "f2", NULL)); - ASSERT(upb_fielddef_setsubdef(f2, upb_upcast(m2), NULL)); + ASSERT(upb_fielddef_setmsgsubdef(f2, m2, NULL)); ASSERT(upb_msgdef_addfield(m3, f1, &f1, NULL)); ASSERT(upb_msgdef_addfield(m3, f2, &f2, NULL)); // Freeze M1 and M2, which should cause the group to be split // and m3 (left mutable) to take references on m1 and m2. - upb_def *defs[] = {upb_upcast(m1), upb_upcast(m2)}; + upb_def *defs[] = {UPB_UPCAST(m1), UPB_UPCAST(m2)}; ASSERT(upb_def_freeze(defs, 2, NULL)); ASSERT(upb_msgdef_isfrozen(m1)); diff --git a/tests/test_table.cc b/tests/test_table.cc index 80b0139..747a97b 100644 --- a/tests/test_table.cc +++ b/tests/test_table.cc @@ -6,6 +6,7 @@ * Tests for upb_table. */ +#include <limits.h> #include <string.h> #include <sys/resource.h> #include <ext/hash_map> @@ -16,7 +17,7 @@ #include <vector> #include "tests/test_util.h" #include "tests/upb_test.h" -#include "upb/table.h" +#include "upb/table.int.h" bool benchmark = false; #define CPU_TIME_PER_TEST 0.5 @@ -126,6 +127,19 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc) { } } + // Test replace. + for(uint32_t i = 0; i <= largest_key; i++) { + upb_value v = upb_value_uint32(i*3); + bool replaced = upb_inttable_replace(&table, i, v); + if(m.find(i) != m.end()) { /* Assume map implementation is correct. */ + ASSERT(replaced); + m[i] = i * 3; + hm[i] = i * 3; + } else { + ASSERT(!replaced); + } + } + // Compact and test correctness again. upb_inttable_compact(&table); for(uint32_t i = 0; i <= largest_key; i++) { @@ -133,9 +147,9 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc) { bool found = upb_inttable_lookup(&table, i, &v); if(m.find(i) != m.end()) { /* Assume map implementation is correct. */ ASSERT(found); - ASSERT(upb_value_getuint32(v) == i*2); - ASSERT(m[i] == i*2); - ASSERT(hm[i] == i*2); + ASSERT(upb_value_getuint32(v) == i*3); + ASSERT(m[i] == i*3); + ASSERT(hm[i] == i*3); } else { ASSERT(!found); } diff --git a/tests/test_varint.c b/tests/test_varint.c index bdbc573..fc7eb40 100644 --- a/tests/test_varint.c +++ b/tests/test_varint.c @@ -5,7 +5,7 @@ */ #include <stdio.h> -#include "upb/pb/varint.h" +#include "upb/pb/varint.int.h" #include "upb_test.h" // Test that we can round-trip from int->varint->int. @@ -66,7 +66,7 @@ static void test_varint_decoder(upb_decoderet (*decoder)(const char*)) { ASSERT(r.p == NULL); - for (uint64_t num = 5; num * 1.5 > num; num *= 1.5) { + for (uint64_t num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) { test_varint_for_num(decoder, num); } test_varint_for_num(decoder, 0); diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc index bc6df46..d766b42 100644 --- a/tests/test_vs_proto2.cc +++ b/tests/test_vs_proto2.cc @@ -12,6 +12,7 @@ #include <google/protobuf/descriptor.h> #include <google/protobuf/dynamic_message.h> #include <google/protobuf/message.h> +#include <google/protobuf/text_format.h> #include <google/protobuf/wire_format_lite.h> #include <inttypes.h> #include <stdint.h> @@ -24,14 +25,15 @@ #include "upb/handlers.h" #include "upb/pb/decoder.h" #include "upb/pb/glue.h" -#include "upb/pb/varint.h" +#include "upb/pb/varint.int.h" #include "upb_test.h" void compare_metadata(const google::protobuf::Descriptor* d, const upb::MessageDef *upb_md) { ASSERT(d->field_count() == upb_md->field_count()); - for (upb::MessageDef::ConstIterator i(upb_md); !i.Done(); i.Next()) { - const upb::FieldDef* upb_f = i.field(); + for (upb::MessageDef::const_iterator i = upb_md->begin(); i != upb_md->end(); + ++i) { + const upb::FieldDef* upb_f = *i; const google::protobuf::FieldDescriptor *proto2_f = d->FindFieldByNumber(upb_f->number()); ASSERT(upb_f); @@ -65,6 +67,9 @@ void parse_and_compare(google::protobuf::Message *msg1, msg2->Clear(); bool ok = upb::PutStringToBytestream(decoder_sink, str, len); + if (!ok) { + fprintf(stderr, "error parsing: %s\n", pipeline.status().GetString()); + } ASSERT(ok); ASSERT(pipeline.status().ok()); @@ -76,12 +81,21 @@ void parse_and_compare(google::protobuf::Message *msg1, std::string str2; msg1->SerializeToString(&str1); msg2->SerializeToString(&str2); + + std::string text_str1; + std::string text_str2; + google::protobuf::TextFormat::PrintToString(*msg1, &text_str1); + google::protobuf::TextFormat::PrintToString(*msg2, &text_str2); + if (str1 != str2) { + fprintf(stderr, "str1: %s, str2: %s\n", + text_str1.c_str(), text_str2.c_str()); + } ASSERT(str1 == str2); ASSERT(std::string(str, len) == str2); } void test_zig_zag() { - for (uint64_t num = 5; num * 1.5 > num; num *= 1.5) { + for (uint64_t num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) { ASSERT(upb_zzenc_64(num) == google::protobuf::internal::WireFormatLite::ZigZagEncode64(num)); if (num < UINT32_MAX) { |