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/last_fit_block_allocator.h"
16
17 #include "pw_allocator/block_allocator_testing.h"
18 #include "pw_unit_test/framework.h"
19
20 namespace {
21
22 // Test fixtures.
23
24 using ::pw::allocator::Layout;
25 using ::pw::allocator::test::Preallocation;
26 using LastFitBlockAllocator = ::pw::allocator::LastFitBlockAllocator<uint16_t>;
27 using BlockAllocatorTest =
28 ::pw::allocator::test::BlockAllocatorTest<LastFitBlockAllocator>;
29
30 class LastFitBlockAllocatorTest : public BlockAllocatorTest {
31 public:
LastFitBlockAllocatorTest()32 LastFitBlockAllocatorTest() : BlockAllocatorTest(allocator_) {}
33
34 private:
35 LastFitBlockAllocator allocator_;
36 };
37
38 // Unit tests.
39
TEST_F(LastFitBlockAllocatorTest,CanAutomaticallyInit)40 TEST_F(LastFitBlockAllocatorTest, CanAutomaticallyInit) {
41 LastFitBlockAllocator allocator(GetBytes());
42 CanAutomaticallyInit(allocator);
43 }
44
TEST_F(LastFitBlockAllocatorTest,CanExplicitlyInit)45 TEST_F(LastFitBlockAllocatorTest, CanExplicitlyInit) {
46 LastFitBlockAllocator allocator;
47 CanExplicitlyInit(allocator);
48 }
49
TEST_F(LastFitBlockAllocatorTest,GetCapacity)50 TEST_F(LastFitBlockAllocatorTest, GetCapacity) { GetCapacity(); }
51
TEST_F(LastFitBlockAllocatorTest,AllocateLarge)52 TEST_F(LastFitBlockAllocatorTest, AllocateLarge) { AllocateLarge(); }
53
TEST_F(LastFitBlockAllocatorTest,AllocateSmall)54 TEST_F(LastFitBlockAllocatorTest, AllocateSmall) { AllocateSmall(); }
55
TEST_F(LastFitBlockAllocatorTest,AllocateLargeAlignment)56 TEST_F(LastFitBlockAllocatorTest, AllocateLargeAlignment) {
57 AllocateLargeAlignment();
58 }
59
TEST_F(LastFitBlockAllocatorTest,AllocateAlignmentFailure)60 TEST_F(LastFitBlockAllocatorTest, AllocateAlignmentFailure) {
61 AllocateAlignmentFailure();
62 }
63
TEST_F(LastFitBlockAllocatorTest,AllocatesLastCompatible)64 TEST_F(LastFitBlockAllocatorTest, AllocatesLastCompatible) {
65 auto& allocator = GetAllocator({
66 {kLargeOuterSize, Preallocation::kIndexFree},
67 {kSmallerOuterSize, 1},
68 {kSmallOuterSize, Preallocation::kIndexFree},
69 {kSmallerOuterSize, 3},
70 {kSmallOuterSize, Preallocation::kIndexFree},
71 {Preallocation::kSizeRemaining, 5},
72 });
73
74 Store(0, allocator.Allocate(Layout(kLargeInnerSize, 1)));
75 EXPECT_EQ(NextAfter(0), Fetch(1));
76 Store(4, allocator.Allocate(Layout(kSmallInnerSize, 1)));
77 EXPECT_EQ(NextAfter(3), Fetch(4));
78 EXPECT_EQ(NextAfter(4), Fetch(5));
79 }
80
TEST_F(LastFitBlockAllocatorTest,DeallocateNull)81 TEST_F(LastFitBlockAllocatorTest, DeallocateNull) { DeallocateNull(); }
82
TEST_F(LastFitBlockAllocatorTest,DeallocateShuffled)83 TEST_F(LastFitBlockAllocatorTest, DeallocateShuffled) { DeallocateShuffled(); }
84
TEST_F(LastFitBlockAllocatorTest,IterateOverBlocks)85 TEST_F(LastFitBlockAllocatorTest, IterateOverBlocks) { IterateOverBlocks(); }
86
TEST_F(LastFitBlockAllocatorTest,ResizeNull)87 TEST_F(LastFitBlockAllocatorTest, ResizeNull) { ResizeNull(); }
88
TEST_F(LastFitBlockAllocatorTest,ResizeLargeSame)89 TEST_F(LastFitBlockAllocatorTest, ResizeLargeSame) { ResizeLargeSame(); }
90
TEST_F(LastFitBlockAllocatorTest,ResizeLargeSmaller)91 TEST_F(LastFitBlockAllocatorTest, ResizeLargeSmaller) { ResizeLargeSmaller(); }
92
TEST_F(LastFitBlockAllocatorTest,ResizeLargeLarger)93 TEST_F(LastFitBlockAllocatorTest, ResizeLargeLarger) { ResizeLargeLarger(); }
94
TEST_F(LastFitBlockAllocatorTest,ResizeLargeLargerFailure)95 TEST_F(LastFitBlockAllocatorTest, ResizeLargeLargerFailure) {
96 ResizeLargeLargerFailure();
97 }
98
TEST_F(LastFitBlockAllocatorTest,ResizeSmallSame)99 TEST_F(LastFitBlockAllocatorTest, ResizeSmallSame) { ResizeSmallSame(); }
100
TEST_F(LastFitBlockAllocatorTest,ResizeSmallSmaller)101 TEST_F(LastFitBlockAllocatorTest, ResizeSmallSmaller) { ResizeSmallSmaller(); }
102
TEST_F(LastFitBlockAllocatorTest,ResizeSmallLarger)103 TEST_F(LastFitBlockAllocatorTest, ResizeSmallLarger) { ResizeSmallLarger(); }
104
TEST_F(LastFitBlockAllocatorTest,ResizeSmallLargerFailure)105 TEST_F(LastFitBlockAllocatorTest, ResizeSmallLargerFailure) {
106 ResizeSmallLargerFailure();
107 }
108
TEST_F(LastFitBlockAllocatorTest,CanMeasureFragmentation)109 TEST_F(LastFitBlockAllocatorTest, CanMeasureFragmentation) {
110 CanMeasureFragmentation();
111 }
112
113 } // namespace
114