From 377871f10403c7b4e1cc6f769b9443b5197aecc8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 16 Dec 2018 14:32:14 -0800 Subject: Got test_decoder working! --- tests/pb/test_decoder.cc | 91 ++++++++++++++++++++++++--------------------- tests/pb/test_decoder.proto | 42 +++++++++++++++++++++ 2 files changed, 91 insertions(+), 42 deletions(-) (limited to 'tests') diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index a931779..d0e3fa3 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -36,6 +36,7 @@ #include "tests/test_util.h" #include "tests/upb_test.h" +#include "tests/pb/test_decoder.upbdefs.h" #ifdef AMALGAMATED #include "upb.h" @@ -387,7 +388,6 @@ void reg_subm(upb_handlers *h, uint32_t num) { ASSERT( h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); - ASSERT(upb_handlers_setsubhandlers(h, f, h)); } void reg_str(upb_handlers *h, uint32_t num) { @@ -399,52 +399,60 @@ void reg_str(upb_handlers *h, uint32_t num) { ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } -upb::reffed_ptr NewHandlers(TestMode mode) { - - upb::reffed_ptr h(upb::Handlers::New(NewMessageDef().get())); +struct HandlerRegisterData { + TestMode mode; +}; - if (mode == ALL_HANDLERS) { +void callback(const void *closure, upb_handlers *h) { + const HandlerRegisterData* data = + static_cast(closure); + if (data->mode == ALL_HANDLERS) { h->SetStartMessageHandler(UpbMakeHandler(startmsg)); h->SetEndMessageHandler(UpbMakeHandler(endmsg)); // Register handlers for each type. - reg(h.get(), UPB_DESCRIPTOR_TYPE_DOUBLE); - reg (h.get(), UPB_DESCRIPTOR_TYPE_FLOAT); - reg (h.get(), UPB_DESCRIPTOR_TYPE_INT64); - reg(h.get(), UPB_DESCRIPTOR_TYPE_UINT64); - reg (h.get(), UPB_DESCRIPTOR_TYPE_INT32); - reg(h.get(), UPB_DESCRIPTOR_TYPE_FIXED64); - reg(h.get(), UPB_DESCRIPTOR_TYPE_FIXED32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_BOOL); - reg(h.get(), UPB_DESCRIPTOR_TYPE_UINT32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_ENUM); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED64); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SINT32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SINT64); - - reg_str(h.get(), UPB_DESCRIPTOR_TYPE_STRING); - reg_str(h.get(), UPB_DESCRIPTOR_TYPE_BYTES); - reg_str(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_STRING)); - reg_str(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_BYTES)); + reg(h, UPB_DESCRIPTOR_TYPE_DOUBLE); + reg (h, UPB_DESCRIPTOR_TYPE_FLOAT); + reg (h, UPB_DESCRIPTOR_TYPE_INT64); + reg(h, UPB_DESCRIPTOR_TYPE_UINT64); + reg (h, UPB_DESCRIPTOR_TYPE_INT32); + reg(h, UPB_DESCRIPTOR_TYPE_FIXED64); + reg(h, UPB_DESCRIPTOR_TYPE_FIXED32); + reg (h, UPB_DESCRIPTOR_TYPE_BOOL); + reg(h, UPB_DESCRIPTOR_TYPE_UINT32); + reg (h, UPB_DESCRIPTOR_TYPE_ENUM); + reg (h, UPB_DESCRIPTOR_TYPE_SFIXED32); + reg (h, UPB_DESCRIPTOR_TYPE_SFIXED64); + reg (h, UPB_DESCRIPTOR_TYPE_SINT32); + reg (h, UPB_DESCRIPTOR_TYPE_SINT64); + + reg_str(h, UPB_DESCRIPTOR_TYPE_STRING); + reg_str(h, UPB_DESCRIPTOR_TYPE_BYTES); + reg_str(h, rep_fn(UPB_DESCRIPTOR_TYPE_STRING)); + reg_str(h, rep_fn(UPB_DESCRIPTOR_TYPE_BYTES)); // Register submessage/group handlers that are self-recursive // to this type, eg: message M { optional M m = 1; } - reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_MESSAGE); - reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); - reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_GROUP); - reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); + reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); + reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); + + if (h->message_def()->full_name() == std::string("DecoderTest")) { + reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP); + reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); + } // For NOP_FIELD we register no handlers, so we can pad a proto freely without // changing the output. } - - bool ok = h->Freeze(NULL); - ASSERT(ok); - - return h; } +upb::reffed_ptr NewHandlers(upb::SymbolTable* symtab, + TestMode mode) { + HandlerRegisterData handlerdata; + handlerdata.mode = mode; + return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback, + &handlerdata); +} /* Running of test cases ******************************************************/ @@ -1132,14 +1140,11 @@ upb::reffed_ptr NewMethod( return cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers)); } -void test_emptyhandlers(bool allowjit) { +void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { // Create an empty handlers to make sure that the decoder can handle empty // messages. - upb::reffed_ptr md = upb::MessageDef::New(); - ASSERT(md->set_full_name("Empty", NULL)); - ASSERT(md->Freeze(NULL)); - - upb::reffed_ptr h(upb::Handlers::New(md.get())); + const upb::MessageDef* md = Empty_getmsgdef(symtab); + upb::reffed_ptr h(upb::Handlers::New(md)); bool ok = h->Freeze(NULL); ASSERT(ok); upb::reffed_ptr method = @@ -1178,9 +1183,9 @@ upb::reffed_ptr method = void run_tests(bool use_jit) { upb::reffed_ptr method; upb::reffed_ptr handlers; - upb::SymbolTable symtab; + upb::SymbolTable* symtab = upb::SymbolTable::New(); - handlers = NewHandlers(test_mode); + handlers = NewHandlers(symtab, test_mode); global_handlers = handlers.get(); method = NewMethod(handlers.get(), use_jit); @@ -1191,7 +1196,9 @@ void run_tests(bool use_jit) { test_invalid(); test_valid(); - test_emptyhandlers(use_jit); + test_emptyhandlers(symtab, use_jit); + + upb::SymbolTable::Free(symtab); } void run_test_suite() { diff --git a/tests/pb/test_decoder.proto b/tests/pb/test_decoder.proto index 8197dea..e9fa6ad 100644 --- a/tests/pb/test_decoder.proto +++ b/tests/pb/test_decoder.proto @@ -5,6 +5,8 @@ enum TestEnum { FOO = 1; } +message Empty {} + message DecoderTest { optional double f_double = 1; optional float f_float = 2; @@ -62,6 +64,26 @@ message DecoderTest { optional sfixed64 f_sfixed64 = 16; optional sint32 f_sint32 = 17; optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; } optional group R_group = 536869921 { @@ -82,5 +104,25 @@ message DecoderTest { optional sfixed64 f_sfixed64 = 16; optional sint32 f_sint32 = 17; optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; } } -- cgit v1.2.3