summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_cpp.cc21
-rw-r--r--tests/test_stream.c127
-rw-r--r--tests/test_table.cc39
-rw-r--r--tests/test_vs_proto2.cc32
-rw-r--r--tests/tests.c11
5 files changed, 60 insertions, 170 deletions
diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc
index 35526fb..11542bf 100644
--- a/tests/test_cpp.cc
+++ b/tests/test_cpp.cc
@@ -1,12 +1,20 @@
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * Tests for C++ wrappers.
+ */
#include <iostream>
#include "upb/def.hpp"
#include "upb/pb/glue.hpp"
-static void TestSymbolTable() {
+static void TestSymbolTable(const char *descriptor_file) {
upb::SymbolTable *s = upb::SymbolTable::New();
upb::Status status;
- if (!upb::LoadDescriptorFileIntoSymtab(s, "tests/test.proto.pb", &status)) {
+ if (!upb::LoadDescriptorFileIntoSymtab(s, descriptor_file, &status)) {
std::cerr << "Couldn't load descriptor: " << status;
exit(1);
}
@@ -17,6 +25,11 @@ static void TestSymbolTable() {
md->Unref();
}
-int main() {
- TestSymbolTable();
+int main(int argc, char *argv[]) {
+ if (argc < 2) {
+ fprintf(stderr, "Usage: test_cpp <descriptor file>\n");
+ return 1;
+ }
+ TestSymbolTable(argv[1]);
+ return 0;
}
diff --git a/tests/test_stream.c b/tests/test_stream.c
deleted file mode 100644
index 4c9880d..0000000
--- a/tests/test_stream.c
+++ /dev/null
@@ -1,127 +0,0 @@
-
-#undef NDEBUG /* ensure tests always assert. */
-#include "upb/handlers.h"
-
-typedef struct {
- upb_string *str;
- bool should_delegate;
-} test_data;
-
-extern upb_handlerset test_handlers;
-
-static void strappendf(upb_string *s, const char *format, ...) {
- upb_string *str = upb_string_new();
- va_list args;
- va_start(args, format);
- upb_string_vprintf(str, format, args);
- va_end(args);
- upb_strcat(s, str);
- upb_string_unref(str);
-}
-
-static upb_flow_t startmsg(void *closure) {
- test_data *d = closure;
- strappendf(d->str, "startmsg\n");
- return UPB_CONTINUE;
-}
-
-static upb_flow_t endmsg(void *closure) {
- test_data *d = closure;
- strappendf(d->str, "endmsg\n");
- return UPB_CONTINUE;
-}
-
-static upb_flow_t value(void *closure, struct _upb_fielddef *f, upb_value val) {
- (void)f;
- test_data *d = closure;
- strappendf(d->str, "value, %lld\n", upb_value_getint64(val));
- return UPB_CONTINUE;
-}
-
-static upb_flow_t startsubmsg(void *closure, struct _upb_fielddef *f,
- upb_handlers *delegate_to) {
- (void)f;
- test_data *d = closure;
- strappendf(d->str, "startsubmsg\n");
- if (d->should_delegate) {
- upb_register_handlerset(delegate_to, &test_handlers);
- upb_set_handler_closure(delegate_to, closure, NULL);
- return UPB_DELEGATE;
- } else {
- return UPB_CONTINUE;
- }
-}
-
-static upb_flow_t endsubmsg(void *closure, struct _upb_fielddef *f) {
- (void)f;
- test_data *d = closure;
- strappendf(d->str, "endsubmsg\n");
- return UPB_CONTINUE;
-}
-
-static upb_flow_t unknownval(void *closure, upb_field_number_t fieldnum,
- upb_value val) {
- (void)val;
- test_data *d = closure;
- strappendf(d->str, "unknownval, %d\n", fieldnum);
- return UPB_CONTINUE;
-}
-
-upb_handlerset test_handlers = {
- &startmsg,
- &endmsg,
- &value,
- &startsubmsg,
- &endsubmsg,
- &unknownval,
-};
-
-static void test_dispatcher() {
- test_data data;
- data.should_delegate = false;
- data.str = upb_string_new();
- upb_handlers h;
- upb_handlers_init(&h);
- upb_handlers_reset(&h);
- upb_register_handlerset(&h, &test_handlers);
- upb_set_handler_closure(&h, &data, NULL);
- upb_dispatcher d;
- upb_dispatcher_init(&d);
- upb_dispatcher_reset(&d, &h, false);
-
- upb_dispatch_startmsg(&d);
- upb_value val;
- upb_value_setint64(&val, 5);
- upb_dispatch_value(&d, NULL, val);
- upb_dispatch_startsubmsg(&d, NULL);
- data.should_delegate = true;
- upb_dispatch_startsubmsg(&d, NULL);
- data.should_delegate = false;
- upb_dispatch_startsubmsg(&d, NULL);
- upb_dispatch_value(&d, NULL, val);
- upb_dispatch_endsubmsg(&d, NULL);
- upb_dispatch_endsubmsg(&d, NULL);
- upb_dispatch_endsubmsg(&d, NULL);
- upb_dispatch_endmsg(&d);
-
- upb_string expected = UPB_STACK_STRING(
- "startmsg\n"
- "value, 5\n"
- "startsubmsg\n"
- "startsubmsg\n"
- "startmsg\n" // Because of the delegation.
- "startsubmsg\n"
- "value, 5\n"
- "endsubmsg\n"
- "endmsg\n" // Because of the delegation.
- "endsubmsg\n"
- "endsubmsg\n"
- "endmsg\n");
- assert(upb_streql(data.str, &expected));
- upb_string_unref(data.str);
-}
-
-int main() {
- test_dispatcher();
- return 0;
-}
diff --git a/tests/test_table.cc b/tests/test_table.cc
index 68e8325..47e083f 100644
--- a/tests/test_table.cc
+++ b/tests/test_table.cc
@@ -15,7 +15,6 @@
bool benchmark = false;
#define CPU_TIME_PER_TEST 0.5
-using std::string;
using std::vector;
typedef struct {
@@ -34,15 +33,15 @@ double get_usertime()
}
/* num_entries must be a power of 2. */
-void test_strtable(const vector<string>& keys, uint32_t num_to_insert)
+void test_strtable(const vector<std::string>& keys, uint32_t num_to_insert)
{
/* Initialize structures. */
upb_strtable table;
- std::map<string, int32_t> m;
+ std::map<std::string, int32_t> m;
upb_strtable_init(&table, 0, sizeof(strtable_entry));
- std::set<string> all;
+ std::set<std::string> all;
for(size_t i = 0; i < num_to_insert; i++) {
- const string& key = keys[i];
+ const std::string& key = keys[i];
all.insert(key);
strtable_entry e;
e.value = key[0];
@@ -52,8 +51,9 @@ void test_strtable(const vector<string>& keys, uint32_t num_to_insert)
/* Test correctness. */
for(uint32_t i = 0; i < keys.size(); i++) {
- const string& key = keys[i];
- strtable_entry *e = (strtable_entry*)upb_strtable_lookup(&table, key.c_str());
+ const std::string& key = keys[i];
+ strtable_entry *e =
+ (strtable_entry*)upb_strtable_lookup(&table, key.c_str());
if(m.find(key) != m.end()) { /* Assume map implementation is correct. */
assert(e);
assert(e->value == key[0]);
@@ -67,8 +67,8 @@ void test_strtable(const vector<string>& keys, uint32_t num_to_insert)
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);
+ std::string tmp(key, strlen(key));
+ std::set<std::string>::iterator i = all.find(tmp);
assert(i != all.end());
all.erase(i);
}
@@ -135,7 +135,7 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
printf("%s\n", desc);
/* Test performance. We only test lookups for keys that are known to exist. */
- uint16_t rand_order[num_entries];
+ uint16_t *rand_order = new uint16_t[num_entries];
for(uint16_t i = 0; i < num_entries; i++) {
rand_order[i] = i;
}
@@ -155,8 +155,12 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
fflush(stdout);
double before = get_usertime();
unsigned int i;
+
+#define MAYBE_BREAK \
+ if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) \
+ break;
for(i = 0; true; i++) {
- if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) break;
+ MAYBE_BREAK;
int32_t key = keys[i & mask];
inttable_entry *e = (inttable_entry*)upb_inttable_lookup(&table, key);
x += (uintptr_t)e;
@@ -168,7 +172,7 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
fflush(stdout);
before = get_usertime();
for(i = 0; true; i++) {
- if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) break;
+ MAYBE_BREAK;
int32_t key = keys[rand_order[i & mask]];
inttable_entry *e = (inttable_entry*)upb_inttable_lookup(&table, key);
x += (uintptr_t)e;
@@ -180,7 +184,7 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
fflush(stdout);
before = get_usertime();
for(i = 0; true; i++) {
- if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) break;
+ MAYBE_BREAK;
int32_t key = keys[i & mask];
x += m[key];
}
@@ -191,7 +195,7 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
fflush(stdout);
before = get_usertime();
for(i = 0; true; i++) {
- if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) break;
+ MAYBE_BREAK;
int32_t key = keys[rand_order[i & mask]];
x += m[key];
}
@@ -202,7 +206,7 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
fflush(stdout);
before = get_usertime();
for(i = 0; true; i++) {
- if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) break;
+ MAYBE_BREAK;
int32_t key = keys[rand_order[i & mask]];
x += hm[key];
}
@@ -213,13 +217,14 @@ void test_inttable(int32_t *keys, uint16_t num_entries, const char *desc)
fflush(stdout);
before = get_usertime();
for(i = 0; true; i++) {
- if ((i & time_mask) == 0 && (get_usertime() - before) > CPU_TIME_PER_TEST) break;
+ MAYBE_BREAK;
int32_t key = keys[rand_order[i & mask]];
x += hm[key];
}
total = get_usertime() - before;
printf("%s/s\n\n", eng(i/total, 3, false));
upb_inttable_free(&table);
+ delete rand_order;
}
int32_t *get_contiguous_keys(int32_t num)
@@ -236,7 +241,7 @@ int main(int argc, char *argv[])
if (strcmp(argv[i], "--benchmark") == 0) benchmark = true;
}
- vector<string> keys;
+ vector<std::string> keys;
keys.push_back("google.protobuf.FileDescriptorSet");
keys.push_back("google.protobuf.FileDescriptorProto");
keys.push_back("google.protobuf.DescriptorProto");
diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc
index 222bcdb..8d13f33 100644
--- a/tests/test_vs_proto2.cc
+++ b/tests/test_vs_proto2.cc
@@ -8,16 +8,16 @@
*/
#include <assert.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <google/protobuf/descriptor.h>
+#include "benchmarks/google_messages.pb.h"
#include "upb/def.h"
#include "upb/msg.h"
#include "upb/pb/glue.h"
#include "upb_test.h"
-#include MESSAGE_HFILE
-
size_t string_size;
void compare(const google::protobuf::Message& proto2_msg,
@@ -87,7 +87,6 @@ void compare_arrays(const google::protobuf::Reflection *r,
ASSERT(upb_seq_done(iter));
}
-#include <inttypes.h>
void compare_values(const google::protobuf::Reflection *r,
const google::protobuf::Message& proto2_msg,
const google::protobuf::FieldDescriptor *proto2_f,
@@ -200,32 +199,25 @@ void parse_and_compare(MESSAGE_CIDENT *proto2_msg,
int main(int argc, char *argv[])
{
- // Change cwd to where the binary is.
- (void)argc;
- char *lastslash = strrchr(argv[0], '/');
- char *progname = argv[0];
- if(lastslash) {
- *lastslash = '\0';
- if(chdir(argv[0]) < 0) {
- fprintf(stderr, "Error changing directory to %s.\n", argv[0]);
- return 1;
- }
- *lastslash = '/';
- progname = lastslash + 3; /* "/b_" */
+ if (argc < 3) {
+ fprintf(stderr, "Usage: test_vs_proto2 <descriptor file> <message file>\n");
+ return 1;
}
+ const char *descriptor_file = argv[1];
+ const char *message_file = argv[2];
// Initialize upb state, parse descriptor.
upb_status status = UPB_STATUS_INIT;
upb_symtab *symtab = upb_symtab_new();
size_t fds_len;
- const char *fds = upb_readfile(MESSAGE_DESCRIPTOR_FILE, &fds_len);
+ const char *fds = upb_readfile(descriptor_file, &fds_len);
if(fds == NULL) {
- fprintf(stderr, "Couldn't read " MESSAGE_DESCRIPTOR_FILE ".\n");
+ fprintf(stderr, "Couldn't read %s.\n", descriptor_file);
return 1;
}
upb_load_descriptor_into_symtab(symtab, fds, fds_len, &status);
if(!upb_ok(&status)) {
- fprintf(stderr, "Error importing " MESSAGE_DESCRIPTOR_FILE ": %s",
+ fprintf(stderr, "Error importing %s: %s", descriptor_file,
upb_status_getstr(&status));
return 1;
}
@@ -240,9 +232,9 @@ int main(int argc, char *argv[])
// Read the message data itself.
size_t len;
- const char *str = upb_readfile(MESSAGE_FILE, &len);
+ const char *str = upb_readfile(message_file, &len);
if(str == NULL) {
- fprintf(stderr, "Error reading " MESSAGE_FILE "\n");
+ fprintf(stderr, "Error reading %s\n", message_file);
return 1;
}
diff --git a/tests/tests.c b/tests/tests.c
index 178254f..e8c335b 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -9,11 +9,13 @@
#include "upb/pb/glue.h"
#include "upb_test.h"
+const char *descriptor_file;
+
static upb_symtab *load_test_proto() {
upb_symtab *s = upb_symtab_new();
ASSERT(s);
upb_status status = UPB_STATUS_INIT;
- if (!upb_load_descriptor_file_into_symtab(s, "tests/test.proto.pb", &status)) {
+ if (!upb_load_descriptor_file_into_symtab(s, descriptor_file, &status)) {
fprintf(stderr, "Error loading descriptor file: %s\n", upb_status_getstr(&status));
exit(1);
}
@@ -91,8 +93,13 @@ static void test_upb_two_fielddefs() {
upb_fielddef_unref(f2);
}
-int main()
+int main(int argc, char *argv[])
{
+ if (argc < 2) {
+ fprintf(stderr, "Usage: test_cpp <descriptor file>\n");
+ return 1;
+ }
+ descriptor_file = argv[1];
#define TEST(func) do { \
int assertions_before = num_assertions; \
printf("Running " #func "..."); fflush(stdout); \
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback