Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
customization.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_TESTING_CUSTOMIZATION_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_TESTING_CUSTOMIZATION_HPP_
7
12#include "arene/base/detail/iterator_interface.hpp"
13#include "arene/base/stdlib_choice/cstddef.hpp"
14#include "arene/base/stdlib_choice/enable_if.hpp"
15#include "arene/base/stdlib_choice/initializer_list.hpp"
16#include "arene/base/stdlib_choice/integral_constant.hpp" // IWYU pragma: keep
17#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
18#include "arene/base/stdlib_choice/is_floating_point.hpp"
19#include "arene/base/stdlib_choice/is_integral.hpp"
20#include "arene/base/stdlib_choice/is_move_assignable.hpp"
21#include "arene/base/stdlib_choice/is_move_constructible.hpp"
22#include "arene/base/stdlib_choice/is_signed.hpp"
23#include "arene/base/stdlib_choice/is_unsigned.hpp"
24#include "arene/base/stdlib_choice/iterator_tags.hpp"
25#include "arene/base/stdlib_choice/make_signed.hpp"
26#include "arene/base/stdlib_choice/move.hpp"
27#include "testlibs/utilities/configurable_value.hpp"
28
29namespace arene {
30namespace base {
31namespace testing {
32
33//////////////////////////////////////////////////////////////////////
34// Customization points for generating values of user-defined types //
35//////////////////////////////////////////////////////////////////////
36
37/// @brief An array used in the default implementation of @c ::test_value ; by default it's empty and unusable
38/// @note This must be explicitly specialized to be usable; specializations can use different array sizes
39/// @tparam T The type of which this is an array of test values
40template <typename T, typename = ::arene::base::constraints<>>
41extern constexpr ::arene::base::array<T, 0UL> test_value_array{};
42
43/// @brief A partial specialization of @c test_value_array for built-in unsigned integers to make it easier to use
44/// @tparam T The type of which this is an array of test values
45template <typename T>
46extern constexpr auto test_value_array<
47 T,
48 ::arene::base::
49 constraints<std::enable_if_t<std::is_integral<T>::value>, std::enable_if_t<std::is_unsigned<T>::value>>> =
50 to_array_of<T>({0U, 8U, 19U, 6612U, 777124U, 7U, 609124U, 1224545862U, 2561U, 976173U});
51
52/// @brief A partial specialization of @c test_value_array for built-in signed integers to make it easier to use
53/// @tparam T The type of which this is an array of test values
54template <typename T>
55extern constexpr auto test_value_array<
56 T,
57 ::arene::base::
58 constraints<std::enable_if_t<std::is_integral<T>::value>, std::enable_if_t<std::is_signed<T>::value>>> =
59 to_array_of<T>({1, -2, 6, -24, 120, -720, 5'040, -40'320, 362'880, -3'628'800});
60
61/// @brief A partial specialization of @c test_value_array for built-in floating point numbers to make it easier to use
62/// @tparam T The type of which this is an array of test values
63template <typename T>
64extern constexpr auto
66 to_array_of<T>({0.0, -1.0, 3.14, 2.72, 1.41, 0.577});
67
68namespace customization_detail {
69/// @brief A class to hold the default generation function so that it can be partially specialized for our test classes
70/// @tparam T The type to generate a test value of
71template <typename T>
72struct test_value_generator {
73 /// @brief The default implementation of <tt>test_value</tt>, returning a value copied from @c test_value_array
74 /// @param idx The index of the test value to be generated
75 static constexpr auto test_value(std::size_t idx) noexcept(std::is_nothrow_copy_constructible<T>::value) -> T {
76 static_assert(
77 !test_value_array<T>.empty(),
78 "To use a type T for parameterized testing, you must specialize arene::base::testing::test_value_array<T> for "
79 "copyable types or arene::base::testing::test_value<T> for non-copyable types"
80 );
81 return test_value_array<T>[idx % test_value_array<T>.size()];
82 }
83};
84
85/// @brief A partial specialization of @c test_value for @c configurable_value
86/// @tparam T The underlying value type held by the @c configurable_value
87/// @tparam ThrowsOn A specification of which operations should throw
88/// @tparam Disable A specification of which operations to disable entirely
89/// @tparam IsConstexpr A specification of whether or not the @c configurable_value should be usable in @c constexpr
90template <typename T, ::testing::throws_on ThrowsOn, ::testing::disable Disable, ::testing::is_constexpr IsConstexpr>
91struct test_value_generator<::testing::configurable_value<T, ThrowsOn, Disable, IsConstexpr>> {
92 /// @brief An implementation of @c test_value for @c configurable_value which generates based on the value type
93 /// @param idx The index of the test value to be generated
94 static constexpr auto test_value(std::size_t idx
95 ) noexcept((ThrowsOn & ::testing::throws_on::value_construct) == ::testing::throws_on::nothing) -> T {
96 return {test_value_generator<T>::test_value(idx)};
97 }
98};
99} // namespace customization_detail
100
101/// @brief Get the @c idx 'th test value of type @c T
102/// @note The default implementation copies the @c idx 'th element of @c test_value_array , wrapping to stay in bounds
103/// @note For non-copyable types, this function can be specialized to construct the test value without copying
104/// @tparam T The type to return test values of
105/// @param idx The index of the test value to be generated
106/// @return A test value of type @c T ; test values with different @c idx may be different from each other, but may not
107template <typename T>
108constexpr auto test_value(std::size_t idx
109) noexcept(noexcept(customization_detail::test_value_generator<T>::test_value(idx))) -> T {
110 return customization_detail::test_value_generator<T>::test_value(idx);
111}
112
113/// @brief Check whether or not a given type's @c test_value function works in @c constexpr contexts
114/// @tparam T The type to check
115template <typename T, typename = std::integral_constant<bool, true>>
116constexpr bool has_constexpr_test_value = false;
117
118/// @brief Check whether or not a given type's @c test_value function works in @c constexpr contexts
119/// @tparam T The type to check
120// This uses the comma operator to instantiate a test_value<T> and then discard it; if it compiles the trait is true.
121template <typename T>
123
124namespace customization_detail {
125
126/// @brief A function object to provide a @c std::initializer_list implemented differently for @c constexpr and runtime
127/// @tparam T The type for which an initializer list is requested
128/// @tparam IsConstexpr Whether or not the provided initializer list should be declared @c constexpr
129/// @tparam Indices A pack of indices, one for each test value in the initializer list
130template <typename T, bool IsConstexpr, std::size_t... Indices>
131class init_list_holder {
132 /// @brief @c constexpr functions can not have static variables until C++23 so this has to be outside the function
133 static constexpr std::initializer_list<T> list{test_value<T>(Indices)...};
134
135 public:
136 /// @brief Call the function object, returning a reference to the statically-allocated @c std::initializer_list
137 /// @return A copy of a statically-allocated @c std::initializer_list<T> containing one test value per index
138 constexpr auto operator()() const noexcept -> std::initializer_list<T> { return list; }
139};
140
141// This static variable definition is required until C++17.
142template <typename T, bool IsConstexpr, std::size_t... Indices>
143constexpr std::initializer_list<T> init_list_holder<T, IsConstexpr, Indices...>::list;
144
145/// @brief A function object to provide a @c std::initializer_list implemented differently for @c constexpr and runtime
146/// @tparam T The type for which an initializer list is requested
147/// @tparam IsConstexpr Whether or not the provided initializer list should be declared @c constexpr
148/// @tparam Indices A pack of indices, one for each test value in the initializer list
149template <typename T, std::size_t... Indices>
150class init_list_holder<T, false, Indices...> {
151 public:
152 /// @brief Call the function object, returning a reference to the statically-allocated @c std::initializer_list
153 /// @return A copy of a statically-allocated @c std::initializer_list<T> containing one test value per index
154 auto operator()() const noexcept -> std::initializer_list<T> {
155 // For non-constexpr T, this needs to be *inside* the function to ensure it's initialized after test_value_array
156 static std::initializer_list<T> const list{test_value<T>(Indices)...};
157 return list;
158 }
159};
160
161} // namespace customization_detail
162
163/// @brief A function object returning a @c std::initializer_list of one @c test_value per index in the given pack
164/// @tparam T The type for which an initializer list is requested
165/// @tparam Indices A pack of indices, one for each test value in the initializer list
166/// @return A copy of a statically-allocated @c std::initializer_list<T> containing one test value per index
167template <typename T, std::size_t... Indices>
168extern constexpr customization_detail::init_list_holder<T, has_constexpr_test_value<T>, Indices...>
170
171/// @brief An iterator whose i'th position yields an rvalue reference to @c test_value(i) of the given type
172/// @tparam T The type to yield
173template <typename T>
174class rvalue_iterator : public detail::iterator_interface<rvalue_iterator<T>> {
175 /// @brief A type trait indicating whether or not retrieving a @c test_value<T> can throw an exception
176 static constexpr bool test_value_is_noexcept = noexcept(test_value<T>(0));
177
178 /// @brief Alias for the iterator mixin, used to initialize it in various constructors
179 using iterator_interface = detail::iterator_interface<rvalue_iterator<T>>;
180
181 public:
182 /// @brief The value type of the iterator; this is the type whose test values are being retrieved
183 using value_type = T;
184 /// @brief The pointer type of the iterator; basically not meaningful, and it would be ill-formed to use it
185 using pointer = T*;
186 /// @brief The reference type of the iterator; a value of the expected type, since there's nothing to rvalue-refer to
187 using reference = T&&;
188 /// @brief The difference type of the iterator; test_value takes @c std::size_t so the difference is that but signed
190 /// @brief The iterator category; random access because we just store an index and yield from it
192
193 /// @brief Construct an rvalue_iterator containing the test value with the given index (defaulting to 0)
194 /// @param index Index to retrieve the test value with
195 /// @pre <c>index >= 0</c>, otherwise ARENE_PRECONDITION violation
196 explicit constexpr rvalue_iterator(difference_type index = 0) noexcept(test_value_is_noexcept)
197 : iterator_interface(),
198 current_index_(index),
199 current_value_(get_test_value(current_index_)) {}
200
201 /// @brief Copy construct an rvalue_iterator, retrieving a new test value so that the value type can be move-only
202 /// @param other Iterator to copy from
203 constexpr rvalue_iterator(rvalue_iterator const& other) noexcept(test_value_is_noexcept)
204 : iterator_interface(),
205 current_index_(other.current_index_),
206 current_value_(get_test_value(current_index_)) {}
207
208 /// @brief Move construct an rvalue_iterator
209 /// @param other Iterator to move from
210 // NOLINTNEXTLINE(hicpp-noexcept-move) noexcept-specification accurately reflects that of the underlying type
212 : iterator_interface(),
213 current_index_(other.current_index_),
214 current_value_(std::move(other.current_value_)) {}
215
216 /// @brief Copy assign an rvalue_iterator, retrieving a new test value so that the value type can be move-only
217 /// @param other Iterator to copy from
218 constexpr auto operator=(rvalue_iterator const& other) noexcept(test_value_is_noexcept) -> rvalue_iterator& {
219 current_index_ = other.current_index_;
220 current_value_ = get_test_value(current_index_);
221 }
222
223 /// @brief Move assign an rvalue_iterator
224 /// @param other Iterator to move from
225 // NOLINTNEXTLINE(hicpp-noexcept-move) noexcept-specification accurately reflects that of the underlying type
227 -> rvalue_iterator& {
228 current_index_ = other.current_index_;
229 current_value_ = std::move(other.current_value_);
230 }
231
232 /// @brief Destroy an rvalue_iterator
233 ~rvalue_iterator() = default;
234
235 /// @brief Dereference an rvalue_iterator; data may no longer be meaningful after the first use
236 /// @return An rvalue reference to the current test value
237 constexpr auto operator*() noexcept -> reference { return std::move(current_value_); }
238
239 /// @brief Shift an rvalue_iterator by the given amount
240 /// @param diff Amount by which to shift the iterator
241 /// @return A reference to @c *this
242 /// @pre <c>current_index_ + diff >= 0</c>, otherwise ARENE_PRECONDITION violation
243 constexpr auto operator+=(difference_type diff) noexcept(test_value_is_noexcept) -> rvalue_iterator& {
244 current_index_ += diff;
245 current_value_ = get_test_value(current_index_);
246 return *this;
247 }
248
249 /// @brief Get the difference between two rvalue_iterators
250 /// @param left Left side of the difference
251 /// @param right Right side of the difference
252 /// @return The difference between the operands' respective current indices
253 friend constexpr auto operator-(rvalue_iterator const& left, rvalue_iterator const& right) noexcept
254 -> difference_type {
255 return left.current_index_ - right.current_index_;
256 }
257
258 private:
259 /// @brief Current index whose @c test_value is yielded upon dereferencing
260 difference_type current_index_;
261 /// @brief Current value which will be yielded upon dereferencing; needs to be saved so we can get an rvalue ref to it
262 T current_value_;
263
264 /// @brief Get the @c test_value<T> with the given index, checking to make sure the index hasn't become negative
265 /// @param index Index to retrieve the test value with
266 /// @return @c test_value<T>(index)
267 /// @pre <c>index >= 0</c>, otherwise ARENE_PRECONDITION violation
268 static constexpr auto get_test_value(difference_type index) noexcept(test_value_is_noexcept) -> T {
269 ARENE_PRECONDITION(index >= 0);
270 return test_value<T>(static_cast<std::size_t>(index));
271 }
272};
273
274} // namespace testing
275} // namespace base
276} // namespace arene
277
278#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_TESTING_CUSTOMIZATION_HPP_
constexpr auto operator+=(difference_type diff) noexcept(test_value_is_noexcept) -> rvalue_iterator &
Shift an rvalue_iterator by the given amount.
Definition customization.hpp:243
T && reference
The reference type of the iterator; a value of the expected type, since there's nothing to rvalue-ref...
Definition customization.hpp:187
std::random_access_iterator_tag iterator_category
The iterator category; random access because we just store an index and yield from it.
Definition customization.hpp:191
constexpr auto operator=(rvalue_iterator const &other) noexcept(test_value_is_noexcept) -> rvalue_iterator &
Copy assign an rvalue_iterator, retrieving a new test value so that the value type can be move-only.
Definition customization.hpp:218
friend constexpr auto operator-(rvalue_iterator const &left, rvalue_iterator const &right) noexcept -> difference_type
Get the difference between two rvalue_iterators.
Definition customization.hpp:253
~rvalue_iterator()=default
Destroy an rvalue_iterator.
T * pointer
The pointer type of the iterator; basically not meaningful, and it would be ill-formed to use it.
Definition customization.hpp:185
std::make_signed_t< std::size_t > difference_type
The difference type of the iterator; test_value takes std::size_t so the difference is that but signe...
Definition customization.hpp:189
constexpr auto operator*() noexcept -> reference
Dereference an rvalue_iterator; data may no longer be meaningful after the first use.
Definition customization.hpp:237
T value_type
The value type of the iterator; this is the type whose test values are being retrieved.
Definition customization.hpp:183
constexpr rvalue_iterator(rvalue_iterator &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
Move construct an rvalue_iterator.
Definition customization.hpp:211
constexpr rvalue_iterator(rvalue_iterator const &other) noexcept(test_value_is_noexcept)
Copy construct an rvalue_iterator, retrieving a new test value so that the value type can be move-onl...
Definition customization.hpp:203
constexpr auto operator=(rvalue_iterator &&other) noexcept(std::is_nothrow_move_assignable< T >::value) -> rvalue_iterator &
Move assign an rvalue_iterator.
Definition customization.hpp:226
constexpr rvalue_iterator(difference_type index=0) noexcept(test_value_is_noexcept)
Construct an rvalue_iterator containing the test value with the given index (defaulting to 0)
Definition customization.hpp:196
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
static constexpr value_type value
Definition integral_constant.hpp:34
An empty tag type corresponding to a random access iterator.
Definition iterator_tags.hpp:29
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition customization.hpp:31
constexpr bool has_constexpr_test_value
Check whether or not a given type's test_value function works in constexpr contexts.
Definition customization.hpp:116
constexpr customization_detail::init_list_holder< T, has_constexpr_test_value< T >, Indices... > test_initializer_list
A function object returning a std::initializer_list of one test_value per index in the given pack.
constexpr auto test_value(std::size_t idx) noexcept(noexcept(customization_detail::test_value_generator< T >::test_value(idx))) -> T
Get the idx 'th test value of type T.
Definition customization.hpp:108
constexpr ::arene::base::array< T, 0UL > test_value_array
An array used in the default implementation of test_value ; by default it's empty and unusable.
Definition accumulate.hpp:37
constexpr auto move(InIterator first, InIterator last, OutIterator output) noexcept(noexcept(*first++) &&noexcept(output++) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InIterator >::value_type >::value) -> OutIterator
Moves the elements in the range [first, last), to another range beginning at output,...
Definition move.hpp:46
constexpr auto to_array_of
construct an array with a desired element type from a C-array.
Definition to_array.hpp:134
Definition accumulate.hpp:36
STL namespace.
typename make_signed< Type >::type make_signed_t
A type alias for the equivalent signed type for integral and enumeration types other than bool.
Definition make_signed.hpp:252
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41