-
Notifications
You must be signed in to change notification settings - Fork 180
/
monster_test.c
2905 lines (2626 loc) · 90.3 KB
/
monster_test.c
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
#include <stdio.h>
#include "monster_test_builder.h"
#include "monster_test_verifier.h"
#include "flatcc/support/hexdump.h"
#include "flatcc/support/elapsed.h"
#include "../../config/config.h"
/*
* Convenience macro to deal with long namespace names,
* and to make code reusable with other namespaces.
*
* Note: we could also use
*
* #define ns(x) MyGame_Example_ ## x
*
* but it wouldn't doesn't handled nested ns calls.
*
* For historic reason some of this test does not use the ns macro
* and some avoid nesting ns calls by placing parenthesis differently
* although this isn't required with this wrapper macro.
*/
#undef ns
#define ns(x) FLATBUFFERS_WRAP_NAMESPACE(MyGame_Example, x)
#undef nsf
#define nsf(x) FLATBUFFERS_WRAP_NAMESPACE(Fantasy, x)
/*
* Wrap the common namespace (flatbuffers_). Many operations in the
* common namespace such as `flatbuffers_string_create` are also mapped
* to member fields such as `MyGame_Example_Monster_name_create` and
* this macro provides a consistent interface to namespaces with
* `nsc(string_create)` similar to `ns(Monster_name_create)`.
*/
#undef nsc
#define nsc(x) FLATBUFFERS_WRAP_NAMESPACE(flatbuffers, x)
/* A helper to simplify creating buffers vectors from C-arrays. */
#define c_vec_len(V) (sizeof(V)/sizeof((V)[0]))
static const char zero_pad[100];
int verify_empty_monster(void *buffer)
{
/* Proper id given. */
ns(Monster_table_t) monster = ns(Monster_as_root_with_identifier)(buffer, ns(Monster_file_identifier));
/* Invalid id. */
ns(Monster_table_t) monster2 = ns(Monster_as_root_with_identifier(buffer, "1234"));
/* `with_id` can also mean ignore id when given a null argument. */
ns(Monster_table_t) monster3 = ns(Monster_as_root_with_identifier(buffer, 0));
/* Excessive text in identifier is ignored. */
ns(Monster_table_t) monster4 = ns(Monster_as_root_with_identifier(buffer, "MONSX"));
/* Default id should match proper id. */
ns(Monster_table_t) monster5 = ns(Monster_as_root(buffer));
if (!monster) {
printf("Monster not available\n");
return -1;
}
if (monster2) {
printf("Monster should not accept invalid identifier\n");
return -1;
}
if (monster3 != monster) {
printf("Monster should ignore identifier when given a null id\n");
return -1;
}
if (monster4 != monster) {
printf("Monster should accept a string as valid identifier");
return -1;
}
if (monster5 != monster) {
printf("Monster with default id should be accepted");
return -1;
}
if (ns(Monster_hp(monster)) != 100) {
printf("Health points are not as expected\n");
return -1;
}
if (ns(Monster_hp_is_present(monster))) {
printf("Health Points should default\n");
return -1;
}
if (ns(Monster_pos_is_present(monster))) {
printf("Position should be present\n");
return -1;
}
if (ns(Monster_pos(monster)) != 0) {
printf("Position shouldn't be available\n");
return -1;
}
return 0;
}
int test_enums(flatcc_builder_t *B)
{
if (ns(neg_enum_neg1) != -12) {
printf("neg_enum_neg1 should be -12, was %d\n", ns(neg_enum_neg1));
return -1;
}
if (ns(neg_enum_neg2) != -11) {
printf("neg_enum_neg1 should be -11, was %d\n", ns(neg_enum_neg2));
return -1;
}
if (ns(int_enum_int1) != 2) {
printf("int_enum_int1 should be 2\n");
return -1;
}
if (ns(int_enum_int2) != 42) {
printf("int_enum_int2 should be 42\n");
return -1;
}
if (ns(hex_enum_hexneg) != -2) {
printf("enum hexneg should be -2\n");
return -1;
}
if (ns(hex_enum_hex1) != 3) {
printf("hex_enum_hex1 should be 3\n");
return -1;
}
if (ns(hex_enum_hex2) != INT32_C(0x7eafbeaf)) {
printf("hex_enum_hex2 should be 0x7eafbeaf\n");
return -1;
}
return 0;
}
int test_type_aliases(flatcc_builder_t *B)
{
int ret = 0;
void *buffer = 0;
size_t size;
ns(TypeAliases_table_t) ta;
flatbuffers_uint8_vec_ref_t v8_ref;
flatbuffers_double_vec_ref_t vf64_ref;
flatcc_builder_reset(B);
v8_ref = flatbuffers_uint8_vec_create(B, 0, 0);
vf64_ref = flatbuffers_double_vec_create(B, 0, 0);
ns(TypeAliases_create_as_root(B,
INT8_MIN, UINT8_MAX, INT16_MIN, UINT16_MAX,
INT32_MIN, UINT32_MAX, INT64_MIN, UINT64_MAX, 2.3f, 2.3, v8_ref, vf64_ref));
buffer = flatcc_builder_finalize_aligned_buffer(B, &size);
if ((ret = ns(TypeAliases_verify_as_root(buffer, size)))) {
hexdump("TypeAliases buffer", buffer, size, stderr);
printf("could not verify TypeAliases table, got %s\n", flatcc_verify_error_string(ret));
goto done;
}
ta = ns(TypeAliases_as_root(buffer));
if (ns(TypeAliases_i8(ta)) != INT8_MIN) goto failed;
if (ns(TypeAliases_i16(ta)) != INT16_MIN) goto failed;
if (ns(TypeAliases_i32(ta)) != INT32_MIN) goto failed;
if (ns(TypeAliases_i64(ta)) != INT64_MIN) goto failed;
if (ns(TypeAliases_u8(ta)) != UINT8_MAX) goto failed;
if (ns(TypeAliases_u16(ta)) != UINT16_MAX) goto failed;
if (ns(TypeAliases_u32(ta)) != UINT32_MAX) goto failed;
if (ns(TypeAliases_u64(ta)) != UINT64_MAX) goto failed;
if (ns(TypeAliases_f32(ta)) != 2.3f) goto failed;
if (ns(TypeAliases_f64(ta)) != 2.3) goto failed;
if (sizeof(ns(TypeAliases_i8(ta))) != 1) goto failed;
if (sizeof(ns(TypeAliases_i16(ta))) != 2) goto failed;
if (sizeof(ns(TypeAliases_i32(ta))) != 4) goto failed;
if (sizeof(ns(TypeAliases_i64(ta))) != 8) goto failed;
if (sizeof(ns(TypeAliases_u8(ta))) != 1) goto failed;
if (sizeof(ns(TypeAliases_u16(ta))) != 2) goto failed;
if (sizeof(ns(TypeAliases_u32(ta))) != 4) goto failed;
if (sizeof(ns(TypeAliases_u64(ta))) != 8) goto failed;
if (sizeof(ns(TypeAliases_f32(ta))) != 4) goto failed;
if (sizeof(ns(TypeAliases_f64(ta))) != 8) goto failed;
done:
flatcc_builder_aligned_free(buffer);
return ret;
failed:
ret = -1;
printf("Scalar type alias has unexpected value or size\n");
goto done;
}
int test_empty_monster(flatcc_builder_t *B)
{
int ret;
ns(Monster_ref_t) root;
void *buffer;
size_t size;
flatcc_builder_reset(B);
flatbuffers_buffer_start(B, ns(Monster_file_identifier));
ns(Monster_start(B));
/* Cannot make monster empty as name is required. */
ns(Monster_name_create_str(B, "MyMonster"));
root = ns(Monster_end(B));
flatbuffers_buffer_end(B, root);
buffer = flatcc_builder_finalize_aligned_buffer(B, &size);
hexdump("empty monster table", buffer, size, stderr);
if ((ret = verify_empty_monster(buffer))) {
goto done;
}
if ((ret = ns(Monster_verify_as_root_with_identifier(buffer, size, ns(Monster_file_identifier))))) {
printf("could not verify empty monster, got %s\n", flatcc_verify_error_string(ret));
return -1;
}
/*
* Note: this will assert if the verifier is set to assert during
* debugging. Also not that a buffer size - 1 is not necessarily
* invalid, but because we pack vtables tight at the end, we expect
* failure in this case.
*/
if (flatcc_verify_ok == ns(Monster_verify_as_root(
buffer, size - 1))) {
printf("Monster verify failed to detect short buffer\n");
return -1;
}
done:
flatcc_builder_aligned_free(buffer);
return ret;
}
int test_typed_empty_monster(flatcc_builder_t *B)
{
int ret = -1;
ns(Monster_ref_t) root;
void *buffer;
size_t size;
flatbuffers_fid_t fid = { 0 };
flatcc_builder_reset(B);
flatbuffers_buffer_start(B, ns(Monster_type_identifier));
ns(Monster_start(B));
/* Cannot make monster empty as name is required. */
ns(Monster_name_create_str(B, "MyMonster"));
root = ns(Monster_end(B));
flatbuffers_buffer_end(B, root);
buffer = flatcc_builder_finalize_aligned_buffer(B, &size);
hexdump("empty typed monster table", buffer, size, stderr);
if (flatbuffers_get_type_hash(buffer) != flatbuffers_type_hash_from_name("MyGame.Example.Monster")) {
printf("Monster does not have the expected type, got %lx\n", (unsigned long)flatbuffers_get_type_hash(buffer));
goto done;
}
if (!flatbuffers_has_type_hash(buffer, ns(Monster_type_hash))) {
printf("Monster does not have the expected type\n");
goto done;
}
if (!flatbuffers_has_type_hash(buffer, 0x330ef481)) {
printf("Monster does not have the expected type\n");
goto done;
}
if (!verify_empty_monster(buffer)) {
printf("typed empty monster should not verify with default identifier\n");
goto done;
}
if ((ret = ns(Monster_verify_as_root_with_identifier(buffer, size, ns(Monster_type_identifier))))) {
printf("could not verify typed empty monster, got %s\n", flatcc_verify_error_string(ret));
goto done;
}
if ((ret = ns(Monster_verify_as_typed_root(buffer, size)))) {
printf("could not verify typed empty monster, got %s\n", flatcc_verify_error_string(ret));
goto done;
}
if ((ret = ns(Monster_verify_as_root_with_type_hash(buffer, size, ns(Monster_type_hash))))) {
printf("could not verify empty monster with type hash, got %s\n", flatcc_verify_error_string(ret));
goto done;
}
if ((ret = ns(Monster_verify_as_root_with_type_hash(buffer, size, flatbuffers_type_hash_from_name("MyGame.Example.Monster"))))) {
printf("could not verify empty monster with explicit type hash, got %s\n", flatcc_verify_error_string(ret));
goto done;
}
flatbuffers_identifier_from_type_hash(0x330ef481, fid);
if ((ret = ns(Monster_verify_as_root_with_identifier(buffer, size, fid)))) {
printf("could not verify typed empty monster, got %s\n", flatcc_verify_error_string(ret));
goto done;
}
if (!ns(Monster_verify_as_root(buffer, size))) {
printf("should not have verified with the original identifier since we use types\n");
goto done;
}
ret = 0;
done:
flatcc_builder_aligned_free(buffer);
return ret;
}
int verify_monster(void *buffer)
{
ns(Monster_table_t) monster, mon, mon2;
ns(Monster_vec_t) monsters;
ns(Any_union_type_t) test_type;
ns(Any_union_t) test_union;
/* This is an encoded struct pointer. */
ns(Vec3_struct_t) vec;
const char *name;
/* This is a more precise type as there is a length field prefix. */
nsc(string_t) name2;
/* This is a native struct type. */
ns(Vec3_t) v;
ns(Test_vec_t) testvec;
ns(Test_t) testvec_data[] = {
{0x10, 0x20}, {0x30, 0x40}, {0x50, 0x60}, {0x70, (int8_t)0x80}, {0x191, (int8_t)0x91}
};
ns(Test_struct_t) test;
nsc(string_vec_t) strings;
nsc(string_t) s;
nsc(bool_vec_t) bools;
ns(Stat_table_t) stat;
int booldata[] = { 0, 1, 1, 0 };
const uint8_t *inv;
size_t i;
if (!nsc(has_identifier(buffer, 0))) {
printf("wrong monster identifier (when ignoring)\n");
return -1;
}
if (!nsc(has_identifier(buffer, "MONS"))) {
printf("wrong monster identifier (when explicit)\n");
return -1;
}
if (!nsc(has_identifier(buffer, "MONSTER"))) {
printf("extra characters in identifier should be ignored\n");
return -1;
}
if (nsc(has_identifier(buffer, "MON1"))) {
printf("accepted wrong monster identifier (when explicit)\n");
return -1;
}
if (!nsc(has_identifier(buffer, ns(Monster_file_identifier)))) {
printf("wrong monster identifier (via defined identifier)\n");
return -1;
}
if (!(monster = ns(Monster_as_root(buffer)))) {
printf("Monster not available\n");
return -1;
}
if (ns(Monster_hp(monster)) != 80) {
printf("Health points are not as expected\n");
return -1;
}
if (!(vec = ns(Monster_pos(monster)))) {
printf("Position is absent\n");
return -1;
}
if ((size_t)vec & 15) {
printf("Force align of Vec3 struct not correct\n");
}
/* -3.2f is actually -3.20000005 and not -3.2 due to representation loss. */
if (ns(Vec3_z(vec)) != -3.2f) {
printf("Position failing on z coordinate\n");
return -1;
}
if (nsc(is_native_pe())) {
if (vec->x != 1.0f || vec->y != 2.0f || vec->z != -3.2f) {
printf("Position is incorrect\n");
return -1;
}
}
/*
* NOTE: copy_from_pe and friends are provided in the builder
* interface, not the read only interface, but for advanced uses
* these may also be used for read operations.
* Also note that if we want the target struct fully null padded
* the struct must be zeroed first. The _clear operation is one way
* to achieve this - but it is not required for normal read access.
* See common_builder for more details. These operations can
* actually be very useful in their own right, disregarding any
* other flatbuffer logic when dealing with struct endian
* conversions in other protocols.
*/
ns(Vec3_clear(&v)); /* Not strictly needed here. */
ns(Vec3_copy_from_pe(&v, vec));
if (v.x != 1.0f || v.y != 2.0f || v.z != -3.2f) {
printf("Position is incorrect after copy\n");
return -1;
}
if (vec->test1 != 0 || vec->test1 != 0 ||
memcmp(&vec->test3, zero_pad, sizeof(vec->test3)) != 0) {
printf("Zero default not correct for struct\n");
return -1;
}
name = ns(Monster_name(monster));
if (!name || strcmp(name, "MyMonster")) {
printf("Name is not correct\n");
return -1;
}
name2 = ns(Monster_name(monster));
if (nsc(string_len(name)) != 9 || nsc(string_len(name2)) != 9) {
printf("Name length is not correct\n");
return -1;
}
if (ns(Monster_color(monster)) != ns(Color_Green)) {
printf("Monster isn't a green monster\n");
return -1;
}
if (strcmp(ns(Color_name)(ns(Color_Green)), "Green")) {
printf("Enum name map does not have a green solution\n");
return -1;
}
/*
* This is bit tricky because Color is a bit flag, so we can have
* combinations that are expected, but that we do not know. The
* known value logic does not accomodate for that.
*/
if (!ns(Color_is_known_value(ns(Color_Green)))) {
printf("Color enum does not recognize the value of the Green flag\n");
return -1;
}
if (!ns(Color_is_known_value(1))) {
printf("Color enum does not recognize the value of the Red flag\n");
return -1;
}
if (ns(Color_is_known_value(4))) {
printf("Color enum recognizes a value it shouldn't\n");
return -1;
}
if (!ns(Color_is_known_value(8))) {
printf("Color enum does not recognize the value of the Blue flag\n");
return -1;
}
if (ns(Color_is_known_value(9))) {
printf("Color enum recognizes a value it shouldn't\n");
return -1;
}
if (!ns(Any_is_known_type(ns(Any_Monster)))) {
printf("Any type does not accept Monster\n");
return -1;
}
if (ns(Any_is_known_type(42))) {
printf("Any type recognizes unexpected type\n");
return -1;
}
inv = ns(Monster_inventory(monster));
if ((nsc(uint8_vec_len(inv))) != 10) {
printf("Inventory length unexpected\n");
return -1;
}
for (i = 0; i < nsc(uint8_vec_len(inv)); ++i) {
if (nsc(uint8_vec_at(inv, i)) != i) {
printf("inventory item #%d is wrong\n", (int)i);
return -1;
}
}
if (ns(Monster_mana(monster) != 150)) {
printf("Mana not default\n");
return -1;
}
if (ns(Monster_mana_is_present(monster))) {
printf("Mana should default\n");
return -1;
}
if (!ns(Monster_hp_is_present(monster))) {
printf("Health points should be present\n");
return -1;
}
if (!ns(Monster_pos_is_present(monster))) {
printf("Position should be present\n");
return -1;
}
testvec = ns(Monster_test4(monster));
if (ns(Test_vec_len(testvec)) != 5) {
printf("Test4 vector is not the right length.\n");
return -1;
}
/*
* This particular test requires that the in-memory
* array layout matches the array layout in the buffer.
*/
if (flatbuffers_is_native_pe()) {
for (i = 0; i < 5; ++i) {
test = ns(Test_vec_at(testvec, i));
if (testvec_data[i].a != ns(Test_a(test))) {
printf("Test4 vec failed at index %d, member a\n", (int)i);
return -1;
}
if (testvec_data[i].b != ns(Test_b(test))) {
printf("Test4 vec failed at index %d, member a\n", (int)i);
return -1;
}
}
} else {
printf("SKIPPING DIRECT VECTOR ACCESS WITH NON-NATIVE ENDIAN PROTOCOL\n");
}
monsters = ns(Monster_testarrayoftables(monster));
if (ns(Monster_vec_len(monsters)) != 8) {
printf("unexpected monster vector length\n");
return -1;
}
mon = ns(Monster_vec_at(monsters, 5));
assert(mon);
name = ns(Monster_name(mon));
if (strcmp(name, "TwoFace")) {
printf("monster 5 isn't TwoFace");
return -1;
}
mon2 = ns(Monster_vec_at(monsters, 1));
if (mon2 != mon) {
printf("DAG test failed, monster[5] != monster[1] as pointer\n");
return -1;
}
name = ns(Monster_name(mon2));
if (strcmp(name, "TwoFace")) {
printf("monster 1 isn't Joker, it is: %s\n", name);
return -1;
}
mon = ns(Monster_vec_at(monsters, 2));
name = ns(Monster_name(mon));
if (strcmp(name, "Joker")) {
printf("monster 2 isn't Joker, it is: %s\n", name);
return -1;
}
mon = ns(Monster_vec_at(monsters, 0));
name = ns(Monster_name(mon));
if (strcmp(name, "Gulliver")) {
printf("monster 0 isn't Gulliver, it is: %s\n", name);
return -1;
}
mon = ns(Monster_vec_at(monsters, 3));
name = ns(Monster_name(mon));
if (strcmp(name, "TwoFace")) {
printf("monster 3 isn't TwoFace, it is: %s\n", name);
return -1;
}
mon = ns(Monster_vec_at(monsters, 4));
name = ns(Monster_name(mon));
if (strcmp(name, "Joker")) {
printf("monster 4 isn't Joker, it is: %s\n", name);
return -1;
}
mon = ns(Monster_vec_at(monsters, 6));
name = ns(Monster_name(mon));
if (strcmp(name, "Gulliver")) {
printf("monster 6 isn't Gulliver, it is: %s\n", name);
return -1;
}
mon = ns(Monster_vec_at(monsters, 7));
name = ns(Monster_name(mon));
if (strcmp(name, "Joker")) {
printf("monster 7 isn't Gulliver, it is: %s\n", name);
return -1;
}
strings = ns(Monster_testarrayofstring(monster));
if (nsc(string_vec_len(strings) != 3)) {
printf("Monster array of strings has wrong length\n");
return -1;
}
if (strcmp(nsc(string_vec_at(strings, 0)), "Hello")) {
printf("string elem 0 is wrong\n");
return -1;
}
s = nsc(string_vec_at(strings, 1));
if (nsc(string_len(s)) != 2) {
printf("string 1 has wrong length");
return -1;
}
if (memcmp(s, ",\0", 2)) {
printf("string elem 1 has wrong content\n");
return -1;
}
if (strcmp(nsc(string_vec_at(strings, 2)), "world!")) {
printf("string elem 2 is wrong\n");
return -1;
}
if (!ns(Monster_testarrayofbools_is_present(monster))) {
printf("array of bools is missing\n");
return -1;
}
bools = ns(Monster_testarrayofbools(monster));
if (nsc(bool_vec_len(bools) != 4)) {
printf("bools have wrong vector length\n");
return -1;
}
if (sizeof(bools[0]) != 1) {
printf("bools have wrong element size\n");
return -1;
}
for (i = 0; i < 4; ++i) {
if (nsc(bool_vec_at(bools, i) != booldata[i])) {
printf("bools vector elem %d is wrong\n", (int)i);
return -1;
}
}
test_type = ns(Monster_test_type(monster));
if (test_type != ns(Any_Monster)) {
printf("the monster test type is not Any_Monster\n");
return -1;
}
mon = ns(Monster_test(monster));
if (strcmp(ns(Monster_name(mon)), "TwoFace")) {
printf("the test monster is not TwoFace\n");
return -1;
}
mon = ns(Monster_enemy(monster));
if (strcmp(ns(Monster_name(mon)), "the enemy")) {
printf("the monster is not the enemy\n");
return -1;
}
if (ns(Monster_test_type(mon)) != ns(Any_NONE)) {
printf("the enemy test type is not Any_NONE\n");
return -1;
}
test_union = ns(Monster_test_union(monster));
if (test_union.type != test_type) {
printf("the monster test union type is not Any_Monster\n");
return -1;
}
if (test_union.value != ns(Monster_test(monster))) {
printf("the union monster has gone awol\n");
return -1;
}
monsters = ns(Monster_testarrayoftables(mon));
i = ns(Monster_vec_len(monsters));
mon = ns(Monster_vec_at(monsters, i - 1));
if (ns(Monster_test_type)(mon) != ns(Any_Monster)) {
printf("The monster variant added with value, type methods is not working\n");
return -1;
}
mon = ns(Monster_test(mon));
if (strcmp(ns(Monster_name(mon)), "TwoFace")) {
printf("The monster variant added with value method is incorrect\n");
return -1;
}
if (ns(Monster_testbool(monster))) {
printf("testbool should not\n");
return -1;
}
if (!ns(Monster_testempty_is_present(monster))) {
printf("The empty table isn't present\n");
return -1;
}
stat = ns(Monster_testempty(monster));
if (ns(Stat_id_is_present(stat))
|| ns(Stat_val_is_present(stat))
|| ns(Stat_count_is_present(stat))) {
printf("empty table isn't empty\n");
return -1;
}
return 0;
}
int gen_monster(flatcc_builder_t *B, int with_size)
{
uint8_t inv[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ns(Vec3_t) *vec;
ns(Test_t) *test, x;
ns(Monster_ref_t) mon, mon2, monsters[2];
ns(Monster_ref_t) *aoft;
nsc(string_ref_t) name;
nsc(string_ref_t) strings[3];
nsc(bool_t)bools[] = { 0, 1, 1, 0 };
flatcc_builder_reset(B);
/*
* Some FlatBuffer language interfaces require a string and other
* non-embeddable objects to be created before the table storing it
* is being created. This is not necessary (but possible) here
* because the flatcc_builder maintains an internal stack.
*/
if (with_size) {
ns(Monster_start_as_root_with_size(B));
} else {
ns(Monster_start_as_root(B));
}
ns(Monster_hp_add(B, 80));
vec = ns(Monster_pos_start(B));
vec->x = 1, vec->y = 2, vec->z = -3.2f;
/* _end call converts to protocol endian format. */
ns(Monster_pos_end(B));
/*
* NOTE: Monster_name_add requires a reference to an
* already created string - adding a string directly
* will compile with a warning but fail badly. Instead
* create the string first, or do it in-place with
* the helper function `Monster_name_create_str`, or
* with one of several other options.
*
* Wrong: ns(Monster_name_add(B, "MyMonster"));
*/
ns(Monster_name_create_str(B, "MyMonster"));
ns(Monster_color_add)(B, ns(Color_Green));
ns(Monster_inventory_create(B, inv, c_vec_len(inv)));
/* The vector is built in native endian format. */
ns(Monster_test4_start(B));
test = ns(Monster_test4_extend(B, 1));
test->a = 0x10;
test->b = 0x20;
test = ns(Monster_test4_extend(B, 2));
test->a = 0x30;
test->b = 0x40;
test[1].a = 0x50;
test[1].b = 0x60;
ns(Monster_test4_push_create(B, 0x70, (int8_t)0x80));
x.a = 0x190; /* This is a short. */
x.b = (int8_t)0x91; /* This is a byte. */
ns(Monster_test4_push(B, &x));
ns(Monster_test4_push(B, &x));
/* We can use either field mapped push or push on the type. */
ns(Test_vec_push(B, &x));
/*
* `_reserved_len` is similar to the `_vec_len` function in the
* reader interface but `_vec_len` would not work here.
*/
assert(ns(Monster_test4_reserved_len(B)) == 7);
ns(Monster_test4_truncate(B, 2));
assert(ns(Monster_test4_reserved_len(B)) == 5);
/* It is not valid to dereference old pointers unless we call edit first. */
test = ns(Monster_test4_edit(B));
test[4].a += 1; /* 0x191 */
/* Each vector element is converted to protocol endian format at end. */
ns(Monster_test4_end(B));
/* Test creating object before containing vector. */
ns(Monster_start(B));
name = nsc(string_create(B, "TwoFace", 7));
ns(Monster_name_add(B, name));
mon = ns(Monster_end(B));
/*
* Here we create several monsters with only a name - this also
* tests reuse of vtables.
*/
ns(Monster_testarrayoftables_start(B));
aoft = ns(Monster_testarrayoftables_extend(B, 2));
/*
* It is usually not ideal to update reference vectors directly and
* there must not be any unassigned elements (null) when the array
* ends. Normally a push_start ... push_end, or a push_create
* operation is preferable.
*/
aoft[0] = mon;
/*
* But we can do things not otherwise possible - like constructing a
* DAG. Note that reference values (unlike pointers) are stable as
* long as the buffer is open for write, also past this vector.
*/
aoft[1] = mon;
ns(Monster_testarrayoftables_push_start(B));
ns(Monster_name_create_strn(B, "Joker", 30));
mon2 = *ns(Monster_testarrayoftables_push_end(B));
aoft = ns(Monster_testarrayoftables_extend(B, 3));
aoft[0] = mon;
aoft[1] = mon2;
ns(Monster_testarrayoftables_truncate(B, 1));
assert(ns(Monster_testarrayoftables_reserved_len(B)) == 5);
ns(Monster_testarrayoftables_push_start(B));
ns(Monster_name_create_strn(B, "Gulliver at the Big Endians", 8));
/* We cannot call reserved_len while a monster is still open, */
monsters[0] = *ns(Monster_testarrayoftables_push_end(B));
/* but here the vector is on top of the stack again. */
assert(ns(Monster_testarrayoftables_reserved_len(B)) == 6);
/* Swap monsters[0] and monsters[5] */
aoft = ns(Monster_testarrayoftables_edit(B));
mon2 = aoft[5];
monsters[1] = aoft[2];
aoft[5] = mon;
aoft[0] = mon2;
ns(Monster_testarrayoftables_append(B, monsters, 2));
/*
* The end call converts the reference array into an endian encoded
* offset vector.
*/
ns(Monster_testarrayoftables_end(B));
strings[0] = nsc(string_create_str(B, "Hello"));
/* Test embedded null character.
* Note _strn is at most n, or up to 0 termination:
* wrong: strings[1] = nsc(string_create_strn(B, ",\0", 2));
*/
strings[1] = nsc(string_create(B, ",\0", 2));
strings[2] = nsc(string_create_str(B, "world!"));
ns(Monster_testarrayofstring_create(B, strings, 3));
assert(c_vec_len(bools) == 4);
ns(Monster_testarrayofbools_start(B));
ns(Monster_testarrayofbools_append(B, bools, 1));
ns(Monster_testarrayofbools_append(B, bools + 1, 3));
ns(Monster_testarrayofbools_end(B));
/*
* This is using a constructor argument list where a union
* is a single argument, unlike the C++ interface.
* A union is given a type and a table reference.
*
* We are not verifying the result as this is only to stress
* the type system of the builder - except: the last array
* element is tested to ensure add_value is getting through.
*/
ns(Monster_test_add)(B, ns(Any_as_Monster(mon)));
ns(Monster_enemy_start(B));
ns(Monster_name_create_str(B, "the enemy"));
/* Create array of monsters to test various union constructors. */
ns(Monster_testarrayoftables_start(B));
ns(Monster_vec_push_start(B));
ns(Monster_test_add)(B, ns(Any_as_Monster(mon)));
/* Name is required. */
ns(Monster_name_create_str(B, "any name"));
ns(Monster_testarrayoftables_push_end(B));
ns(Monster_testarrayoftables_push_start(B));
ns(Monster_test_Monster_add(B, mon));
ns(Monster_name_create_str(B, "any name"));
ns(Monster_vec_push_end(B));
/*
* `push_start`: We can use the field specific method, or the type specific method
* that the field maps to.
*/
ns(Monster_testarrayoftables_push_start(B));
/*
* This is mostly for internal use in create methods so the type
* can be added last and pack better in the table.
* `add_value` still takes union_ref because it is a NOP if
* the union type is NONE.
*/
ns(Monster_test_add_value(B, ns(Any_as_Monster(mon))));
ns(Monster_name_create_str(B, "any name"));
ns(Monster_test_add_type(B, ns(Any_Monster)));
ns(Monster_testarrayoftables_push_end(B));
ns(Monster_testarrayoftables_end(B));
ns(Monster_enemy_end(B));
ns(Monster_testbool_add(B, 0));
ns(Monster_testempty_start(B));
ns(Monster_testempty_end(B));
ns(Monster_end_as_root(B));
return 0;
}
int test_monster(flatcc_builder_t *B)
{
void *buffer;
size_t size;
int ret;
gen_monster(B, 0);
buffer = flatcc_builder_finalize_aligned_buffer(B, &size);
hexdump("monster table", buffer, size, stderr);
if ((ret = ns(Monster_verify_as_root(buffer, size)))) {
printf("Monster buffer failed to verify, got: %s\n", flatcc_verify_error_string(ret));
return -1;
}
ret = verify_monster(buffer);
flatcc_builder_aligned_free(buffer);
return ret;
}
int test_monster_with_size(flatcc_builder_t *B)
{
void *buffer, *frame;
size_t size, size2, esize;
int ret;
gen_monster(B, 1);
frame = flatcc_builder_finalize_aligned_buffer(B, &size);
hexdump("monster table with size", frame, size, stderr);
if (((size_t)frame & 15)) {
printf("Platform did not provide 16 byte aligned allocation and needs special attention.");
printf("buffer address: %x\n", (flatbuffers_uoffset_t)(size_t)frame);
return -1;
}
buffer = flatbuffers_read_size_prefix(frame, &size2);
esize = size - sizeof(flatbuffers_uoffset_t);
if (size2 != esize) {
printf("Size prefix has unexpected size, got %i, expected %i\n", (int)size2, (int)esize);
return -1;
}
if ((ret = ns(Monster_verify_as_root(buffer, size2)))) {
printf("Monster buffer with size prefix failed to verify, got: %s\n", flatcc_verify_error_string(ret));
return -1;
}
ret = verify_monster(buffer);
flatcc_builder_aligned_free(frame);
return ret;
}
int test_cloned_monster(flatcc_builder_t *B)
{
void *buffer;
void *cloned_buffer;
size_t size;
int ret;
flatcc_refmap_t refmap, *refmap_old;
flatcc_refmap_init(&refmap);
gen_monster(B, 0);
buffer = flatcc_builder_finalize_aligned_buffer(B, &size);
hexdump("monster table", buffer, size, stderr);
if ((ret = ns(Monster_verify_as_root(buffer, size)))) {
printf("Monster buffer failed to verify, got: %s\n", flatcc_verify_error_string(ret));
return -1;
}
if (verify_monster(buffer)) {
return -1;
}
flatcc_builder_reset(B);
/*
* Clone works without setting a refmap - but then shared references
* get expanded - and then the verify monster check fails on a DAG
* test.
*/
refmap_old = flatcc_builder_set_refmap(B, &refmap);
if (!ns(Monster_clone_as_root(B, ns(Monster_as_root(buffer))))) {
printf("Cloned Monster didn't actually clone.");
return -1;
};
/*
* Restoring old refmap (or zeroing) is optional if we cleared the
* buffer in this scope, but we don't so we must detach and clean up
* the refmap manually. refmap_old is likely just null, but this
* way we do not interfere with caller.
*/
flatcc_builder_set_refmap(B, refmap_old);
cloned_buffer = flatcc_builder_finalize_aligned_buffer(B, &size);
hexdump("cloned monster table", cloned_buffer, size, stderr);
if ((ret = ns(Monster_verify_as_root(cloned_buffer, size)))) {
printf("Cloned Monster buffer failed to verify, got: %s\n", flatcc_verify_error_string(ret));
return -1;
}
if (verify_monster(cloned_buffer)) {
printf("Cloned Monster did not have the expected content.");
return -1;
}
flatcc_refmap_clear(&refmap);
flatcc_builder_aligned_free(buffer);
flatcc_builder_aligned_free(cloned_buffer);
return ret;
}
int test_string(flatcc_builder_t *B)
{
ns(Monster_table_t) mon;
void *buffer;
char *s;
flatcc_builder_reset(B);
ns(Monster_start_as_root(B));
ns(Monster_name_start(B));
s = ns(Monster_name_extend(B, 3));
s[0] = '1';
s[1] = '2';
s[2] = '3';
ns(Monster_name_append_str(B, "4"));
assert(ns(Monster_name_reserved_len(B)) == 4);
ns(Monster_name_append_strn(B, "5678", 30));
assert(ns(Monster_name_reserved_len(B)) == 8);
ns(Monster_name_append(B, "90", 2));
assert(ns(Monster_name_reserved_len(B)) == 10);
ns(Monster_name_truncate(B, 3));
assert(ns(Monster_name_reserved_len(B)) == 7);
s = ns(Monster_name_edit(B));
s[4] = '.';
ns(Monster_name_end(B));
ns(Monster_end_as_root(B));
/* Only with small buffers and the default emitter. */
buffer = flatcc_builder_get_direct_buffer(B, 0);