From cfdb9907cb87d15eaab72ceefbfa42fd7a4c3127 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 11 May 2013 16:45:38 -0700 Subject: 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. --- tests/test_vs_proto2.cc | 53 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 12 deletions(-) (limited to 'tests/test_vs_proto2.cc') 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 #include +#include #include #include #include @@ -17,10 +18,11 @@ #include #include #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(proto2_f->type())); + ASSERT(upb_f->descriptor_type() == + static_cast(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::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; } -- cgit v1.2.3