Unit test comparing parsed results with proto2.

pull/13171/head
Joshua Haberman 15 years ago
parent 75134e6f9d
commit 00ba0d1ac1
  1. 32
      Makefile
  2. 220
      tests/test_vs_proto2.cc

@ -23,7 +23,7 @@
# Function to expand a wildcard pattern recursively. # Function to expand a wildcard pattern recursively.
rwildcard=$(strip $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)$(filter $(subst *,%,$2),$d))) rwildcard=$(strip $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)$(filter $(subst *,%,$2),$d)))
.PHONY: all clean test benchmarks benchmark descriptorgen .PHONY: all clean test benchmarks benchmark descriptorgen tests
CC=gcc CC=gcc
CXX=g++ CXX=g++
CFLAGS=-std=c99 CFLAGS=-std=c99
@ -34,10 +34,13 @@ LDLIBS=-lpthread
LIBUPB=src/libupb.a LIBUPB=src/libupb.a
LIBUPB_PIC=src/libupb_pic.a LIBUPB_PIC=src/libupb_pic.a
LIBUPB_SHARED=src/libupb.so LIBUPB_SHARED=src/libupb.so
ALL=deps $(OBJ) $(LIBUPB) $(LIBUPB_PIC) tests/test_table tests/tests tools/upbc ALL=deps $(OBJ) $(LIBUPB) $(LIBUPB_PIC) tools/upbc
all: $(ALL) all: $(ALL)
clean: clean:
rm -rf $(call rwildcard,,*.o) $(call rwildcard,,*.lo) $(ALL) benchmark/google_messages.proto.pb benchmark/google_messages.pb.* benchmarks/b.* benchmarks/*.pb* rm -rf $(LIBUPB) $(LIBUPB_PIC)
rm -rf $(call rwildcard,,*.o) $(call rwildcard,,*.lo)
rm -rf benchmark/google_messages.proto.pb benchmark/google_messages.pb.* benchmarks/b.* benchmarks/*.pb*
rm -rf tests/tests tests/t.* tests/test_table
rm -rf descriptor/descriptor.proto.pb rm -rf descriptor/descriptor.proto.pb
cd lang_ext/python && python setup.py clean --all cd lang_ext/python && python setup.py clean --all
@ -69,8 +72,31 @@ python: $(LIBUPB_PIC)
cd lang_ext/python && python setup.py build cd lang_ext/python && python setup.py build
# Tests # Tests
tests: tests/tests \
tests/t.test_vs_proto2.googlemessage1 \
tests/t.test_vs_proto2.googlemessage2
test: tests/tests test: tests/tests
./tests/tests ./tests/tests
tests/t.test_vs_proto2.googlemessage1 \
tests/t.test_vs_proto2.googlemessage2: \
tests/test_vs_proto2.cc $(LIBUPB) benchmarks/google_messages.proto.pb \
benchmarks/google_messages.pb.cc
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o tests/test_vs_proto2.googlemessage1 $< \
-DMESSAGE_NAME=\"benchmarks.SpeedMessage1\" \
-DMESSAGE_DESCRIPTOR_FILE=\"../benchmarks/google_messages.proto.pb\" \
-DMESSAGE_FILE=\"../benchmarks/google_message1.dat\" \
-DMESSAGE_CIDENT="benchmarks::SpeedMessage1" \
-DMESSAGE_HFILE=\"../benchmarks/google_messages.pb.h\" \
benchmarks/google_messages.pb.cc -lprotobuf -lpthread $(LIBUPB)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o tests/test_vs_proto2.googlemessage2 $< \
-DMESSAGE_NAME=\"benchmarks.SpeedMessage2\" \
-DMESSAGE_DESCRIPTOR_FILE=\"../benchmarks/google_messages.proto.pb\" \
-DMESSAGE_FILE=\"../benchmarks/google_message2.dat\" \
-DMESSAGE_CIDENT="benchmarks::SpeedMessage2" \
-DMESSAGE_HFILE=\"../benchmarks/google_messages.pb.h\" \
benchmarks/google_messages.pb.cc -lprotobuf -lpthread $(LIBUPB)
tests/test_table: src/libupb.a tests/test_table: src/libupb.a
tests/tests: src/libupb.a tests/tests: src/libupb.a

@ -0,0 +1,220 @@
#undef NDEBUG /* ensure tests always assert. */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <google/protobuf/descriptor.h>
#include "upb_parse.h"
#include "upb_context.h"
#include "upb_context.h"
#include "upb_msg.h"
#include "upb_mm.h"
int num_assertions = 0;
#define ASSERT(expr) do { \
++num_assertions; \
assert(expr); \
} while(0)
#include MESSAGE_HFILE
void compare(const google::protobuf::Message& proto2_msg,
struct upb_msg *upb_msg);
void compare_arrays(const google::protobuf::Reflection *r,
const google::protobuf::Message& proto2_msg,
const google::protobuf::FieldDescriptor *proto2_f,
struct upb_msg *upb_msg, struct upb_msg_fielddef *upb_f)
{
struct upb_array *arr = *upb_msg_getptr(upb_msg, upb_f).arr;
ASSERT(arr->len == (upb_arraylen_t)r->FieldSize(proto2_msg, proto2_f));
for(upb_arraylen_t i = 0; i < arr->len; i++) {
union upb_value_ptr p = upb_array_getelementptr(arr, i);
switch(upb_f->type) {
default:
ASSERT(false);
case UPB_TYPENUM(DOUBLE):
ASSERT(r->GetRepeatedDouble(proto2_msg, proto2_f, i) == *p._double);
break;
case UPB_TYPENUM(FLOAT):
ASSERT(r->GetRepeatedFloat(proto2_msg, proto2_f, i) == *p._float);
break;
case UPB_TYPENUM(INT64):
case UPB_TYPENUM(SINT64):
case UPB_TYPENUM(SFIXED64):
ASSERT(r->GetRepeatedInt64(proto2_msg, proto2_f, i) == *p.int64);
break;
case UPB_TYPENUM(UINT64):
case UPB_TYPENUM(FIXED64):
ASSERT(r->GetRepeatedUInt64(proto2_msg, proto2_f, i) == *p.uint64);
break;
case UPB_TYPENUM(SFIXED32):
case UPB_TYPENUM(SINT32):
case UPB_TYPENUM(INT32):
case UPB_TYPENUM(ENUM):
ASSERT(r->GetRepeatedInt32(proto2_msg, proto2_f, i) == *p.int32);
break;
case UPB_TYPENUM(FIXED32):
case UPB_TYPENUM(UINT32):
ASSERT(r->GetRepeatedUInt32(proto2_msg, proto2_f, i) == *p.uint32);
break;
case UPB_TYPENUM(BOOL):
ASSERT(r->GetRepeatedBool(proto2_msg, proto2_f, i) == *p._bool);
break;
case UPB_TYPENUM(STRING):
case UPB_TYPENUM(BYTES): {
std::string str = r->GetRepeatedString(proto2_msg, proto2_f, i);
std::string str2((*p.str)->ptr, (*p.str)->byte_len);
ASSERT(str == str2);
break;
}
case UPB_TYPENUM(GROUP):
case UPB_TYPENUM(MESSAGE):
compare(r->GetRepeatedMessage(proto2_msg, proto2_f, i), *p.msg);
}
}
}
void compare_values(const google::protobuf::Reflection *r,
const google::protobuf::Message& proto2_msg,
const google::protobuf::FieldDescriptor *proto2_f,
struct upb_msg *upb_msg, struct upb_msg_fielddef *upb_f)
{
union upb_value_ptr p = upb_msg_getptr(upb_msg, upb_f);
switch(upb_f->type) {
default:
ASSERT(false);
case UPB_TYPENUM(DOUBLE):
ASSERT(r->GetDouble(proto2_msg, proto2_f) == *p._double);
break;
case UPB_TYPENUM(FLOAT):
ASSERT(r->GetFloat(proto2_msg, proto2_f) == *p._float);
break;
case UPB_TYPENUM(INT64):
case UPB_TYPENUM(SINT64):
case UPB_TYPENUM(SFIXED64):
ASSERT(r->GetInt64(proto2_msg, proto2_f) == *p.int64);
break;
case UPB_TYPENUM(UINT64):
case UPB_TYPENUM(FIXED64):
ASSERT(r->GetUInt64(proto2_msg, proto2_f) == *p.uint64);
break;
case UPB_TYPENUM(SFIXED32):
case UPB_TYPENUM(SINT32):
case UPB_TYPENUM(INT32):
case UPB_TYPENUM(ENUM):
ASSERT(r->GetInt32(proto2_msg, proto2_f) == *p.int32);
break;
case UPB_TYPENUM(FIXED32):
case UPB_TYPENUM(UINT32):
ASSERT(r->GetUInt32(proto2_msg, proto2_f) == *p.uint32);
break;
case UPB_TYPENUM(BOOL):
ASSERT(r->GetBool(proto2_msg, proto2_f) == *p._bool);
break;
case UPB_TYPENUM(STRING):
case UPB_TYPENUM(BYTES): {
std::string str = r->GetString(proto2_msg, proto2_f);
std::string str2((*p.str)->ptr, (*p.str)->byte_len);
ASSERT(str == str2);
break;
}
case UPB_TYPENUM(GROUP):
case UPB_TYPENUM(MESSAGE):
compare(r->GetMessage(proto2_msg, proto2_f), *p.msg);
}
}
void compare(const google::protobuf::Message& proto2_msg,
struct upb_msg *upb_msg)
{
const google::protobuf::Reflection *r = proto2_msg.GetReflection();
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++) {
struct upb_msg_fielddef *upb_f = &def->fields[i];
struct google_protobuf_FieldDescriptorProto *upb_fd =
upb_msg_field_descriptor(upb_f, def);
const google::protobuf::FieldDescriptor *proto2_f =
d->FindFieldByNumber(upb_fd->number);
// Make sure the definitions are equal.
ASSERT(upb_f);
ASSERT(upb_fd);
ASSERT(proto2_f);
ASSERT(upb_fd->number == proto2_f->number());
ASSERT(std::string(upb_fd->name->ptr, upb_fd->name->byte_len) ==
proto2_f->name());
ASSERT(upb_fd->type == proto2_f->type());
ASSERT(upb_isarray(upb_f) == proto2_f->is_repeated());
if(!upb_msg_isset(upb_msg, upb_f)) {
if(upb_isarray(upb_f))
ASSERT(r->FieldSize(proto2_msg, proto2_f) == 0);
else
ASSERT(r->HasField(proto2_msg, proto2_f) == false);
} else {
if(upb_isarray(upb_f))
compare_arrays(r, proto2_msg, proto2_f, upb_msg, upb_f);
else
compare_values(r, proto2_msg, proto2_f, upb_msg, upb_f);
}
}
}
void parse_and_compare(MESSAGE_CIDENT *proto2_msg, struct upb_msg *upb_msg,
struct upb_string *str)
{
// Parse to both proto2 and upb.
ASSERT(proto2_msg->ParseFromArray(str->ptr, str->byte_len));
ASSERT(upb_msg_parsestr(upb_msg, str->ptr, str->byte_len) == UPB_STATUS_OK);
compare(*proto2_msg, upb_msg);
}
int main()
{
// Initialize upb state, parse descriptor.
struct upb_context *c = upb_context_new();
struct upb_string *fds = upb_strreadfile(MESSAGE_DESCRIPTOR_FILE);
if(!fds) {
fprintf(stderr, "Couldn't read " MESSAGE_DESCRIPTOR_FILE ".\n");
return 1;
}
if(!upb_context_parsefds(c, fds)) {
fprintf(stderr, "Error importing " MESSAGE_DESCRIPTOR_FILE ".\n");
return 1;
}
upb_string_unref(fds);
struct upb_string *proto_name = upb_strdupc(MESSAGE_NAME);
struct upb_symtab_entry e;
bool success = upb_context_lookup(c, proto_name, &e);
if(!success || e.type != UPB_SYM_MESSAGE) {
fprintf(stderr, "Error finding symbol '" UPB_STRFMT "'.\n",
UPB_STRARG(proto_name));
return 1;
}
upb_string_unref(proto_name);
struct upb_msgdef *def = e.ref.msg;
// Read the message data itself.
struct upb_string *str = upb_strreadfile(MESSAGE_FILE);
if(!str) {
fprintf(stderr, "Error reading " MESSAGE_FILE "\n");
return 1;
}
// Run twice to test proper object reuse.
MESSAGE_CIDENT proto2_msg;
struct upb_msg *upb_msg = upb_msg_new(def);
parse_and_compare(&proto2_msg, upb_msg, str);
parse_and_compare(&proto2_msg, upb_msg, str);
printf("All tests passed, %d assertions.\n", num_assertions);
upb_msg_unref(upb_msg);
upb_string_unref(str);
upb_context_unref(c);
return 0;
}
Loading…
Cancel
Save