summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2019-01-12 19:12:57 -0800
committerJoshua Haberman <jhaberman@gmail.com>2019-01-12 19:12:57 -0800
commit48863ea0be94ea3d3d61206ad7ce9ead206770fa (patch)
tree9c4b647586ecb7c52fa56f8cef26301027ec5ae7 /tests
parentd2f9bec5c6f3c34362cf13e35e11d3dbc7888a32 (diff)
A lot more tests are working now.
Diffstat (limited to 'tests')
-rw-r--r--tests/json/test_json.cc36
-rw-r--r--tests/pb/test_decoder.cc157
-rw-r--r--tests/test_handlers.c42
-rw-r--r--tests/test_util.h10
4 files changed, 93 insertions, 152 deletions
diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc
index b9b50cd..b0fd3e3 100644
--- a/tests/json/test_json.cc
+++ b/tests/json/test_json.cc
@@ -144,7 +144,7 @@ class StringSink {
}
~StringSink() { }
- upb_bytessink* Sink() { return &bytessink_; }
+ upb_bytessink Sink() { return bytessink_; }
const std::string& Data() { return s_; }
@@ -169,16 +169,15 @@ class StringSink {
void test_json_roundtrip_message(const char* json_src,
const char* json_expected,
const upb::Handlers* serialize_handlers,
- const upb::json::ParserMethod* parser_method,
+ const upb::json::ParserMethodPtr parser_method,
int seam) {
VerboseParserEnvironment env(verbose);
StringSink data_sink;
- upb::json::Printer* printer = upb::json::Printer::Create(
+ upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create(
env.env(), serialize_handlers, data_sink.Sink());
- upb::json::Parser* parser =
- upb::json::Parser::Create(
- env.env(), parser_method, NULL, printer->input(), false);
- env.ResetBytesSink(parser->input());
+ upb::json::ParserPtr parser = upb::json::ParserPtr::Create(
+ env.env(), parser_method, NULL, printer.input(), false);
+ env.ResetBytesSink(parser.input());
env.Reset(json_src, strlen(json_src), false, false);
bool ok = env.Start() &&
@@ -203,16 +202,16 @@ void test_json_roundtrip_message(const char* json_src,
// Starts with a message in JSON format, parses and directly serializes again,
// and compares the result.
void test_json_roundtrip() {
- upb::SymbolTable* symtab = upb::SymbolTable::New();
- upb::HandlerCache* serialize_handlercache = upb::json::Printer::NewCache(false);
- upb::json::CodeCache* parse_codecache = upb::json::CodeCache::New();
+ upb::SymbolTable symtab;
+ upb::HandlerCache serialize_handlercache(
+ upb::json::PrinterPtr::NewCache(false));
+ upb::json::CodeCache parse_codecache;
- const upb::MessageDef* md = upb_test_json_TestMessage_getmsgdef(symtab);
+ upb::MessageDefPtr md(upb_test_json_TestMessage_getmsgdef(symtab.ptr()));
ASSERT(md);
- const upb::Handlers* serialize_handlers = serialize_handlercache->Get(md);
- const upb::json::ParserMethod* parser_method = parse_codecache->Get(md);
+ const upb::Handlers* serialize_handlers = serialize_handlercache.Get(md);
+ const upb::json::ParserMethodPtr parser_method = parse_codecache.Get(md);
ASSERT(serialize_handlers);
- ASSERT(parser_method);
for (const TestCase* test_case = kTestRoundtripMessages;
test_case->input != NULL; test_case++) {
@@ -227,9 +226,8 @@ void test_json_roundtrip() {
}
}
- upb::HandlerCache::Free(serialize_handlercache);
- serialize_handlercache = upb::json::Printer::NewCache(true);
- serialize_handlers = serialize_handlercache->Get(md);
+ serialize_handlercache = upb::json::PrinterPtr::NewCache(true);
+ serialize_handlers = serialize_handlercache.Get(md);
for (const TestCase* test_case = kTestRoundtripMessagesPreserve;
test_case->input != NULL; test_case++) {
@@ -243,10 +241,6 @@ void test_json_roundtrip() {
serialize_handlers, parser_method, i);
}
}
-
- upb::HandlerCache::Free(serialize_handlercache);
- upb::json::CodeCache::Free(parse_codecache);
- upb::SymbolTable::Free(symtab);
}
extern "C" {
diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc
index d0e3fa3..ec7a788 100644
--- a/tests/pb/test_decoder.cc
+++ b/tests/pb/test_decoder.cc
@@ -279,7 +279,7 @@ int* startstr(int* depth, const uint32_t* num, size_t size_hint) {
}
size_t value_string(int* depth, const uint32_t* num, const char* buf,
- size_t n, const upb::BufferHandle* handle) {
+ size_t n, const upb_bufhandle* handle) {
UPB_UNUSED(num);
UPB_UNUSED(depth);
check_stack_alignment();
@@ -348,13 +348,13 @@ void free_uint32(void *val) {
}
template<class T, bool F(int*, const uint32_t*, T)>
-void doreg(upb_handlers *h, uint32_t num) {
- const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num);
+void doreg(upb::HandlersPtr h, uint32_t num) {
+ upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num);
ASSERT(f);
- ASSERT(h->SetValueHandler<T>(f, UpbBindT(F, new uint32_t(num))));
- if (f->IsSequence()) {
- ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num))));
- ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num))));
+ ASSERT(h.SetValueHandler<T>(f, UpbBind(F, new uint32_t(num))));
+ if (f.IsSequence()) {
+ ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num))));
+ ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num))));
}
}
@@ -368,7 +368,7 @@ uint32_t rep_fn(uint32_t fn) {
#define UNKNOWN_FIELD 666
template <class T, bool F(int*, const uint32_t*, T)>
-void reg(upb_handlers *h, upb_descriptortype_t type) {
+void reg(upb::HandlersPtr h, upb_descriptortype_t type) {
// We register both a repeated and a non-repeated field for every type.
// For the non-repeated field we make the field number the same as the
// type. For the repeated field we make it a function of the type.
@@ -376,39 +376,40 @@ void reg(upb_handlers *h, upb_descriptortype_t type) {
doreg<T, F>(h, rep_fn(type));
}
-void regseq(upb::Handlers* h, const upb::FieldDef* f, uint32_t num) {
- ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num))));
- ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num))));
+void regseq(upb::HandlersPtr h, upb::FieldDefPtr f, uint32_t num) {
+ ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num))));
+ ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num))));
}
-void reg_subm(upb_handlers *h, uint32_t num) {
- const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num);
+void reg_subm(upb::HandlersPtr h, uint32_t num) {
+ upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num);
ASSERT(f);
- if (f->IsSequence()) regseq(h, f, num);
+ if (f.IsSequence()) regseq(h, f, num);
ASSERT(
- h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num))));
- ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num))));
+ h.SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num))));
+ ASSERT(h.SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num))));
}
-void reg_str(upb_handlers *h, uint32_t num) {
- const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num);
+void reg_str(upb::HandlersPtr h, uint32_t num) {
+ upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num);
ASSERT(f);
- if (f->IsSequence()) regseq(h, f, num);
- ASSERT(h->SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num))));
- ASSERT(h->SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num))));
- ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num))));
+ if (f.IsSequence()) regseq(h, f, num);
+ ASSERT(h.SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num))));
+ ASSERT(h.SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num))));
+ ASSERT(h.SetStringHandler(f, UpbBind(value_string, new uint32_t(num))));
}
struct HandlerRegisterData {
TestMode mode;
};
-void callback(const void *closure, upb_handlers *h) {
+void callback(const void *closure, upb::Handlers* h_ptr) {
+ upb::HandlersPtr h(h_ptr);
const HandlerRegisterData* data =
static_cast<const HandlerRegisterData*>(closure);
if (data->mode == ALL_HANDLERS) {
- h->SetStartMessageHandler(UpbMakeHandler(startmsg));
- h->SetEndMessageHandler(UpbMakeHandler(endmsg));
+ h.SetStartMessageHandler(UpbMakeHandler(startmsg));
+ h.SetEndMessageHandler(UpbMakeHandler(endmsg));
// Register handlers for each type.
reg<double, value_double>(h, UPB_DESCRIPTOR_TYPE_DOUBLE);
@@ -436,7 +437,7 @@ void callback(const void *closure, upb_handlers *h) {
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")) {
+ 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));
}
@@ -446,25 +447,16 @@ void callback(const void *closure, upb_handlers *h) {
}
}
-upb::reffed_ptr<const upb::Handlers> NewHandlers(upb::SymbolTable* symtab,
- TestMode mode) {
- HandlerRegisterData handlerdata;
- handlerdata.mode = mode;
- return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback,
- &handlerdata);
-}
-
/* Running of test cases ******************************************************/
const upb::Handlers *global_handlers;
-const upb::pb::DecoderMethod *global_method;
-
-upb::pb::Decoder* CreateDecoder(upb::Environment* env,
- const upb::pb::DecoderMethod* method,
- upb::Sink* sink) {
- upb::pb::Decoder *ret = upb::pb::Decoder::Create(env, method, sink);
- ASSERT(ret != NULL);
- ret->set_max_nesting(MAX_NESTING);
+upb::pb::DecoderMethodPtr global_method;
+
+upb::pb::DecoderPtr CreateDecoder(upb::Environment* env,
+ upb::pb::DecoderMethodPtr method,
+ upb::Sink sink) {
+ upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(env, method, sink);
+ ret.set_max_nesting(MAX_NESTING);
return ret;
}
@@ -479,7 +471,7 @@ uint32_t Hash(const string& proto, const string* expected_output, size_t seam1,
return hash;
}
-void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) {
+void CheckBytesParsed(upb::pb::DecoderPtr decoder, size_t ofs) {
// We can't have parsed more data than the decoder callback is telling us it
// parsed.
ASSERT(decoder.BytesParsed() <= ofs);
@@ -491,7 +483,7 @@ void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) {
}
static bool parse(VerboseParserEnvironment* env,
- const upb::pb::Decoder& decoder, int bytes) {
+ upb::pb::DecoderPtr decoder, int bytes) {
CheckBytesParsed(decoder, env->ofs());
bool ret = env->ParseBuffer(bytes);
if (ret) {
@@ -501,11 +493,11 @@ static bool parse(VerboseParserEnvironment* env,
return ret;
}
-void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
+void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder,
const string& proto, const string* expected_output,
size_t i, size_t j, bool may_skip) {
env->Reset(proto.c_str(), proto.size(), may_skip, expected_output == NULL);
- decoder->Reset();
+ decoder.Reset();
testhash = Hash(proto, expected_output, i, j, may_skip);
if (filter_hash && testhash != filter_hash) return;
@@ -515,7 +507,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
if (filter_hash) {
fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash);
fprintf(stderr, "JIT on: %s\n",
- global_method->is_native() ? "true" : "false");
+ global_method.is_native() ? "true" : "false");
fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size());
PrintBinary(proto);
fprintf(stderr, "\n");
@@ -534,9 +526,9 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
}
bool ok = env->Start() &&
- parse(env, *decoder, i) &&
- parse(env, *decoder, j - i) &&
- parse(env, *decoder, -1) &&
+ parse(env, decoder, i) &&
+ parse(env, decoder, j - i) &&
+ parse(env, decoder, -1) &&
env->End();
ASSERT(env->CheckConsistency());
@@ -564,8 +556,8 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder,
void run_decoder(const string& proto, const string* expected_output) {
VerboseParserEnvironment env(filter_hash != 0);
upb::Sink sink(global_handlers, &closures[0]);
- upb::pb::Decoder *decoder = CreateDecoder(env.env(), global_method, &sink);
- env.ResetBytesSink(decoder->input());
+ upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink);
+ env.ResetBytesSink(decoder.input());
for (size_t i = 0; i < proto.size(); i++) {
for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) {
do_run_decoder(&env, decoder, proto, expected_output, i, j, true);
@@ -883,9 +875,9 @@ void test_valid() {
upb::Environment env;
env.ReportErrorsTo(&status);
upb::Sink sink(global_handlers, &closures[0]);
- upb::pb::Decoder* decoder = CreateDecoder(&env, global_method, &sink);
+ upb::pb::DecoderPtr decoder = CreateDecoder(&env, global_method, sink);
output.clear();
- bool ok = upb::BufferSource::PutBuffer("", 0, decoder->input());
+ bool ok = upb::PutBuffer(std::string(), decoder.input());
ASSERT(ok);
ASSERT(status.ok());
if (test_mode == ALL_HANDLERS) {
@@ -1133,23 +1125,22 @@ void test_valid() {
run_decoder(buf, &textbuf);
}
-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.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers));
-}
+void empty_callback(const void *closure, upb::Handlers* h_ptr) {}
void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) {
// Create an empty handlers to make sure that the decoder can handle empty
// messages.
- const upb::MessageDef* md = Empty_getmsgdef(symtab);
- upb::reffed_ptr<upb::Handlers> h(upb::Handlers::New(md));
- bool ok = h->Freeze(NULL);
- ASSERT(ok);
-upb::reffed_ptr<const upb::pb::DecoderMethod> method =
- NewMethod(h.get(), allowjit);
- ASSERT(method.get());
+ HandlerRegisterData handlerdata;
+ handlerdata.mode = test_mode;
+
+ upb::HandlerCache handler_cache(empty_callback, &handlerdata);
+ upb::pb::CodeCache pb_code_cache(&handler_cache);
+
+ pb_code_cache.set_allow_jit(allowjit);
+
+ upb::MessageDefPtr md = upb::MessageDefPtr(Empty_getmsgdef(symtab->ptr()));
+ global_handlers = handler_cache.Get(md);
+ global_method = pb_code_cache.Get(md);
// TODO: also test the case where a message has fields, but the fields are
// submessage fields and have no handlers. This also results in a decoder
@@ -1169,9 +1160,9 @@ upb::reffed_ptr<const upb::pb::DecoderMethod> method =
};
for (int i = 0; testdata[i].data; i++) {
VerboseParserEnvironment env(filter_hash != 0);
- upb::Sink sink(method->dest_handlers(), &closures[0]);
- upb::pb::Decoder* decoder = CreateDecoder(env.env(), method.get(), &sink);
- env.ResetBytesSink(decoder->input());
+ upb::Sink sink(global_method.dest_handlers(), &closures[0]);
+ upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink);
+ env.ResetBytesSink(decoder.input());
env.Reset(testdata[i].data, testdata[i].length, true, false);
ASSERT(env.Start());
ASSERT(env.ParseBuffer(-1));
@@ -1181,24 +1172,25 @@ upb::reffed_ptr<const upb::pb::DecoderMethod> method =
}
void run_tests(bool use_jit) {
- upb::reffed_ptr<const upb::pb::DecoderMethod> method;
- upb::reffed_ptr<const upb::Handlers> handlers;
- upb::SymbolTable* symtab = upb::SymbolTable::New();
+ HandlerRegisterData handlerdata;
+ handlerdata.mode = test_mode;
- handlers = NewHandlers(symtab, test_mode);
- global_handlers = handlers.get();
+ upb::SymbolTable symtab;
+ upb::HandlerCache handler_cache(callback, &handlerdata);
+ upb::pb::CodeCache pb_code_cache(&handler_cache);
- method = NewMethod(handlers.get(), use_jit);
- global_method = method.get();
- ASSERT(use_jit == global_method->is_native());
+ pb_code_cache.set_allow_jit(use_jit);
+
+ upb::MessageDefPtr md(DecoderTest_getmsgdef(symtab.ptr()));
+ global_handlers = handler_cache.Get(md);
+ global_method = pb_code_cache.Get(md);
+ ASSERT(use_jit == global_method.is_native());
completed = 0;
test_invalid();
test_valid();
- test_emptyhandlers(symtab, use_jit);
-
- upb::SymbolTable::Free(symtab);
+ test_emptyhandlers(&symtab, use_jit);
}
void run_test_suite() {
@@ -1218,9 +1210,6 @@ 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;
-
// Count tests.
count = &total;
total = 0;
diff --git a/tests/test_handlers.c b/tests/test_handlers.c
deleted file mode 100644
index 2b19cab..0000000
--- a/tests/test_handlers.c
+++ /dev/null
@@ -1,42 +0,0 @@
-
-#include <stdlib.h>
-#include <string.h>
-#include "google/protobuf/descriptor.upbdefs.h"
-#include "upb/handlers.h"
-#include "upb_test.h"
-
-static bool startmsg(void *c, const void *hd) {
- UPB_UNUSED(c);
- UPB_UNUSED(hd);
- return true;
-}
-
-static void test_error() {
- /* Test creating handlers of a static msgdef. */
- upb_symtab *s = upb_symtab_new();
- const upb_msgdef *m = google_protobuf_DescriptorProto_getmsgdef(s);
- upb_handlers *h = upb_handlers_new(m, &h);
-
- /* Attempt to set the same handler twice causes error. */
- ASSERT(upb_ok(upb_handlers_status(h)));
- upb_handlers_setstartmsg(h, &startmsg, NULL);
- ASSERT(upb_ok(upb_handlers_status(h)));
- upb_handlers_setstartmsg(h, &startmsg, NULL);
- ASSERT(!upb_ok(upb_handlers_status(h)));
- ASSERT(!upb_handlers_freeze(&h, 1, NULL));
-
- /* Clearing the error will let us proceed. */
- upb_handlers_clearerr(h);
- ASSERT(upb_handlers_freeze(&h, 1, NULL));
- ASSERT(upb_handlers_isfrozen(h));
-
- upb_handlers_unref(h, &h);
- upb_symtab_free(s);
-}
-
-int run_tests(int argc, char *argv[]) {
- UPB_UNUSED(argc);
- UPB_UNUSED(argv);
- test_error();
- return 0;
-}
diff --git a/tests/test_util.h b/tests/test_util.h
index 1b1ff01..0b5ddd4 100644
--- a/tests/test_util.h
+++ b/tests/test_util.h
@@ -78,14 +78,14 @@ class VerboseParserEnvironment {
if (verbose_) {
fprintf(stderr, "Calling start()\n");
}
- return sink_->Start(len_, &subc_);
+ return sink_.Start(len_, &subc_);
}
bool End() {
if (verbose_) {
fprintf(stderr, "Calling end()\n");
}
- end_ok_ = sink_->End();
+ end_ok_ = sink_.End();
end_ok_set_ = true;
return end_ok_;
@@ -137,7 +137,7 @@ class VerboseParserEnvironment {
(unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes));
}
- int parsed = sink_->PutBuffer(subc_, buf2, bytes, &global_handle);
+ int parsed = sink_.PutBuffer(subc_, buf2, bytes, &global_handle);
free(buf2);
if (verbose_) {
@@ -170,7 +170,7 @@ class VerboseParserEnvironment {
return true;
}
- void ResetBytesSink(upb::BytesSink* sink) {
+ void ResetBytesSink(upb::BytesSink sink) {
sink_ = sink;
}
@@ -181,7 +181,7 @@ class VerboseParserEnvironment {
private:
upb::Environment env_;
- upb::BytesSink* sink_;
+ upb::BytesSink sink_;
const char* buf_;
size_t len_;
bool verbose_;
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback