xref: /aosp_15_r20/external/pigweed/pw_allocator/bucket/sequenced_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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/bucket/sequenced.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 
20 #include "pw_allocator/block/detailed_block.h"
21 #include "pw_allocator/bucket/testing.h"
22 #include "pw_unit_test/framework.h"
23 
24 namespace {
25 
26 using ::pw::allocator::SequencedBucket;
27 using ::pw::allocator::SequencedItem;
28 using ::pw::allocator::test::BucketTest;
29 
30 using BlockType = ::pw::allocator::DetailedBlock<uint32_t, SequencedItem>;
31 using SequencedBucketTest = BucketTest<SequencedBucket<BlockType>>;
32 
TEST_F(SequencedBucketTest,SetsAndGetsMaxInnerSize)33 TEST_F(SequencedBucketTest, SetsAndGetsMaxInnerSize) {
34   SetsAndGetsMaxInnerSize();
35 }
36 
TEST_F(SequencedBucketTest,AddsAndRemovesBlocks)37 TEST_F(SequencedBucketTest, AddsAndRemovesBlocks) { AddsAndRemovesBlocks(); }
38 
TEST_F(SequencedBucketTest,FailsToAddWhenBlockIsTooSmall)39 TEST_F(SequencedBucketTest, FailsToAddWhenBlockIsTooSmall) {
40   FailsToAddWhenBlockIsTooSmall();
41 }
42 
TEST_F(SequencedBucketTest,FailsToRemoveBlockWhenNotFound)43 TEST_F(SequencedBucketTest, FailsToRemoveBlockWhenNotFound) {
44   FailsToRemoveBlockWhenNotFound();
45 }
46 
TEST_F(SequencedBucketTest,RemovesUnspecifiedBlock)47 TEST_F(SequencedBucketTest, RemovesUnspecifiedBlock) {
48   RemovesUnspecifiedBlock();
49 }
50 
TEST_F(SequencedBucketTest,RemovesByLayout)51 TEST_F(SequencedBucketTest, RemovesByLayout) { RemovesByLayout(); }
52 
TEST_F(SequencedBucketTest,FailsToRemoveByExcessiveSize)53 TEST_F(SequencedBucketTest, FailsToRemoveByExcessiveSize) {
54   FailsToRemoveByExcessiveSize();
55 }
56 
TEST_F(SequencedBucketTest,CanAddAndRemoveWithThreshold)57 TEST_F(SequencedBucketTest, CanAddAndRemoveWithThreshold) {
58   SequencedBucket<BlockType>& bucket = this->bucket();
59   bucket.set_threshold(kLayout2.size());
60 
61   // Create blocks, and use the some duplicate sizes.
62   BlockType& block1 = CreateBlockAndAddToBucket(kLayout1);
63   BlockType& block2 = CreateBlockAndAddToBucket(kLayout2);
64   BlockType& block3 = CreateBlockAndAddToBucket(kLayout3);
65   BlockType& block4 = CreateBlockAndAddToBucket(kLayout1);
66   BlockType& block5 = CreateBlockAndAddToBucket(kLayout3);
67 
68   // Since `kBlockSize3` is greater the threshold, the bucket should be searched
69   // from the beginning and find block 3 before block 5.
70   EXPECT_EQ(bucket.RemoveCompatible(kLayout3), &block3);
71   EXPECT_EQ(bucket.RemoveCompatible(kLayout3), &block5);
72 
73   EXPECT_EQ(bucket.RemoveCompatible(kLayout2), &block2);
74 
75   // Since `kBlockSize1` is less the threshold, the bucket should be searched
76   // from the end and find block 4 before block 1.
77   EXPECT_EQ(bucket.RemoveCompatible(kLayout1), &block4);
78   EXPECT_EQ(bucket.RemoveCompatible(kLayout1), &block1);
79   EXPECT_TRUE(bucket.empty());
80 }
81 
82 }  // namespace
83