-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
alignment_test.cpp
76 lines (58 loc) · 2.62 KB
/
alignment_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
#include "alignment_test.h"
#include "alignment_test_generated.h"
#include "flatbuffers/flatbuffer_builder.h"
#include "flatbuffers/util.h"
#include "test_assert.h"
namespace flatbuffers {
namespace tests {
void AlignmentTest() {
FlatBufferBuilder builder;
BadAlignmentLarge large;
Offset<OuterLarge> outer_large = CreateOuterLarge(builder, &large);
BadAlignmentSmall *small;
Offset<Vector<const BadAlignmentSmall *>> small_offset =
builder.CreateUninitializedVectorOfStructs(9, &small);
(void)small; // We do not have to write data to trigger the test failure
Offset<BadAlignmentRoot> root =
CreateBadAlignmentRoot(builder, outer_large, small_offset);
builder.Finish(root);
Verifier verifier(builder.GetBufferPointer(), builder.GetSize());
TEST_ASSERT(verifier.VerifyBuffer<BadAlignmentRoot>(nullptr));
// ============= Test Small Structs Vector misalignment ========
builder.Clear();
// creating 5 structs with 2 bytes each
// 10 bytes in total for Vector data is needed
std::vector<JustSmallStruct> small_vector = {
{ 2, 1 }, { 3, 1 }, { 4, 1 }
};
// CreateVectorOfStructs is used in the generated CreateSmallStructsDirect()
// method, but we test it directly
Offset<Vector<const JustSmallStruct *>> small_structs_offset =
builder.CreateVectorOfStructs<JustSmallStruct>(small_vector);
Offset<SmallStructs> small_structs_root =
CreateSmallStructs(builder, small_structs_offset);
builder.Finish(small_structs_root);
// Save the binary that we later can annotate with `flatc --annotate` command
// NOTE: the conversion of the JSON data to --binary via `flatc --binary`
// command is not changed with that fix and was always producing the
// correct binary data.
// SaveFile("alignment_test_{before,after}_fix.bin",
// reinterpret_cast<char *>(builder.GetBufferPointer()),
// builder.GetSize(), true);
Verifier verifier_small_structs(builder.GetBufferPointer(),
builder.GetSize());
TEST_ASSERT(verifier_small_structs.VerifyBuffer<SmallStructs>(nullptr));
// Reading SmallStructs vector values back and compare with original
auto root_msg =
flatbuffers::GetRoot<SmallStructs>(builder.GetBufferPointer());
TEST_EQ(root_msg->small_structs()->size(), small_vector.size());
for (flatbuffers::uoffset_t i = 0; i < root_msg->small_structs()->size();
++i) {
TEST_EQ(small_vector[i].var_0(),
root_msg->small_structs()->Get(i)->var_0());
TEST_EQ(small_vector[i].var_1(),
root_msg->small_structs()->Get(i)->var_1());
}
}
} // namespace tests
} // namespace flatbuffers