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 #pragma once
15
16 #include <cstddef>
17 #include <cstring>
18 #include <optional>
19
20 #include "pw_allocator/testing.h"
21 #include "pw_assert/check.h"
22 #include "pw_multibuf/header_chunk_region_tracker.h"
23 #include "pw_multibuf/multibuf.h"
24
25 namespace pw::multibuf::test_utils {
26
27 // Arbitrary size intended to be large enough to store the Chunk and data
28 // slices. This may be increased if `MakeChunk` or a Chunk-splitting operation
29 // fails.
30 inline constexpr size_t kArbitraryAllocatorSize = 2048;
31 inline constexpr size_t kArbitraryChunkSize = 32;
32
33 inline constexpr std::byte kPoisonByte{0x9d};
34
35 using ::pw::allocator::test::AllocatorForTest;
36
37 inline OwnedChunk MakeChunk(pw::allocator::Allocator& allocator,
38 size_t size,
39 std::byte initializer = std::byte{0}) {
40 std::optional<OwnedChunk> chunk =
41 HeaderChunkRegionTracker::AllocateRegionAsChunk(allocator, size);
42 // If this check fails, `kArbitraryAllocatorSize` above may need increasing.
43 PW_CHECK(chunk.has_value());
44 std::memset(chunk->data(), static_cast<uint8_t>(initializer), size);
45 return std::move(*chunk);
46 }
47
MakeChunk(pw::allocator::Allocator & allocator,std::initializer_list<std::byte> data)48 inline OwnedChunk MakeChunk(pw::allocator::Allocator& allocator,
49 std::initializer_list<std::byte> data) {
50 std::optional<OwnedChunk> chunk =
51 HeaderChunkRegionTracker::AllocateRegionAsChunk(allocator, data.size());
52 // If this check fails, `kArbitraryAllocatorSize` above may need increasing.
53 PW_CHECK(chunk.has_value());
54 std::copy(data.begin(), data.end(), (*chunk)->begin());
55 return std::move(*chunk);
56 }
57
MakeChunk(pw::allocator::Allocator & allocator,pw::span<const std::byte> data)58 inline OwnedChunk MakeChunk(pw::allocator::Allocator& allocator,
59 pw::span<const std::byte> data) {
60 std::optional<OwnedChunk> chunk =
61 HeaderChunkRegionTracker::AllocateRegionAsChunk(allocator, data.size());
62 // If this check fails, `kArbitraryAllocatorSize` above may need increasing.
63 PW_CHECK(chunk.has_value());
64 std::copy(data.begin(), data.end(), (*chunk)->begin());
65 return std::move(*chunk);
66 }
67
68 template <typename ActualIterable, typename ExpectedIterable>
ExpectElementsEqual(const ActualIterable & actual,const ExpectedIterable & expected)69 void ExpectElementsEqual(const ActualIterable& actual,
70 const ExpectedIterable& expected) {
71 auto actual_iter = actual.begin();
72 auto expected_iter = expected.begin();
73 for (; expected_iter != expected.end(); ++actual_iter, ++expected_iter) {
74 ASSERT_NE(actual_iter, actual.end());
75 EXPECT_EQ(*actual_iter, *expected_iter);
76 }
77 }
78
79 template <typename ActualIterable, typename T>
ExpectElementsEqual(const ActualIterable & actual,std::initializer_list<T> expected)80 void ExpectElementsEqual(const ActualIterable& actual,
81 std::initializer_list<T> expected) {
82 ExpectElementsEqual<ActualIterable, std::initializer_list<T>>(actual,
83 expected);
84 }
85
86 template <typename ActualIterable,
87 typename T = typename ActualIterable::iterator::value_type>
ExpectElementsAre(const ActualIterable & actual,T value)88 void ExpectElementsAre(const ActualIterable& actual, T value) {
89 auto actual_iter = actual.begin();
90 for (; actual_iter != actual.end(); ++actual_iter) {
91 ASSERT_NE(actual_iter, actual.end());
92 EXPECT_EQ(*actual_iter, value);
93 }
94 }
95
96 } // namespace pw::multibuf::test_utils
97