/
test.cpp
1558 lines (1369 loc) · 54.5 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <cmath>
#include <memory>
#include <string>
#include "evolution_test.h"
#include "alignment_test.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/minireflect.h"
#include "flatbuffers/registry.h"
#include "flatbuffers/util.h"
#include "fuzz_test.h"
#include "json_test.h"
#include "monster_test.h"
#include "monster_test_generated.h"
#include "optional_scalars_test.h"
#include "native_inline_table_test_generated.h"
#include "parser_test.h"
#include "proto_test.h"
#include "reflection_test.h"
#include "union_vector/union_vector_generated.h"
#if !defined(_MSC_VER) || _MSC_VER >= 1700
# include "arrays_test_generated.h"
#endif
#include "flexbuffers_test.h"
#include "is_quiet_nan.h"
#include "monster_test_bfbs_generated.h" // Generated using --bfbs-comments --bfbs-builtins --cpp --bfbs-gen-embed
#include "native_type_test_generated.h"
#include "test_assert.h"
#include "util_test.h"
void FlatBufferBuilderTest();
namespace flatbuffers {
namespace tests {
namespace {
// clang-format off
// Check that char* and uint8_t* are interoperable types.
// The reinterpret_cast<> between the pointers are used to simplify data loading.
static_assert(flatbuffers::is_same<uint8_t, char>::value ||
flatbuffers::is_same<uint8_t, unsigned char>::value,
"unexpected uint8_t type");
#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
// Ensure IEEE-754 support if tests of floats with NaN/Inf will run.
static_assert(std::numeric_limits<float>::is_iec559 &&
std::numeric_limits<double>::is_iec559,
"IEC-559 (IEEE-754) standard required");
#endif
// clang-format on
using namespace MyGame::Example;
void TriviallyCopyableTest() {
// clang-format off
#if __GNUG__ && __GNUC__ < 5 && \
!(defined(__clang__) && __clang_major__ >= 16)
TEST_EQ(__has_trivial_copy(Vec3), true);
#else
#if __cplusplus >= 201103L
TEST_EQ(std::is_trivially_copyable<Vec3>::value, true);
#endif
#endif
// clang-format on
}
void GenerateTableTextTest(const std::string &tests_data_path) {
std::string schemafile;
std::string jsonfile;
bool ok =
flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
false, &schemafile) &&
flatbuffers::LoadFile((tests_data_path + "monsterdata_test.json").c_str(),
false, &jsonfile);
TEST_EQ(ok, true);
auto include_test_path =
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
const char *include_directories[] = { tests_data_path.c_str(),
include_test_path.c_str(), nullptr };
flatbuffers::IDLOptions opt;
opt.indent_step = -1;
flatbuffers::Parser parser(opt);
ok = parser.Parse(schemafile.c_str(), include_directories) &&
parser.Parse(jsonfile.c_str(), include_directories);
TEST_EQ(ok, true);
// Test root table
const Monster *monster = GetMonster(parser.builder_.GetBufferPointer());
const auto abilities = monster->testarrayofsortedstruct();
TEST_EQ(abilities->size(), 3);
TEST_EQ(abilities->Get(0)->id(), 0);
TEST_EQ(abilities->Get(0)->distance(), 45);
TEST_EQ(abilities->Get(1)->id(), 1);
TEST_EQ(abilities->Get(1)->distance(), 21);
TEST_EQ(abilities->Get(2)->id(), 5);
TEST_EQ(abilities->Get(2)->distance(), 12);
std::string jsongen;
auto result = GenerateTextFromTable(parser, monster, "MyGame.Example.Monster",
&jsongen);
TEST_EQ(result, true);
// Test sub table
const Vec3 *pos = monster->pos();
jsongen.clear();
result = GenerateTextFromTable(parser, pos, "MyGame.Example.Vec3", &jsongen);
TEST_EQ(result, true);
TEST_EQ_STR(
jsongen.c_str(),
"{x: 1.0,y: 2.0,z: 3.0,test1: 3.0,test2: \"Green\",test3: {a: 5,b: 6}}");
const Test &test3 = pos->test3();
jsongen.clear();
result =
GenerateTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen);
TEST_EQ(result, true);
TEST_EQ_STR(jsongen.c_str(), "{a: 5,b: 6}");
const Test *test4 = monster->test4()->Get(0);
jsongen.clear();
result =
GenerateTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen);
TEST_EQ(result, true);
TEST_EQ_STR(jsongen.c_str(), "{a: 10,b: 20}");
}
void MultiFileNameClashTest(const std::string &tests_data_path) {
const auto name_clash_path =
flatbuffers::ConCatPathFileName(tests_data_path, "name_clash_test");
const char *include_directories[] = { name_clash_path.c_str() };
// Load valid 2 file Flatbuffer schema
const auto valid_path =
flatbuffers::ConCatPathFileName(name_clash_path, "valid_test1.fbs");
std::string valid_schema;
TEST_ASSERT(flatbuffers::LoadFile(valid_path.c_str(), false, &valid_schema));
// Clashing table and union names in different namespaces must be parsable
TEST_ASSERT(
flatbuffers::Parser().Parse(valid_schema.c_str(), include_directories));
flatbuffers::Parser p;
TEST_ASSERT(p.Parse(valid_schema.c_str(), include_directories));
// Load invalid 2 file Flatbuffer schema
const auto invalid_path =
flatbuffers::ConCatPathFileName(name_clash_path, "invalid_test1.fbs");
std::string invalid_schema;
TEST_ASSERT(
flatbuffers::LoadFile(invalid_path.c_str(), false, &invalid_schema));
// Clashing table and union names in same namespace must fail to parse
TEST_EQ(
flatbuffers::Parser().Parse(invalid_schema.c_str(), include_directories),
false);
}
void InvalidNestedFlatbufferTest(const std::string &tests_data_path) {
// First, load and parse FlatBuffer schema (.fbs)
std::string schemafile;
TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
false, &schemafile),
true);
auto include_test_path =
flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
const char *include_directories[] = { tests_data_path.c_str(),
include_test_path.c_str(), nullptr };
flatbuffers::Parser parser1;
TEST_EQ(parser1.Parse(schemafile.c_str(), include_directories), true);
// "color" inside nested flatbuffer contains invalid enum value
TEST_EQ(parser1.Parse("{ name: \"Bender\", testnestedflatbuffer: { name: "
"\"Leela\", color: \"nonexistent\"}}"),
false);
}
void UnionVectorTest(const std::string &tests_data_path) {
// load FlatBuffer fbs schema and json.
std::string schemafile, jsonfile;
TEST_EQ(flatbuffers::LoadFile(
(tests_data_path + "union_vector/union_vector.fbs").c_str(),
false, &schemafile),
true);
TEST_EQ(flatbuffers::LoadFile(
(tests_data_path + "union_vector/union_vector.json").c_str(),
false, &jsonfile),
true);
// parse schema.
flatbuffers::IDLOptions idl_opts;
idl_opts.lang_to_generate |= flatbuffers::IDLOptions::kBinary;
flatbuffers::Parser parser(idl_opts);
TEST_EQ(parser.Parse(schemafile.c_str()), true);
flatbuffers::FlatBufferBuilder fbb;
// union types.
std::vector<uint8_t> types;
types.push_back(static_cast<uint8_t>(Character_Belle));
types.push_back(static_cast<uint8_t>(Character_MuLan));
types.push_back(static_cast<uint8_t>(Character_BookFan));
types.push_back(static_cast<uint8_t>(Character_Other));
types.push_back(static_cast<uint8_t>(Character_Unused));
// union values.
std::vector<flatbuffers::Offset<void>> characters;
characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/7)).Union());
characters.push_back(CreateAttacker(fbb, /*sword_attack_damage=*/5).Union());
characters.push_back(fbb.CreateStruct(BookReader(/*books_read=*/2)).Union());
characters.push_back(fbb.CreateString("Other").Union());
characters.push_back(fbb.CreateString("Unused").Union());
// create Movie.
const auto movie_offset =
CreateMovie(fbb, Character_Rapunzel,
fbb.CreateStruct(Rapunzel(/*hair_length=*/6)).Union(),
fbb.CreateVector(types), fbb.CreateVector(characters));
FinishMovieBuffer(fbb, movie_offset);
flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
TEST_EQ(VerifyMovieBuffer(verifier), true);
auto flat_movie = GetMovie(fbb.GetBufferPointer());
auto TestMovie = [](const Movie *movie) {
TEST_EQ(movie->main_character_type() == Character_Rapunzel, true);
auto cts = movie->characters_type();
TEST_EQ(movie->characters_type()->size(), 5);
TEST_EQ(cts->GetEnum<Character>(0) == Character_Belle, true);
TEST_EQ(cts->GetEnum<Character>(1) == Character_MuLan, true);
TEST_EQ(cts->GetEnum<Character>(2) == Character_BookFan, true);
TEST_EQ(cts->GetEnum<Character>(3) == Character_Other, true);
TEST_EQ(cts->GetEnum<Character>(4) == Character_Unused, true);
auto rapunzel = movie->main_character_as_Rapunzel();
TEST_NOTNULL(rapunzel);
TEST_EQ(rapunzel->hair_length(), 6);
auto cs = movie->characters();
TEST_EQ(cs->size(), 5);
auto belle = cs->GetAs<BookReader>(0);
TEST_EQ(belle->books_read(), 7);
auto mu_lan = cs->GetAs<Attacker>(1);
TEST_EQ(mu_lan->sword_attack_damage(), 5);
auto book_fan = cs->GetAs<BookReader>(2);
TEST_EQ(book_fan->books_read(), 2);
auto other = cs->GetAsString(3);
TEST_EQ_STR(other->c_str(), "Other");
auto unused = cs->GetAsString(4);
TEST_EQ_STR(unused->c_str(), "Unused");
};
TestMovie(flat_movie);
// Also test the JSON we loaded above.
TEST_EQ(parser.Parse(jsonfile.c_str()), true);
auto jbuf = parser.builder_.GetBufferPointer();
flatbuffers::Verifier jverifier(jbuf, parser.builder_.GetSize());
TEST_EQ(VerifyMovieBuffer(jverifier), true);
TestMovie(GetMovie(jbuf));
auto movie_object = flat_movie->UnPack();
TEST_EQ(movie_object->main_character.AsRapunzel()->hair_length(), 6);
TEST_EQ(movie_object->characters[0].AsBelle()->books_read(), 7);
TEST_EQ(movie_object->characters[1].AsMuLan()->sword_attack_damage, 5);
TEST_EQ(movie_object->characters[2].AsBookFan()->books_read(), 2);
TEST_EQ_STR(movie_object->characters[3].AsOther()->c_str(), "Other");
TEST_EQ_STR(movie_object->characters[4].AsUnused()->c_str(), "Unused");
fbb.Clear();
fbb.Finish(Movie::Pack(fbb, movie_object));
delete movie_object;
auto repacked_movie = GetMovie(fbb.GetBufferPointer());
TestMovie(repacked_movie);
// Generate text using mini-reflection.
auto s =
flatbuffers::FlatBufferToString(fbb.GetBufferPointer(), MovieTypeTable());
TEST_EQ_STR(
s.c_str(),
"{ main_character_type: Rapunzel, main_character: { hair_length: 6 }, "
"characters_type: [ Belle, MuLan, BookFan, Other, Unused ], "
"characters: [ { books_read: 7 }, { sword_attack_damage: 5 }, "
"{ books_read: 2 }, \"Other\", \"Unused\" ] }");
flatbuffers::ToStringVisitor visitor("\n", true, " ");
IterateFlatBuffer(fbb.GetBufferPointer(), MovieTypeTable(), &visitor);
TEST_EQ_STR(visitor.s.c_str(),
"{\n"
" \"main_character_type\": \"Rapunzel\",\n"
" \"main_character\": {\n"
" \"hair_length\": 6\n"
" },\n"
" \"characters_type\": [\n"
" \"Belle\",\n"
" \"MuLan\",\n"
" \"BookFan\",\n"
" \"Other\",\n"
" \"Unused\"\n"
" ],\n"
" \"characters\": [\n"
" {\n"
" \"books_read\": 7\n"
" },\n"
" {\n"
" \"sword_attack_damage\": 5\n"
" },\n"
" {\n"
" \"books_read\": 2\n"
" },\n"
" \"Other\",\n"
" \"Unused\"\n"
" ]\n"
"}");
// Generate text using parsed schema.
std::string jsongen;
auto result = GenerateText(parser, fbb.GetBufferPointer(), &jsongen);
TEST_EQ(result, true);
TEST_EQ_STR(jsongen.c_str(),
"{\n"
" main_character_type: \"Rapunzel\",\n"
" main_character: {\n"
" hair_length: 6\n"
" },\n"
" characters_type: [\n"
" \"Belle\",\n"
" \"MuLan\",\n"
" \"BookFan\",\n"
" \"Other\",\n"
" \"Unused\"\n"
" ],\n"
" characters: [\n"
" {\n"
" books_read: 7\n"
" },\n"
" {\n"
" sword_attack_damage: 5\n"
" },\n"
" {\n"
" books_read: 2\n"
" },\n"
" \"Other\",\n"
" \"Unused\"\n"
" ]\n"
"}\n");
// Simple test with reflection.
parser.Serialize();
auto schema = reflection::GetSchema(parser.builder_.GetBufferPointer());
auto ok = flatbuffers::Verify(*schema, *schema->root_table(),
fbb.GetBufferPointer(), fbb.GetSize());
TEST_EQ(ok, true);
flatbuffers::Parser parser2(idl_opts);
TEST_EQ(parser2.Parse("struct Bool { b:bool; }"
"union Any { Bool }"
"table Root { a:Any; }"
"root_type Root;"),
true);
TEST_EQ(parser2.Parse("{a_type:Bool,a:{b:true}}"), true);
}
void EndianSwapTest() {
TEST_EQ(flatbuffers::EndianSwap(static_cast<int16_t>(0x1234)), 0x3412);
TEST_EQ(flatbuffers::EndianSwap(static_cast<int32_t>(0x12345678)),
0x78563412);
TEST_EQ(flatbuffers::EndianSwap(static_cast<int64_t>(0x1234567890ABCDEF)),
0xEFCDAB9078563412);
TEST_EQ(flatbuffers::EndianSwap(flatbuffers::EndianSwap(3.14f)), 3.14f);
}
void UninitializedVectorTest() {
flatbuffers::FlatBufferBuilder builder;
Test *buf = nullptr;
auto vector_offset =
builder.CreateUninitializedVectorOfStructs<Test>(2, &buf);
TEST_NOTNULL(buf);
buf[0] = Test(10, 20);
buf[1] = Test(30, 40);
auto required_name = builder.CreateString("myMonster");
auto monster_builder = MonsterBuilder(builder);
monster_builder.add_name(
required_name); // required field mandated for monster.
monster_builder.add_test4(vector_offset);
builder.Finish(monster_builder.Finish());
auto p = builder.GetBufferPointer();
auto uvt = flatbuffers::GetRoot<Monster>(p);
TEST_NOTNULL(uvt);
auto vec = uvt->test4();
TEST_NOTNULL(vec);
auto test_0 = vec->Get(0);
auto test_1 = vec->Get(1);
TEST_EQ(test_0->a(), 10);
TEST_EQ(test_0->b(), 20);
TEST_EQ(test_1->a(), 30);
TEST_EQ(test_1->b(), 40);
}
void EqualOperatorTest() {
MonsterT a;
MonsterT b;
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
b.mana = 33;
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.mana = 150;
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
b.inventory.push_back(3);
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.inventory.clear();
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
a.enemy.reset(new MonsterT());
TEST_EQ(b != a, true);
a.enemy->mana = 33;
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.enemy.reset(new MonsterT());
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.enemy->mana = 33;
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
a.enemy.reset(nullptr);
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
b.enemy->mana = 150;
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
a.enemy.reset(new MonsterT());
TEST_EQ(b == a, true);
TEST_EQ(b != a, false);
b.enemy.reset(nullptr);
b.test.type = Any_Monster;
TEST_EQ(b == a, false);
TEST_EQ(b != a, true);
// Test that vector of tables are compared by value and not by reference.
{
// Two tables are equal by default.
MonsterT a, b;
TEST_EQ(a == b, true);
// Adding only a table to one of the monster vectors should make it not
// equal (due to size mistmatch).
a.testarrayoftables.push_back(
flatbuffers::unique_ptr<MonsterT>(new MonsterT));
TEST_EQ(a == b, false);
// Adding an equalivant table to the other monster vector should make it
// equal again.
b.testarrayoftables.push_back(
flatbuffers::unique_ptr<MonsterT>(new MonsterT));
TEST_EQ(a == b, true);
// Create two new monsters that are different.
auto c = flatbuffers::unique_ptr<MonsterT>(new MonsterT);
auto d = flatbuffers::unique_ptr<MonsterT>(new MonsterT);
c->hp = 1;
d->hp = 2;
TEST_EQ(c == d, false);
// Adding them to the original monsters should also make them different.
a.testarrayoftables.push_back(std::move(c));
b.testarrayoftables.push_back(std::move(d));
TEST_EQ(a == b, false);
// Remove the mismatching monsters to get back to equality
a.testarrayoftables.pop_back();
b.testarrayoftables.pop_back();
TEST_EQ(a == b, true);
// Check that nullptr are OK.
a.testarrayoftables.push_back(nullptr);
b.testarrayoftables.push_back(
flatbuffers::unique_ptr<MonsterT>(new MonsterT));
TEST_EQ(a == b, false);
}
}
void CreateSharedStringTest() {
flatbuffers::FlatBufferBuilder builder;
const auto one1 = builder.CreateSharedString("one");
const auto two = builder.CreateSharedString("two");
const auto one2 = builder.CreateSharedString("one");
TEST_EQ(one1.o, one2.o);
const auto onetwo = builder.CreateSharedString("onetwo");
TEST_EQ(onetwo.o != one1.o, true);
TEST_EQ(onetwo.o != two.o, true);
// Support for embedded nulls
const char chars_b[] = { 'a', '\0', 'b' };
const char chars_c[] = { 'a', '\0', 'c' };
const auto null_b1 = builder.CreateSharedString(chars_b, sizeof(chars_b));
const auto null_c = builder.CreateSharedString(chars_c, sizeof(chars_c));
const auto null_b2 = builder.CreateSharedString(chars_b, sizeof(chars_b));
TEST_EQ(null_b1.o != null_c.o, true); // Issue#5058 repro
TEST_EQ(null_b1.o, null_b2.o);
// Put the strings into an array for round trip verification.
std::array<flatbuffers::Offset<flatbuffers::String>, 7> array = {
one1, two, one2, onetwo, null_b1, null_c, null_b2
};
const auto vector_offset =
builder.CreateVector<flatbuffers::Offset<flatbuffers::String>>(array);
MonsterBuilder monster_builder(builder);
monster_builder.add_name(two);
monster_builder.add_testarrayofstring(vector_offset);
builder.Finish(monster_builder.Finish());
// Read the Monster back.
const auto *monster =
flatbuffers::GetRoot<Monster>(builder.GetBufferPointer());
TEST_EQ_STR(monster->name()->c_str(), "two");
const auto *testarrayofstring = monster->testarrayofstring();
TEST_EQ(testarrayofstring->size(), flatbuffers::uoffset_t(7));
const auto &a = *testarrayofstring;
TEST_EQ_STR(a[0]->c_str(), "one");
TEST_EQ_STR(a[1]->c_str(), "two");
TEST_EQ_STR(a[2]->c_str(), "one");
TEST_EQ_STR(a[3]->c_str(), "onetwo");
TEST_EQ(a[4]->str(), (std::string(chars_b, sizeof(chars_b))));
TEST_EQ(a[5]->str(), (std::string(chars_c, sizeof(chars_c))));
TEST_EQ(a[6]->str(), (std::string(chars_b, sizeof(chars_b))));
// Make sure String::operator< works, too, since it is related to
// StringOffsetCompare.
TEST_EQ((*a[0]) < (*a[1]), true);
TEST_EQ((*a[1]) < (*a[0]), false);
TEST_EQ((*a[1]) < (*a[2]), false);
TEST_EQ((*a[2]) < (*a[1]), true);
TEST_EQ((*a[4]) < (*a[3]), true);
TEST_EQ((*a[5]) < (*a[4]), false);
TEST_EQ((*a[5]) < (*a[4]), false);
TEST_EQ((*a[6]) < (*a[5]), true);
}
#if !defined(FLATBUFFERS_USE_STD_SPAN) && !defined(FLATBUFFERS_SPAN_MINIMAL)
void FlatbuffersSpanTest() {
// Compile-time checking of non-const [] to const [] conversions.
using flatbuffers::internal::is_span_convertible;
(void)is_span_convertible<int, 1, int, 1>::type(123);
(void)is_span_convertible<const int, 1, int, 1>::type(123);
(void)is_span_convertible<const int64_t, 1, int64_t, 1>::type(123);
(void)is_span_convertible<const uint64_t, 1, uint64_t, 1>::type(123);
(void)is_span_convertible<const int, 1, const int, 1>::type(123);
(void)is_span_convertible<const int64_t, 1, const int64_t, 1>::type(123);
(void)is_span_convertible<const uint64_t, 1, const uint64_t, 1>::type(123);
using flatbuffers::span;
span<char, 0> c1;
TEST_EQ(c1.size(), 0);
span<char, flatbuffers::dynamic_extent> c2;
TEST_EQ(c2.size(), 0);
span<char> c3;
TEST_EQ(c3.size(), 0);
TEST_ASSERT(c1.empty() && c2.empty() && c3.empty());
int i_data7[7] = { 0, 1, 2, 3, 4, 5, 6 };
span<int, 7> i1(&i_data7[0], 7);
span<int> i2(i1); // make dynamic from static
TEST_EQ(i1.size(), 7);
TEST_EQ(i1.empty(), false);
TEST_EQ(i1.size(), i2.size());
TEST_EQ(i1.data(), i_data7);
TEST_EQ(i1[2], 2);
// Make const span from a non-const one.
span<const int, 7> i3(i1);
// Construct from a C-array.
span<int, 7> i4(i_data7);
span<const int, 7> i5(i_data7);
span<int> i6(i_data7);
span<const int> i7(i_data7);
TEST_EQ(i7.size(), 7);
// Check construction from a const array.
const int i_cdata5[5] = { 4, 3, 2, 1, 0 };
span<const int, 5> i8(i_cdata5);
span<const int> i9(i_cdata5);
TEST_EQ(i9.size(), 5);
// Construction from a (ptr, size) pair.
span<int, 7> i10(i_data7, 7);
span<int> i11(i_data7, 7);
TEST_EQ(i11.size(), 7);
span<const int, 5> i12(i_cdata5, 5);
span<const int> i13(i_cdata5, 5);
TEST_EQ(i13.size(), 5);
// Construction from std::array.
std::array<int, 6> i_arr6 = { { 0, 1, 2, 3, 4, 5 } };
span<int, 6> i14(i_arr6);
span<const int, 6> i15(i_arr6);
span<int> i16(i_arr6);
span<const int> i17(i_arr6);
TEST_EQ(i17.size(), 6);
const std::array<int, 8> i_carr8 = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
span<const int, 8> i18(i_carr8);
span<const int> i19(i_carr8);
TEST_EQ(i18.size(), 8);
TEST_EQ(i19.size(), 8);
TEST_EQ(i19[7], 7);
// Check compatibility with flatbuffers::Array.
int fbs_int3_underlaying[3] = { 0 };
int fbs_int3_data[3] = { 1, 2, 3 };
auto &fbs_int3 = flatbuffers::CastToArray(fbs_int3_underlaying);
fbs_int3.CopyFromSpan(fbs_int3_data);
TEST_EQ(fbs_int3.Get(1), 2);
const int fbs_cint3_data[3] = { 2, 3, 4 };
fbs_int3.CopyFromSpan(fbs_cint3_data);
TEST_EQ(fbs_int3.Get(1), 3);
// Check with Array<Enum, N>
enum class Dummy : uint16_t { Zero = 0, One, Two };
Dummy fbs_dummy3_underlaying[3] = {};
Dummy fbs_dummy3_data[3] = { Dummy::One, Dummy::Two, Dummy::Two };
auto &fbs_dummy3 = flatbuffers::CastToArray(fbs_dummy3_underlaying);
fbs_dummy3.CopyFromSpan(fbs_dummy3_data);
TEST_EQ(fbs_dummy3.Get(1), Dummy::Two);
}
#else
void FlatbuffersSpanTest() {}
#endif
// VS10 does not support typed enums, exclude from tests
#if !defined(_MSC_VER) || _MSC_VER >= 1700
void FixedLengthArrayTest() {
// Generate an ArrayTable containing one ArrayStruct.
flatbuffers::FlatBufferBuilder fbb;
MyGame::Example::NestedStruct nStruct0(MyGame::Example::TestEnum::B);
TEST_NOTNULL(nStruct0.mutable_a());
nStruct0.mutable_a()->Mutate(0, 1);
nStruct0.mutable_a()->Mutate(1, 2);
TEST_NOTNULL(nStruct0.mutable_c());
nStruct0.mutable_c()->Mutate(0, MyGame::Example::TestEnum::C);
nStruct0.mutable_c()->Mutate(1, MyGame::Example::TestEnum::A);
TEST_NOTNULL(nStruct0.mutable_d());
nStruct0.mutable_d()->Mutate(0, flatbuffers::numeric_limits<int64_t>::max());
nStruct0.mutable_d()->Mutate(1, flatbuffers::numeric_limits<int64_t>::min());
MyGame::Example::NestedStruct nStruct1(MyGame::Example::TestEnum::C);
TEST_NOTNULL(nStruct1.mutable_a());
nStruct1.mutable_a()->Mutate(0, 3);
nStruct1.mutable_a()->Mutate(1, 4);
TEST_NOTNULL(nStruct1.mutable_c());
nStruct1.mutable_c()->Mutate(0, MyGame::Example::TestEnum::C);
nStruct1.mutable_c()->Mutate(1, MyGame::Example::TestEnum::A);
TEST_NOTNULL(nStruct1.mutable_d());
nStruct1.mutable_d()->Mutate(0, flatbuffers::numeric_limits<int64_t>::min());
nStruct1.mutable_d()->Mutate(1, flatbuffers::numeric_limits<int64_t>::max());
MyGame::Example::ArrayStruct aStruct(2, 12, 1);
TEST_NOTNULL(aStruct.b());
TEST_NOTNULL(aStruct.mutable_b());
TEST_NOTNULL(aStruct.mutable_d());
TEST_NOTNULL(aStruct.mutable_f());
for (int i = 0; i < aStruct.b()->size(); i++)
aStruct.mutable_b()->Mutate(i, i + 1);
aStruct.mutable_d()->Mutate(0, nStruct0);
aStruct.mutable_d()->Mutate(1, nStruct1);
auto aTable = MyGame::Example::CreateArrayTable(fbb, &aStruct);
MyGame::Example::FinishArrayTableBuffer(fbb, aTable);
// Verify correctness of the ArrayTable.
flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
TEST_ASSERT(MyGame::Example::VerifyArrayTableBuffer(verifier));
// Do test.
auto p = MyGame::Example::GetMutableArrayTable(fbb.GetBufferPointer());
auto mArStruct = p->mutable_a();
TEST_NOTNULL(mArStruct);
TEST_NOTNULL(mArStruct->b());
TEST_NOTNULL(mArStruct->d());
TEST_NOTNULL(mArStruct->f());
TEST_NOTNULL(mArStruct->mutable_b());
TEST_NOTNULL(mArStruct->mutable_d());
TEST_NOTNULL(mArStruct->mutable_f());
TEST_EQ(mArStruct->a(), 2);
TEST_EQ(mArStruct->b()->size(), 15);
mArStruct->mutable_b()->Mutate(14, -14);
TEST_EQ(mArStruct->b()->Get(14), -14);
TEST_EQ(mArStruct->c(), 12);
TEST_NOTNULL(mArStruct->d()->Get(0));
TEST_NOTNULL(mArStruct->d()->Get(0)->a());
TEST_EQ(mArStruct->d()->Get(0)->a()->Get(0), 1);
TEST_EQ(mArStruct->d()->Get(0)->a()->Get(1), 2);
TEST_NOTNULL(mArStruct->d()->Get(1));
TEST_NOTNULL(mArStruct->d()->Get(1)->a());
TEST_EQ(mArStruct->d()->Get(1)->a()->Get(0), 3);
TEST_EQ(mArStruct->d()->Get(1)->a()->Get(1), 4);
TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1));
TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a());
mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a()->Mutate(1, 5);
TEST_EQ(5, mArStruct->d()->Get(1)->a()->Get(1));
TEST_EQ(MyGame::Example::TestEnum::B, mArStruct->d()->Get(0)->b());
TEST_NOTNULL(mArStruct->d()->Get(0)->c());
TEST_EQ(MyGame::Example::TestEnum::C, mArStruct->d()->Get(0)->c()->Get(0));
TEST_EQ(MyGame::Example::TestEnum::A, mArStruct->d()->Get(0)->c()->Get(1));
TEST_EQ(flatbuffers::numeric_limits<int64_t>::max(),
mArStruct->d()->Get(0)->d()->Get(0));
TEST_EQ(flatbuffers::numeric_limits<int64_t>::min(),
mArStruct->d()->Get(0)->d()->Get(1));
TEST_EQ(MyGame::Example::TestEnum::C, mArStruct->d()->Get(1)->b());
TEST_NOTNULL(mArStruct->d()->Get(1)->c());
TEST_EQ(MyGame::Example::TestEnum::C, mArStruct->d()->Get(1)->c()->Get(0));
TEST_EQ(MyGame::Example::TestEnum::A, mArStruct->d()->Get(1)->c()->Get(1));
TEST_EQ(flatbuffers::numeric_limits<int64_t>::min(),
mArStruct->d()->Get(1)->d()->Get(0));
TEST_EQ(flatbuffers::numeric_limits<int64_t>::max(),
mArStruct->d()->Get(1)->d()->Get(1));
for (int i = 0; i < mArStruct->b()->size() - 1; i++)
TEST_EQ(mArStruct->b()->Get(i), i + 1);
// Check alignment
TEST_EQ(0, reinterpret_cast<uintptr_t>(mArStruct->d()) % 8);
TEST_EQ(0, reinterpret_cast<uintptr_t>(mArStruct->f()) % 8);
// Check if default constructor set all memory zero
const size_t arr_size = sizeof(MyGame::Example::ArrayStruct);
char non_zero_memory[arr_size];
// set memory chunk of size ArrayStruct to 1's
std::memset(static_cast<void *>(non_zero_memory), 1, arr_size);
// after placement-new it should be all 0's
# if defined(_MSC_VER) && defined(_DEBUG)
# undef new
# endif
MyGame::Example::ArrayStruct *ap =
new (non_zero_memory) MyGame::Example::ArrayStruct;
# if defined(_MSC_VER) && defined(_DEBUG)
# define new DEBUG_NEW
# endif
(void)ap;
for (size_t i = 0; i < arr_size; ++i) { TEST_EQ(non_zero_memory[i], 0); }
}
#else
void FixedLengthArrayTest() {}
#endif // !defined(_MSC_VER) || _MSC_VER >= 1700
#if !defined(FLATBUFFERS_SPAN_MINIMAL) && \
(!defined(_MSC_VER) || _MSC_VER >= 1700)
void FixedLengthArrayConstructorTest() {
const int32_t nested_a[2] = { 1, 2 };
MyGame::Example::TestEnum nested_c[2] = { MyGame::Example::TestEnum::A,
MyGame::Example::TestEnum::B };
const int64_t int64_2[2] = { -2, -1 };
std::array<MyGame::Example::NestedStruct, 2> init_d = {
{ MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::B,
nested_c, int64_2),
MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::A,
nested_c,
std::array<int64_t, 2>{ { 12, 13 } }) }
};
MyGame::Example::ArrayStruct arr_struct(
8.125,
std::array<int32_t, 0xF>{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } },
-17, init_d, 10, int64_2);
TEST_EQ(arr_struct.a(), 8.125);
TEST_EQ(arr_struct.b()->Get(2), 3);
TEST_EQ(arr_struct.c(), -17);
TEST_NOTNULL(arr_struct.d());
const auto &arr_d_0 = *arr_struct.d()->Get(0);
TEST_EQ(arr_d_0.a()->Get(0), 1);
TEST_EQ(arr_d_0.a()->Get(1), 2);
TEST_EQ(arr_d_0.b(), MyGame::Example::TestEnum::B);
TEST_EQ(arr_d_0.c()->Get(0), MyGame::Example::TestEnum::A);
TEST_EQ(arr_d_0.c()->Get(1), MyGame::Example::TestEnum::B);
TEST_EQ(arr_d_0.d()->Get(0), -2);
TEST_EQ(arr_d_0.d()->Get(1), -1);
const auto &arr_d_1 = *arr_struct.d()->Get(1);
TEST_EQ(arr_d_1.a()->Get(0), 1);
TEST_EQ(arr_d_1.a()->Get(1), 2);
TEST_EQ(arr_d_1.b(), MyGame::Example::TestEnum::A);
TEST_EQ(arr_d_1.c()->Get(0), MyGame::Example::TestEnum::A);
TEST_EQ(arr_d_1.c()->Get(1), MyGame::Example::TestEnum::B);
TEST_EQ(arr_d_1.d()->Get(0), 12);
TEST_EQ(arr_d_1.d()->Get(1), 13);
TEST_EQ(arr_struct.e(), 10);
TEST_EQ(arr_struct.f()->Get(0), -2);
TEST_EQ(arr_struct.f()->Get(1), -1);
}
#else
void FixedLengthArrayConstructorTest() {}
#endif
void NativeTypeTest() {
const int N = 3;
Geometry::ApplicationDataT src_data;
src_data.vectors.reserve(N);
src_data.vectors_alt.reserve(N);
for (int i = 0; i < N; ++i) {
src_data.vectors.push_back(
Native::Vector3D(10 * i + 0.1f, 10 * i + 0.2f, 10 * i + 0.3f));
src_data.vectors_alt.push_back(
Native::Vector3D(20 * i + 0.1f, 20 * i + 0.2f, 20 * i + 0.3f));
}
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &src_data));
auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
for (int i = 0; i < N; ++i) {
const Native::Vector3D &v = dstDataT->vectors[i];
TEST_EQ(v.x, 10 * i + 0.1f);
TEST_EQ(v.y, 10 * i + 0.2f);
TEST_EQ(v.z, 10 * i + 0.3f);
const Native::Vector3D &v2 = dstDataT->vectors_alt[i];
TEST_EQ(v2.x, 20 * i + 0.1f);
TEST_EQ(v2.y, 20 * i + 0.2f);
TEST_EQ(v2.z, 20 * i + 0.3f);
}
}
// VS10 does not support typed enums, exclude from tests
#if !defined(_MSC_VER) || _MSC_VER >= 1700
void FixedLengthArrayJsonTest(const std::string &tests_data_path, bool binary) {
// load FlatBuffer schema (.fbs) and JSON from disk
std::string schemafile;
std::string jsonfile;
TEST_EQ(flatbuffers::LoadFile(
(tests_data_path + "arrays_test." + (binary ? "bfbs" : "fbs"))
.c_str(),
binary, &schemafile),
true);
TEST_EQ(
flatbuffers::LoadFile((tests_data_path + "arrays_test.golden").c_str(),
false, &jsonfile),
true);
// parse schema first, so we can use it to parse the data after
flatbuffers::Parser parserOrg, parserGen;
if (binary) {
flatbuffers::Verifier verifier(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size());
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
TEST_EQ(parserOrg.Deserialize(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size()),
true);
TEST_EQ(parserGen.Deserialize(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size()),
true);
} else {
TEST_EQ(parserOrg.Parse(schemafile.c_str()), true);
TEST_EQ(parserGen.Parse(schemafile.c_str()), true);
}
TEST_EQ(parserOrg.Parse(jsonfile.c_str()), true);
// First, verify it, just in case:
flatbuffers::Verifier verifierOrg(parserOrg.builder_.GetBufferPointer(),
parserOrg.builder_.GetSize());
TEST_EQ(VerifyArrayTableBuffer(verifierOrg), true);
// Export to JSON
std::string jsonGen;
TEST_EQ(
GenerateText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen),
true);
// Import from JSON
TEST_EQ(parserGen.Parse(jsonGen.c_str()), true);
// Verify buffer from generated JSON
flatbuffers::Verifier verifierGen(parserGen.builder_.GetBufferPointer(),
parserGen.builder_.GetSize());
TEST_EQ(VerifyArrayTableBuffer(verifierGen), true);
// Compare generated buffer to original
TEST_EQ(parserOrg.builder_.GetSize(), parserGen.builder_.GetSize());
TEST_EQ(std::memcmp(parserOrg.builder_.GetBufferPointer(),
parserGen.builder_.GetBufferPointer(),
parserOrg.builder_.GetSize()),
0);
}
void FixedLengthArraySpanTest(const std::string &tests_data_path) {
// load FlatBuffer schema (.fbs) and JSON from disk
std::string schemafile;
std::string jsonfile;
TEST_EQ(flatbuffers::LoadFile((tests_data_path + "arrays_test.fbs").c_str(),
false, &schemafile),
true);
TEST_EQ(
flatbuffers::LoadFile((tests_data_path + "arrays_test.golden").c_str(),
false, &jsonfile),
true);
// parse schema first, so we can use it to parse the data after
flatbuffers::Parser parser;
TEST_EQ(parser.Parse(schemafile.c_str()), true);
TEST_EQ(parser.Parse(jsonfile.c_str()), true);
auto &fbb = parser.builder_;
auto verifier = flatbuffers::Verifier(fbb.GetBufferPointer(), fbb.GetSize());
TEST_EQ(true, VerifyArrayTableBuffer(verifier));
auto p = MyGame::Example::GetMutableArrayTable(fbb.GetBufferPointer());
TEST_NOTNULL(p);
auto table_struct = p->mutable_a();
TEST_NOTNULL(table_struct);
TEST_EQ(2, table_struct->d()->size());
TEST_NOTNULL(table_struct->d());
TEST_NOTNULL(table_struct->mutable_d());
// test array of structs
auto const_d = flatbuffers::make_span(*table_struct->d());
auto mutable_d = flatbuffers::make_span(*table_struct->mutable_d());
TEST_EQ(2, const_d.size());
TEST_EQ(2, mutable_d.size());
TEST_ASSERT(const_d[0] == mutable_d[0]);
TEST_ASSERT(const_d[1] == mutable_d[1]);
mutable_d[0] = const_d[0]; // mutate
// test scalars
auto &const_nested = const_d[0];
auto &mutable_nested = mutable_d[0];
static_assert(sizeof(MyGame::Example::TestEnum) == sizeof(uint8_t),
"TestEnum's underlaying type must by byte");
TEST_NOTNULL(const_nested.d());
TEST_NOTNULL(mutable_nested.d());
{
flatbuffers::span<const MyGame::Example::TestEnum, 2> const_d_c =
flatbuffers::make_span(*const_nested.c());
auto mutable_d_c = flatbuffers::make_span(*mutable_nested.mutable_c());
TEST_EQ(2, const_d_c.size());
TEST_EQ(2, mutable_d_c.size());
TEST_EQ(MyGame::Example::TestEnum::C, const_d_c[0]);
TEST_EQ(MyGame::Example::TestEnum::B, const_d_c[1]);
TEST_ASSERT(mutable_d_c.end() == std::copy(const_d_c.begin(),
const_d_c.end(),
mutable_d_c.begin()));
TEST_ASSERT(
std::equal(const_d_c.begin(), const_d_c.end(), mutable_d_c.begin()));
}
// test little endian array of int32
# if FLATBUFFERS_LITTLEENDIAN
{
flatbuffers::span<const int32_t, 2> const_d_a =
flatbuffers::make_span(*const_nested.a());
auto mutable_d_a = flatbuffers::make_span(*mutable_nested.mutable_a());
TEST_EQ(2, const_d_a.size());
TEST_EQ(2, mutable_d_a.size());
TEST_EQ(-1, const_d_a[0]);
TEST_EQ(2, const_d_a[1]);
TEST_ASSERT(mutable_d_a.end() == std::copy(const_d_a.begin(),
const_d_a.end(),
mutable_d_a.begin()));
TEST_ASSERT(
std::equal(const_d_a.begin(), const_d_a.end(), mutable_d_a.begin()));
}
# endif
}
#else
void FixedLengthArrayJsonTest(bool /*binary*/) {}
void FixedLengthArraySpanTest() {}
#endif
void TestEmbeddedBinarySchema(const std::string &tests_data_path) {
// load JSON from disk
std::string jsonfile;
TEST_EQ(flatbuffers::LoadFile(
(tests_data_path + "monsterdata_test.golden").c_str(), false,
&jsonfile),
true);
// parse schema first, so we can use it to parse the data after
flatbuffers::Parser parserOrg, parserGen;
flatbuffers::Verifier verifier(MyGame::Example::MonsterBinarySchema::data(),
MyGame::Example::MonsterBinarySchema::size());
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
TEST_EQ(parserOrg.Deserialize(MyGame::Example::MonsterBinarySchema::data(),