Skip to content

Commit e491358

Browse files
Add tests for native_type on tables.
1 parent 45f75b7 commit e491358

File tree

5 files changed

+91
-62
lines changed

5 files changed

+91
-62
lines changed

tests/native_type_test.fbs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ struct Vector3DAlt (native_type:"Native::Vector3D", native_type_pack_name:"Vecto
1414
c:float;
1515
}
1616

17-
// table Matrix (native_type:"Native::Matrix") {
18-
table Matrix {
17+
table Matrix (native_type:"Native::Matrix") {
1918
rows:int32;
2019
columns:int32;
2120
values:[float];

tests/native_type_test_generated.h

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ struct Vector3DAlt;
2323

2424
struct Matrix;
2525
struct MatrixBuilder;
26-
struct MatrixT;
2726

2827
struct ApplicationData;
2928
struct ApplicationDataBuilder;
3029
struct ApplicationDataT;
3130

32-
bool operator==(const MatrixT &lhs, const MatrixT &rhs);
33-
bool operator!=(const MatrixT &lhs, const MatrixT &rhs);
3431
bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs);
3532
bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs);
3633

@@ -124,15 +121,8 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vector3DAlt FLATBUFFERS_FINAL_CLASS {
124121
};
125122
FLATBUFFERS_STRUCT_END(Vector3DAlt, 12);
126123

127-
struct MatrixT : public ::flatbuffers::NativeTable {
128-
typedef Matrix TableType;
129-
int32_t rows = 0;
130-
int32_t columns = 0;
131-
std::vector<float> values{};
132-
};
133-
134124
struct Matrix FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
135-
typedef MatrixT NativeTableType;
125+
typedef Native::Matrix NativeTableType;
136126
typedef MatrixBuilder Builder;
137127
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
138128
return MatrixTypeTable();
@@ -168,9 +158,9 @@ struct Matrix FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
168158
verifier.VerifyVector(values()) &&
169159
verifier.EndTable();
170160
}
171-
MatrixT *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
172-
void UnPackTo(MatrixT *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
173-
static ::flatbuffers::Offset<Matrix> Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixT* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
161+
Native::Matrix *UnPack(const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
162+
void UnPackTo(Native::Matrix *_o, const ::flatbuffers::resolver_function_t *_resolver = nullptr) const;
163+
static ::flatbuffers::Offset<Matrix> Pack(::flatbuffers::FlatBufferBuilder &_fbb, const Native::Matrix* _o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
174164
};
175165

176166
struct MatrixBuilder {
@@ -222,16 +212,16 @@ inline ::flatbuffers::Offset<Matrix> CreateMatrixDirect(
222212
values__);
223213
}
224214

225-
::flatbuffers::Offset<Matrix> CreateMatrix(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
215+
::flatbuffers::Offset<Matrix> CreateMatrix(::flatbuffers::FlatBufferBuilder &_fbb, const Native::Matrix *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
226216

227217
struct ApplicationDataT : public ::flatbuffers::NativeTable {
228218
typedef ApplicationData TableType;
229219
std::vector<Native::Vector3D> vectors{};
230220
std::vector<Native::Vector3D> vectors_alt{};
231221
std::unique_ptr<Native::Vector3D> position{};
232222
Native::Vector3D position_inline{};
233-
std::unique_ptr<Geometry::MatrixT> matrix{};
234-
std::vector<std::unique_ptr<Geometry::MatrixT>> matrices{};
223+
std::unique_ptr<Native::Matrix> matrix{};
224+
std::vector<std::unique_ptr<Native::Matrix>> matrices{};
235225
ApplicationDataT() = default;
236226
ApplicationDataT(const ApplicationDataT &o);
237227
ApplicationDataT(ApplicationDataT&&) FLATBUFFERS_NOEXCEPT = default;
@@ -382,51 +372,16 @@ inline ::flatbuffers::Offset<ApplicationData> CreateApplicationDataDirect(
382372

383373
::flatbuffers::Offset<ApplicationData> CreateApplicationData(::flatbuffers::FlatBufferBuilder &_fbb, const ApplicationDataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr);
384374

385-
386-
inline bool operator==(const MatrixT &lhs, const MatrixT &rhs) {
387-
return
388-
(lhs.rows == rhs.rows) &&
389-
(lhs.columns == rhs.columns) &&
390-
(lhs.values == rhs.values);
391-
}
392-
393-
inline bool operator!=(const MatrixT &lhs, const MatrixT &rhs) {
394-
return !(lhs == rhs);
395-
}
396-
397-
398-
inline MatrixT *Matrix::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
399-
auto _o = std::unique_ptr<MatrixT>(new MatrixT());
375+
inline Native::Matrix *Matrix::UnPack(const ::flatbuffers::resolver_function_t *_resolver) const {
376+
auto _o = std::unique_ptr<Native::Matrix>(new Native::Matrix());
400377
UnPackTo(_o.get(), _resolver);
401378
return _o.release();
402379
}
403380

404-
inline void Matrix::UnPackTo(MatrixT *_o, const ::flatbuffers::resolver_function_t *_resolver) const {
405-
(void)_o;
406-
(void)_resolver;
407-
{ auto _e = rows(); _o->rows = _e; }
408-
{ auto _e = columns(); _o->columns = _e; }
409-
{ auto _e = values(); if (_e) { _o->values.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->values[_i] = _e->Get(_i); } } else { _o->values.resize(0); } }
410-
}
411-
412-
inline ::flatbuffers::Offset<Matrix> CreateMatrix(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {
381+
inline ::flatbuffers::Offset<Matrix> CreateMatrix(::flatbuffers::FlatBufferBuilder &_fbb, const Native::Matrix *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {
413382
return Matrix::Pack(_fbb, _o, _rehasher);
414383
}
415384

416-
inline ::flatbuffers::Offset<Matrix> Matrix::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const MatrixT* _o, const ::flatbuffers::rehasher_function_t *_rehasher) {
417-
(void)_rehasher;
418-
(void)_o;
419-
struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const MatrixT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va;
420-
auto _rows = _o->rows;
421-
auto _columns = _o->columns;
422-
auto _values = _o->values.size() ? _fbb.CreateVector(_o->values) : 0;
423-
return Geometry::CreateMatrix(
424-
_fbb,
425-
_rows,
426-
_columns,
427-
_values);
428-
}
429-
430385

431386
inline bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs) {
432387
return
@@ -435,7 +390,7 @@ inline bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs)
435390
((lhs.position == rhs.position) || (lhs.position && rhs.position && *lhs.position == *rhs.position)) &&
436391
(lhs.position_inline == rhs.position_inline) &&
437392
((lhs.matrix == rhs.matrix) || (lhs.matrix && rhs.matrix && *lhs.matrix == *rhs.matrix)) &&
438-
(lhs.matrices.size() == rhs.matrices.size() && std::equal(lhs.matrices.cbegin(), lhs.matrices.cend(), rhs.matrices.cbegin(), [](std::unique_ptr<Geometry::MatrixT> const &a, std::unique_ptr<Geometry::MatrixT> const &b) { return (a == b) || (a && b && *a == *b); }));
393+
(lhs.matrices.size() == rhs.matrices.size() && std::equal(lhs.matrices.cbegin(), lhs.matrices.cend(), rhs.matrices.cbegin(), [](std::unique_ptr<Native::Matrix> const &a, std::unique_ptr<Native::Matrix> const &b) { return (a == b) || (a && b && *a == *b); }));
439394
}
440395

441396
inline bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs) {
@@ -448,9 +403,9 @@ inline ApplicationDataT::ApplicationDataT(const ApplicationDataT &o)
448403
vectors_alt(o.vectors_alt),
449404
position((o.position) ? new Native::Vector3D(*o.position) : nullptr),
450405
position_inline(o.position_inline),
451-
matrix((o.matrix) ? new Geometry::MatrixT(*o.matrix) : nullptr) {
406+
matrix((o.matrix) ? new Native::Matrix(*o.matrix) : nullptr) {
452407
matrices.reserve(o.matrices.size());
453-
for (const auto &matrices_ : o.matrices) { matrices.emplace_back((matrices_) ? new Geometry::MatrixT(*matrices_) : nullptr); }
408+
for (const auto &matrices_ : o.matrices) { matrices.emplace_back((matrices_) ? new Native::Matrix(*matrices_) : nullptr); }
454409
}
455410

456411
inline ApplicationDataT &ApplicationDataT::operator=(ApplicationDataT o) FLATBUFFERS_NOEXCEPT {
@@ -476,8 +431,8 @@ inline void ApplicationData::UnPackTo(ApplicationDataT *_o, const ::flatbuffers:
476431
{ auto _e = vectors_alt(); if (_e) { _o->vectors_alt.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vectors_alt[_i] = ::flatbuffers::UnPackVector3DAlt(*_e->Get(_i)); } } else { _o->vectors_alt.resize(0); } }
477432
{ auto _e = position(); if (_e) _o->position = std::unique_ptr<Native::Vector3D>(new Native::Vector3D(::flatbuffers::UnPack(*_e))); }
478433
{ auto _e = position_inline(); if (_e) _o->position_inline = ::flatbuffers::UnPack(*_e); }
479-
{ auto _e = matrix(); if (_e) { if(_o->matrix) { _e->UnPackTo(_o->matrix.get(), _resolver); } else { _o->matrix = std::unique_ptr<Geometry::MatrixT>(_e->UnPack(_resolver)); } } else if (_o->matrix) { _o->matrix.reset(); } }
480-
{ auto _e = matrices(); if (_e) { _o->matrices.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->matrices[_i]) { _e->Get(_i)->UnPackTo(_o->matrices[_i].get(), _resolver); } else { _o->matrices[_i] = std::unique_ptr<Geometry::MatrixT>(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->matrices.resize(0); } }
434+
{ auto _e = matrix(); if (_e) { if(_o->matrix) { _e->UnPackTo(_o->matrix.get(), _resolver); } else { _o->matrix = std::unique_ptr<Native::Matrix>(_e->UnPack(_resolver)); } } else if (_o->matrix) { _o->matrix.reset(); } }
435+
{ auto _e = matrices(); if (_e) { _o->matrices.resize(_e->size()); for (::flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { if(_o->matrices[_i]) { _e->Get(_i)->UnPackTo(_o->matrices[_i].get(), _resolver); } else { _o->matrices[_i] = std::unique_ptr<Native::Matrix>(_e->Get(_i)->UnPack(_resolver)); } } } else { _o->matrices.resize(0); } }
481436
}
482437

483438
inline ::flatbuffers::Offset<ApplicationData> CreateApplicationData(::flatbuffers::FlatBufferBuilder &_fbb, const ApplicationDataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher) {

tests/native_type_test_impl.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,34 @@ const Native::Vector3D UnPackVector3DAlt(const Geometry::Vector3DAlt& obj) {
1919
return Native::Vector3D(obj.a(), obj.b(), obj.c());
2020
}
2121
} // namespace flatbuffers
22+
23+
namespace Geometry {
24+
void Matrix::UnPackTo(
25+
Native::Matrix *_o,
26+
const ::flatbuffers::resolver_function_t *_resolver) const {
27+
(void)_resolver;
28+
29+
auto _rows = rows();
30+
if (_rows) { _o->rows = _rows; }
31+
32+
auto _columns = columns();
33+
if (_columns) { _o->columns = _columns; }
34+
35+
auto _values = values();
36+
if (_values) {
37+
_o->values.resize(_values->size());
38+
for (::flatbuffers::uoffset_t i = 0; i < _values->size(); i++) {
39+
_o->values[i] = _values->Get(i);
40+
}
41+
}
42+
}
43+
44+
::flatbuffers::Offset<Matrix> Matrix::Pack(
45+
::flatbuffers::FlatBufferBuilder &_fbb, const Native::Matrix *_o,
46+
const ::flatbuffers::rehasher_function_t *_rehasher) {
47+
(void)_rehasher;
48+
49+
return CreateMatrix(_fbb, _o->rows, _o->columns,
50+
_fbb.CreateVector<float>(_o->values));
51+
}
52+
} // namespace Geometry

tests/native_type_test_impl.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef NATIVE_TYPE_TEST_IMPL_H
22
#define NATIVE_TYPE_TEST_IMPL_H
33

4+
#include <vector>
5+
46
namespace Native {
57
struct Vector3D {
68
float x;
@@ -22,6 +24,25 @@ struct Vector3D {
2224
return (x == other.x) && (y == other.y) && (z == other.z);
2325
}
2426
};
27+
28+
struct Matrix {
29+
int rows;
30+
int columns;
31+
std::vector<float> values;
32+
33+
Matrix() : Matrix(0, 0) {}
34+
35+
Matrix(int _rows, int _columns) {
36+
this->rows = _rows;
37+
this->columns = _columns;
38+
values.resize(_rows * _columns);
39+
}
40+
41+
bool operator==(const Matrix &other) const {
42+
return (rows == other.rows) && (columns == other.columns) &&
43+
(values == other.values);
44+
}
45+
};
2546
} // namespace Native
2647

2748
namespace Geometry {

tests/test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,15 @@ void NativeTypeTest() {
920920
Native::Vector3D(20 * i + 0.1f, 20 * i + 0.2f, 20 * i + 0.3f));
921921
}
922922

923+
src_data.matrix = std::make_unique<Native::Matrix>(1, 2);
924+
src_data.matrix->values = {3, 4};
925+
926+
for (int i = 0; i < N; ++i) {
927+
src_data.matrices.push_back(std::make_unique<Native::Matrix>(1, i));
928+
std::fill(src_data.matrices[i]->values.begin(),
929+
src_data.matrices[i]->values.end(), i + 0.5f);
930+
}
931+
923932
flatbuffers::FlatBufferBuilder fbb;
924933
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &src_data));
925934

@@ -943,6 +952,20 @@ void NativeTypeTest() {
943952
TEST_EQ(v2.y, 20 * i + 0.2f);
944953
TEST_EQ(v2.z, 20 * i + 0.3f);
945954
}
955+
956+
TEST_EQ(dstDataT->matrix->rows, 1);
957+
TEST_EQ(dstDataT->matrix->columns, 2);
958+
TEST_EQ(dstDataT->matrix->values[0], 3);
959+
TEST_EQ(dstDataT->matrix->values[1], 4);
960+
961+
for (int i = 0; i < N; ++i) {
962+
const Native::Matrix &m = *dstDataT->matrices[i];
963+
TEST_EQ(m.rows, 1);
964+
TEST_EQ(m.columns, i);
965+
for (int j = 0; j < i; ++j) {
966+
TEST_EQ(m.values[j], i + 0.5f);
967+
}
968+
}
946969
}
947970

948971
// Guard against -Wunused-function on platforms without file tests.

0 commit comments

Comments
 (0)