mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 08:21:36 +00:00
[C++] Add unit test for native_type usage
This commit is contained in:
@@ -117,10 +117,15 @@ set(FlatBuffers_Tests_SRCS
|
|||||||
tests/test_assert.cpp
|
tests/test_assert.cpp
|
||||||
tests/test_builder.h
|
tests/test_builder.h
|
||||||
tests/test_builder.cpp
|
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
|
# file generate by running compiler on tests/monster_test.fbs
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
|
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
|
||||||
# file generate by running compiler on tests/arrays_test.fbs
|
# file generate by running compiler on tests/arrays_test.fbs
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h
|
${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
|
set(FlatBuffers_Sample_Binary_SRCS
|
||||||
|
|||||||
@@ -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 -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 --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 --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" (
|
IF NOT "%MONSTER_EXTRA%"=="skip" (
|
||||||
@echo Generate MosterExtra
|
@echo Generate MosterExtra
|
||||||
|
|||||||
18
tests/native_type_test.fbs
Normal file
18
tests/native_type_test.fbs
Normal file
@@ -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;
|
||||||
@@ -40,6 +40,7 @@
|
|||||||
#include "test_assert.h"
|
#include "test_assert.h"
|
||||||
|
|
||||||
#include "flatbuffers/flexbuffers.h"
|
#include "flatbuffers/flexbuffers.h"
|
||||||
|
#include "native_type_test_generated.h"
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
// Check that char* and uint8_t* are interoperable types.
|
// Check that char* and uint8_t* are interoperable types.
|
||||||
@@ -2841,6 +2842,29 @@ void FixedLengthArrayTest() {
|
|||||||
#endif
|
#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) {
|
void FixedLengthArrayJsonTest(bool binary) {
|
||||||
// VS10 does not support typed enums, exclude from tests
|
// VS10 does not support typed enums, exclude from tests
|
||||||
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
@@ -2984,6 +3008,7 @@ int FlatBufferTests() {
|
|||||||
InvalidFloatTest();
|
InvalidFloatTest();
|
||||||
TestMonsterExtraFloats();
|
TestMonsterExtraFloats();
|
||||||
FixedLengthArrayTest();
|
FixedLengthArrayTest();
|
||||||
|
NativeTypeTest();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
tests/vector3d.h
Normal file
16
tests/vector3d.h
Normal file
@@ -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
|
||||||
13
tests/vector3d_pack.cpp
Normal file
13
tests/vector3d_pack.cpp
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
15
tests/vector3d_pack.h
Normal file
15
tests/vector3d_pack.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user