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