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 <variant>
18
19#include "pw_allocator/allocator.h"
20#include "pw_containers/vector.h"
21#include "pw_random/random.h"
22
23namespace pw::allocator::test {
24
27 size_t size = 0;
28 size_t alignment = 1;
29};
30
33 size_t index = 0;
34};
35
38 size_t index = 0;
39 size_t new_size = 0;
40};
41
42using Request =
43 std::variant<AllocationRequest, DeallocationRequest, ReallocationRequest>;
44
47size_t AlignmentFromLShift(size_t lshift, size_t size);
48
59 public:
64 virtual ~TestHarnessGeneric() = default;
65
72 size_t max_size,
73 size_t num_requests);
74
80 void GenerateRequest(random::RandomGenerator& prng, size_t max_size);
81
86 void HandleRequests(const Vector<Request>& requests);
87
104 void HandleRequest(const Request& request);
105
107 void Reset();
108
109 protected:
111 struct Allocation {
112 void* ptr;
113 Layout layout;
114 };
115
116 constexpr TestHarnessGeneric(Vector<Allocation>& allocations)
117 : allocations_(allocations) {}
118
119 private:
120 virtual Allocator* Init() = 0;
121
131 void AddAllocation(void* ptr, Layout layout);
132
136 Allocation RemoveAllocation(size_t index);
137
139 Allocator* allocator_ = nullptr;
140
142 Vector<Allocation>& allocations_;
143
145 size_t num_requests_ = 0;
146};
147
177template <size_t kMaxConcurrentAllocations>
179 public:
180 constexpr TestHarness() : TestHarnessGeneric(allocations_) {}
181
182 private:
183 Vector<Allocation, kMaxConcurrentAllocations> allocations_;
184};
185
186} // namespace pw::allocator::test
Definition: allocator.h:32
Definition: layout.h:39
Definition: test_harness.h:58
void HandleRequests(const Vector< Request > &requests)
void GenerateRequests(random::RandomGenerator &prng, size_t max_size, size_t num_requests)
void GenerateRequest(random::RandomGenerator &prng, size_t max_size)
void Reset()
Deallocates any pointers stored in the vector of allocated pointers.
void HandleRequest(const Request &request)
Definition: test_harness.h:178
Definition: random.h:34
Represents a request to allocate some memory.
Definition: test_harness.h:26
Represents a request to free some allocated memory.
Definition: test_harness.h:32
Represents a request to reallocate allocated memory with a new size.
Definition: test_harness.h:37
Associates a pointer to memory with the Layout used to allocate it.
Definition: test_harness.h:111