blob: 87d89fad58e39499a4fb7abb61468b3f3c69b3db [file] [log] [blame]
James Kuszmaulf5eb4682023-09-22 17:16:59 -07001#include "aos/flatbuffers/base.h"
2
Stephan Pleines6191f1d2024-05-30 20:44:45 -07003#include <stddef.h>
4
5#include <algorithm>
6
James Kuszmaulf5eb4682023-09-22 17:16:59 -07007#include "gtest/gtest.h"
8
Austin Schuh3c9f92c2024-04-30 17:56:42 -07009#include "aos/flatbuffers/aligned_allocator.h"
10
James Kuszmaulf5eb4682023-09-22 17:16:59 -070011namespace aos::fbs::testing {
12// Tests that PaddedSize() behaves as expected.
13TEST(BaseTest, PaddedSize) {
14 EXPECT_EQ(0, PaddedSize(0, 4));
15 EXPECT_EQ(4, PaddedSize(4, 4));
16 EXPECT_EQ(8, PaddedSize(5, 4));
17 EXPECT_EQ(8, PaddedSize(6, 4));
18 EXPECT_EQ(8, PaddedSize(7, 4));
19}
20
Austin Schuh3c9f92c2024-04-30 17:56:42 -070021inline constexpr size_t kDefaultSize = AlignedVectorAllocator::kAlignment * 2;
James Kuszmaulf5eb4682023-09-22 17:16:59 -070022template <typename T>
23class AllocatorTest : public ::testing::Test {
24 protected:
25 AllocatorTest() : allocator_(std::make_unique<T>()) {}
26 std::vector<uint8_t> buffer_;
27 // unique_ptr so that we can destroy the allocator at will.
28 std::unique_ptr<T> allocator_;
29};
30
31template <>
32AllocatorTest<SpanAllocator>::AllocatorTest()
33 : buffer_(kDefaultSize),
34 allocator_(std::make_unique<SpanAllocator>(
35 std::span<uint8_t>{buffer_.data(), buffer_.size()})) {}
36
Austin Schuh3c9f92c2024-04-30 17:56:42 -070037using AllocatorTypes =
38 ::testing::Types<SpanAllocator, VectorAllocator, AlignedVectorAllocator>;
James Kuszmaulf5eb4682023-09-22 17:16:59 -070039TYPED_TEST_SUITE(AllocatorTest, AllocatorTypes);
40
41// Tests that we can create and not use a VectorAllocator.
42TYPED_TEST(AllocatorTest, UnusedAllocator) {}
43
44// Tests that a simple allocate works.
45TYPED_TEST(AllocatorTest, BasicAllocate) {
46 std::span<uint8_t> span =
47 this->allocator_->Allocate(kDefaultSize, 4, SetZero::kYes).value();
48 ASSERT_EQ(kDefaultSize, span.size());
49 // We set SetZero::kYes; it should be zero-initialized.
50 EXPECT_EQ(kDefaultSize, std::count(span.begin(), span.end(), 0));
51 this->allocator_->Deallocate(span);
52}
53
54// Tests that we can insert bytes into an arbitrary spot in the buffer.
55TYPED_TEST(AllocatorTest, InsertBytes) {
56 const size_t half_size = kDefaultSize / 2;
57 std::span<uint8_t> span =
58 this->allocator_->Allocate(half_size, 4, SetZero::kYes).value();
59 ASSERT_EQ(half_size, span.size());
60 // Set the span with some sentinel values so that we can detect that the
61 // insertion occurred correctly.
62 for (size_t ii = 0; ii < span.size(); ++ii) {
63 span[ii] = ii + 1;
64 }
65
66 // Insert new bytes such that one old byte will still be at the start.
67 span = this->allocator_
68 ->InsertBytes(span.data() + 1u, half_size, 0, SetZero::kYes)
69 .value();
70 ASSERT_EQ(kDefaultSize, span.size());
71 size_t index = 0;
72 EXPECT_EQ(1u, span[index]);
73 index++;
74 for (; index < half_size + 1u; ++index) {
75 EXPECT_EQ(0u, span[index]);
76 }
77 for (; index < span.size(); ++index) {
78 EXPECT_EQ(index - half_size + 1, span[index]);
79 }
80 this->allocator_->Deallocate(span);
81}
82
83// Tests that we can remove bytes from an arbitrary spot in the buffer.
84TYPED_TEST(AllocatorTest, RemoveBytes) {
Austin Schuh3c9f92c2024-04-30 17:56:42 -070085 // Deletion doesn't require resizing, so we don't need to worry about it being
86 // larger than the alignment to test everything. The test requires the size
87 // to be < 255 to store the sentinal values.
88 const size_t kDefaultSize = 128;
89
James Kuszmaulf5eb4682023-09-22 17:16:59 -070090 const size_t half_size = kDefaultSize / 2;
91 std::span<uint8_t> span =
92 this->allocator_->Allocate(kDefaultSize, 4, SetZero::kYes).value();
93 ASSERT_EQ(kDefaultSize, span.size());
94 // Set the span with some sentinel values so that we can detect that the
95 // removal occurred correctly.
96 for (size_t ii = 0; ii < span.size(); ++ii) {
97 span[ii] = ii + 1;
98 }
99
100 // Remove bytes such that one old byte will remain at the start, and a chunk
101 // of 8 bytes will be cut out after that..
102 span = this->allocator_->RemoveBytes(span.subspan(1, half_size));
103 ASSERT_EQ(half_size, span.size());
104 size_t index = 0;
105 EXPECT_EQ(1u, span[index]);
106 index++;
107 for (; index < span.size(); ++index) {
108 EXPECT_EQ(index + half_size + 1, span[index]);
109 }
110 this->allocator_->Deallocate(span);
111}
112
113// Tests that if we fail to deallocate that we fail during destruction.
114TYPED_TEST(AllocatorTest, NoDeallocate) {
115 EXPECT_DEATH(
116 {
117 EXPECT_EQ(
118 4, this->allocator_->Allocate(4, 4, SetZero::kYes).value().size());
119 this->allocator_.reset();
120 },
121 "Must deallocate");
122}
123
124// Tests that if we never allocate that we cannot deallocate.
125TYPED_TEST(AllocatorTest, NoAllocateThenDeallocate) {
126 EXPECT_DEATH(this->allocator_->Deallocate(std::span<uint8_t>()),
127 "prior allocation");
128}
129
130// Tests that if we attempt to allocate more than the backing span allows that
131// we correctly return an empty span.
132TEST(SpanAllocatorTest, OverAllocate) {
133 std::vector<uint8_t> buffer(kDefaultSize);
134 SpanAllocator allocator({buffer.data(), buffer.size()});
135 EXPECT_FALSE(
136 allocator.Allocate(kDefaultSize + 1u, 0, SetZero::kYes).has_value());
137}
138
139// Tests that if we attempt to insert more than the backing span allows that
140// we correctly return an empty span.
141TEST(SpanAllocatorTest, OverInsert) {
142 std::vector<uint8_t> buffer(kDefaultSize);
143 SpanAllocator allocator({buffer.data(), buffer.size()});
144 std::span<uint8_t> span =
145 allocator.Allocate(kDefaultSize, 0, SetZero::kYes).value();
146 EXPECT_EQ(kDefaultSize, span.size());
147 EXPECT_FALSE(
148 allocator.InsertBytes(span.data(), 1u, 0, SetZero::kYes).has_value());
149 allocator.Deallocate(span);
150}
151
152// Because we really aren't meant to instantiate ResizeableObject's directly (if
153// nothing else it has virtual member functions), define a testing
154// implementation.
155
156class TestResizeableObject : public ResizeableObject {
157 public:
158 TestResizeableObject(std::span<uint8_t> buffer, ResizeableObject *parent)
159 : ResizeableObject(buffer, parent) {}
160 TestResizeableObject(std::span<uint8_t> buffer, Allocator *allocator)
161 : ResizeableObject(buffer, allocator) {}
162 virtual ~TestResizeableObject() {}
163 using ResizeableObject::SubObject;
164 bool InsertBytes(void *insertion_point, size_t bytes) {
165 return ResizeableObject::InsertBytes(insertion_point, bytes, SetZero::kYes);
166 }
167 TestResizeableObject(TestResizeableObject &&) = default;
168
169 struct TestObject {
170 uoffset_t inline_entry_offset;
171 std::unique_ptr<TestResizeableObject> object;
172 size_t absolute_offset;
173 };
174
175 // Adds a new object of the requested size.
176 void AddEntry(uoffset_t inline_entry_offset, size_t absolute_offset,
177 size_t buffer_size, bool set_object) {
178 *reinterpret_cast<uoffset_t *>(buffer_.data() + inline_entry_offset) =
179 set_object ? (absolute_offset - inline_entry_offset) : 0;
180 objects_.emplace_back(
181 TestObject{inline_entry_offset, nullptr, absolute_offset});
182 if (set_object) {
183 objects_.back().object = std::make_unique<TestResizeableObject>(
184 buffer().subspan(absolute_offset, buffer_size), this);
185 }
186 }
187
188 size_t NumberOfSubObjects() const override { return objects_.size(); }
189 SubObject GetSubObject(size_t index) override {
190 TestObject &subobject = objects_.at(index);
191 return {reinterpret_cast<uoffset_t *>(buffer_.data() +
192 subobject.inline_entry_offset),
193 subobject.object.get(), &subobject.absolute_offset};
194 }
195
196 TestObject &GetObject(size_t index) { return objects_.at(index); }
197
198 size_t Alignment() const override { return 64; }
199 size_t AbsoluteOffsetOffset() const override { return 0; }
200
201 private:
202 std::vector<TestObject> objects_;
203};
204
205class ResizeableObjectTest : public ::testing::Test {
206 protected:
207 static constexpr size_t kInitialSize = 128;
208 ResizeableObjectTest()
209 : object_(allocator_.Allocate(kInitialSize, 4, SetZero::kYes).value(),
210 &allocator_) {}
211 ~ResizeableObjectTest() { allocator_.Deallocate(object_.buffer()); }
212 VectorAllocator allocator_;
213 TestResizeableObject object_;
214};
215
216// Tests that if we created an object and then do nothing with it that nothing
217// untoward happens.
218TEST_F(ResizeableObjectTest, DoNothing) {}
219
220// Test that when we move the ResizeableObject we clear the reference to the old
221// buffer.
222TEST_F(ResizeableObjectTest, Move) {
223 TestResizeableObject target_object = std::move(object_);
224 ASSERT_EQ(0u, object_.buffer().size());
225 ASSERT_EQ(kInitialSize, target_object.buffer().size());
226}
227
228// Tests the pathways for resizing a nested ResizeableObject works.
229TEST_F(ResizeableObjectTest, ResizeNested) {
230 constexpr size_t kAbsoluteOffset = 64;
231 object_.AddEntry(4, kAbsoluteOffset, 64, true);
232 TestResizeableObject *subobject = object_.GetObject(0).object.get();
233 object_.AddEntry(0, kAbsoluteOffset, 64, false);
234 EXPECT_EQ(60, *object_.GetSubObject(0).inline_entry);
235 EXPECT_EQ(0, *object_.GetSubObject(1).inline_entry);
236 EXPECT_EQ(64, object_.GetObject(0).object->buffer().data() -
237 object_.buffer().data());
238
239 constexpr size_t kInsertBytes = 5;
240 // The insert should succeed.
241 ASSERT_TRUE(
242 subobject->InsertBytes(subobject->buffer().data() + 1u, kInsertBytes));
243 // We should now observe the size of the buffers increasing, but the start
244 // _not_ moving.
245 // We should've rounded the insert up to the alignment we areusing (64 bytes).
246 EXPECT_EQ(kInitialSize + 64, object_.buffer().size());
247 EXPECT_EQ(128, subobject->buffer().size());
248 EXPECT_EQ(60, *object_.GetSubObject(0).inline_entry);
249 EXPECT_EQ(0, *object_.GetSubObject(1).inline_entry);
250 EXPECT_EQ(kAbsoluteOffset, object_.GetObject(0).absolute_offset);
251 EXPECT_EQ(kAbsoluteOffset, object_.GetObject(1).absolute_offset);
252
253 // And next we insert before the subobjects, so that we can see their offsets
254 // shift. The insert should succeed.
255 ASSERT_TRUE(object_.InsertBytes(subobject->buffer().data(), kInsertBytes));
256 EXPECT_EQ(kInitialSize + 2 * 64, object_.buffer().size());
257 EXPECT_EQ(128, subobject->buffer().size());
258 EXPECT_EQ(60 + 64, *object_.GetSubObject(0).inline_entry);
259 // The unpopulated object's inline entry should not have changed since
260 // it was zero.
261 EXPECT_EQ(0, *object_.GetSubObject(1).inline_entry);
262 EXPECT_EQ(kAbsoluteOffset + 64, object_.GetObject(0).absolute_offset);
263 EXPECT_EQ(kAbsoluteOffset + 64, object_.GetObject(1).absolute_offset);
264}
265
266} // namespace aos::fbs::testing