pull/13171/head
Joshua Haberman 3 years ago
parent 4e97915fde
commit 738b1d6d9c
  1. 3
      tests/BUILD
  2. 42
      tests/test_cpp.cc

@ -73,7 +73,6 @@ cc_test(
":empty_upbdefs_proto", ":empty_upbdefs_proto",
":test_messages_proto3_proto_upb", ":test_messages_proto3_proto_upb",
":test_upb_proto", ":test_upb_proto",
#":upb_test",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
) )
@ -119,11 +118,11 @@ cc_test(
deps = [ deps = [
":test_cpp_upb_proto", ":test_cpp_upb_proto",
":test_cpp_upb_proto_reflection", ":test_cpp_upb_proto_reflection",
":upb_test",
"//:json", "//:json",
"//:port", "//:port",
"//:reflection", "//:reflection",
"//:upb", "//:upb",
"@com_google_googletest//:gtest_main",
], ],
) )

@ -35,9 +35,9 @@
#include <set> #include <set>
#include <sstream> #include <sstream>
#include "gtest/gtest.h"
#include "tests/test_cpp.upb.h" #include "tests/test_cpp.upb.h"
#include "tests/test_cpp.upbdefs.h" #include "tests/test_cpp.upbdefs.h"
#include "tests/upb_test.h"
#include "upb/def.h" #include "upb/def.h"
#include "upb/def.hpp" #include "upb/def.hpp"
#include "upb/json_decode.h" #include "upb/json_decode.h"
@ -47,7 +47,7 @@
// Must be last. // Must be last.
#include "upb/port_def.inc" #include "upb/port_def.inc"
void TestIteration() { TEST(Cpp, Iteration) {
upb::SymbolTable symtab; upb::SymbolTable symtab;
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
@ -57,17 +57,17 @@ void TestIteration() {
UPB_UNUSED(field); UPB_UNUSED(field);
field_count++; field_count++;
} }
ASSERT(field_count == md.field_count()); EXPECT_EQ(field_count, md.field_count());
int oneof_count = 0; int oneof_count = 0;
for (auto oneof : md.oneofs()) { for (auto oneof : md.oneofs()) {
UPB_UNUSED(oneof); UPB_UNUSED(oneof);
oneof_count++; oneof_count++;
} }
ASSERT(oneof_count == md.oneof_count()); EXPECT_EQ(oneof_count, md.oneof_count());
} }
void TestArena() { TEST(Cpp, Arena) {
int n = 100000; int n = 100000;
struct Decrementer { struct Decrementer {
@ -89,7 +89,7 @@ void TestArena() {
// Test a large allocation. // Test a large allocation.
upb_Arena_Malloc(arena.ptr(), 1000000); upb_Arena_Malloc(arena.ptr(), 1000000);
} }
ASSERT(n == 0); EXPECT_EQ(0, n);
{ {
// Test fuse. // Test fuse.
@ -103,7 +103,7 @@ void TestArena() {
} }
} }
void TestInlinedArena() { TEST(Cpp, InlinedArena) {
int n = 100000; int n = 100000;
struct Decrementer { struct Decrementer {
@ -125,36 +125,26 @@ void TestInlinedArena() {
// Test a large allocation. // Test a large allocation.
upb_Arena_Malloc(arena.ptr(), 1000000); upb_Arena_Malloc(arena.ptr(), 1000000);
} }
ASSERT(n == 0); EXPECT_EQ(0, n);
} }
void TestDefault() { TEST(Cpp, Default) {
upb::SymbolTable symtab; upb::SymbolTable symtab;
upb::Arena arena; upb::Arena arena;
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr()); upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr());
size_t size = upb_JsonEncode(msg, md.ptr(), NULL, 0, NULL, 0, NULL); size_t size = upb_JsonEncode(msg, md.ptr(), NULL, 0, NULL, 0, NULL);
ASSERT(size == 2); // "{}" EXPECT_EQ(2, size); // "{}"
} }
void TestJsonNull() { TEST(Cpp, JsonNull) {
upb::SymbolTable symtab; upb::SymbolTable symtab;
upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
upb::FieldDefPtr i32_f = md.FindFieldByName("i32"); upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
upb::FieldDefPtr str_f = md.FindFieldByName("str"); upb::FieldDefPtr str_f = md.FindFieldByName("str");
ASSERT(i32_f && str_f); ASSERT_TRUE(i32_f);
ASSERT(i32_f.default_value().int32_val == 5); ASSERT_TRUE(str_f);
ASSERT(strcmp(str_f.default_value().str_val.data, "abc") == 0); EXPECT_EQ(5, i32_f.default_value().int32_val);
ASSERT(str_f.default_value().str_val.size == 3); EXPECT_EQ(0, strcmp(str_f.default_value().str_val.data, "abc"));
} EXPECT_EQ(3, str_f.default_value().str_val.size);
extern "C" {
int run_tests() {
TestIteration();
TestArena();
TestDefault();
return 0;
}
} }

Loading…
Cancel
Save