xref: /aosp_15_r20/external/pigweed/pw_multibuf/single_chunk_region_tracker_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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_multibuf/single_chunk_region_tracker.h"
16 
17 #include <cstddef>
18 #include <optional>
19 
20 #include "pw_multibuf/chunk.h"
21 #include "pw_unit_test/framework.h"
22 
23 namespace pw::multibuf {
24 namespace {
25 
26 const size_t kArbitraryRegionSize = 1024;
27 const size_t kArbitraryChunkSize = 32;
28 
TEST(SingleChunkRegionTrackerTest,GetChunkSmallerThanRegionSuccess)29 TEST(SingleChunkRegionTrackerTest, GetChunkSmallerThanRegionSuccess) {
30   std::array<std::byte, kArbitraryRegionSize> chunk_storage{};
31   SingleChunkRegionTracker chunk_tracker(chunk_storage);
32   std::optional<OwnedChunk> chunk = chunk_tracker.GetChunk(kArbitraryChunkSize);
33   EXPECT_TRUE(chunk.has_value());
34   EXPECT_EQ(chunk->size(), kArbitraryChunkSize);
35 }
36 
TEST(SingleChunkRegionTrackerTest,GetChunkSameSizeAsRegionSuccess)37 TEST(SingleChunkRegionTrackerTest, GetChunkSameSizeAsRegionSuccess) {
38   std::array<std::byte, kArbitraryRegionSize> chunk_storage{};
39   SingleChunkRegionTracker chunk_tracker(chunk_storage);
40   std::optional<OwnedChunk> chunk =
41       chunk_tracker.GetChunk(kArbitraryRegionSize);
42   EXPECT_TRUE(chunk.has_value());
43   EXPECT_EQ(chunk->size(), kArbitraryRegionSize);
44 }
45 
TEST(SingleChunkRegionTrackerTest,GetChunkFailChunkInUse)46 TEST(SingleChunkRegionTrackerTest, GetChunkFailChunkInUse) {
47   std::array<std::byte, kArbitraryRegionSize> chunk_storage{};
48   SingleChunkRegionTracker chunk_tracker(chunk_storage);
49   std::optional<OwnedChunk> chunk1 =
50       chunk_tracker.GetChunk(kArbitraryChunkSize);
51   ASSERT_TRUE(chunk1.has_value());
52 
53   std::optional<OwnedChunk> chunk2 =
54       chunk_tracker.GetChunk(kArbitraryChunkSize);
55   EXPECT_FALSE(chunk2.has_value());
56 }
57 
TEST(SingleChunkRegionTrackerTest,GetChunkAfterReleasedChunkSuccess)58 TEST(SingleChunkRegionTrackerTest, GetChunkAfterReleasedChunkSuccess) {
59   std::array<std::byte, kArbitraryRegionSize> chunk_storage{};
60   SingleChunkRegionTracker chunk_tracker(chunk_storage);
61   std::optional<OwnedChunk> chunk1 =
62       chunk_tracker.GetChunk(kArbitraryChunkSize);
63   ASSERT_TRUE(chunk1.has_value());
64 
65   std::optional<OwnedChunk> chunk2 =
66       chunk_tracker.GetChunk(kArbitraryChunkSize);
67   ASSERT_FALSE(chunk2.has_value());
68 
69   chunk1->Release();
70 
71   std::optional<OwnedChunk> chunk3 =
72       chunk_tracker.GetChunk(kArbitraryChunkSize);
73   EXPECT_TRUE(chunk3.has_value());
74 }
75 
76 }  // namespace
77 }  // namespace pw::multibuf
78