Changes to get upb compiling inside Google.

pull/13171/head
Joshua Haberman 13 years ago
parent d29339d126
commit 99ae0ed397
  1. 8
      Makefile
  2. 12
      bindings/cpp/upb/def.hpp
  3. 21
      tests/test_cpp.cc
  4. 127
      tests/test_stream.c
  5. 39
      tests/test_table.cc
  6. 30
      tests/test_vs_proto2.cc
  7. 11
      tests/tests.c
  8. 7
      upb/def.c
  9. 2
      upb/def.h
  10. 1
      upb/pb/jit_debug_elf_file.s

@ -232,13 +232,15 @@ VALGRIND=valgrind --leak-check=full --error-exitcode=1
test: tests
@echo Running all tests under valgrind.
@set -e # Abort on error.
@for test in $(TESTS); do \
@for test in $(SIMPLE_TESTS) $(SIMPLE_CXX_TESTS); do \
if [ -x ./$$test ] ; then \
echo !!! $(VALGRIND) ./$$test; \
$(VALGRIND) ./$$test || exit 1; \
$(VALGRIND) ./$$test tests/test.proto.pb || exit 1; \
fi \
done; \
echo "All tests passed!"
$(VALGRIND) ./tests/t.test_vs_proto2.googlemessage1 benchmarks/google_messages.proto.pb benchmarks/google_message1.dat
$(VALGRIND) ./tests/t.test_vs_proto2.googlemessage2 benchmarks/google_messages.proto.pb benchmarks/google_message2.dat
@echo "All tests passed!"
tests/t.test_vs_proto2.googlemessage1 \
tests/t.test_vs_proto2.googlemessage2: \

@ -69,8 +69,10 @@ class FieldDef : public upb_fielddef {
MessageDef* message() { return (MessageDef*)upb_fielddef_msgdef(this); }
const MessageDef* message() const { return (MessageDef*)upb_fielddef_msgdef(this); }
//Def* subdef() { return upb_fielddef_subdef(this); }
//const Def* subdef() { return upb_fielddef_subdef(this); }
// Will be added once upb::Def is defined:
// Def* subdef() { return upb_fielddef_subdef(this); }
// const Def* subdef() { return upb_fielddef_subdef(this); }
// Returns true if this FieldDef is finalized
bool IsFinalized() const { return upb_fielddef_finalized(this); }
@ -127,13 +129,11 @@ class MessageDef : public upb_msgdef {
// name and number must be set, and the message may not already contain any
// field with this name or number, and this fielddef may not be part of
// another message, otherwise false is returned and no action is performed.
bool AddFields(FieldDef** f, int n) {
bool AddFields(FieldDef*const * f, int n) {
return upb_msgdef_addfields(this, (upb_fielddef**)f, n);
}
bool AddFields(const std::vector<FieldDef*>& fields) {
FieldDef *arr[fields.size()];
std::copy(fields.begin(), fields.end(), arr);
return AddFields(arr, fields.size());
return AddFields(&fields[0], fields.size());
}
// Lookup fields by name or number, returning NULL if no such field exists.

@ -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;
}

@ -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;
}

@ -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");

@ -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]);
if (argc < 3) {
fprintf(stderr, "Usage: test_vs_proto2 <descriptor file> <message file>\n");
return 1;
}
*lastslash = '/';
progname = lastslash + 3; /* "/b_" */
}
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;
}

@ -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); \

@ -495,7 +495,7 @@ bool upb_msgdef_setextrange(upb_msgdef *m, uint32_t start, uint32_t end) {
return true;
}
bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef **fields, int n) {
bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef *const *fields, int n) {
// Check constraints for all fields before performing any action.
for (int i = 0; i < n; i++) {
upb_fielddef *f = fields[i];
@ -729,7 +729,6 @@ bool upb_symtab_dfs(upb_def *def, upb_def **open_defs, int n,
bool replacing = (upb_strtable_lookup(addtab, m->base.fqname) != NULL);
if (needcopy && !replacing) {
upb_symtab_ent e = {upb_def_dup(def)};
//fprintf(stderr, "Replacing def: %p\n", e.def);
upb_strtable_insert(addtab, def->fqname, &e);
replacing = true;
}
@ -798,9 +797,6 @@ bool upb_symtab_add(upb_symtab *s, upb_def **defs, int n, upb_status *status) {
// Check the type of the found def.
upb_fieldtype_t expected = upb_issubmsg(f) ? UPB_DEF_MSG : UPB_DEF_ENUM;
//fprintf(stderr, "found: %p\n", found);
//fprintf(stderr, "found->def: %p\n", found->def);
//fprintf(stderr, "found->def->type: %d\n", found->def->type);
if(found->def->type != expected) {
upb_status_seterrliteral(status, "Unexpected type");
return false;
@ -821,7 +817,6 @@ bool upb_symtab_add(upb_symtab *s, upb_def **defs, int n, upb_status *status) {
upb_deflist_push(&s->olddefs, symtab_e->def);
symtab_e->def = tmptab_e->def;
} else {
//fprintf(stderr, "Inserting def: %p\n", tmptab_e->def);
upb_strtable_insert(&s->symtab, tmptab_e->def->fqname, tmptab_e);
}
}

@ -277,7 +277,7 @@ bool upb_msgdef_setextrange(upb_msgdef *m, uint32_t start, uint32_t end);
// number must be set, and the message may not already contain any field with
// this name or number, and this fielddef may not be part of another message,
// otherwise false is returned and no action is performed.
bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef **f, int n);
bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef *const *f, int n);
INLINE bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f) {
return upb_msgdef_addfields(m, &f, 1);
}

@ -3,5 +3,4 @@
upb_jit_compiled_decoder:
.globl upb_jit_compiled_decoder
.size upb_jit_compiled_decoder, 0x321
.type upb_jit_compiled_decoder STT_FUNC
.space 0x321

Loading…
Cancel
Save