11 #define BOOST_TEST_MODULE ( MappedContainer_test ) 12 #include <boost/test/unit_test.hpp> 24 #include <type_traits> 34 static constexpr std::size_t
Dim = 10U;
42 {{ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }};
44 template <
typename Cont>
56 template <
typename T, std::
size_t N>
79 template <
typename Cont>
84 template <
typename Cont>
87 using Container_t = Cont;
89 using Data_t = Literal_t;
91 constexpr Literal_t defaultValue { 42 };
95 auto const data = makeTestData<Cont>();
97 static_assert(std::is_copy_assignable_v<std::decay_t<decltype(
data)>>);
98 static_assert(std::is_copy_constructible_v<std::decay_t<decltype(
data)>>);
99 static_assert(std::is_move_assignable_v<std::decay_t<decltype(
data)>>);
100 static_assert(std::is_move_constructible_v<std::decay_t<decltype(
data)>>);
104 std::array<std::size_t, 12U>
const mapping1 = {{
109 std::array<Data_t, 12U>
const expectedMappedData1 = {{
110 -4, -3, -2, -1, 0, defaultValue,
111 -9, -8, -7, -6, -5, defaultValue
115 (
data, mapping1, expectedMappedData1.size(), defaultValue);
117 BOOST_TEST(mappedData1.size() == expectedMappedData1.size());
118 BOOST_TEST(mappedData1.minimal_size() == expectedMappedData1.size());
120 BOOST_TEST(mappedData1.front() == expectedMappedData1.front());
121 BOOST_TEST(mappedData1.back() == expectedMappedData1.back());
124 auto const beginIterator = mappedData1.
begin();
125 auto const secondIterator = beginIterator + 1;
126 auto mappedIterator = beginIterator;
127 for (
auto&& mappedValue: mappedData1) {
128 BOOST_TEST_CHECKPOINT(
"mapped item: " << i);
130 Data_t
const expectedValue = expectedMappedData1[i];
131 Data_t
const& expectedRef = (mapping1[i] ==
InvalidIndex)
132 ? mappedData1.defaultValue()
135 BOOST_TEST(expectedRef == expectedValue);
137 BOOST_TEST(mappedValue == expectedMappedData1[i]);
138 BOOST_TEST(mappedData1[i] == expectedMappedData1[i]);
139 BOOST_TEST(mappedData1.at(i) == expectedMappedData1[i]);
140 BOOST_TEST(beginIterator[i] == expectedMappedData1[i]);
141 BOOST_TEST(*(beginIterator + i) == expectedMappedData1[i]);
142 BOOST_TEST(*mappedIterator == expectedMappedData1[i]);
144 BOOST_TEST(secondIterator[i-1] == expectedMappedData1[i]);
145 BOOST_TEST(*(secondIterator + i - 1) == expectedMappedData1[i]);
146 BOOST_TEST( (mappedIterator != beginIterator));
147 BOOST_TEST(!(mappedIterator == beginIterator));
148 BOOST_TEST( (mappedIterator > beginIterator));
149 BOOST_TEST( (mappedIterator >= beginIterator));
150 BOOST_TEST(!(mappedIterator < beginIterator));
151 BOOST_TEST(!(mappedIterator <= beginIterator));
152 BOOST_TEST( (beginIterator != mappedIterator));
153 BOOST_TEST(!(beginIterator == mappedIterator));
154 BOOST_TEST(!(beginIterator > mappedIterator));
155 BOOST_TEST(!(beginIterator >= mappedIterator));
156 BOOST_TEST( (beginIterator < mappedIterator));
157 BOOST_TEST( (beginIterator <= mappedIterator));
160 BOOST_TEST(!(mappedIterator != beginIterator));
161 BOOST_TEST( (mappedIterator == beginIterator));
162 BOOST_TEST(!(mappedIterator > beginIterator));
163 BOOST_TEST( (mappedIterator >= beginIterator));
164 BOOST_TEST(!(mappedIterator < beginIterator));
165 BOOST_TEST( (mappedIterator <= beginIterator));
166 BOOST_TEST(!(beginIterator != mappedIterator));
167 BOOST_TEST( (beginIterator == mappedIterator));
168 BOOST_TEST(!(beginIterator > mappedIterator));
169 BOOST_TEST( (beginIterator >= mappedIterator));
170 BOOST_TEST(!(beginIterator < mappedIterator));
171 BOOST_TEST( (beginIterator <= mappedIterator));
174 BOOST_TEST(&mappedData1[i] != &expectedMappedData1[i]);
178 BOOST_TEST(i == expectedMappedData1.size());
181 while (i < expectedMappedData1.size() + 3U) {
182 BOOST_TEST_CHECKPOINT(
"mapped item: " << i <<
" (overflow)");
183 BOOST_CHECK_THROW(mappedData1.at(i), std::out_of_range);
192 template <
typename Cont>
195 using Container_t = Cont;
197 using Data_t = Literal_t;
199 constexpr Literal_t defaultValue { 42 };
203 auto const data = makeTestData<Cont>();
207 std::array<std::size_t, 12U>
const mapping1 = {{
212 std::array<Literal_t, 12U>
const expectedMappedData1 = {{
213 -4, -3, -2, -1, 0, defaultValue,
214 -9, -8, -7, -6, -5, defaultValue
218 std::ref(
data), mapping1, expectedMappedData1.size(),
227 typename decltype(mappedData1)::const_iterator::reference,
232 BOOST_TEST(mappedData1.size() == expectedMappedData1.size());
233 BOOST_TEST(mappedData1.minimal_size() == expectedMappedData1.size());
235 BOOST_TEST(mappedData1.front() == expectedMappedData1.front());
236 BOOST_TEST(mappedData1.back() == expectedMappedData1.back());
239 auto const beginIterator = mappedData1.begin();
240 auto const secondIterator = beginIterator + 1;
241 auto mappedIterator = beginIterator;
242 for (
auto&& mappedValue: mappedData1) {
243 BOOST_TEST_CHECKPOINT(
"mapped item: " << i);
245 std::size_t
const& expectedMappedIndex = mapping1[i];
246 Data_t
const expectedValue = expectedMappedData1[i];
247 Data_t
const& expectedRef = (expectedMappedIndex ==
InvalidIndex)
248 ? mappedData1.defaultValue()
249 :
data[expectedMappedIndex]
251 BOOST_TEST(expectedRef == expectedValue);
253 decltype(
auto) mappedDataRef = mappedData1[i];
254 static_assert(std::is_reference_v<decltype(mappedDataRef)>);
256 BOOST_TEST(mappedData1.map_index(i) == expectedMappedIndex);
257 BOOST_TEST(mappedValue == expectedValue);
258 BOOST_TEST(mappedDataRef == expectedValue);
259 BOOST_TEST(mappedData1.at(i) == expectedValue);
260 BOOST_TEST(beginIterator[i] == expectedValue);
261 BOOST_TEST(*(beginIterator + i) == expectedValue);
262 BOOST_TEST(*mappedIterator == expectedValue);
264 BOOST_TEST(secondIterator[i-1] == expectedValue);
265 BOOST_TEST(*(secondIterator + i - 1) == expectedValue);
266 BOOST_TEST( (mappedIterator != beginIterator));
267 BOOST_TEST(!(mappedIterator == beginIterator));
268 BOOST_TEST( (mappedIterator > beginIterator));
269 BOOST_TEST( (mappedIterator >= beginIterator));
270 BOOST_TEST(!(mappedIterator < beginIterator));
271 BOOST_TEST(!(mappedIterator <= beginIterator));
272 BOOST_TEST( (beginIterator != mappedIterator));
273 BOOST_TEST(!(beginIterator == mappedIterator));
274 BOOST_TEST(!(beginIterator > mappedIterator));
275 BOOST_TEST(!(beginIterator >= mappedIterator));
276 BOOST_TEST( (beginIterator < mappedIterator));
277 BOOST_TEST( (beginIterator <= mappedIterator));
280 BOOST_TEST(!(mappedIterator != beginIterator));
281 BOOST_TEST( (mappedIterator == beginIterator));
282 BOOST_TEST(!(mappedIterator > beginIterator));
283 BOOST_TEST( (mappedIterator >= beginIterator));
284 BOOST_TEST(!(mappedIterator < beginIterator));
285 BOOST_TEST( (mappedIterator <= beginIterator));
286 BOOST_TEST(!(beginIterator != mappedIterator));
287 BOOST_TEST( (beginIterator == mappedIterator));
288 BOOST_TEST(!(beginIterator > mappedIterator));
289 BOOST_TEST( (beginIterator >= mappedIterator));
290 BOOST_TEST(!(beginIterator < mappedIterator));
291 BOOST_TEST( (beginIterator <= mappedIterator));
297 BOOST_TEST(&mappedDataRef == &expectedRef);
301 if (mapping1[i] != InvalidIndex)
302 BOOST_TEST(mappedData2[i] == mappedData1[i]);
303 BOOST_TEST(&mappedData2.map_index(i) == &expectedMappedIndex);
308 BOOST_TEST(i == expectedMappedData1.size());
311 while (i < expectedMappedData1.size() + 3U) {
312 BOOST_TEST_CHECKPOINT(
"mapped item: " << i <<
" (overflow)");
313 BOOST_CHECK_THROW(mappedData1.at(i), std::out_of_range);
323 using Mapping_t = std::array<std::size_t, 6U>;
344 std::array<double, 4U>
const data {{ 0.0, -1.0, -2.0, -3.0 }};
346 std::array<std::size_t, 6U>
const mapping = {{
353 BOOST_TEST(mappedData.
size() == mapping.size());
384 std::array<double, 4U>
const data {{ 0.0, -1.0, -2.0, -3.0 }};
386 std::array<std::size_t, 6U>
const mapping = {{
392 (std::ref(
data), mapping, 6U, std::numeric_limits<double>::quiet_NaN());
394 for (std::size_t i = 0; i < 6U; ++i) {
396 <<
"Mapped element #" << i <<
": " << mappedData[i] <<
std::endl;
411 copyTest<std::array<double, 10U>>();
412 referenceTest<std::array<double, 10U>>();
413 referenceTest<std::unique_ptr<double[]>>();
Provides MappedContainer class.
static Container_t make()
A meta-container providing transparent mapping on top of another.
static Container_t make()
static constexpr std::size_t sourceSize()
BOOST_AUTO_TEST_CASE(TestCase)
size_type size() const
Returns the nominal size of the container (after mapping).
std::unique_ptr< T[]> Container_t
const_iterator begin() const
Returns a constant iterator to the first mapped element.
static constexpr T invalidIndex()
static std::array< int, Dim > const sourceValues
static constexpr std::size_t Dim
auto array(Array const &a)
Returns a manipulator which will print the specified array.
std::array< T, N > Container_t
static Container_t make()
reference defaultValue()
Returns the default value for elements with no original content.
void defaultConstructorTest()
QTextStream & endl(QTextStream &s)