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