parent
2a85bef825
commit
806c8c9c6e
6 changed files with 0 additions and 376 deletions
@ -1,165 +0,0 @@ |
||||
/*
|
||||
* |
||||
* A test that verifies that our results are identical to proto2 for a |
||||
* given proto type and input protobuf. |
||||
*/ |
||||
|
||||
#define __STDC_LIMIT_MACROS // So we get UINT32_MAX
|
||||
#include <assert.h> |
||||
#include <google/protobuf/descriptor.h> |
||||
#include <google/protobuf/dynamic_message.h> |
||||
#include <google/protobuf/message.h> |
||||
#include <google/protobuf/text_format.h> |
||||
#include <google/protobuf/wire_format_lite.h> |
||||
#include <inttypes.h> |
||||
#include <stdint.h> |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include "tests/google_messages.pb.h" |
||||
#include "tests/upb_test.h" |
||||
#include "upb/bindings/googlepb/bridge.h" |
||||
#include "upb/def.h" |
||||
#include "upb/handlers.h" |
||||
#include "upb/pb/decoder.h" |
||||
#include "upb/pb/glue.h" |
||||
#include "upb/pb/varint.int.h" |
||||
|
||||
// Pull in string data from tests/google_message{1,2}.dat
|
||||
// (the .h files are generated with xxd).
|
||||
const unsigned char message1_data[] = { |
||||
#include "tests/google_message1.h" |
||||
}; |
||||
|
||||
const unsigned char message2_data[] = { |
||||
#include "tests/google_message2.h" |
||||
}; |
||||
|
||||
void compare_metadata(const google::protobuf::Descriptor* d, |
||||
const upb::MessageDef *upb_md) { |
||||
ASSERT(d->field_count() == upb_md->field_count()); |
||||
for (upb::MessageDef::const_field_iterator i = upb_md->field_begin(); |
||||
i != upb_md->field_end(); ++i) { |
||||
const upb::FieldDef* upb_f = *i; |
||||
const google::protobuf::FieldDescriptor *proto2_f = |
||||
d->FindFieldByNumber(upb_f->number()); |
||||
ASSERT(upb_f); |
||||
ASSERT(proto2_f); |
||||
ASSERT(upb_f->number() == (uint32_t)proto2_f->number()); |
||||
ASSERT(std::string(upb_f->name()) == proto2_f->name()); |
||||
ASSERT(upb_f->descriptor_type() == |
||||
static_cast<upb::FieldDef::DescriptorType>(proto2_f->type())); |
||||
ASSERT(upb_f->IsSequence() == proto2_f->is_repeated()); |
||||
} |
||||
} |
||||
|
||||
void print_diff(const google::protobuf::Message& msg1, |
||||
const google::protobuf::Message& msg2) { |
||||
std::string text_str1; |
||||
std::string text_str2; |
||||
google::protobuf::TextFormat::PrintToString(msg1, &text_str1); |
||||
google::protobuf::TextFormat::PrintToString(msg2, &text_str2); |
||||
fprintf(stderr, "str1: %s, str2: %s\n", text_str1.c_str(), text_str2.c_str()); |
||||
} |
||||
|
||||
void parse_and_compare(google::protobuf::Message *msg1, |
||||
google::protobuf::Message *msg2, |
||||
const upb::Handlers *protomsg_handlers, |
||||
const char *str, size_t len, bool allow_jit) { |
||||
// Parse to both proto2 and upb.
|
||||
ASSERT(msg1->ParseFromArray(str, len)); |
||||
|
||||
upb::pb::CodeCache cache; |
||||
ASSERT(cache.set_allow_jit(allow_jit)); |
||||
upb::reffed_ptr<const upb::pb::DecoderMethod> decoder_method( |
||||
cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(protomsg_handlers))); |
||||
|
||||
upb::Status status; |
||||
upb::Environment env; |
||||
env.ReportErrorsTo(&status); |
||||
upb::Sink protomsg_sink(protomsg_handlers, msg2); |
||||
upb::pb::Decoder* decoder = |
||||
upb::pb::Decoder::Create(&env, decoder_method.get(), &protomsg_sink); |
||||
|
||||
msg2->Clear(); |
||||
bool ok = upb::BufferSource::PutBuffer(str, len, decoder->input()); |
||||
if (!ok) { |
||||
fprintf(stderr, "error parsing: %s\n", status.error_message()); |
||||
print_diff(*msg1, *msg2); |
||||
} |
||||
ASSERT(ok); |
||||
ASSERT(status.ok()); |
||||
|
||||
// Would like to just compare the message objects themselves, but
|
||||
// unfortunately MessageDifferencer is not part of the open-source release of
|
||||
// proto2, so we compare their serialized strings, which we expect will be
|
||||
// equivalent.
|
||||
std::string str1; |
||||
std::string str2; |
||||
msg1->SerializeToString(&str1); |
||||
msg2->SerializeToString(&str2); |
||||
if (str1 != str2) { |
||||
print_diff(*msg1, *msg2); |
||||
} |
||||
ASSERT(str1 == str2); |
||||
ASSERT(std::string(str, len) == str2); |
||||
} |
||||
|
||||
void test_zig_zag() { |
||||
for (uint64_t num = 5; num * 1.5 < UINT64_MAX; num *= 1.5) { |
||||
ASSERT(upb_zzenc_64(num) == |
||||
google::protobuf::internal::WireFormatLite::ZigZagEncode64(num)); |
||||
if (num < UINT32_MAX) { |
||||
ASSERT(upb_zzenc_32(num) == |
||||
google::protobuf::internal::WireFormatLite::ZigZagEncode32(num)); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
extern "C" { |
||||
|
||||
int run_tests(int argc, char *argv[]) { |
||||
UPB_UNUSED(argc); |
||||
UPB_UNUSED(argv); |
||||
UPB_UNUSED(message1_data); |
||||
UPB_UNUSED(message2_data); |
||||
size_t len = sizeof(MESSAGE_DATA_IDENT); |
||||
const char *str = (const char*)MESSAGE_DATA_IDENT; |
||||
|
||||
MESSAGE_CIDENT msg1; |
||||
MESSAGE_CIDENT msg2; |
||||
|
||||
upb::reffed_ptr<const upb::Handlers> h( |
||||
upb::googlepb::WriteHandlers::New(msg1)); |
||||
|
||||
compare_metadata(msg1.GetDescriptor(), h->message_def()); |
||||
|
||||
// Run twice to test proper object reuse.
|
||||
parse_and_compare(&msg1, &msg2, h.get(), str, len, false); |
||||
parse_and_compare(&msg1, &msg2, h.get(), str, len, true); |
||||
parse_and_compare(&msg1, &msg2, h.get(), str, len, false); |
||||
parse_and_compare(&msg1, &msg2, h.get(), str, len, true); |
||||
|
||||
// Test with DynamicMessage.
|
||||
google::protobuf::DynamicMessageFactory* factory = |
||||
new google::protobuf::DynamicMessageFactory; |
||||
const google::protobuf::Message* prototype = |
||||
factory->GetPrototype(msg1.descriptor()); |
||||
google::protobuf::Message* dyn_msg1 = prototype->New(); |
||||
google::protobuf::Message* dyn_msg2 = prototype->New(); |
||||
h = upb::googlepb::WriteHandlers::New(*dyn_msg1); |
||||
parse_and_compare(dyn_msg1, dyn_msg2, h.get(), str, len, false); |
||||
parse_and_compare(dyn_msg1, dyn_msg2, h.get(), str, len, true); |
||||
delete dyn_msg1; |
||||
delete dyn_msg2; |
||||
delete factory; |
||||
|
||||
test_zig_zag(); |
||||
|
||||
printf("All tests passed, %d assertions.\n", num_assertions); |
||||
|
||||
google::protobuf::ShutdownProtobufLibrary(); |
||||
return 0; |
||||
} |
||||
|
||||
} |
@ -1,62 +0,0 @@ |
||||
#!/usr/bin/ruby |
||||
# |
||||
# Tests for Ruby upb extension. |
||||
|
||||
require 'test/unit' |
||||
require 'set' |
||||
require 'upb' |
||||
|
||||
def get_descriptor |
||||
File.open("upb/descriptor/descriptor.pb").read |
||||
end |
||||
|
||||
def load_descriptor |
||||
symtab = Upb::SymbolTable.new |
||||
symtab.load_descriptor(get_descriptor()) |
||||
return symtab |
||||
end |
||||
|
||||
def get_message_class(name) |
||||
return Upb.get_message_class(load_descriptor().lookup(name)) |
||||
end |
||||
|
||||
class TestRubyExtension < Test::Unit::TestCase |
||||
def test_parsedescriptor |
||||
msgdef = load_descriptor.lookup("google.protobuf.FileDescriptorSet") |
||||
assert_instance_of(Upb::MessageDef, msgdef) |
||||
|
||||
file_descriptor_set = Upb.get_message_class(msgdef) |
||||
msg = file_descriptor_set.parse(get_descriptor()) |
||||
|
||||
# A couple message types we know should exist. |
||||
names = Set.new(["DescriptorProto", "FieldDescriptorProto"]) |
||||
|
||||
msg.file.each { |file| |
||||
file.message_type.each { |message_type| |
||||
names.delete(message_type.name) |
||||
} |
||||
} |
||||
|
||||
assert_equal(0, names.size) |
||||
end |
||||
|
||||
def test_parseserialize |
||||
field_descriptor_proto = get_message_class("google.protobuf.FieldDescriptorProto") |
||||
field_options = get_message_class("google.protobuf.FieldOptions") |
||||
|
||||
field = field_descriptor_proto.new |
||||
|
||||
field.name = "MyName" |
||||
field.number = 5 |
||||
field.options = field_options.new |
||||
field.options.packed = true |
||||
|
||||
serialized = Upb::Message.serialize(field) |
||||
|
||||
field2 = field_descriptor_proto.parse(serialized) |
||||
|
||||
assert_equal("MyName", field2.name) |
||||
assert_equal(5, field2.number) |
||||
assert_equal(true, field2.options.packed) |
||||
end |
||||
end |
Binary file not shown.
Binary file not shown.
@ -1,149 +0,0 @@ |
||||
|
||||
package benchmarks; |
||||
|
||||
option optimize_for = SPEED; |
||||
|
||||
enum Foo { |
||||
FOO_VALUE = 1; |
||||
FOO_VALUE2 = 2; |
||||
} |
||||
|
||||
message Simple { |
||||
message M2 { |
||||
optional int32 f1 = 1234567; |
||||
} |
||||
optional M2 m2 = 1; |
||||
} |
||||
|
||||
message SpeedMessage1 { |
||||
required string field1 = 1; |
||||
optional string field9 = 9; |
||||
optional string field18 = 18; |
||||
optional bool field80 = 80 [default=false]; |
||||
optional bool field81 = 81 [default=true]; |
||||
required int32 field2 = 2; |
||||
required int32 field3 = 3; |
||||
optional int32 field280 = 280; |
||||
optional int32 field6 = 6 [default=0]; |
||||
optional int64 field22 = 22; |
||||
optional string field4 = 4; |
||||
repeated fixed64 field5 = 5; |
||||
optional bool field59 = 59 [default=false]; |
||||
optional string field7 = 7; |
||||
optional int32 field16 = 16; |
||||
optional int32 field130 = 130 [default=0]; |
||||
optional bool field12 = 12 [default=true]; |
||||
optional bool field17 = 17 [default=true]; |
||||
optional bool field13 = 13 [default=true]; |
||||
optional bool field14 = 14 [default=true]; |
||||
optional int32 field104 = 104 [default=0]; |
||||
optional int32 field100 = 100 [default=0]; |
||||
optional int32 field101 = 101 [default=0]; |
||||
optional string field102 = 102; |
||||
optional string field103 = 103; |
||||
optional int32 field29 = 29 [default=0]; |
||||
optional bool field30 = 30 [default=false]; |
||||
optional int32 field60 = 60 [default=-1]; |
||||
optional int32 field271 = 271 [default=-1]; |
||||
optional int32 field272 = 272 [default=-1]; |
||||
optional int32 field150 = 150; |
||||
optional int32 field23 = 23 [default=0]; |
||||
optional bool field24 = 24 [default=false]; |
||||
optional int32 field25 = 25 [default=0]; |
||||
optional SpeedMessage1SubMessage field15 = 15; |
||||
optional bool field78 = 78; |
||||
optional int32 field67 = 67 [default=0]; |
||||
optional int32 field68 = 68; |
||||
optional int32 field128 = 128 [default=0]; |
||||
optional string field129 = 129 [default="xxxxxxxxxxxxxxxxxxxxx"]; |
||||
optional int32 field131 = 131 [default=0]; |
||||
optional Foo field132 = 132 [default=FOO_VALUE]; |
||||
} |
||||
|
||||
message SpeedMessage1SubMessage { |
||||
optional int32 field1 = 1 [default=0]; |
||||
optional int32 field2 = 2 [default=0]; |
||||
optional int32 field3 = 3 [default=0]; |
||||
optional string field15 = 15 [default="FOOBAR!"]; |
||||
optional bool field12 = 12 [default=true]; |
||||
optional int64 field13 = 13; |
||||
optional int64 field14 = 14; |
||||
optional int32 field16 = 16; |
||||
optional int32 field19 = 19 [default=2]; |
||||
optional bool field20 = 20 [default=true]; |
||||
optional bool field28 = 28 [default=true]; |
||||
optional fixed64 field21 = 21; |
||||
optional int32 field22 = 22; |
||||
optional bool field23 = 23 [ default=false ]; |
||||
optional bool field206 = 206 [default=false]; |
||||
optional fixed32 field203 = 203; |
||||
optional int32 field204 = 204; |
||||
optional string field205 = 205; |
||||
optional uint64 field207 = 207; |
||||
optional uint64 field300 = 300; |
||||
} |
||||
|
||||
message SpeedMessage2 { |
||||
optional string field1 = 1; |
||||
optional int64 field3 = 3; |
||||
optional int64 field4 = 4; |
||||
optional int64 field30 = 30; |
||||
optional bool field75 = 75 [default=false]; |
||||
optional string field6 = 6; |
||||
optional bytes field2 = 2; |
||||
optional int32 field21 = 21 [default=0]; |
||||
optional int32 field71 = 71; |
||||
optional float field25 = 25; |
||||
optional int32 field109 = 109 [default=0]; |
||||
optional int32 field210 = 210 [default=0]; |
||||
optional int32 field211 = 211 [default=0]; |
||||
optional int32 field212 = 212 [default=0]; |
||||
optional int32 field213 = 213 [default=0]; |
||||
optional int32 field216 = 216 [default=0]; |
||||
optional int32 field217 = 217 [default=0]; |
||||
optional int32 field218 = 218 [default=0]; |
||||
optional int32 field220 = 220 [default=0]; |
||||
optional int32 field221 = 221 [default=0]; |
||||
optional float field222 = 222 [default=0.0]; |
||||
optional int32 field63 = 63; |
||||
|
||||
repeated group Group1 = 10 { |
||||
required float field11 = 11; |
||||
optional float field26 = 26; |
||||
optional string field12 = 12; |
||||
optional string field13 = 13; |
||||
repeated string field14 = 14; |
||||
required uint64 field15 = 15; |
||||
optional int32 field5 = 5; |
||||
optional string field27 = 27; |
||||
optional int32 field28 = 28; |
||||
optional string field29 = 29; |
||||
optional string field16 = 16; |
||||
repeated string field22 = 22; |
||||
repeated int32 field73 = 73; |
||||
optional int32 field20 = 20 [default=0]; |
||||
optional string field24 = 24; |
||||
optional SpeedMessage2GroupedMessage field31 = 31; |
||||
} |
||||
repeated string field128 = 128; |
||||
optional int64 field131 = 131; |
||||
repeated string field127 = 127; |
||||
optional int32 field129 = 129; |
||||
repeated int64 field130 = 130; |
||||
optional bool field205 = 205 [default=false]; |
||||
optional bool field206 = 206 [default=false]; |
||||
} |
||||
|
||||
message SpeedMessage2GroupedMessage { |
||||
optional float field1 = 1; |
||||
optional float field2 = 2; |
||||
optional float field3 = 3 [default=0.0]; |
||||
optional bool field4 = 4; |
||||
optional bool field5 = 5; |
||||
optional bool field6 = 6 [default=true]; |
||||
optional bool field7 = 7 [default=false]; |
||||
optional float field8 = 8; |
||||
optional bool field9 = 9; |
||||
optional float field10 = 10; |
||||
optional int64 field11 = 11; |
||||
} |
Binary file not shown.
Loading…
Reference in new issue