From 0a4bf1d6d32bf423fb270b6c60c9e2d697277944 Mon Sep 17 00:00:00 2001 From: Alexey Geraskin Date: Tue, 16 Jul 2019 18:20:21 +0300 Subject: [PATCH] [C++] Add unit test for native_type usage --- CMakeLists.txt | 5 +++++ tests/generate_code.bat | 1 + tests/native_type_test.fbs | 18 ++++++++++++++++++ tests/test.cpp | 25 +++++++++++++++++++++++++ tests/vector3d.h | 16 ++++++++++++++++ tests/vector3d_pack.cpp | 13 +++++++++++++ tests/vector3d_pack.h | 15 +++++++++++++++ 7 files changed, 93 insertions(+) create mode 100644 tests/native_type_test.fbs create mode 100644 tests/vector3d.h create mode 100644 tests/vector3d_pack.cpp create mode 100644 tests/vector3d_pack.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c7da97f5a..061903a64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,10 +117,15 @@ set(FlatBuffers_Tests_SRCS tests/test_assert.cpp tests/test_builder.h tests/test_builder.cpp + tests/vector3d.h + tests/vector3d_pack.h + tests/vector3d_pack.cpp # file generate by running compiler on tests/monster_test.fbs ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h # file generate by running compiler on tests/arrays_test.fbs ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h + # file generate by running compiler on tests/native_type_test.fbs + ${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h ) set(FlatBuffers_Sample_Binary_SRCS diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 3164dff4a..d0b3b8283 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -22,6 +22,7 @@ if "%1"=="-b" set buildtype=%2 ..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL +..\%buildtype%\flatc.exe --cpp --gen-mutable --gen-object-api native_type_test.fbs || goto FAIL IF NOT "%MONSTER_EXTRA%"=="skip" ( @echo Generate MosterExtra diff --git a/tests/native_type_test.fbs b/tests/native_type_test.fbs new file mode 100644 index 000000000..d917cfa26 --- /dev/null +++ b/tests/native_type_test.fbs @@ -0,0 +1,18 @@ +native_include "vector3d.h"; +native_include "vector3d_pack.h"; + +namespace Geometry; + +struct Vector3D (native_type:"Native::Vector3D") +{ + x:double; + y:double; + z:double; +} + +table ApplicationData +{ + vectors:[Vector3D]; +} + +root_type ApplicationData; \ No newline at end of file diff --git a/tests/test.cpp b/tests/test.cpp index 903c58df7..5ca87b9a4 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -40,6 +40,7 @@ #include "test_assert.h" #include "flatbuffers/flexbuffers.h" +#include "native_type_test_generated.h" // clang-format off // Check that char* and uint8_t* are interoperable types. @@ -2841,6 +2842,29 @@ void FixedLengthArrayTest() { #endif } +void NativeTypeTest() { + const int N = 3; + + Geometry::ApplicationDataT srcDataT; + srcDataT.vectors.reserve(N); + + for (int i = 0; i < N; ++i) { + srcDataT.vectors.push_back (Native::Vector3D(10 * i + 0.1, 10 * i + 0.2, 10 * i + 0.3)); + } + + flatbuffers::FlatBufferBuilder fbb; + fbb.Finish(Geometry::ApplicationData::Pack(fbb, &srcDataT)); + + auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer()); + + for (int i = 0; i < N; ++i) { + Native::Vector3D& v = dstDataT->vectors[i]; + TEST_EQ(v.x, 10 * i + 0.1); + TEST_EQ(v.y, 10 * i + 0.2); + TEST_EQ(v.z, 10 * i + 0.3); + } +} + void FixedLengthArrayJsonTest(bool binary) { // VS10 does not support typed enums, exclude from tests #if !defined(_MSC_VER) || _MSC_VER >= 1700 @@ -2984,6 +3008,7 @@ int FlatBufferTests() { InvalidFloatTest(); TestMonsterExtraFloats(); FixedLengthArrayTest(); + NativeTypeTest(); return 0; } diff --git a/tests/vector3d.h b/tests/vector3d.h new file mode 100644 index 000000000..62f55094a --- /dev/null +++ b/tests/vector3d.h @@ -0,0 +1,16 @@ +#ifndef VECTOR3D_H +#define VECTOR3D_H + +namespace Native { + struct Vector3D { + public: + double x; + double y; + double z; + + Vector3D() = default; + Vector3D(double x, double y, double z) { this->x = x; this->y = y; this->z = z; } + }; +} + +#endif // VECTOR3D_H diff --git a/tests/vector3d_pack.cpp b/tests/vector3d_pack.cpp new file mode 100644 index 000000000..2b35743d1 --- /dev/null +++ b/tests/vector3d_pack.cpp @@ -0,0 +1,13 @@ +#include "vector3d_pack.h" + +#include "native_type_test_generated.h" + +namespace flatbuffers { + Geometry::Vector3D Pack(const Native::Vector3D& obj) { + return Geometry::Vector3D(obj.x, obj.y, obj.z); + } + + const Native::Vector3D UnPack(const Geometry::Vector3D& obj) { + return Native::Vector3D(obj.x(), obj.y(), obj.z()); + } +} diff --git a/tests/vector3d_pack.h b/tests/vector3d_pack.h new file mode 100644 index 000000000..beaa43e26 --- /dev/null +++ b/tests/vector3d_pack.h @@ -0,0 +1,15 @@ +#ifndef VECTOR3D_PACK_H +#define VECTOR3D_PACK_H + +#include "vector3d.h" + +namespace Geometry { + struct Vector3D; +} + +namespace flatbuffers { + Geometry::Vector3D Pack(const Native::Vector3D& obj); + const Native::Vector3D UnPack(const Geometry::Vector3D& obj); +} + +#endif // VECTOR3D_PACK_H