Sequoia
Loading...
Searching...
No Matches
NodeStorageTestingUtilities.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2019. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
13
17
19
20namespace sequoia::testing
21{
22 namespace impl
23 {
24 template<class Nodes>
26 {
27 using type = Nodes;
28
29 template<test_mode Mode>
30 static void test(equality_check_t, test_logger<Mode>& logger, const Nodes& nodes, const Nodes& prediction)
31 {
32 check(equality, "Sizes different", logger, nodes.size(), prediction.size());
33 check(with_best_available, "const_node_iter", logger, nodes.cbegin_node_weights(), nodes.cend_node_weights(), prediction.cbegin_node_weights(), prediction.cend_node_weights());
34 }
35 };
36
37 template<class Nodes>
38 requires std::is_empty_v<typename Nodes::weight_type>
39 struct node_value_tester<Nodes>
40 {
41 using type = Nodes;
42
43 template<test_mode Mode>
44 static void test(equality_check_t, test_logger<Mode>&, const Nodes&, const Nodes&)
45 {
46 }
47 };
48
49 template<class Nodes>
51 {
52 using type = Nodes;
53
54 using equivalent_type = std::initializer_list<typename type::weight_type>;
55
56 template<test_mode Mode>
57 static void test(equivalence_check_t, test_logger<Mode>& logger, const Nodes& nodes, equivalent_type prediction)
58 {
59 check(equality, "Sizes different", logger, nodes.size(), prediction.size());
60
61 check(with_best_available, "const_node_iter", logger, nodes.cbegin_node_weights(), nodes.cend_node_weights(), prediction.begin(), prediction.end());
62 check(with_best_available, "const_reverse_node_iter", logger, nodes.crbegin_node_weights(), nodes.crend_node_weights(), std::reverse_iterator(prediction.end()), std::reverse_iterator(prediction.begin()));
63 check(with_best_available, "implicitly const range", logger, nodes.node_weights().begin(), nodes.node_weights().end(), prediction.begin(), prediction.end());
64 check(with_best_available, "explicitly const range", logger, nodes.cnode_weights().begin(), nodes.cnode_weights().end(), prediction.begin(), prediction.end());
65
66 using iterator_type = Nodes::iterator;
67 if constexpr(std::indirectly_writable<iterator_type, std::iter_value_t<iterator_type>>)
68 {
69 auto& n{const_cast<Nodes&>(nodes)};
70 check(with_best_available, "range", logger, n.node_weights().begin(), n.node_weights().end(), prediction.begin(), prediction.end());
71 }
72 }
73 };
74
75 template<class Nodes>
76 requires std::is_empty_v<typename Nodes::weight_type>
78 {
79 using type = Nodes;
80
81 using equivalent_type = std::initializer_list<typename type::weight_type>;
82
83 template<test_mode Mode>
84 static void test(equivalence_check_t, test_logger<Mode>& logger, const Nodes& nodes, equivalent_type)
85 {
86 testing::check("Node storage should have zero size for empty node weights", logger, nodes.empty());
87 }
88 };
89 }
90
91 // Details Checkers
92
93 template<class Weight, class Container>
94 struct value_tester<maths::node_storage<Weight, Container>>
95 : impl::node_value_tester<maths::node_storage<Weight, Container>>
96 , impl::node_equivalence_checker<maths::node_storage<Weight, Container>>
97 {
100 };
101
102 // Static
103
104 template<class Weight, std::size_t N>
105 struct value_tester<maths::static_node_storage<Weight, N>>
106 : impl::node_value_tester<maths::static_node_storage<Weight, N>>
107 , impl::node_equivalence_checker<maths::static_node_storage<Weight, N>>
108 {
111 };
112
113 // Heterogeneous
114
115 template<class... Ts>
117 {
119 using equivalent_type = std::tuple<Ts...>;
120
121 template<test_mode Mode>
122 static void test(equality_check_t, test_logger<Mode>& logger, const type& nodes, const type& prediction)
123 {
124 if(check(equality, "Node storaage sizes different", logger, nodes.size(), prediction.size()))
125 {
126 check_elements(logger, nodes, prediction);
127 }
128 }
129
130 template<test_mode Mode>
131 static void test(equivalence_check_t, test_logger<Mode>& logger, const type& nodes, const equivalent_type& prediction)
132 {
133 if (check(equality, "Node storage sizes different", logger, nodes.size(), sizeof...(Ts)))
134 {
135 check_elements(logger, nodes, prediction);
136 }
137 }
138
139 private:
140 template<test_mode Mode, std::size_t I=0>
141 static void check_elements([[maybe_unused]] test_logger<Mode>& logger,
142 [[maybe_unused]] const type& nodes,
143 [[maybe_unused]] const type& prediction)
144 {
145 if constexpr(I < sizeof...(Ts))
146 {
147 const std::string message{std::to_string(I) + "th element incorrect"};
148 check(equality, message, logger, nodes.template node_weight<I>(), prediction.template get_node_weight<I>());
149 check_elements<Mode, I+1>(logger, nodes, prediction);
150 }
151 }
152
153 template<test_mode Mode, std::size_t I = 0>
154 static void check_elements([[maybe_unused]] test_logger<Mode>& logger,
155 [[maybe_unused]] const type& nodes,
156 [[maybe_unused]] const equivalent_type& prediction)
157 {
158 if constexpr (I < sizeof...(Ts))
159 {
160 const auto message{ std::to_string(I).append("th element incorrect") };
161 check(equality, message, logger, nodes.template get_node_weight<I>(), std::get<I>(prediction));
162 check_elements<Mode, I + 1>(logger, nodes, prediction);
163 }
164 }
165 };
166
167 template<class Weight, bool PropagateCopy=true, bool PropagateMove=true, bool PropagateSwap=true>
169 : public maths::node_storage_base<Weight, std::vector<Weight, shared_counting_allocator<Weight, PropagateCopy, PropagateMove, PropagateSwap>>>
170 {
171 private:
173 public:
174 using allocator_type = typename base_t::node_weight_container_type::allocator_type;
175 using size_type = typename base_t::size_type;
176 using weight_type = typename base_t::weight_type;
177
178 node_storage_tester() = default;
179
180 explicit node_storage_tester(const allocator_type& allocator)
181 : base_t(allocator)
182 {}
183
184 explicit node_storage_tester(const size_type n)
185 : base_t(n)
186 {}
187
188 node_storage_tester(const size_type n, const allocator_type& allocator)
189 : base_t(n, allocator)
190 {}
191
192 node_storage_tester(std::initializer_list<weight_type> weights)
193 : base_t{weights}
194 {}
195
196 node_storage_tester(std::initializer_list<weight_type> weights, const allocator_type& allocator)
197 : base_t{weights, allocator}
198 {}
199
201
202 node_storage_tester(const node_storage_tester& s, const allocator_type& allocator)
203 : base_t{s, allocator}
204 {}
205
206 node_storage_tester(node_storage_tester&&) noexcept = default;
207
208 node_storage_tester(node_storage_tester&& s, const allocator_type& allocator)
209 : base_t{std::move(s), allocator}
210 {}
211
212 ~node_storage_tester() = default;
213
214 node_storage_tester& operator=(const node_storage_tester&) = default;
215
216 node_storage_tester& operator=(node_storage_tester&&) = default;
217
218 friend void swap(node_storage_tester& lhs, node_storage_tester& rhs)
219 {
220 lhs.swap(rhs);
221 }
222
223 using base_t::reserve;
224 using base_t::capacity;
225 using base_t::shrink_to_fit;
226 using base_t::add_node;
227 using base_t::insert_node;
228 using base_t::erase_node;
229 using base_t::erase_nodes;
230 using base_t::clear;
231 using base_t::get_node_allocator;
232 };
233
234 template<class Weight, std::size_t N>
236 {
237 public:
238 using maths::static_node_storage<Weight, N>::static_node_storage;
239 };
240
241
242 template<class Weight, bool PropagateCopy, bool PropagateMove, bool PropagateSwap>
243 struct value_tester<node_storage_tester<Weight, PropagateCopy, PropagateMove, PropagateSwap>>
244 : impl::node_value_tester<node_storage_tester<Weight, PropagateCopy, PropagateMove, PropagateSwap>>
245 , impl::node_equivalence_checker<node_storage_tester<Weight, PropagateCopy, PropagateMove, PropagateSwap>>
246 {
249 };
250
251
252
253 template<class Weight, std::size_t N>
255 : public value_tester<maths::static_node_storage<Weight, N>>
256 {};
257}
Utilities for allocation testing.
bool check(CheckType flavour, std::string description, test_logger< Mode > &logger, Iter first, Sentinel last, PredictionIter predictionFirst, PredictionSentinel predictionLast, tutor< Advisor > advisor={})
The workhorse for comparing the contents of ranges.
Definition: FreeCheckers.hpp:377
Node storage for graphs with heterogeneous node weights.
Classes to allow homogeneous treatment of graphs with empty/non-empty node weights.
Utilities for checking regular semantics.
Classes for node storage that may be used in a constexpr context.
Definition: HeterogeneousNodeStorage.hpp:27
Definition: NodeStorage.hpp:37
Definition: NodeStorage.hpp:272
Definition: StaticNodeStorage.hpp:26
Definition: NodeStorageTestingUtilities.hpp:170
Definition: NodeStorageTestingUtilities.hpp:236
Definition: TestLogger.hpp:183
Definition: FreeCheckers.hpp:82
Definition: FreeCheckers.hpp:87
Definition: NodeStorageTestingUtilities.hpp:51
Definition: NodeStorageTestingUtilities.hpp:26
class template, specializations of which implement various comparisons for the specified type.
Definition: FreeCheckers.hpp:78