diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test.proto | 14 | ||||
-rw-r--r-- | tests/test_cpp.cc | 737 | ||||
-rw-r--r-- | tests/test_decoder.cc | 159 | ||||
-rw-r--r-- | tests/test_def.c | 3 | ||||
-rw-r--r-- | tests/test_handlers.c | 6 | ||||
-rw-r--r-- | tests/test_pipeline.c | 118 | ||||
-rw-r--r-- | tests/test_vs_proto2.cc | 42 | ||||
-rw-r--r-- | tests/upb_test.h | 2 |
8 files changed, 836 insertions, 245 deletions
diff --git a/tests/test.proto b/tests/test.proto index e634ed2..40e185b 100644 --- a/tests/test.proto +++ b/tests/test.proto @@ -38,10 +38,12 @@ message F { // A proto with a bunch of simple primitives. message SimplePrimitives { - optional fixed64 a = 1; - optional fixed32 b = 2; - optional double c = 3; - optional float d = 5; - //optional sint64 e = 6; - //optional sint32 f = 7; + optional fixed64 u64 = 1; + optional fixed32 u32 = 2; + optional double dbl = 3; + optional float flt = 5; + optional sint64 i64 = 6; + optional sint32 i32 = 7; + optional bool b = 8; + optional string str = 9; } diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 2b122a3..8d06135 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -9,8 +9,11 @@ #include <stdio.h> #include <string.h> + #include <iostream> #include <set> +#include <type_traits> + #include "upb/def.h" #include "upb/descriptor/reader.h" #include "upb/handlers.h" @@ -25,39 +28,133 @@ void AssertInsert(T* const container, const typename T::value_type& val) { ASSERT(inserted); } -static void TestCasts1() { - const upb::MessageDef* md = upb::MessageDef::New(&md); - const upb::Def* def = md->Upcast(); +static void TestCastsUpDown() { + upb::reffed_ptr<const upb::MessageDef> reffed_md(upb::MessageDef::New()); + const upb::MessageDef* md = reffed_md.get(); + + // Upcast to reffed_ptr implicitly. + upb::reffed_ptr<const upb::Def> reffed_def = reffed_md; + ASSERT(reffed_def.get() == upb::upcast(reffed_md.get())); + + // Upcast to raw pointer must be explicit. + const upb::Def* def = upb::upcast(md); + ASSERT(def == reffed_def.get()); + const upb::Def* def2 = upb::upcast(reffed_md.get()); + ASSERT(def2 == reffed_def.get()); + + // Downcast/dyncast of raw pointer uses upb::down_cast/upb::dyn_cast. const upb::MessageDef* md2 = upb::down_cast<const upb::MessageDef*>(def); const upb::MessageDef* md3 = upb::dyn_cast<const upb::MessageDef*>(def); - ASSERT(md == md2); ASSERT(md == md3); - const upb::EnumDef* ed = upb::dyn_cast<const upb::EnumDef*>(def); - ASSERT(!ed); + // Downcast/dyncast of reffed_ptr uses down_cast/dyn_cast members. + upb::reffed_ptr<const upb::MessageDef> md4( + reffed_def.down_cast<const upb::MessageDef>()); + upb::reffed_ptr<const upb::MessageDef> md5( + reffed_def.dyn_cast<const upb::MessageDef>()); + ASSERT(md == md4.get()); + ASSERT(md == md5.get()); - md->Unref(&md); + // Failed dyncast returns NULL. + ASSERT(upb::dyn_cast<const upb::EnumDef*>(def) == NULL); + ASSERT(reffed_def.dyn_cast<const upb::EnumDef>().get() == NULL); } -static void TestCasts2() { - // Test non-const -> const cast. - upb::MessageDef* md = upb::MessageDef::New(&md); - upb::Def* def = md->Upcast(); - const upb::MessageDef* const_md = upb::down_cast<const upb::MessageDef*>(def); - ASSERT(const_md == md); - md->Unref(&md); +static void TestCastsConst0() { + // Should clean up properly even if it is not assigned to anything. + upb::MessageDef::New(); +} + +static void TestCastsConst1() { + // Test reffed mutable -> reffed mutable construction/assignment. + upb::reffed_ptr<upb::MessageDef> md(upb::MessageDef::New()); + upb::MessageDef *md2 = md.get(); + md = upb::MessageDef::New(); + ASSERT(md.get()); + ASSERT(md.get() != md2); +} + +static void TestCastsConst2() { + // Test reffed mutable -> reffed mutable upcast construction/assignment. + upb::reffed_ptr<upb::MessageDef> md(upb::MessageDef::New()); + upb::reffed_ptr<upb::Def> def = md; + ASSERT(upb::upcast(md.get()) == def.get()); + def = md; + ASSERT(upb::upcast(md.get()) == def.get()); +} + +static void TestCastsConst3() { + // Test reffed mutable -> reffed mutable downcast. + upb::reffed_ptr<upb::Def> def(upb::MessageDef::New()); + upb::reffed_ptr<upb::MessageDef> md = def.down_cast<upb::MessageDef>(); + ASSERT(upb::upcast(md.get()) == def.get()); +} + +static void TestCastsConst4() { + // Test reffed mutable -> reffed mutable dyncast. + upb::reffed_ptr<upb::Def> def(upb::MessageDef::New()); + upb::reffed_ptr<upb::MessageDef> md = def.dyn_cast<upb::MessageDef>(); + ASSERT(upb::upcast(md.get()) == def.get()); +} + +static void TestCastsConst5() { + // Test reffed mutable -> reffed const construction/assignment. + upb::reffed_ptr<const upb::MessageDef> md(upb::MessageDef::New()); + const upb::MessageDef *md2 = md.get(); + md = upb::MessageDef::New(); + ASSERT(md.get()); + ASSERT(md.get() != md2); +} + +static void TestCastsConst6() { + // Test reffed mutable -> reffed const upcast construction/assignment. + upb::reffed_ptr<upb::MessageDef> md(upb::MessageDef::New()); + upb::reffed_ptr<const upb::Def> def = md; + ASSERT(upb::upcast(md.get()) == def.get()); + def = md; + ASSERT(upb::upcast(md.get()) == def.get()); +} + +static void TestCastsConst7() { + // Test reffed mutable -> reffed const downcast. + upb::reffed_ptr<upb::Def> def(upb::MessageDef::New()); + upb::reffed_ptr<const upb::MessageDef> md = + def.down_cast<const upb::MessageDef>(); + ASSERT(upb::upcast(md.get()) == def.get()); +} + +static void TestCastsConst8() { + // Test reffed mutable -> reffed const dyncast. + upb::reffed_ptr<upb::Def> def(upb::MessageDef::New()); + upb::reffed_ptr<const upb::MessageDef> md = + def.dyn_cast<const upb::MessageDef>(); + ASSERT(upb::upcast(md.get()) == def.get()); +} + +static void TestCastsConst9() { + // Test plain mutable -> plain mutable upcast + upb::reffed_ptr<upb::MessageDef> md(upb::MessageDef::New()); + upb::Def* def = upb::upcast(md.get()); + ASSERT(upb::down_cast<upb::MessageDef*>(def) == md.get()); +} + +static void TestCastsConst10() { + // Test plain const -> plain const upcast + upb::reffed_ptr<const upb::MessageDef> md(upb::MessageDef::New()); + const upb::Def* def = upb::upcast(md.get()); + ASSERT(upb::down_cast<const upb::MessageDef*>(def) == md.get()); } static void TestSymbolTable(const char *descriptor_file) { - upb::SymbolTable *s = upb::SymbolTable::New(&s); + upb::reffed_ptr<upb::SymbolTable> s(upb::SymbolTable::New()); upb::Status status; - if (!upb::LoadDescriptorFileIntoSymtab(s, descriptor_file, &status)) { - std::cerr << "Couldn't load descriptor: " << status.GetString(); + if (!upb::LoadDescriptorFileIntoSymtab(s.get(), descriptor_file, &status)) { + std::cerr << "Couldn't load descriptor: " << status.error_message(); exit(1); } - const upb::MessageDef* md = s->LookupMessage("C", &md); - ASSERT(md); + upb::reffed_ptr<const upb::MessageDef> md(s->LookupMessage("C")); + ASSERT(md.get()); // We want a def that satisfies this to test iteration. ASSERT(md->field_count() > 1); @@ -65,15 +162,554 @@ static void TestSymbolTable(const char *descriptor_file) { #ifdef UPB_CXX11 // Test range-based for. std::set<const upb::FieldDef*> fielddefs; - for (const upb::FieldDef* f : *md) { + for (const upb::FieldDef* f : *md.get()) { AssertInsert(&fielddefs, f); - ASSERT(f->containing_type() == md); + ASSERT(f->containing_type() == md.get()); } ASSERT(fielddefs.size() == md->field_count()); #endif - s->Unref(&s); - md->Unref(&md); + ASSERT(md.get()); +} + +static void TestCasts1() { + upb::reffed_ptr<const upb::MessageDef> md(upb::MessageDef::New()); + const upb::Def* def = upb::upcast(md.get()); + const upb::MessageDef* md2 = upb::down_cast<const upb::MessageDef*>(def); + const upb::MessageDef* md3 = upb::dyn_cast<const upb::MessageDef*>(def); + + ASSERT(md.get() == md2); + ASSERT(md.get() == md3); + + const upb::EnumDef* ed = upb::dyn_cast<const upb::EnumDef*>(def); + ASSERT(!ed); +} + +static void TestCasts2() { + // Test mutable -> const cast. + upb::reffed_ptr<upb::MessageDef> md(upb::MessageDef::New()); + upb::Def* def = upb::upcast(md.get()); + const upb::MessageDef* const_md = upb::down_cast<const upb::MessageDef*>(def); + ASSERT(const_md == md.get()); +} + +// +// Tests for registering and calling handlers in all their variants. +// This test code is very repetitive because we have to declare each +// handler function variant separately, and they all have different +// signatures so it does not lend itself well to templates. +// +// We test three handler types: +// StartMessage (no data params) +// Int32 (1 data param (int32_t)) +// String Buf (2 data params (const char*, size_t)) +// +// For each handler type we test all 8 handler variants: +// (handler data?) x (function/method) x (returns {void, success}) +// +// The one notable thing we don't test at the moment is +// StartSequence/StartString handlers: these are different from StartMessage() +// in that they return void* for the sub-closure. But this is exercised in +// other tests. +// + +static const int kExpectedHandlerData = 1232323; + +class StringBufTesterBase { + public: + static const upb::FieldDef::Type kFieldType = UPB_TYPE_STRING; + + StringBufTesterBase() : seen_(false), handler_data_val_(0) {} + + void CallAndVerify(upb::Sink* sink, const upb::FieldDef* f) { + upb::Handlers::Selector start; + ASSERT(upb::Handlers::GetSelector(f, UPB_HANDLER_STARTSTR, &start)); + upb::Handlers::Selector str; + ASSERT(upb::Handlers::GetSelector(f, UPB_HANDLER_STRING, &str)); + + ASSERT(!seen_); + upb::Sink sub; + sink->StartMessage(); + sink->StartString(start, 0, &sub); + sub.PutStringBuffer(str, NULL, 5); + ASSERT(seen_); + ASSERT(len_ == 5); + ASSERT(handler_data_val_ == kExpectedHandlerData); + } + + protected: + bool seen_; + int handler_data_val_; + size_t len_; +}; + +// Test all 8 combinations of: +// (handler data?) x (function/method) x (returns {void, size_t}) + +class StringBufTesterVoidFunctionNoHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterVoidFunctionNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler(f, UpbMakeHandler(&Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + static void Handler(ME* t, const char *buf, size_t len) { + t->seen_ = true; + t->len_ = len; + } +}; + +class StringBufTesterSizeTFunctionNoHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterSizeTFunctionNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler(f, UpbMakeHandler(&Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + static size_t Handler(ME* t, const char *buf, size_t len) { + t->seen_ = true; + t->len_ = len; + return len; + } +}; + +class StringBufTesterVoidMethodNoHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterVoidMethodNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + void Handler(const char *buf, size_t len) { + seen_ = true; + len_ = len; + } +}; + +class StringBufTesterSizeTMethodNoHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterSizeTMethodNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + size_t Handler(const char *buf, size_t len) { + seen_ = true; + len_ = len; + return len; + } +}; + +class StringBufTesterVoidFunctionWithHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterVoidFunctionWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler( + f, UpbBind(&Handler, new int(kExpectedHandlerData)))); + } + + private: + static void Handler(ME* t, const int* hd, const char *buf, size_t len) { + t->handler_data_val_ = *hd; + t->seen_ = true; + t->len_ = len; + } +}; + +class StringBufTesterSizeTFunctionWithHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterSizeTFunctionWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler( + f, UpbBind(&Handler, new int(kExpectedHandlerData)))); + } + + private: + static size_t Handler(ME* t, const int* hd, const char *buf, size_t len) { + t->handler_data_val_ = *hd; + t->seen_ = true; + t->len_ = len; + return len; + } +}; + +class StringBufTesterVoidMethodWithHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterVoidMethodWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler( + f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); + } + + private: + void Handler(const int* hd, const char *buf, size_t len) { + handler_data_val_ = *hd; + seen_ = true; + len_ = len; + } +}; + +class StringBufTesterSizeTMethodWithHandlerData : public StringBufTesterBase { + public: + typedef StringBufTesterSizeTMethodWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStringHandler( + f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); + } + + private: + size_t Handler(const int* hd, const char *buf, size_t len) { + handler_data_val_ = *hd; + seen_ = true; + len_ = len; + return len; + } +}; + +class StartMsgTesterBase { + public: + // We don't need the FieldDef it will create, but the test harness still + // requires that we provide one. + static const upb::FieldDef::Type kFieldType = UPB_TYPE_STRING; + + StartMsgTesterBase() : seen_(false), handler_data_val_(0) {} + + void CallAndVerify(upb::Sink* sink, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(!seen_); + sink->StartMessage(); + ASSERT(seen_); + ASSERT(handler_data_val_ == kExpectedHandlerData); + } + + protected: + bool seen_; + int handler_data_val_; +}; + +// Test all 8 combinations of: +// (handler data?) x (function/method) x (returns {void, bool}) + +class StartMsgTesterVoidFunctionNoHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterVoidFunctionNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + //static void Handler(ME* t) { + static void Handler(ME* t) { + t->seen_ = true; + } +}; + +class StartMsgTesterBoolFunctionNoHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterBoolFunctionNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + static bool Handler(ME* t) { + t->seen_ = true; + return true; + } +}; + +class StartMsgTesterVoidMethodNoHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterVoidMethodNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&ME::Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + void Handler() { + seen_ = true; + } +}; + +class StartMsgTesterBoolMethodNoHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterBoolMethodNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&ME::Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + bool Handler() { + seen_ = true; + return true; + } +}; + +class StartMsgTesterVoidFunctionWithHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterVoidFunctionWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler( + UpbBind(&Handler, new int(kExpectedHandlerData)))); + } + + private: + static void Handler(ME* t, const int* hd) { + t->handler_data_val_ = *hd; + t->seen_ = true; + } +}; + +class StartMsgTesterBoolFunctionWithHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterBoolFunctionWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler( + UpbBind(&Handler, new int(kExpectedHandlerData)))); + } + + private: + static bool Handler(ME* t, const int* hd) { + t->handler_data_val_ = *hd; + t->seen_ = true; + return true; + } +}; + +class StartMsgTesterVoidMethodWithHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterVoidMethodWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler( + UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); + } + + private: + void Handler(const int* hd) { + handler_data_val_ = *hd; + seen_ = true; + } +}; + +class StartMsgTesterBoolMethodWithHandlerData : public StartMsgTesterBase { + public: + typedef StartMsgTesterBoolMethodWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + UPB_UNUSED(f); + ASSERT(h->SetStartMessageHandler( + UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); + } + + private: + bool Handler(const int* hd) { + handler_data_val_ = *hd; + seen_ = true; + return true; + } +}; + +class Int32ValueTesterBase { + public: + static const upb::FieldDef::Type kFieldType = UPB_TYPE_INT32; + + Int32ValueTesterBase() : seen_(false), val_(0), handler_data_val_(0) {} + + void CallAndVerify(upb::Sink* sink, const upb::FieldDef* f) { + upb::Handlers::Selector s; + ASSERT(upb::Handlers::GetSelector(f, UPB_HANDLER_INT32, &s)); + + ASSERT(!seen_); + sink->PutInt32(s, 5); + ASSERT(seen_); + ASSERT(handler_data_val_ == kExpectedHandlerData); + ASSERT(val_ == 5); + } + + protected: + bool seen_; + int32_t val_; + int handler_data_val_; +}; + +// Test all 8 combinations of: +// (handler data?) x (function/method) x (returns {void, bool}) + +class ValueTesterInt32VoidFunctionNoHandlerData + : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32VoidFunctionNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + static void Handler(ME* t, int32_t val) { + t->val_ = val; + t->seen_ = true; + } +}; + +class ValueTesterInt32BoolFunctionNoHandlerData + : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32BoolFunctionNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + static bool Handler(ME* t, int32_t val) { + t->val_ = val; + t->seen_ = true; + return true; + } +}; + +class ValueTesterInt32VoidMethodNoHandlerData : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32VoidMethodNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&ME::Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + void Handler(int32_t val) { + val_ = val; + seen_ = true; + } +}; + +class ValueTesterInt32BoolMethodNoHandlerData : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32BoolMethodNoHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&ME::Handler))); + handler_data_val_ = kExpectedHandlerData; + } + + private: + bool Handler(int32_t val) { + val_ = val; + seen_ = true; + return true; + } +}; + +class ValueTesterInt32VoidFunctionWithHandlerData + : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32VoidFunctionWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler( + f, UpbBind(&Handler, new int(kExpectedHandlerData)))); + } + + private: + static void Handler(ME* t, const int* hd, int32_t val) { + t->val_ = val; + t->handler_data_val_ = *hd; + t->seen_ = true; + } +}; + +class ValueTesterInt32BoolFunctionWithHandlerData + : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32BoolFunctionWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler( + f, UpbBind(&Handler, new int(kExpectedHandlerData)))); + } + + private: + static bool Handler(ME* t, const int* hd, int32_t val) { + t->val_ = val; + t->handler_data_val_ = *hd; + t->seen_ = true; + return true; + } +}; + +class ValueTesterInt32VoidMethodWithHandlerData : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32VoidMethodWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler( + f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); + } + + private: + void Handler(const int* hd, int32_t val) { + val_ = val; + handler_data_val_ = *hd; + seen_ = true; + } +}; + +class ValueTesterInt32BoolMethodWithHandlerData : public Int32ValueTesterBase { + public: + typedef ValueTesterInt32BoolMethodWithHandlerData ME; + void Register(upb::Handlers* h, const upb::FieldDef* f) { + ASSERT(h->SetInt32Handler( + f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); + } + + private: + bool Handler(const int* hd, int32_t val) { + val_ = val; + handler_data_val_ = *hd; + seen_ = true; + return true; + } +}; + +template <class T> +void TestHandler() { + upb::reffed_ptr<upb::MessageDef> md(upb::MessageDef::New()); + upb::reffed_ptr<upb::FieldDef> f(upb::FieldDef::New()); + f->set_type(T::kFieldType); + ASSERT(f->set_name("test", NULL)); + ASSERT(f->set_number(1, NULL)); + ASSERT(md->AddField(f, NULL)); + ASSERT(md->Freeze(NULL)); + + upb::reffed_ptr<upb::Handlers> h(upb::Handlers::New(md.get())); + T tester; + tester.Register(h.get(), f.get()); + ASSERT(h->Freeze(NULL)); + + upb::Sink sink(h.get(), &tester); + tester.CallAndVerify(&sink, f.get()); } extern "C" { @@ -84,8 +720,63 @@ int run_tests(int argc, char *argv[]) { return 1; } TestSymbolTable(argv[1]); + TestCastsUpDown(); TestCasts1(); TestCasts2(); + TestCastsConst0(); + TestCastsConst1(); + TestCastsConst2(); + TestCastsConst3(); + TestCastsConst4(); + TestCastsConst5(); + TestCastsConst6(); + TestCastsConst7(); + TestCastsConst8(); + TestCastsConst9(); + TestCastsConst10(); + + TestHandler<ValueTesterInt32VoidFunctionNoHandlerData>(); + TestHandler<ValueTesterInt32BoolFunctionNoHandlerData>(); + TestHandler<ValueTesterInt32VoidMethodNoHandlerData>(); + TestHandler<ValueTesterInt32BoolMethodNoHandlerData>(); + TestHandler<ValueTesterInt32VoidFunctionWithHandlerData>(); + TestHandler<ValueTesterInt32BoolFunctionWithHandlerData>(); + TestHandler<ValueTesterInt32VoidMethodWithHandlerData>(); + TestHandler<ValueTesterInt32BoolMethodWithHandlerData>(); + + TestHandler<StartMsgTesterVoidFunctionNoHandlerData>(); + TestHandler<StartMsgTesterBoolFunctionNoHandlerData>(); + TestHandler<StartMsgTesterVoidMethodNoHandlerData>(); + TestHandler<StartMsgTesterBoolMethodNoHandlerData>(); + TestHandler<StartMsgTesterVoidFunctionWithHandlerData>(); + TestHandler<StartMsgTesterBoolFunctionWithHandlerData>(); + TestHandler<StartMsgTesterVoidMethodWithHandlerData>(); + TestHandler<StartMsgTesterBoolMethodWithHandlerData>(); + + TestHandler<StringBufTesterVoidFunctionNoHandlerData>(); + TestHandler<StringBufTesterSizeTFunctionNoHandlerData>(); + TestHandler<StringBufTesterVoidMethodNoHandlerData>(); + TestHandler<StringBufTesterSizeTMethodNoHandlerData>(); + TestHandler<StringBufTesterVoidFunctionWithHandlerData>(); + TestHandler<StringBufTesterSizeTFunctionWithHandlerData>(); + TestHandler<StringBufTesterVoidMethodWithHandlerData>(); + TestHandler<StringBufTesterSizeTMethodWithHandlerData>(); + +#ifdef UPB_CXX11 +#define ASSERT_STD_LAYOUT(type) \ + static_assert(std::is_standard_layout<type>::value, \ + #type " must be standard layout"); + ASSERT_STD_LAYOUT(upb::RefCounted); + ASSERT_STD_LAYOUT(upb::Def); + ASSERT_STD_LAYOUT(upb::MessageDef); + ASSERT_STD_LAYOUT(upb::FieldDef); + ASSERT_STD_LAYOUT(upb::EnumDef); + ASSERT_STD_LAYOUT(upb::Handlers); + ASSERT_STD_LAYOUT(upb::SymbolTable); + ASSERT_STD_LAYOUT(upb::pb::Decoder); + ASSERT_STD_LAYOUT(upb::descriptor::Reader); +#endif + return 0; } diff --git a/tests/test_decoder.cc b/tests/test_decoder.cc index 9782013..f1ee510 100644 --- a/tests/test_decoder.cc +++ b/tests/test_decoder.cc @@ -35,7 +35,6 @@ #include <stdint.h> #include <stdlib.h> #include <string.h> -#include "upb/bytestream.h" #include "upb/handlers.h" #include "upb/pb/decoder.h" #include "upb/pb/varint.int.h" @@ -345,45 +344,53 @@ void reg_str(upb_handlers *h, uint32_t num) { ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } -void reghandlers(upb_handlers *h) { +upb::reffed_ptr<const upb::Handlers> NewHandlers() { + upb::reffed_ptr<upb::Handlers> h( + upb::Handlers::New(UPB_TEST_DECODER_DECODERTEST)); + h->SetStartMessageHandler(UpbMakeHandler(startmsg)); h->SetEndMessageHandler(UpbMakeHandler(endmsg)); // Register handlers for each type. - reg<double, value_double>(h, UPB_DESCRIPTOR_TYPE_DOUBLE); - reg<float, value_float> (h, UPB_DESCRIPTOR_TYPE_FLOAT); - reg<int64_t, value_int64> (h, UPB_DESCRIPTOR_TYPE_INT64); - reg<uint64_t, value_uint64>(h, UPB_DESCRIPTOR_TYPE_UINT64); - reg<int32_t, value_int32> (h, UPB_DESCRIPTOR_TYPE_INT32); - reg<uint64_t, value_uint64>(h, UPB_DESCRIPTOR_TYPE_FIXED64); - reg<uint32_t, value_uint32>(h, UPB_DESCRIPTOR_TYPE_FIXED32); - reg<bool, value_bool> (h, UPB_DESCRIPTOR_TYPE_BOOL); - reg<uint32_t, value_uint32>(h, UPB_DESCRIPTOR_TYPE_UINT32); - reg<int32_t, value_int32> (h, UPB_DESCRIPTOR_TYPE_ENUM); - reg<int32_t, value_int32> (h, UPB_DESCRIPTOR_TYPE_SFIXED32); - reg<int64_t, value_int64> (h, UPB_DESCRIPTOR_TYPE_SFIXED64); - reg<int32_t, value_int32> (h, UPB_DESCRIPTOR_TYPE_SINT32); - reg<int64_t, value_int64> (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)); + reg<double, value_double>(h.get(), UPB_DESCRIPTOR_TYPE_DOUBLE); + reg<float, value_float> (h.get(), UPB_DESCRIPTOR_TYPE_FLOAT); + reg<int64_t, value_int64> (h.get(), UPB_DESCRIPTOR_TYPE_INT64); + reg<uint64_t, value_uint64>(h.get(), UPB_DESCRIPTOR_TYPE_UINT64); + reg<int32_t, value_int32> (h.get(), UPB_DESCRIPTOR_TYPE_INT32); + reg<uint64_t, value_uint64>(h.get(), UPB_DESCRIPTOR_TYPE_FIXED64); + reg<uint32_t, value_uint32>(h.get(), UPB_DESCRIPTOR_TYPE_FIXED32); + reg<bool, value_bool> (h.get(), UPB_DESCRIPTOR_TYPE_BOOL); + reg<uint32_t, value_uint32>(h.get(), UPB_DESCRIPTOR_TYPE_UINT32); + reg<int32_t, value_int32> (h.get(), UPB_DESCRIPTOR_TYPE_ENUM); + reg<int32_t, value_int32> (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED32); + reg<int64_t, value_int64> (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED64); + reg<int32_t, value_int32> (h.get(), UPB_DESCRIPTOR_TYPE_SINT32); + reg<int64_t, value_int64> (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)); // Register submessage/group handlers that are self-recursive // to this type, eg: message M { optional M m = 1; } - reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); - reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); + reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_MESSAGE); + reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); // 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; } /* Running of test cases ******************************************************/ -const upb::Handlers *handlers; -const upb::Handlers *plan; +const upb::Handlers *global_handlers; +const upb::pb::DecoderMethod *global_method; uint32_t Hash(const buffer& proto, const buffer* expected_output, size_t seam1, size_t seam2) { @@ -395,19 +402,18 @@ uint32_t Hash(const buffer& proto, const buffer* expected_output, size_t seam1, return hash; } -bool parse( - upb_sink *s, const char *buf, size_t start, size_t end, size_t *ofs) { +bool parse(upb::BytesSink* s, void* subc, const char* buf, size_t start, + size_t end, size_t* ofs, upb::Status* status) { start = UPB_MAX(start, *ofs); if (start <= end) { size_t len = end - start; - size_t parsed = - s->PutStringBuffer(UPB_BYTESTREAM_BYTES_STRING, buf + start, len); - if (s->pipeline()->status().ok() != (parsed >= len)) { + size_t parsed = s->PutBuffer(subc, buf + start, len); + if (status->ok() != (parsed >= len)) { fprintf(stderr, "Status: %s, parsed=%zu, len=%zu\n", - s->pipeline()->status().GetString(), parsed, len); + status->error_message(), parsed, len); ASSERT(false); } - if (!s->pipeline()->status().ok()) + if (!status->ok()) return false; *ofs += parsed; } @@ -416,37 +422,35 @@ bool parse( #define LINE(x) x "\n" void run_decoder(const buffer& proto, const buffer* expected_output) { - upb::Pipeline pipeline(NULL, 0, upb_realloc, NULL); - upb::Sink* sink = pipeline.NewSink(handlers); - upb::Sink* decoder_sink = pipeline.NewSink(plan); - upb::pb::Decoder* d = decoder_sink->GetObject<upb::pb::Decoder>(); - upb::pb::ResetDecoderSink(d, sink); + upb::Status status; + upb::pb::Decoder decoder(global_method, &status); + upb::Sink sink(global_handlers, &closures[0]); + decoder.ResetOutput(&sink); for (size_t i = 0; i < proto.len(); i++) { for (size_t j = i; j < UPB_MIN(proto.len(), i + 5); j++) { testhash = Hash(proto, expected_output, i, j); if (filter_hash && testhash != filter_hash) continue; if (!count_only) { - pipeline.Reset(); + decoder.Reset(); output.clear(); - sink->Reset(&closures[0]); + status.Clear(); size_t ofs = 0; + upb::BytesSink* input = decoder.input(); + void *sub; bool ok = - decoder_sink->StartMessage() && - decoder_sink->StartString( - UPB_BYTESTREAM_BYTES_STARTSTR, proto.len()) && - parse(decoder_sink, proto.buf(), 0, i, &ofs) && - parse(decoder_sink, proto.buf(), i, j, &ofs) && - parse(decoder_sink, proto.buf(), j, proto.len(), &ofs) && + input->Start(proto.len(), &sub) && + parse(input, sub, proto.buf(), 0, i, &ofs, &status) && + parse(input, sub, proto.buf(), i, j, &ofs, &status) && + parse(input, sub, proto.buf(), j, proto.len(), &ofs, &status) && ofs == proto.len() && - decoder_sink->EndString(UPB_BYTESTREAM_BYTES_ENDSTR); - if (ok) decoder_sink->EndMessage(); + input->End(); if (expected_output) { if (!output.eql(*expected_output)) { fprintf(stderr, "Text mismatch: '%s' vs '%s'\n", output.buf(), expected_output->buf()); } if (!ok) { - fprintf(stderr, "Failed: %s\n", pipeline.status().GetString()); + fprintf(stderr, "Failed: %s\n", status.error_message()); } ASSERT(ok); ASSERT(output.eql(*expected_output)); @@ -705,6 +709,20 @@ void test_valid() { // Empty protobuf. assert_successful_parse(buffer(""), "<\n>\n"); + // Empty protobuf where we never call PutString between + // StartString/EndString. + { + upb::Status status; + upb::pb::Decoder decoder(global_method, &status); + upb::Sink sink(global_handlers, &closures[0]); + decoder.ResetOutput(&sink); + output.clear(); + bool ok = upb::BufferSource::PutBuffer("", 0, decoder.input()); + ASSERT(ok); + ASSERT(status.ok()); + ASSERT(output.eql(buffer("<\n>\n"))); + } + test_valid_data_for_signed_type(UPB_DESCRIPTOR_TYPE_DOUBLE, dbl(33), dbl(-66)); @@ -860,15 +878,21 @@ void run_tests() { test_valid(); } +upb::reffed_ptr<const upb::pb::DecoderMethod> NewMethod( + const upb::Handlers* dest_handlers, bool allow_jit) { + upb::pb::CodeCache cache; + cache.set_allow_jit(allow_jit); + return cache.GetDecoderMethodForDestHandlers(dest_handlers); +} + void test_emptyhandlers(bool allowjit) { // Create an empty handlers to make sure that the decoder can handle empty // messages. - upb::Handlers* h = upb_handlers_new(UPB_TEST_DECODER_EMPTYMESSAGE, NULL, &h); - bool ok = upb::Handlers::Freeze(&h, 1, NULL); + upb::reffed_ptr<upb::Handlers> h( + upb::Handlers::New(UPB_TEST_DECODER_EMPTYMESSAGE)); + bool ok = h->Freeze(NULL); ASSERT(ok); - const upb::Handlers* plan = upb::pb::GetDecoderHandlers(h, allowjit, &plan); - h->Unref(&h); - plan->Unref(&plan); + NewMethod(h.get(), allowjit); } extern "C" { @@ -880,47 +904,44 @@ int run_tests(int argc, char *argv[]) { closures[i] = i; } + upb::reffed_ptr<const upb::pb::DecoderMethod> method; + upb::reffed_ptr<const upb::Handlers> handlers; + // Construct decoder plan. - upb::Handlers* h = - upb::Handlers::New(UPB_TEST_DECODER_DECODERTEST, NULL, &handlers); - reghandlers(h); - bool ok = upb::Handlers::Freeze(&h, 1, NULL); - ASSERT(ok); - handlers = h; + handlers = NewHandlers(); + global_handlers = handlers.get(); // Count tests. - plan = upb::pb::GetDecoderHandlers(handlers, false, &plan); + method = NewMethod(handlers.get(), false); + global_method = method.get(); count_only = true; count = &total; total = 0; run_tests(); count_only = false; count = &completed; - plan->Unref(&plan); // Test without JIT. - plan = upb::pb::GetDecoderHandlers(handlers, false, &plan); - ASSERT(!upb::pb::HasJitCode(plan)); + method = NewMethod(handlers.get(), false); + global_method = method.get(); + ASSERT(!global_method->is_native()); completed = 0; run_tests(); - plan->Unref(&plan); test_emptyhandlers(false); #ifdef UPB_USE_JIT_X64 // Test JIT. - plan = upb::pb::GetDecoderHandlers(handlers, true, &plan); - ASSERT(upb::pb::HasJitCode(plan)); + method = NewMethod(handlers.get(), true); + global_method = method.get(); + ASSERT(global_method->is_native()); completed = 0; run_tests(); - plan->Unref(&plan); test_emptyhandlers(true); #endif - plan = NULL; printf("All tests passed, %d assertions.\n", num_assertions); - handlers->Unref(&handlers); return 0; } diff --git a/tests/test_def.c b/tests/test_def.c index 9d3805c..fb33871 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -30,10 +30,9 @@ static upb_symtab *load_test_proto(void *owner) { upb_status status = UPB_STATUS_INIT; if (!upb_load_descriptor_file_into_symtab(s, descriptor_file, &status)) { fprintf(stderr, "Error loading descriptor file: %s\n", - upb_status_getstr(&status)); + upb_status_errmsg(&status)); ASSERT(false); } - upb_status_uninit(&status); return s; } diff --git a/tests/test_handlers.c b/tests/test_handlers.c index 36881fe..df22b8a 100644 --- a/tests/test_handlers.c +++ b/tests/test_handlers.c @@ -18,13 +18,13 @@ static bool startmsg(void *c, const void *hd) { } static void test_error() { - upb_handlers *h = upb_handlers_new(GOOGLE_PROTOBUF_DESCRIPTORPROTO, NULL, &h); + upb_handlers *h = upb_handlers_new(GOOGLE_PROTOBUF_DESCRIPTORPROTO, &h); // Attempt to set the same handler twice causes error. ASSERT(upb_ok(upb_handlers_status(h))); - upb_handlers_setstartmsg(h, &startmsg, NULL, NULL); + upb_handlers_setstartmsg(h, &startmsg, NULL); ASSERT(upb_ok(upb_handlers_status(h))); - upb_handlers_setstartmsg(h, &startmsg, NULL, NULL); + upb_handlers_setstartmsg(h, &startmsg, NULL); ASSERT(!upb_ok(upb_handlers_status(h))); ASSERT(!upb_handlers_freeze(&h, 1, NULL)); diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c deleted file mode 100644 index 7214ca5..0000000 --- a/tests/test_pipeline.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * upb - a minimalist implementation of protocol buffers. - * - * Copyright (c) 2013 Google Inc. See LICENSE for details. - * - * Test of upb_pipeline. - */ - -#include "upb/sink.h" -#include "tests/upb_test.h" - -static void *count_realloc(void *ud, void *ptr, size_t size) { - int *count = ud; - *count += 1; - return upb_realloc(ud, ptr, size); -} - -static void test_empty() { - // A pipeline with no initial memory or allocation function should return - // NULL from attempts to allocate. - upb_pipeline pipeline; - upb_pipeline_init(&pipeline, NULL, 0, NULL, NULL); - ASSERT(upb_pipeline_alloc(&pipeline, 1) == NULL); - ASSERT(upb_pipeline_alloc(&pipeline, 1) == NULL); - ASSERT(upb_pipeline_realloc(&pipeline, NULL, 0, 1) == NULL); - upb_pipeline_uninit(&pipeline); -} - -static void test_only_initial() { - upb_pipeline pipeline; - char initial[152]; // 128 + a conservative 24 bytes overhead. - upb_pipeline_init(&pipeline, initial, sizeof(initial), NULL, NULL); - void *p1 = upb_pipeline_alloc(&pipeline, 64); - void *p2 = upb_pipeline_alloc(&pipeline, 64); - void *p3 = upb_pipeline_alloc(&pipeline, 64); - ASSERT(p1); - ASSERT(p2); - ASSERT(!p3); - ASSERT(p1 != p2); - ASSERT((void*)initial <= p1); - ASSERT(p1 < p2); - ASSERT(p2 < (void*)(initial + sizeof(initial))); - upb_pipeline_uninit(&pipeline); -} - -static void test_with_alloc_func() { - upb_pipeline pipeline; - char initial[152]; // 128 + a conservative 24 bytes overhead. - int count = 0; - upb_pipeline_init(&pipeline, initial, sizeof(initial), count_realloc, &count); - void *p1 = upb_pipeline_alloc(&pipeline, 64); - void *p2 = upb_pipeline_alloc(&pipeline, 64); - ASSERT(p1); - ASSERT(p2); - ASSERT(p1 != p2); - ASSERT(count == 0); - - void *p3 = upb_pipeline_alloc(&pipeline, 64); - ASSERT(p3); - ASSERT(p3 != p2); - ASSERT(count == 1); - - // Allocation larger than internal block size should force another alloc. - char *p4 = upb_pipeline_alloc(&pipeline, 16384); - ASSERT(p4); - p4[16383] = 1; // Verify memory is writable without crashing. - ASSERT(p4[16383] == 1); - ASSERT(count == 2); - - upb_pipeline_uninit(&pipeline); - ASSERT(count == 4); // From two calls to free the memory. -} - -static void test_realloc() { - upb_pipeline pipeline; - char initial[152]; // 128 + a conservative 24 bytes overhead. - int count = 0; - upb_pipeline_init(&pipeline, initial, sizeof(initial), count_realloc, &count); - void *p1 = upb_pipeline_alloc(&pipeline, 64); - // This realloc should work in-place. - void *p2 = upb_pipeline_realloc(&pipeline, p1, 64, 128); - ASSERT(p1); - ASSERT(p2); - ASSERT(p1 == p2); - ASSERT(count == 0); - - // This realloc will *not* work in place, due to size. - void *p3 = upb_pipeline_realloc(&pipeline, p2, 128, 256); - ASSERT(p3); - ASSERT(p3 != p2); - ASSERT(count == 1); - - void *p4 = upb_pipeline_alloc(&pipeline, 64); - void *p5 = upb_pipeline_alloc(&pipeline, 64); - // This realloc will *not* work in place because it was not the last - // allocation. - void *p6 = upb_pipeline_realloc(&pipeline, p4, 64, 128); - ASSERT(p4); - ASSERT(p5); - ASSERT(p6); - ASSERT(p4 != p6); - ASSERT(p4 < p5); - ASSERT(p5 < p6); - ASSERT(count == 1); // These should all fit in the first dynamic block. - - upb_pipeline_uninit(&pipeline); - ASSERT(count == 2); -} - -int run_tests(int argc, char *argv[]) { - UPB_UNUSED(argc); - UPB_UNUSED(argv); - test_empty(); - test_only_initial(); - test_with_alloc_func(); - test_realloc(); - return 0; -} diff --git a/tests/test_vs_proto2.cc b/tests/test_vs_proto2.cc index d766b42..07946dd 100644 --- a/tests/test_vs_proto2.cc +++ b/tests/test_vs_proto2.cc @@ -19,7 +19,6 @@ #include <stdio.h> #include <stdlib.h> #include "benchmarks/google_messages.pb.h" -#include "upb/bytestream.h" #include "upb/def.h" #include "upb/google/bridge.h" #include "upb/handlers.h" @@ -53,25 +52,24 @@ void parse_and_compare(google::protobuf::Message *msg1, // Parse to both proto2 and upb. ASSERT(msg1->ParseFromArray(str, len)); - const upb::Handlers* decoder_handlers = upb::pb::GetDecoderHandlers( - protomsg_handlers, allow_jit, &decoder_handlers); + upb::pb::CodeCache cache; + ASSERT(cache.set_allow_jit(allow_jit)); + upb::reffed_ptr<const upb::pb::DecoderMethod> decoder_method( + cache.GetDecoderMethodForDestHandlers(protomsg_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); + upb::Status status; + upb::pb::Decoder decoder(decoder_method.get(), &status); + upb::Sink protomsg_sink(protomsg_handlers, msg2); - protomsg_sink->Reset(msg2); - upb::pb::Decoder* decoder = decoder_sink->GetObject<upb::pb::Decoder>(); - upb::pb::ResetDecoderSink(decoder, protomsg_sink); + decoder.ResetOutput(&protomsg_sink); msg2->Clear(); - bool ok = upb::PutStringToBytestream(decoder_sink, str, len); + bool ok = upb::BufferSource::PutBuffer(str, len, decoder.input()); if (!ok) { - fprintf(stderr, "error parsing: %s\n", pipeline.status().GetString()); + fprintf(stderr, "error parsing: %s\n", status.error_message()); } ASSERT(ok); - ASSERT(pipeline.status().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 @@ -127,16 +125,15 @@ int run_tests(int argc, char *argv[]) MESSAGE_CIDENT msg1; MESSAGE_CIDENT msg2; - const upb::Handlers* h = upb::google::NewWriteHandlers(msg1, &h); + upb::reffed_ptr<const upb::Handlers> h(upb::google::NewWriteHandlers(msg1)); compare_metadata(msg1.GetDescriptor(), h->message_def()); // Run twice to test proper object reuse. - parse_and_compare(&msg1, &msg2, h, str, len, false); - 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); - h->Unref(&h); + 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 = @@ -145,13 +142,12 @@ int run_tests(int argc, char *argv[]) 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); + h = upb::google::NewWriteHandlers(*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; - h->Unref(&h); free((void*)str); diff --git a/tests/upb_test.h b/tests/upb_test.h index 60714ba..76844e5 100644 --- a/tests/upb_test.h +++ b/tests/upb_test.h @@ -46,7 +46,7 @@ uint32_t testhash = 0; ++num_assertions; \ if (!(expr)) { \ PRINT_FAILURE(expr) \ - fprintf(stderr, "failed status: %s\n", upb_status_getstr(status)); \ + fprintf(stderr, "failed status: %s\n", upb_status_errmsg(status)); \ abort(); \ } \ } while (0) |