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