diff options
author | Josh Haberman <haberman@google.com> | 2013-05-11 16:45:38 -0700 |
---|---|---|
committer | Josh Haberman <haberman@google.com> | 2013-05-11 16:45:38 -0700 |
commit | cfdb9907cb87d15eaab72ceefbfa42fd7a4c3127 (patch) | |
tree | 63f5d70ad64daeeb4ffc777c2c3afd50e2e281b1 /tests/test_vs_proto2.cc | |
parent | 7d3e2bd2c4cfd1296d1d6f996d7548de26540d41 (diff) |
Synced with 3 months of Google-internal development.
Major changes:
- Got rid of all bytestream interfaces in favor of
using regular handlers.
- new Pipeline object represents a upb pipeline, does
bump allocation internally to manage memory.
- proto2 support now can handle extensions.
Diffstat (limited to 'tests/test_vs_proto2.cc')
-rw-r--r-- | tests/test_vs_proto2.cc | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc index 5eca399..b1a6053 100644 --- a/tests/test_vs_proto2.cc +++ b/tests/test_vs_proto2.cc @@ -10,6 +10,7 @@ #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/wire_format_lite.h> #include <inttypes.h> @@ -17,10 +18,11 @@ #include <stdio.h> #include <stdlib.h> #include "benchmarks/google_messages.pb.h" -#include "bindings/cpp/upb/pb/decoder.hpp" +#include "upb/bytestream.h" #include "upb/def.h" #include "upb/google/bridge.h" #include "upb/handlers.h" +#include "upb/pb/decoder.h" #include "upb/pb/glue.h" #include "upb/pb/varint.h" #include "upb_test.h" @@ -36,25 +38,36 @@ void compare_metadata(const google::protobuf::Descriptor* d, ASSERT(proto2_f); ASSERT(upb_f->number() == proto2_f->number()); ASSERT(std::string(upb_f->name()) == proto2_f->name()); - ASSERT(upb_f->type() == static_cast<upb::FieldDef::Type>(proto2_f->type())); + ASSERT(upb_f->descriptor_type() == + static_cast<upb::FieldDef::DescriptorType>(proto2_f->type())); ASSERT(upb_f->IsSequence() == proto2_f->is_repeated()); } } -void parse_and_compare(MESSAGE_CIDENT *msg1, MESSAGE_CIDENT *msg2, - const upb::Handlers *handlers, +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::DecoderPlan* plan = upb::DecoderPlan::New(handlers, allow_jit); - upb::StringSource src(str, len); - upb::Decoder decoder; - decoder.ResetPlan(plan); - decoder.ResetInput(src.AllBytes(), msg2); + const upb::Handlers* decoder_handlers = upb::pb::GetDecoderHandlers( + protomsg_handlers, allow_jit, &decoder_handlers); + + upb::Pipeline pipeline(NULL, 0, upb_realloc, NULL); + pipeline.DonateRef(decoder_handlers, &decoder_handlers); + upb::Sink* protomsg_sink = pipeline.NewSink(protomsg_handlers); + upb::Sink* decoder_sink = pipeline.NewSink(decoder_handlers); + + protomsg_sink->Reset(msg2); + upb::pb::Decoder* decoder = + decoder_sink->base()->GetUserdata<upb::pb::Decoder>(); + upb::pb::ResetDecoderSink(decoder, protomsg_sink); + msg2->Clear(); - ASSERT(decoder.Decode() == UPB_OK); - plan->Unref(); + bool ok = upb::PutStringToBytestream(decoder_sink, str, len); + ASSERT(ok); + ASSERT(pipeline.status().ok()); // Would like to just compare the message objects themselves, but // unfortunately MessageDifferencer is not part of the open-source release of @@ -110,13 +123,29 @@ int run_tests(int argc, char *argv[]) parse_and_compare(&msg1, &msg2, h, str, len, true); parse_and_compare(&msg1, &msg2, h, str, len, false); parse_and_compare(&msg1, &msg2, h, str, len, true); - printf("All tests passed, %d assertions.\n", num_assertions); + h->Unref(&h); + // 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::google::NewWriteHandlers(*dyn_msg1, &h); + parse_and_compare(dyn_msg1, dyn_msg2, h, str, len, false); + parse_and_compare(dyn_msg1, dyn_msg2, h, str, len, true); + delete dyn_msg1; + delete dyn_msg2; + delete factory; h->Unref(&h); + free((void*)str); test_zig_zag(); + printf("All tests passed, %d assertions.\n", num_assertions); + google::protobuf::ShutdownProtobufLibrary(); return 0; } |