C/C++ API Reference
Loading...
Searching...
No Matches
test_harness.h
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#pragma once
15
16#include <cstddef>
17#include <optional>
18#include <variant>
19
20#include "pw_allocator/allocator.h"
21#include "pw_containers/intrusive_list.h"
22#include "pw_containers/vector.h"
23#include "pw_random/xor_shift.h"
24
25namespace pw::allocator::test {
26
28
31 size_t size = 0;
32 size_t alignment = 1;
33};
34
37 size_t index = 0;
38};
39
42 size_t index = 0;
43 size_t new_size = 0;
44};
45
46using Request =
47 std::variant<AllocationRequest, DeallocationRequest, ReallocationRequest>;
48
51size_t AlignmentFromLShift(size_t lshift, size_t size);
52
75 public:
77 struct Allocation : public IntrusiveList<Allocation>::Item {
78 Layout layout;
79
80 explicit Allocation(Layout layout_) : layout(layout_) {}
81 };
82
83 TestHarness() = default;
84 explicit TestHarness(Allocator& allocator) : allocator_(&allocator) {}
85 virtual ~TestHarness() = default;
86
87 size_t num_allocations() const { return num_allocations_; }
88 size_t allocated() const { return allocated_; }
89
90 void set_allocator(Allocator* allocator) { allocator_ = allocator; }
91 void set_prng_seed(uint64_t seed) { prng_ = random::XorShiftStarRng64(seed); }
92 void set_available(size_t available) { available_ = available; }
93
99 void GenerateRequests(size_t max_size, size_t num_requests);
100
106 void GenerateRequest(size_t max_size);
107
112 void HandleRequests(const Vector<Request>& requests);
113
136 bool HandleRequest(const Request& request);
137
139 void Reset();
140
141 private:
143 AllocationRequest GenerateAllocationRequest(size_t max_size);
144 DeallocationRequest GenerateDeallocationRequest();
145 ReallocationRequest GenerateReallocationRequest(size_t max_size);
146 size_t GenerateSize(size_t max_size);
147
150 virtual void BeforeAllocate(const Layout&) {}
151 virtual void AfterAllocate(const void*) {}
152 virtual void BeforeReallocate(const Layout&) {}
153 virtual void AfterReallocate(const void*) {}
154 virtual void BeforeDeallocate(const void*) {}
155 virtual void AfterDeallocate() {}
156
166 void AddAllocation(void* ptr, Layout layout);
167
171 Allocation* RemoveAllocation(size_t index);
172
174 Allocator* allocator_ = nullptr;
175
177 IntrusiveList<Allocation> allocations_;
178
180 size_t num_allocations_ = 0;
181
183 size_t allocated_ = 0;
184
188 std::optional<size_t> available_;
189
191 std::optional<random::XorShiftStarRng64> prng_;
192
195 std::optional<size_t> max_size_;
196};
197
199
200} // namespace pw::allocator::test
Definition: allocator.h:36
Definition: vector.h:65
Definition: layout.h:58
Definition: test_harness.h:74
bool HandleRequest(const Request &request)
void GenerateRequests(size_t max_size, size_t num_requests)
void Reset()
Deallocates any pointers stored in the vector of allocated pointers.
void HandleRequests(const Vector< Request > &requests)
virtual void BeforeAllocate(const Layout &)
Definition: test_harness.h:150
void GenerateRequest(size_t max_size)
Definition: intrusive_list.h:88
size_t AlignmentFromLShift(size_t lshift, size_t size)
Represents a request to allocate some memory.
Definition: test_harness.h:30
Represents a request to free some allocated memory.
Definition: test_harness.h:36
Represents a request to reallocate allocated memory with a new size.
Definition: test_harness.h:41
Associates a pointer to memory with the Layout used to allocate it.
Definition: test_harness.h:77