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 { |
| 10 | // Tests that PaddedSize() behaves as expected. |
| 11 | TEST(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 | |
| 19 | inline constexpr size_t kDefaultSize = 16; |
| 20 | template <typename T> |
| 21 | class AllocatorTest : public ::testing::Test { |
| 22 | protected: |
| 23 | AllocatorTest() : allocator_(std::make_unique<T>()) {} |
| 24 | std::vector<uint8_t> buffer_; |
| 25 | // unique_ptr so that we can destroy the allocator at will. |
| 26 | std::unique_ptr<T> allocator_; |
| 27 | }; |
| 28 | |
| 29 | template <> |
| 30 | AllocatorTest<SpanAllocator>::AllocatorTest() |
| 31 | : buffer_(kDefaultSize), |
| 32 | allocator_(std::make_unique<SpanAllocator>( |
| 33 | std::span<uint8_t>{buffer_.data(), buffer_.size()})) {} |
| 34 | |
| 35 | using AllocatorTypes = ::testing::Types<SpanAllocator, VectorAllocator>; |
| 36 | TYPED_TEST_SUITE(AllocatorTest, AllocatorTypes); |
| 37 | |
| 38 | // Tests that we can create and not use a VectorAllocator. |
| 39 | TYPED_TEST(AllocatorTest, UnusedAllocator) {} |
| 40 | |
| 41 | // Tests that a simple allocate works. |
| 42 | TYPED_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. |
| 52 | TYPED_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 | |
| 80 | // Tests that we can remove bytes from an arbitrary spot in the buffer. |
| 81 | TYPED_TEST(AllocatorTest, RemoveBytes) { |
| 82 | const size_t half_size = kDefaultSize / 2; |
| 83 | std::span<uint8_t> span = |
| 84 | this->allocator_->Allocate(kDefaultSize, 4, SetZero::kYes).value(); |
| 85 | ASSERT_EQ(kDefaultSize, span.size()); |
| 86 | // Set the span with some sentinel values so that we can detect that the |
| 87 | // removal occurred correctly. |
| 88 | for (size_t ii = 0; ii < span.size(); ++ii) { |
| 89 | span[ii] = ii + 1; |
| 90 | } |
| 91 | |
| 92 | // Remove bytes such that one old byte will remain at the start, and a chunk |
| 93 | // of 8 bytes will be cut out after that.. |
| 94 | span = this->allocator_->RemoveBytes(span.subspan(1, half_size)); |
| 95 | ASSERT_EQ(half_size, span.size()); |
| 96 | size_t index = 0; |
| 97 | EXPECT_EQ(1u, span[index]); |
| 98 | index++; |
| 99 | for (; index < span.size(); ++index) { |
| 100 | EXPECT_EQ(index + half_size + 1, span[index]); |
| 101 | } |
| 102 | this->allocator_->Deallocate(span); |
| 103 | } |
| 104 | |
| 105 | // Tests that if we fail to deallocate that we fail during destruction. |
| 106 | TYPED_TEST(AllocatorTest, NoDeallocate) { |
| 107 | EXPECT_DEATH( |
| 108 | { |
| 109 | EXPECT_EQ( |
| 110 | 4, this->allocator_->Allocate(4, 4, SetZero::kYes).value().size()); |
| 111 | this->allocator_.reset(); |
| 112 | }, |
| 113 | "Must deallocate"); |
| 114 | } |
| 115 | |
| 116 | // Tests that if we never allocate that we cannot deallocate. |
| 117 | TYPED_TEST(AllocatorTest, NoAllocateThenDeallocate) { |
| 118 | EXPECT_DEATH(this->allocator_->Deallocate(std::span<uint8_t>()), |
| 119 | "prior allocation"); |
| 120 | } |
| 121 | |
| 122 | // Tests that if we attempt to allocate more than the backing span allows that |
| 123 | // we correctly return an empty span. |
| 124 | TEST(SpanAllocatorTest, OverAllocate) { |
| 125 | std::vector<uint8_t> buffer(kDefaultSize); |
| 126 | SpanAllocator allocator({buffer.data(), buffer.size()}); |
| 127 | EXPECT_FALSE( |
| 128 | allocator.Allocate(kDefaultSize + 1u, 0, SetZero::kYes).has_value()); |
| 129 | } |
| 130 | |
| 131 | // Tests that if we attempt to insert more than the backing span allows that |
| 132 | // we correctly return an empty span. |
| 133 | TEST(SpanAllocatorTest, OverInsert) { |
| 134 | std::vector<uint8_t> buffer(kDefaultSize); |
| 135 | SpanAllocator allocator({buffer.data(), buffer.size()}); |
| 136 | std::span<uint8_t> span = |
| 137 | allocator.Allocate(kDefaultSize, 0, SetZero::kYes).value(); |
| 138 | EXPECT_EQ(kDefaultSize, span.size()); |
| 139 | EXPECT_FALSE( |
| 140 | allocator.InsertBytes(span.data(), 1u, 0, SetZero::kYes).has_value()); |
| 141 | allocator.Deallocate(span); |
| 142 | } |
| 143 | |
| 144 | // Because we really aren't meant to instantiate ResizeableObject's directly (if |
| 145 | // nothing else it has virtual member functions), define a testing |
| 146 | // implementation. |
| 147 | |
| 148 | class TestResizeableObject : public ResizeableObject { |
| 149 | public: |
| 150 | TestResizeableObject(std::span<uint8_t> buffer, ResizeableObject *parent) |
| 151 | : ResizeableObject(buffer, parent) {} |
| 152 | TestResizeableObject(std::span<uint8_t> buffer, Allocator *allocator) |
| 153 | : ResizeableObject(buffer, allocator) {} |
| 154 | virtual ~TestResizeableObject() {} |
| 155 | using ResizeableObject::SubObject; |
| 156 | bool InsertBytes(void *insertion_point, size_t bytes) { |
| 157 | return ResizeableObject::InsertBytes(insertion_point, bytes, SetZero::kYes); |
| 158 | } |
| 159 | TestResizeableObject(TestResizeableObject &&) = default; |
| 160 | |
| 161 | struct TestObject { |
| 162 | uoffset_t inline_entry_offset; |
| 163 | std::unique_ptr<TestResizeableObject> object; |
| 164 | size_t absolute_offset; |
| 165 | }; |
| 166 | |
| 167 | // Adds a new object of the requested size. |
| 168 | void AddEntry(uoffset_t inline_entry_offset, size_t absolute_offset, |
| 169 | size_t buffer_size, bool set_object) { |
| 170 | *reinterpret_cast<uoffset_t *>(buffer_.data() + inline_entry_offset) = |
| 171 | set_object ? (absolute_offset - inline_entry_offset) : 0; |
| 172 | objects_.emplace_back( |
| 173 | TestObject{inline_entry_offset, nullptr, absolute_offset}); |
| 174 | if (set_object) { |
| 175 | objects_.back().object = std::make_unique<TestResizeableObject>( |
| 176 | buffer().subspan(absolute_offset, buffer_size), this); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | size_t NumberOfSubObjects() const override { return objects_.size(); } |
| 181 | SubObject GetSubObject(size_t index) override { |
| 182 | TestObject &subobject = objects_.at(index); |
| 183 | return {reinterpret_cast<uoffset_t *>(buffer_.data() + |
| 184 | subobject.inline_entry_offset), |
| 185 | subobject.object.get(), &subobject.absolute_offset}; |
| 186 | } |
| 187 | |
| 188 | TestObject &GetObject(size_t index) { return objects_.at(index); } |
| 189 | |
| 190 | size_t Alignment() const override { return 64; } |
| 191 | size_t AbsoluteOffsetOffset() const override { return 0; } |
| 192 | |
| 193 | private: |
| 194 | std::vector<TestObject> objects_; |
| 195 | }; |
| 196 | |
| 197 | class ResizeableObjectTest : public ::testing::Test { |
| 198 | protected: |
| 199 | static constexpr size_t kInitialSize = 128; |
| 200 | ResizeableObjectTest() |
| 201 | : object_(allocator_.Allocate(kInitialSize, 4, SetZero::kYes).value(), |
| 202 | &allocator_) {} |
| 203 | ~ResizeableObjectTest() { allocator_.Deallocate(object_.buffer()); } |
| 204 | VectorAllocator allocator_; |
| 205 | TestResizeableObject object_; |
| 206 | }; |
| 207 | |
| 208 | // Tests that if we created an object and then do nothing with it that nothing |
| 209 | // untoward happens. |
| 210 | TEST_F(ResizeableObjectTest, DoNothing) {} |
| 211 | |
| 212 | // Test that when we move the ResizeableObject we clear the reference to the old |
| 213 | // buffer. |
| 214 | TEST_F(ResizeableObjectTest, Move) { |
| 215 | TestResizeableObject target_object = std::move(object_); |
| 216 | ASSERT_EQ(0u, object_.buffer().size()); |
| 217 | ASSERT_EQ(kInitialSize, target_object.buffer().size()); |
| 218 | } |
| 219 | |
| 220 | // Tests the pathways for resizing a nested ResizeableObject works. |
| 221 | TEST_F(ResizeableObjectTest, ResizeNested) { |
| 222 | constexpr size_t kAbsoluteOffset = 64; |
| 223 | object_.AddEntry(4, kAbsoluteOffset, 64, true); |
| 224 | TestResizeableObject *subobject = object_.GetObject(0).object.get(); |
| 225 | object_.AddEntry(0, kAbsoluteOffset, 64, false); |
| 226 | EXPECT_EQ(60, *object_.GetSubObject(0).inline_entry); |
| 227 | EXPECT_EQ(0, *object_.GetSubObject(1).inline_entry); |
| 228 | EXPECT_EQ(64, object_.GetObject(0).object->buffer().data() - |
| 229 | object_.buffer().data()); |
| 230 | |
| 231 | constexpr size_t kInsertBytes = 5; |
| 232 | // The insert should succeed. |
| 233 | ASSERT_TRUE( |
| 234 | subobject->InsertBytes(subobject->buffer().data() + 1u, kInsertBytes)); |
| 235 | // We should now observe the size of the buffers increasing, but the start |
| 236 | // _not_ moving. |
| 237 | // We should've rounded the insert up to the alignment we areusing (64 bytes). |
| 238 | EXPECT_EQ(kInitialSize + 64, object_.buffer().size()); |
| 239 | EXPECT_EQ(128, subobject->buffer().size()); |
| 240 | EXPECT_EQ(60, *object_.GetSubObject(0).inline_entry); |
| 241 | EXPECT_EQ(0, *object_.GetSubObject(1).inline_entry); |
| 242 | EXPECT_EQ(kAbsoluteOffset, object_.GetObject(0).absolute_offset); |
| 243 | EXPECT_EQ(kAbsoluteOffset, object_.GetObject(1).absolute_offset); |
| 244 | |
| 245 | // And next we insert before the subobjects, so that we can see their offsets |
| 246 | // shift. The insert should succeed. |
| 247 | ASSERT_TRUE(object_.InsertBytes(subobject->buffer().data(), kInsertBytes)); |
| 248 | EXPECT_EQ(kInitialSize + 2 * 64, object_.buffer().size()); |
| 249 | EXPECT_EQ(128, subobject->buffer().size()); |
| 250 | EXPECT_EQ(60 + 64, *object_.GetSubObject(0).inline_entry); |
| 251 | // The unpopulated object's inline entry should not have changed since |
| 252 | // it was zero. |
| 253 | EXPECT_EQ(0, *object_.GetSubObject(1).inline_entry); |
| 254 | EXPECT_EQ(kAbsoluteOffset + 64, object_.GetObject(0).absolute_offset); |
| 255 | EXPECT_EQ(kAbsoluteOffset + 64, object_.GetObject(1).absolute_offset); |
| 256 | } |
| 257 | |
| 258 | } // namespace aos::fbs::testing |