1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_allocator/dl_allocator.h"
16
17 #include <cstdint>
18
19 #include "pw_allocator/block_allocator_testing.h"
20 #include "pw_unit_test/framework.h"
21
22 namespace {
23
24 // Test fixtures.
25
26 using ::pw::allocator::Layout;
27 using ::pw::allocator::test::Preallocation;
28 using BlockType = ::pw::allocator::DlBlock<uint16_t>;
29 using DlAllocator = ::pw::allocator::DlAllocator<BlockType>;
30 using BlockAllocatorTest =
31 ::pw::allocator::test::BlockAllocatorTest<DlAllocator, 8192>;
32
33 class DlAllocatorTest : public BlockAllocatorTest {
34 public:
DlAllocatorTest()35 DlAllocatorTest() : BlockAllocatorTest(allocator_) {}
36
37 private:
38 DlAllocator allocator_;
39 };
40
41 // Unit tests.
42
TEST_F(DlAllocatorTest,AutomaticallyInit)43 TEST_F(DlAllocatorTest, AutomaticallyInit) {
44 DlAllocator allocator(GetBytes());
45 AutomaticallyInit(allocator);
46 }
47
TEST_F(DlAllocatorTest,ExplicitlyInit)48 TEST_F(DlAllocatorTest, ExplicitlyInit) {
49 DlAllocator allocator;
50 ExplicitlyInit(allocator);
51 }
52
TEST_F(DlAllocatorTest,GetCapacity)53 TEST_F(DlAllocatorTest, GetCapacity) { GetCapacity(8192); }
54
TEST_F(DlAllocatorTest,AllocateLarge)55 TEST_F(DlAllocatorTest, AllocateLarge) { AllocateLarge(); }
56
TEST_F(DlAllocatorTest,AllocateSmall)57 TEST_F(DlAllocatorTest, AllocateSmall) { AllocateSmall(); }
58
TEST_F(DlAllocatorTest,AllocateLargeAlignment)59 TEST_F(DlAllocatorTest, AllocateLargeAlignment) { AllocateLargeAlignment(); }
60
TEST_F(DlAllocatorTest,AllocateAlignmentFailure)61 TEST_F(DlAllocatorTest, AllocateAlignmentFailure) {
62 AllocateAlignmentFailure();
63 }
64
TEST_F(DlAllocatorTest,AllocatesBestCompatible)65 TEST_F(DlAllocatorTest, AllocatesBestCompatible) {
66 auto& allocator = GetAllocator({
67 {kLargeOuterSize, Preallocation::kFree},
68 {kSmallerOuterSize, Preallocation::kUsed},
69 {kSmallOuterSize, Preallocation::kFree},
70 {kSmallerOuterSize, Preallocation::kUsed},
71 {kLargerOuterSize, Preallocation::kFree},
72 {Preallocation::kSizeRemaining, Preallocation::kUsed},
73 });
74
75 void* ptr1 = allocator.Allocate(Layout(kSmallInnerSize, 1));
76 EXPECT_LT(Fetch(1), ptr1);
77 EXPECT_LT(ptr1, Fetch(3));
78
79 void* ptr2 = allocator.Allocate(Layout(kSmallInnerSize, 1));
80 EXPECT_LT(ptr2, Fetch(1));
81
82 // A second small block fits in the leftovers of the first "Large" block.
83 void* ptr3 = allocator.Allocate(Layout(kSmallInnerSize, 1));
84 EXPECT_LT(ptr3, Fetch(1));
85
86 allocator.Deallocate(ptr1);
87 allocator.Deallocate(ptr2);
88 allocator.Deallocate(ptr3);
89 }
90
TEST_F(DlAllocatorTest,DeallocateNull)91 TEST_F(DlAllocatorTest, DeallocateNull) { DeallocateNull(); }
92
TEST_F(DlAllocatorTest,DeallocateShuffled)93 TEST_F(DlAllocatorTest, DeallocateShuffled) { DeallocateShuffled(); }
94
TEST_F(DlAllocatorTest,IterateOverBlocks)95 TEST_F(DlAllocatorTest, IterateOverBlocks) { IterateOverBlocks(); }
96
TEST_F(DlAllocatorTest,ResizeNull)97 TEST_F(DlAllocatorTest, ResizeNull) { ResizeNull(); }
98
TEST_F(DlAllocatorTest,ResizeLargeSame)99 TEST_F(DlAllocatorTest, ResizeLargeSame) { ResizeLargeSame(); }
100
TEST_F(DlAllocatorTest,ResizeLargeSmaller)101 TEST_F(DlAllocatorTest, ResizeLargeSmaller) { ResizeLargeSmaller(); }
102
TEST_F(DlAllocatorTest,ResizeLargeLarger)103 TEST_F(DlAllocatorTest, ResizeLargeLarger) { ResizeLargeLarger(); }
104
TEST_F(DlAllocatorTest,ResizeLargeLargerFailure)105 TEST_F(DlAllocatorTest, ResizeLargeLargerFailure) {
106 ResizeLargeLargerFailure();
107 }
108
TEST_F(DlAllocatorTest,ResizeSmallSame)109 TEST_F(DlAllocatorTest, ResizeSmallSame) { ResizeSmallSame(); }
110
TEST_F(DlAllocatorTest,ResizeSmallSmaller)111 TEST_F(DlAllocatorTest, ResizeSmallSmaller) { ResizeSmallSmaller(); }
112
TEST_F(DlAllocatorTest,ResizeSmallLarger)113 TEST_F(DlAllocatorTest, ResizeSmallLarger) { ResizeSmallLarger(); }
114
TEST_F(DlAllocatorTest,ResizeSmallLargerFailure)115 TEST_F(DlAllocatorTest, ResizeSmallLargerFailure) {
116 ResizeSmallLargerFailure();
117 }
118
TEST_F(DlAllocatorTest,MeasureFragmentation)119 TEST_F(DlAllocatorTest, MeasureFragmentation) { MeasureFragmentation(); }
120
121 // Fuzz tests.
122
123 using ::pw::allocator::test::BlockAlignedBuffer;
124 using ::pw::allocator::test::BlockAllocatorFuzzer;
125 using ::pw::allocator::test::DefaultArbitraryRequests;
126 using ::pw::allocator::test::Request;
127
DoesNotCorruptBlocks(const pw::Vector<Request> & requests)128 void DoesNotCorruptBlocks(const pw::Vector<Request>& requests) {
129 static BlockAlignedBuffer<BlockType> buffer;
130 static DlAllocator allocator(buffer.as_span());
131 static BlockAllocatorFuzzer fuzzer(allocator);
132 fuzzer.DoesNotCorruptBlocks(requests);
133 }
134
135 FUZZ_TEST(DlAllocatorFuzzTest, DoesNotCorruptBlocks)
136 .WithDomains(DefaultArbitraryRequests());
137
138 } // namespace
139