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/header_chunk_region_tracker.h"
16
17 #include <cstddef>
18 #include <optional>
19
20 #include "pw_allocator/testing.h"
21 #include "pw_multibuf/chunk.h"
22 #include "pw_status/status.h"
23 #include "pw_unit_test/framework.h"
24
25 namespace pw::multibuf {
26 namespace {
27
28 using allocator::test::AllocatorForTest;
29
30 const size_t kArbitraryAllocatorSize = 1024;
31 const size_t kArbitraryChunkSize = 32;
32
TEST(HeaderChunkRegionTracker,AllocatesRegionAsChunk)33 TEST(HeaderChunkRegionTracker, AllocatesRegionAsChunk) {
34 AllocatorForTest<kArbitraryAllocatorSize> alloc;
35 std::optional<OwnedChunk> chunk =
36 HeaderChunkRegionTracker::AllocateRegionAsChunk(alloc,
37 kArbitraryChunkSize);
38 ASSERT_TRUE(chunk.has_value());
39 }
40
TEST(HeaderChunkRegionTracker,AllocatedRegionAsChunkTooLarge)41 TEST(HeaderChunkRegionTracker, AllocatedRegionAsChunkTooLarge) {
42 AllocatorForTest<kArbitraryAllocatorSize> alloc;
43 std::optional<OwnedChunk> chunk =
44 HeaderChunkRegionTracker::AllocateRegionAsChunk(alloc,
45 kArbitraryAllocatorSize);
46 ASSERT_FALSE(chunk.has_value());
47 }
48
TEST(HeaderChunkRegionTracker,AllocatesRegion)49 TEST(HeaderChunkRegionTracker, AllocatesRegion) {
50 AllocatorForTest<kArbitraryAllocatorSize> alloc;
51 auto tracker =
52 HeaderChunkRegionTracker::AllocateRegion(alloc, kArbitraryChunkSize);
53 ASSERT_NE(tracker, nullptr);
54 }
55
TEST(HeaderChunkRegionTracker,AllocatedRegionTooLarge)56 TEST(HeaderChunkRegionTracker, AllocatedRegionTooLarge) {
57 AllocatorForTest<kArbitraryAllocatorSize> alloc;
58 auto tracker =
59 HeaderChunkRegionTracker::AllocateRegion(alloc, kArbitraryAllocatorSize);
60 ASSERT_EQ(tracker, nullptr);
61 }
62
63 } // namespace
64 } // namespace pw::multibuf
65