Pigweed
 
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
29 size_t size = 0;
30 size_t alignment = 1;
31};
32
35 size_t index = 0;
36};
37
40 size_t index = 0;
41 size_t new_size = 0;
42};
43
44using Request =
45 std::variant<AllocationRequest, DeallocationRequest, ReallocationRequest>;
46
49size_t AlignmentFromLShift(size_t lshift, size_t size);
50
73 public:
75 struct Allocation : public IntrusiveList<Allocation>::Item {
76 Layout layout;
77
78 explicit Allocation(Layout layout_) : layout(layout_) {}
79 };
80
81 TestHarness() = default;
82 explicit TestHarness(Allocator& allocator) : allocator_(&allocator) {}
83 virtual ~TestHarness() = default;
84
85 size_t num_allocations() const { return num_allocations_; }
86 size_t allocated() const { return allocated_; }
87
88 void set_allocator(Allocator* allocator) { allocator_ = allocator; }
89 void set_prng_seed(uint64_t seed) { prng_ = random::XorShiftStarRng64(seed); }
90 void set_available(size_t available) { available_ = available; }
91
97 void GenerateRequests(size_t max_size, size_t num_requests);
98
104 void GenerateRequest(size_t max_size);
105
110 void HandleRequests(const Vector<Request>& requests);
111
134 bool HandleRequest(const Request& request);
135
137 void Reset();
138
139 private:
141 AllocationRequest GenerateAllocationRequest(size_t max_size);
142 DeallocationRequest GenerateDeallocationRequest();
143 ReallocationRequest GenerateReallocationRequest(size_t max_size);
144 size_t GenerateSize(size_t max_size);
145
148 virtual void BeforeAllocate(const Layout&) {}
149 virtual void AfterAllocate(const void*) {}
150 virtual void BeforeReallocate(const Layout&) {}
151 virtual void AfterReallocate(const void*) {}
152 virtual void BeforeDeallocate(const void*) {}
153 virtual void AfterDeallocate() {}
154
164 void AddAllocation(void* ptr, Layout layout);
165
169 Allocation* RemoveAllocation(size_t index);
170
172 Allocator* allocator_ = nullptr;
173
175 IntrusiveList<Allocation> allocations_;
176
178 size_t num_allocations_ = 0;
179
181 size_t allocated_ = 0;
182
186 std::optional<size_t> available_;
187
189 std::optional<random::XorShiftStarRng64> prng_;
190
193 std::optional<size_t> max_size_;
194};
195
196} // namespace pw::allocator::test
Definition: allocator.h:34
Definition: vector.h:65
Definition: layout.h:56
Definition: test_harness.h:72
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:148
void GenerateRequest(size_t max_size)
Definition: intrusive_list.h:82
Represents a request to allocate some memory.
Definition: test_harness.h:28
Represents a request to free some allocated memory.
Definition: test_harness.h:34
Represents a request to reallocate allocated memory with a new size.
Definition: test_harness.h:39
Associates a pointer to memory with the Layout used to allocate it.
Definition: test_harness.h:75