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