diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_decoder.c | 25 | ||||
-rw-r--r-- | tests/test_string.c | 126 | ||||
-rw-r--r-- | tests/test_table.cc | 23 | ||||
-rw-r--r-- | tests/test_vs_proto2.cc | 58 | ||||
-rw-r--r-- | tests/tests.c | 16 |
5 files changed, 56 insertions, 192 deletions
diff --git a/tests/test_decoder.c b/tests/test_decoder.c index 7b168de..1c3bed0 100644 --- a/tests/test_decoder.c +++ b/tests/test_decoder.c @@ -1,4 +1,5 @@ +#include <stdlib.h> #include "upb_decoder.h" #include "upb_textprinter.h" #include "upb_stdio.h" @@ -11,20 +12,21 @@ int main(int argc, char *argv[]) { } upb_symtab *symtab = upb_symtab_new(); - upb_string *desc = upb_strreadfile(argv[1]); + size_t desc_len; + const char *desc = upb_readfile(argv[1], &desc_len); if (!desc) { fprintf(stderr, "Couldn't open descriptor file: %s\n", argv[1]); return 1; } upb_status status = UPB_STATUS_INIT; - upb_read_descriptor(symtab, desc, &status); + upb_read_descriptor(symtab, desc, desc_len, &status); if (!upb_ok(&status)) { fprintf(stderr, "Error parsing descriptor: "); upb_printerr(&status); return 1; } - upb_string_unref(desc); + free((void*)desc); upb_string *name = upb_strdupc(argv[2]); upb_def *md = upb_symtab_lookup(symtab, name); @@ -40,19 +42,20 @@ int main(int argc, char *argv[]) { return 1; } - upb_stdio *in = upb_stdio_new(); - upb_stdio_reset(in, stdin); - upb_stdio *out = upb_stdio_new(); - upb_stdio_reset(out, stdout); + upb_stdio in, out; + upb_stdio_init(&in); + upb_stdio_init(&out); + upb_stdio_reset(&in, stdin); + upb_stdio_reset(&out, stdout); upb_handlers *handlers = upb_handlers_new(); upb_textprinter *p = upb_textprinter_new(); - upb_textprinter_reset(p, upb_stdio_bytesink(out), false); + upb_textprinter_reset(p, upb_stdio_bytesink(&out), false); upb_textprinter_reghandlers(handlers, m); upb_decoder d; upb_decoder_initforhandlers(&d, handlers); - upb_decoder_reset(&d, upb_stdio_bytesrc(in), p); + upb_decoder_reset(&d, upb_stdio_bytesrc(&in), 0, UINT64_MAX, p); upb_clearerr(&status); upb_decoder_decode(&d, &status); @@ -63,8 +66,8 @@ int main(int argc, char *argv[]) { } upb_status_uninit(&status); - upb_stdio_free(in); - upb_stdio_free(out); + upb_stdio_uninit(&in); + upb_stdio_uninit(&out); upb_decoder_uninit(&d); upb_textprinter_free(p); upb_def_unref(UPB_UPCAST(m)); diff --git a/tests/test_string.c b/tests/test_string.c deleted file mode 100644 index ef0e2a9..0000000 --- a/tests/test_string.c +++ /dev/null @@ -1,126 +0,0 @@ - -#undef NDEBUG /* ensure tests always assert. */ -#include "upb_string.h" - -char static_str[] = "Static string."; -upb_string static_upbstr = UPB_STATIC_STRING(static_str); - -static void test_static() { - // Static string is initialized appropriately. - assert(upb_streql(&static_upbstr, UPB_STRLIT("Static string."))); - - // Taking a ref on a static string returns the same string, and repeated - // refs don't get the string in a confused state. - assert(upb_string_getref(&static_upbstr) == &static_upbstr); - assert(upb_string_getref(&static_upbstr) == &static_upbstr); - assert(upb_string_getref(&static_upbstr) == &static_upbstr); - - // Unreffing a static string does nothing (is not harmful). - upb_string_unref(&static_upbstr); - upb_string_unref(&static_upbstr); - upb_string_unref(&static_upbstr); - upb_string_unref(&static_upbstr); - upb_string_unref(&static_upbstr); - - // Recycling a static string returns a new string (that can be modified). - upb_string *str = &static_upbstr; - upb_string_recycle(&str); - assert(str != &static_upbstr); - - upb_string_unref(str); -} - -static void test_dynamic() { - upb_string *str = upb_string_new(); - assert(str != NULL); - upb_string_unref(str); - - // Can also create a string by recycle(NULL). - str = NULL; - upb_string_recycle(&str); - assert(str != NULL); - - // Take a ref and recycle; should create a new string and release a ref - // on the old one. - upb_string *strcp = upb_string_getref(str); - assert(strcp == str); - assert(upb_atomic_read(&str->refcount) == 2); - upb_string_recycle(&str); - assert(strcp != str); - assert(upb_atomic_read(&str->refcount) == 1); - assert(upb_atomic_read(&strcp->refcount) == 1); - upb_string_unref(strcp); - - upb_strcpyc(str, static_str); - assert(upb_string_len(str) == (sizeof(static_str) - 1)); - const char *robuf = upb_string_getrobuf(str); - assert(robuf != NULL); - assert(upb_streqlc(str, static_str)); - upb_string_endread(str); - - upb_string *str2 = str; - upb_string_recycle(&str2); - // No other referents, so should return the same string. - assert(str2 == str); - - // Write a shorter string, the same memory should be reused. - upb_strcpyc(str, "XX"); - const char *robuf2 = upb_string_getrobuf(str); - assert(robuf2 == robuf); - assert(upb_streqlc(str, "XX")); - assert(upb_streql(str, UPB_STRLIT("XX"))); - - // Make string alias part of another string. - str2 = upb_strdupc("WXYZ"); - upb_string_recycle(&str); - upb_string_substr(str, str2, 1, 2); - assert(upb_string_len(str) == 2); - assert(upb_string_len(str2) == 4); - // The two string should be aliasing the same data. - const char *robuf3 = upb_string_getrobuf(str); - const char *robuf4 = upb_string_getrobuf(str2); - assert(robuf3 == robuf4 + 1); - // The aliased string should have an extra ref. - assert(upb_atomic_read(&str2->refcount) == 2); - - // Recycling str should eliminate the extra ref. - upb_string_recycle(&str); - assert(upb_atomic_read(&str2->refcount) == 1); - - // Resetting str should reuse its old data. - upb_strcpyc(str, "XX"); - const char *robuf5 = upb_string_getrobuf(str); - assert(robuf5 == robuf); - - // Resetting str to something very long should require new data to be - // allocated. - upb_string_recycle(&str); - const char longstring[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; - upb_strcpyc(str, longstring); - const char *robuf6 = upb_string_getrobuf(str); - assert(robuf6 != robuf); - assert(upb_streqlc(str, longstring)); - - // Test printf. - upb_string_recycle(&str); - upb_string_printf(str, "Number: %d, String: %s", 5, "YO!"); - assert(upb_streqlc(str, "Number: 5, String: YO!")); - - // Test asprintf - upb_string *str3 = upb_string_asprintf("Yo %s: " UPB_STRFMT "\n", - "Josh", UPB_STRARG(str)); - const char expected[] = "Yo Josh: Number: 5, String: YO!\n"; - assert(upb_streqlc(str3, expected)); - - upb_string_unref(str); - upb_string_unref(str2); - upb_string_unref(str3); - - // Unref of NULL is harmless. - upb_string_unref(NULL); -} - -int main() { - test_static(); - test_dynamic(); -} diff --git a/tests/test_table.cc b/tests/test_table.cc index 3209e8b..0b47874 100644 --- a/tests/test_table.cc +++ b/tests/test_table.cc @@ -1,7 +1,6 @@ #undef NDEBUG /* ensure tests always assert. */ #include "upb_table.h" -#include "upb_string.h" #include "test_util.h" #include <assert.h> #include <map> @@ -23,7 +22,6 @@ typedef struct { } inttable_entry; typedef struct { - upb_strtable_entry e; int32_t value; /* ASCII Value of first letter */ } strtable_entry; @@ -47,34 +45,29 @@ void test_strtable(const vector<string>& keys, uint32_t num_to_insert) all.insert(key); strtable_entry e; e.value = key[0]; - upb_string *str = upb_strduplen(key.c_str(), key.size()); - e.e.key = str; - upb_strtable_insert(&table, &e.e); - upb_string_unref(str); // The table still owns a ref. + upb_strtable_insert(&table, key.c_str(), &e); m[key] = key[0]; } /* Test correctness. */ for(uint32_t i = 0; i < keys.size(); i++) { const string& key = keys[i]; - upb_string *str = upb_strduplen(key.c_str(), key.size()); - strtable_entry *e = (strtable_entry*)upb_strtable_lookup(&table, str); - printf("Looking up " UPB_STRFMT "...\n", UPB_STRARG(str)); + strtable_entry *e = (strtable_entry*)upb_strtable_lookup(&table, key.c_str()); + printf("Looking up %s...\n", key.c_str()); if(m.find(key) != m.end()) { /* Assume map implementation is correct. */ assert(e); - assert(upb_streql(e->e.key, str)); assert(e->value == key[0]); assert(m[key] == key[0]); } else { assert(e == NULL); } - upb_string_unref(str); } - strtable_entry *e; - for(e = (strtable_entry*)upb_strtable_begin(&table); e; - e = (strtable_entry*)upb_strtable_next(&table, &e->e)) { - string tmp(upb_string_getrobuf(e->e.key), upb_string_len(e->e.key)); + upb_strtable_iter iter; + for(upb_strtable_begin(&iter, &table); !upb_strtable_done(&iter); + upb_strtable_next(&iter)) { + const char *key = upb_strtable_iter_key(&iter); + string tmp(key, strlen(key)); std::set<string>::iterator i = all.find(tmp); assert(i != all.end()); all.erase(i); diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc index 8c2e97d..d220ff3 100644 --- a/tests/test_vs_proto2.cc +++ b/tests/test_vs_proto2.cc @@ -71,18 +71,17 @@ void compare_arrays(const google::protobuf::Reflection *r, case UPB_TYPE(STRING): case UPB_TYPE(BYTES): { std::string str = r->GetRepeatedString(proto2_msg, proto2_f, i); - upb_string *upbstr = upb_value_getstr(v); - std::string str2(upb_string_getrobuf(upbstr), upb_string_len(upbstr)); - string_size += upb_string_len(upbstr); + upb_stdarray *upbstr = (upb_stdarray*)upb_value_getptr(v); + std::string str2(upbstr->ptr, upbstr->len); + string_size += upbstr->len; ASSERT(str == str2); break; } case UPB_TYPE(GROUP): case UPB_TYPE(MESSAGE): - // XXX: getstr ASSERT(upb_dyncast_msgdef(upb_f->def) != NULL); compare(r->GetRepeatedMessage(proto2_msg, proto2_f, i), - upb_value_getstr(v), upb_downcast_msgdef(upb_f->def)); + upb_value_getptr(v), upb_downcast_msgdef(upb_f->def)); } } ASSERT(upb_seq_done(iter)); @@ -129,9 +128,9 @@ void compare_values(const google::protobuf::Reflection *r, case UPB_TYPE(STRING): case UPB_TYPE(BYTES): { std::string str = r->GetString(proto2_msg, proto2_f); - upb_string *upbstr = upb_value_getstr(v); - std::string str2(upb_string_getrobuf(upbstr), upb_string_len(upbstr)); - string_size += upb_string_len(upbstr); + upb_stdarray *upbstr = (upb_stdarray*)upb_value_getptr(v); + std::string str2(upbstr->ptr, upbstr->len); + string_size += upbstr->len; ASSERT(str == str2); break; } @@ -139,7 +138,7 @@ void compare_values(const google::protobuf::Reflection *r, case UPB_TYPE(MESSAGE): // XXX: getstr compare(r->GetMessage(proto2_msg, proto2_f), - upb_value_getstr(v), upb_downcast_msgdef(upb_f->def)); + upb_value_getptr(v), upb_downcast_msgdef(upb_f->def)); } } @@ -159,9 +158,7 @@ void compare(const google::protobuf::Message& proto2_msg, ASSERT(upb_f); ASSERT(proto2_f); ASSERT(upb_f->number == proto2_f->number()); - ASSERT(std::string(upb_string_getrobuf(upb_f->name), - upb_string_len(upb_f->name)) == - proto2_f->name()); + ASSERT(std::string(upb_f->name) == proto2_f->name()); ASSERT(upb_f->type == proto2_f->type()); ASSERT(upb_isseq(upb_f) == proto2_f->is_repeated()); @@ -183,22 +180,22 @@ void compare(const google::protobuf::Message& proto2_msg, void parse_and_compare(MESSAGE_CIDENT *proto2_msg, void *upb_msg, upb_msgdef *upb_md, - upb_string *str) + const char *str, size_t len) { // Parse to both proto2 and upb. - ASSERT(proto2_msg->ParseFromArray(upb_string_getrobuf(str), upb_string_len(str))); + ASSERT(proto2_msg->ParseFromArray(str, len)); upb_status status = UPB_STATUS_INIT; upb_msg_clear(upb_msg, upb_md); - upb_strtomsg(str, upb_msg, upb_md, &status); + upb_strtomsg(str, len, upb_msg, upb_md, &status); if (!upb_ok(&status)) { fprintf(stderr, "Error parsing test protobuf: "); - upb_printerr(&status); + upb_status_print(&status, stderr); exit(1); } string_size = 0; compare(*proto2_msg, upb_msg, upb_md); - printf("Total size: %d, string size: %zd (%0.2f%%)\n", upb_string_len(str), - string_size, (double)string_size / upb_string_len(str) * 100); + printf("Total size: %zd, string size: %zd (%0.2f%%)\n", len, + string_size, (double)string_size / len * 100); upb_status_uninit(&status); } @@ -221,31 +218,30 @@ int main(int argc, char *argv[]) // Initialize upb state, parse descriptor. upb_status status = UPB_STATUS_INIT; upb_symtab *symtab = upb_symtab_new(); - upb_string *fds = upb_strreadfile(MESSAGE_DESCRIPTOR_FILE); + size_t fds_len; + const char *fds = upb_readfile(MESSAGE_DESCRIPTOR_FILE, &fds_len); if(fds == NULL) { fprintf(stderr, "Couldn't read " MESSAGE_DESCRIPTOR_FILE ".\n"); return 1; } - upb_read_descriptor(symtab, fds, &status); + upb_read_descriptor(symtab, fds, fds_len, &status); if(!upb_ok(&status)) { fprintf(stderr, "Error importing " MESSAGE_DESCRIPTOR_FILE ": "); - upb_printerr(&status); + upb_status_print(&status, stderr); return 1; } - upb_string_unref(fds); + free((void*)fds); - upb_string *proto_name = upb_strdupc(MESSAGE_NAME); - upb_def *def = upb_symtab_lookup(symtab, proto_name); + upb_def *def = upb_symtab_lookup(symtab, MESSAGE_NAME); upb_msgdef *msgdef; if(!def || !(msgdef = upb_dyncast_msgdef(def))) { - fprintf(stderr, "Error finding symbol '" UPB_STRFMT "'.\n", - UPB_STRARG(proto_name)); + fprintf(stderr, "Error finding symbol '%s'.\n", MESSAGE_NAME); return 1; } - upb_string_unref(proto_name); // Read the message data itself. - upb_string *str = upb_strreadfile(MESSAGE_FILE); + size_t len; + const char *str = upb_readfile(MESSAGE_FILE, &len); if(str == NULL) { fprintf(stderr, "Error reading " MESSAGE_FILE "\n"); return 1; @@ -254,13 +250,13 @@ int main(int argc, char *argv[]) // Run twice to test proper object reuse. MESSAGE_CIDENT proto2_msg; void *upb_msg = upb_stdmsg_new(msgdef); - parse_and_compare(&proto2_msg, upb_msg, msgdef, str); - parse_and_compare(&proto2_msg, upb_msg, msgdef, str); + parse_and_compare(&proto2_msg, upb_msg, msgdef, str, len); + parse_and_compare(&proto2_msg, upb_msg, msgdef, str, len); printf("All tests passed, %d assertions.\n", num_assertions); upb_stdmsg_free(upb_msg, msgdef); upb_def_unref(UPB_UPCAST(msgdef)); - upb_string_unref(str); + free((void*)str); upb_symtab_unref(symtab); upb_status_uninit(&status); google::protobuf::ShutdownProtobufLibrary(); diff --git a/tests/tests.c b/tests/tests.c index c6b5051..aa692f6 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -11,16 +11,18 @@ static upb_symtab *load_test_proto() { upb_symtab *s = upb_symtab_new(); ASSERT(s); - upb_string *descriptor = upb_strreadfile("tests/test.proto.pb"); + size_t len; + char *descriptor = upb_readfile("tests/test.proto.pb", &len); if(!descriptor) { fprintf(stderr, "Couldn't read input file tests/test.proto.pb\n"); exit(1); } upb_status status = UPB_STATUS_INIT; - upb_read_descriptor(s, descriptor, &status); + upb_read_descriptor(s, descriptor, len, &status); + upb_status_print(&status, stderr); ASSERT(upb_ok(&status)); upb_status_uninit(&status); - upb_string_unref(descriptor); + free(descriptor); return s; } @@ -33,9 +35,7 @@ static upb_flow_t upb_test_onvalue(void *closure, upb_value fval, upb_value val) static void test_upb_jit() { upb_symtab *s = load_test_proto(); - upb_string *symname = upb_strdupc("SimplePrimitives"); - upb_def *def = upb_symtab_lookup(s, symname); - upb_string_unref(symname); + upb_def *def = upb_symtab_lookup(s, "SimplePrimitives"); ASSERT(def); upb_handlers *h = upb_handlers_new(); @@ -54,9 +54,7 @@ static void test_upb_symtab() { // Test cycle detection by making a cyclic def's main refcount go to zero // and then be incremented to one again. - upb_string *symname = upb_strdupc("A"); - upb_def *def = upb_symtab_lookup(s, symname); - upb_string_unref(symname); + upb_def *def = upb_symtab_lookup(s, "A"); ASSERT(def); upb_symtab_unref(s); upb_msgdef *m = upb_downcast_msgdef(def); |