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