Skip to content

Commit

Permalink
Add FlatBufferBuilder move semantics tests to the main test suite (#4902
Browse files Browse the repository at this point in the history
)

* Add FlatBufferBuilder move semantics tests to main

Do not eagerly delete/reset allocators in release and release_raw functions
Update android, vs2010 build files
New tests for various types of FlatBufferBuilders and move semantics

* Improve test failure output with function names
  • Loading branch information
sutambe authored and aardappel committed Sep 24, 2018
1 parent b1a925d commit 49fed8c
Show file tree
Hide file tree
Showing 12 changed files with 684 additions and 177 deletions.
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ cc_test(
"tests/namespace_test/namespace_test1_generated.h",
"tests/namespace_test/namespace_test2_generated.h",
"tests/test.cpp",
"tests/test_builder.h",
"tests/test_assert.h",
"tests/test_builder.cpp",
"tests/test_assert.cpp",
"tests/union_vector/union_vector_generated.h",
":public_headers",
],
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ set(FlatBuffers_Tests_SRCS
${FlatBuffers_Library_SRCS}
src/idl_gen_fbs.cpp
tests/test.cpp
tests/test_assert.h
tests/test_assert.cpp
tests/test_builder.h
tests/test_builder.cpp
# file generate by running compiler on tests/monster_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
)
Expand All @@ -100,7 +104,11 @@ set(FlatBuffers_GRPCTest_SRCS
include/flatbuffers/flatbuffers.h
include/flatbuffers/grpc.h
tests/monster_test.grpc.fb.h
tests/test_assert.h
tests/test_builder.h
tests/monster_test.grpc.fb.cc
tests/test_assert.cpp
tests/test_builder.cpp
grpc/tests/grpctest.cpp
grpc/tests/message_builder_test.cpp
# file generated by running compiler on samples/monster.fbs
Expand Down
4 changes: 4 additions & 0 deletions android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ include $(CLEAR_VARS)
LOCAL_MODULE := FlatBufferTest
LOCAL_SRC_FILES := android/jni/main.cpp \
tests/test.cpp \
tests/test_assert.h \
tests/test_builder.h \
tests/test_assert.cpp \
tests/test_builder.cpp \
src/idl_gen_fbs.cpp \
src/idl_gen_general.cpp
LOCAL_LDLIBS := -llog -landroid -latomic
Expand Down
14 changes: 12 additions & 2 deletions grpc/tests/grpctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

#include "monster_test.grpc.fb.h"
#include "monster_test_generated.h"
#include "test_assert.h"

using namespace MyGame::Example;
int builder_tests();
void message_builder_tests();

// The callback implementation of our server, that derives from the generated
// code. It implements all rpcs specified in the FlatBuffers schema.
Expand Down Expand Up @@ -166,6 +167,15 @@ int grpc_server_test() {
}

int main(int /*argc*/, const char * /*argv*/ []) {
return builder_tests() + grpc_server_test();
message_builder_tests();
grpc_server_test();

if (!testing_fails) {
TEST_OUTPUT_LINE("ALL TESTS PASSED");
return 0;
} else {
TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
return 1;
}
}

283 changes: 156 additions & 127 deletions grpc/tests/message_builder_test.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
#include "flatbuffers/grpc.h"
#include "monster_test_generated.h"
#include "test_assert.h"
#include "test_builder.h"

static int builder_test_error = 0;

#define test_assert(condition) do { \
if(!(condition)) { \
fprintf(stderr, "%s:%d: %s failed.\n", __FILE__, __LINE__, #condition);\
builder_test_error = 1;\
} \
} while(0)

using namespace MyGame::Example;

const std::string m1_name = "Cyberdemon";
const Color m1_color = Color_Red;
const std::string m2_name = "Imp";
const Color m2_color = Color_Green;

flatbuffers::Offset<Monster> populate1(flatbuffers::FlatBufferBuilder &builder) {
auto name_offset = builder.CreateString(m1_name);
return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m1_color);
}

flatbuffers::Offset<Monster> populate2(flatbuffers::FlatBufferBuilder &builder) {
auto name_offset = builder.CreateString(m2_name);
return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m2_color);
}

bool release_n_verify(flatbuffers::FlatBufferBuilder &fbb, const std::string &expected_name, Color color) {
flatbuffers::DetachedBuffer buf = fbb.Release();
const Monster *monster = flatbuffers::GetRoot<Monster>(buf.data());
bool verify(flatbuffers::grpc::Message<Monster> &msg, const std::string &expected_name, Color color) {
const Monster *monster = msg.GetRoot();
return (monster->name()->str() == expected_name) && (monster->color() == color);
}

Expand All @@ -39,121 +14,175 @@ bool release_n_verify(flatbuffers::grpc::MessageBuilder &mbb, const std::string
return (monster->name()->str() == expected_name) && (monster->color() == color);
}

struct OwnedAllocator : public flatbuffers::DefaultAllocator {};

struct TestHeapMessageBuilder : public flatbuffers::FlatBufferBuilder {
TestHeapMessageBuilder()
: flatbuffers::FlatBufferBuilder(2048, new OwnedAllocator(), true) {}
};

template <class Builder>
struct BuilderTests {
static void empty_builder_movector_test() {
Builder b1;
size_t b1_size = b1.GetSize();
Builder b2(std::move(b1));
size_t b2_size = b2.GetSize();
test_assert(b1_size == 0);
test_assert(b1_size == b2_size);
template <>
struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder> {
static void builder_reusable_after_release_message_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE)) {
return;
}

flatbuffers::grpc::MessageBuilder b1;
std::vector<flatbuffers::grpc::Message<Monster>> buffers;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
buffers.push_back(b1.ReleaseMessage<Monster>());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));
}
}

static void nonempty_builder_movector_test() {
Builder b1;
populate1(b1);
size_t b1_size = b1.GetSize();
Builder b2(std::move(b1));
test_assert(b1_size == b2.GetSize());
test_assert(0 == b1.GetSize());
static void builder_reusable_after_release_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE)) {
return;
}

// FIXME: Populate-Release loop fails assert(GRPC_SLICE_IS_EMPTY(slice_)).

flatbuffers::grpc::MessageBuilder b1;
std::vector<flatbuffers::DetachedBuffer> buffers;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
buffers.push_back(b1.Release());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));
}
}

static void builder_movector_before_finish_test() {
Builder b1;
auto root_offset1 = populate1(b1);
Builder b2(std::move(b1));
b2.Finish(root_offset1);
test_assert(release_n_verify(b2, m1_name, m1_color));
test_assert(0 == b1.GetSize());
static void builder_reusable_after_releaseraw_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW)) {
return;
}

flatbuffers::grpc::MessageBuilder b1;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
size_t size, offset;
grpc_slice slice;
const uint8_t *buf = b1.ReleaseRaw(size, offset, slice);
TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color));
grpc_slice_unref(slice);
}
}

static void builder_movector_after_finish_test() {
Builder b1;
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
Builder b2(std::move(b1));
test_assert(release_n_verify(b2, m1_name, m1_color));
test_assert(0 == b1.GetSize());
static void builder_reusable_after_release_and_move_assign_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN)) {
return;
}

// FIXME: Release-move_assign loop fails assert(p == GRPC_SLICE_START_PTR(slice_)).

flatbuffers::grpc::MessageBuilder b1;
std::vector<flatbuffers::DetachedBuffer> buffers;

for (int i = 0; i < 1; ++i) {
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
buffers.push_back(b1.Release());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));

// bring b1 back to life.
flatbuffers::grpc::MessageBuilder b2;
b1 = std::move(b2);
TEST_EQ_FUNC(b1.GetSize(), 0);
TEST_EQ_FUNC(b2.GetSize(), 0);
}
}

static void builder_move_assign_before_finish_test() {
Builder b1;
auto root_offset1 = populate1(b1);
Builder b2;
populate2(b2);
b2 = std::move(b1);
b2.Finish(root_offset1);
test_assert(release_n_verify(b2, m1_name, m1_color));
test_assert(0 == b1.GetSize());
static void builder_reusable_after_release_message_and_move_assign_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE_AND_MOVE_ASSIGN)) {
return;
}

flatbuffers::grpc::MessageBuilder b1;
std::vector<flatbuffers::grpc::Message<Monster>> buffers;

for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
buffers.push_back(b1.ReleaseMessage<Monster>());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));

// bring b1 back to life.
flatbuffers::grpc::MessageBuilder b2;
b1 = std::move(b2);
TEST_EQ_FUNC(b1.GetSize(), 0);
TEST_EQ_FUNC(b2.GetSize(), 0);
}
}

static void builder_move_assign_after_finish_test() {
Builder b1;
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
Builder b2;
auto root_offset2 = populate2(b2);
b2.Finish(root_offset2);
b2 = std::move(b1);
test_assert(release_n_verify(b2, m1_name, m1_color));
test_assert(0 == b1.GetSize());
static void builder_reusable_after_releaseraw_and_move_assign_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN)) {
return;
}

flatbuffers::grpc::MessageBuilder b1;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
size_t size, offset;
grpc_slice slice = grpc_empty_slice();
const uint8_t *buf = b1.ReleaseRaw(size, offset, slice);
TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color));
grpc_slice_unref(slice);

flatbuffers::grpc::MessageBuilder b2;
b1 = std::move(b2);
TEST_EQ_FUNC(b1.GetSize(), 0);
TEST_EQ_FUNC(b2.GetSize(), 0);
}
}

static void builder_swap_before_finish_test() {
Builder b1;
auto root_offset1 = populate1(b1);
auto size1 = b1.GetSize();
Builder b2;
auto root_offset2 = populate2(b2);
auto size2 = b2.GetSize();
b1.Swap(b2);
b1.Finish(root_offset2);
b2.Finish(root_offset1);
test_assert(b1.GetSize() > size2);
test_assert(b2.GetSize() > size1);
test_assert(release_n_verify(b1, m2_name, m2_color));
test_assert(release_n_verify(b2, m1_name, m1_color));
static void run_tests(TestSelector selector) {
builder_reusable_after_release_test(selector);
builder_reusable_after_release_message_test(selector);
builder_reusable_after_releaseraw_test(selector);
builder_reusable_after_release_and_move_assign_test(selector);
builder_reusable_after_releaseraw_and_move_assign_test(selector);
builder_reusable_after_release_message_and_move_assign_test(selector);
}
};

static void builder_swap_after_finish_test() {
Builder b1;
auto root_offset1 = populate1(b1);
b1.Finish(root_offset1);
auto size1 = b1.GetSize();
Builder b2;
auto root_offset2 = populate2(b2);
b2.Finish(root_offset2);
auto size2 = b2.GetSize();
b1.Swap(b2);
test_assert(b1.GetSize() == size2);
test_assert(b2.GetSize() == size1);
test_assert(release_n_verify(b1, m2_name, m2_color));
test_assert(release_n_verify(b2, m1_name, m1_color));
void slice_allocator_tests() {
// move-construct no-delete test
{
size_t size = 2048;
flatbuffers::grpc::SliceAllocator sa1;
uint8_t *buf = sa1.allocate(size);
TEST_ASSERT_FUNC(buf != 0);
buf[0] = 100;
buf[size-1] = 200;
flatbuffers::grpc::SliceAllocator sa2(std::move(sa1));
// buf should be deleted after move-construct
TEST_EQ_FUNC(buf[0], 100);
TEST_EQ_FUNC(buf[size-1], 200);
// buf is freed here
}

static void all_tests() {
empty_builder_movector_test();
nonempty_builder_movector_test();
builder_movector_before_finish_test();
builder_movector_after_finish_test();
builder_move_assign_before_finish_test();
builder_move_assign_after_finish_test();
builder_swap_before_finish_test();
builder_swap_after_finish_test();
// move-assign test
{
flatbuffers::grpc::SliceAllocator sa1, sa2;
uint8_t *buf = sa1.allocate(2048);
sa1 = std::move(sa2);
// sa1 deletes previously allocated memory in move-assign.
// So buf is no longer usable here.
TEST_ASSERT_FUNC(buf != 0);
}
};
}

int builder_tests() {
void message_builder_tests() {
slice_allocator_tests();
BuilderTests<flatbuffers::grpc::MessageBuilder>::all_tests();
BuilderTests<flatbuffers::FlatBufferBuilder>::all_tests();
BuilderTests<TestHeapMessageBuilder>::all_tests();
return builder_test_error;

BuilderReuseTestSelector tests[6] = {
// REUSABLE_AFTER_RELEASE, // Assertion failed: (GRPC_SLICE_IS_EMPTY(slice_))
// REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN, // Assertion failed: (p == GRPC_SLICE_START_PTR(slice_)

REUSABLE_AFTER_RELEASE_RAW,
REUSABLE_AFTER_RELEASE_MESSAGE,
REUSABLE_AFTER_RELEASE_MESSAGE_AND_MOVE_ASSIGN,
REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN
};

BuilderReuseTests<flatbuffers::grpc::MessageBuilder>::run_tests(TestSelector(tests, tests+6));
}
Loading

0 comments on commit 49fed8c

Please sign in to comment.