From 08b4a91204ff7174c381f887d87878d8bb1181ff Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 5 Dec 2009 19:54:38 -0800 Subject: Add a test for circularly-linked descriptors. The test currently triggers valgrind-detected memory errors. --- Makefile | 7 ++++++- src/upb_def.c | 5 +++-- tests/test_vs_proto2.cc | 4 ++-- tests/tests.c | 15 ++++++++++++--- tools/upbc.c | 2 +- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 9496ce5..cc30814 100644 --- a/Makefile +++ b/Makefile @@ -74,10 +74,15 @@ python: $(LIBUPB_PIC) cd lang_ext/python && python setup.py build # Tests +tests/test.proto.pb: tests/test.proto + # TODO: replace with upbc + protoc tests/test.proto -otests/test.proto.pb + tests: tests/tests \ tests/test_table \ tests/t.test_vs_proto2.googlemessage1 \ - tests/t.test_vs_proto2.googlemessage2 + tests/t.test_vs_proto2.googlemessage2 \ + tests/test.proto.pb #VALGRIND=valgrind --leak-check=full --error-exitcode=1 VALGRIND= diff --git a/src/upb_def.c b/src/upb_def.c index 08140e4..bc4f293 100644 --- a/src/upb_def.c +++ b/src/upb_def.c @@ -27,8 +27,9 @@ static int div_round_up(int numerator, int denominator) { // // Our scheme is as follows. First we give each def a flag indicating whether // it is part of a cycle or not. Because defs are immutable, this flag will -// never change. For acyclic defs, we can use a naive algorithm and avoid -// the overhead of dealing with cycles. Most defs will be acyclic. +// never change. For acyclic defs, we can use a naive algorithm and avoid the +// overhead of dealing with cycles. Most defs will be acyclic, and most cycles +// will be very short. // // For defs that participate in cycles we keep two reference counts. One // tracks references that come from outside the cycle (we call these external diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc index 926d731..58c8e50 100644 --- a/tests/test_vs_proto2.cc +++ b/tests/test_vs_proto2.cc @@ -131,8 +131,8 @@ void compare(const google::protobuf::Message& proto2_msg, const google::protobuf::Descriptor *d = proto2_msg.GetDescriptor(); struct upb_msgdef *def = upb_msg->def; - ASSERT((uint32_t)d->field_count() == def->num_fields); - for(uint32_t i = 0; i < def->num_fields; i++) { + ASSERT((upb_field_count_t)d->field_count() == def->num_fields); + for(upb_field_count_t i = 0; i < def->num_fields; i++) { struct upb_fielddef *upb_f = &def->fields[i]; const google::protobuf::FieldDescriptor *proto2_f = d->FindFieldByNumber(upb_f->number); diff --git a/tests/tests.c b/tests/tests.c index 9fbfa3a..3580c2e 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -228,9 +228,18 @@ static void test_get_f_uint32_t() } static void test_upb_symtab() { - struct upb_symtab *c = upb_symtab_new(); - ASSERT(c); - upb_symtab_unref(c); + struct upb_symtab *s = upb_symtab_new(); + ASSERT(s); + struct upb_string *descriptor = upb_strreadfile("tests/test.proto.pb"); + if(!descriptor) { + fprintf(stderr, "Couldn't read input file tests/test.proto.pb\n"); + exit(1); + } + struct upb_status status = UPB_STATUS_INIT; + upb_symtab_add_desc(s, descriptor, &status); + ASSERT(upb_ok(&status)); + upb_string_unref(descriptor); + upb_symtab_unref(s); } diff --git a/tools/upbc.c b/tools/upbc.c index cb41ecc..8d41abc 100644 --- a/tools/upbc.c +++ b/tools/upbc.c @@ -708,6 +708,7 @@ int main(int argc, char *argv[]) int symcount; struct upb_def **defs = upb_symtab_getandref_defs(s, &symcount); + upb_symtab_unref(s); write_h(defs, symcount, h_filename, cident, h_file); write_const_h(defs, symcount, h_filename, h_const_file); for (int i = 0; i < symcount; i++) upb_def_unref(defs[i]); @@ -719,7 +720,6 @@ int main(int argc, char *argv[]) fclose(c_file); } upb_msg_unref(fds_msg); - upb_symtab_unref(s); upb_string_unref(descriptor); fclose(h_file); fclose(h_const_file); -- cgit v1.2.3