1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507 | // *****************************************************************************
/*!
\file tests/unit/Base/TestContainerUtil.cpp
\copyright 2012-2015 J. Bakosi,
2016-2018 Los Alamos National Security, LLC.,
2019-2021 Triad National Security, LLC.,
2022-2024 J. Bakosi
All rights reserved. See the LICENSE file for details.
\brief Unit tests for Base/ContainerUtil.hpp
\details Unit tests for Base/ContainerUtil.hpp
*/
// *****************************************************************************
#include <map>
#include <vector>
#include <string>
#include <unordered_map>
#include <unistd.h>
#include "NoWarning/tut.hpp"
#include "TUTConfig.hpp"
#include "ContainerUtil.hpp"
#include "StatCtr.hpp"
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace tut {
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#elif defined(STRICT_GNUC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-attribute=noreturn"
#endif
//! All tests in group inherited from this base
struct ContainerUtil_common {
// cppcheck-suppress unusedStructMember
double precision = 1.0e-15; // required floating-point precision<--- Unmatched suppression: unusedStructMember
};
//! Test group shortcuts
using ContainerUtil_group =
test_group< ContainerUtil_common, MAX_TESTS_IN_GROUP >;
using ContainerUtil_object = ContainerUtil_group::object;
//! Define test group
static ContainerUtil_group ContainerUtil( "Base/ContainerUtil" );
//! Test definitions for group
//! Test uniquecopy making the elements of a container unique (on copy)
template<> template<>
void ContainerUtil_object::test< 1 >() {
set_test_name( "uniquecopy" );
// std::vector
std::vector< int > v{{ 1, 1, 2, 6, -2, 3, 5, 5 }},
correct{{ -2, 1, 2, 3, 5, 6 }};
auto v1 = tk::uniquecopy( v );
ensure( "make vector (c)unique incorrect", v1 == correct );
// std::string
std::string s( "blahblah" );
auto s1 = tk::uniquecopy( s );
ensure( "make string (c)unique incorrect", s1 == "abhl" );
// std::vector< std::vector< tk::ctr::Term > >
std::vector< std::vector< tk::ctr::Term > > p{ tk::ctr::mean('c',1),
tk::ctr::variance('y',2),
tk::ctr::variance('y',2),
tk::ctr::variance('y',2) };
std::vector< std::vector< tk::ctr::Term > > c{ tk::ctr::mean('c',1),
tk::ctr::variance('y',2) };
auto p1 = tk::uniquecopy( p );
ensure( "make vector<vector<tk::ctr::Term>> (c)unique incorrect", p1 == c );
}
//! Test unique making the elements of a container unique
template<> template<>
void ContainerUtil_object::test< 2 >() {
set_test_name( "unique" );
// std::vector
std::vector< int > v{{ 1, 1, 2, 6, -2, 3, 5, 5 }},
correct{{ -2, 1, 2, 3, 5, 6 }};
tk::unique( v );
ensure( "make vector unique incorrect", v == correct );
// std::string
std::string s( "blahblah" );
tk::unique( s );
ensure( "make string unique incorrect", s == "abhl" );
// std::vector< std::vector< tk::ctr::Term > >
std::vector< std::vector< tk::ctr::Term > > p{ tk::ctr::mean('c',1),
tk::ctr::variance('y',2),
tk::ctr::variance('y',2),
tk::ctr::variance('y',2) };
std::vector< std::vector< tk::ctr::Term > > c{ tk::ctr::mean('c',1),
tk::ctr::variance('y',2) };
tk::unique( p );
ensure( "make vector<vector<tk::ctr::Term>> unique incorrect", c == p );
}
//! Test cref_find returning a const-ref to value found for key in map
template<> template<>
void ContainerUtil_object::test< 3 >() {
set_test_name( "[c]ref_find" );
std::map< int, std::string > m{ {1,"one"}, {2,"two"} };
std::unordered_map< int, std::string > u{ {1,"one"}, {2,"two"} };
const auto one = tk::cref_find(m,1);
ensure_equals( "cref_find incorrect", one, "one" );
auto two = tk::ref_find(u,2);
ensure_equals( "ref_find incorrect", two, "two" );
// test if cref_find throws in DEBUG when cannot find key
// skipped in RELEASE mode, would yield segmentation fault
#ifdef NDEBUG
skip( "in RELEASE mode, would yield segmentation fault" );
#else
try {
auto three = tk::ref_find(u,3);
fail( "should throw exception in DEBUG mode" );
}
catch ( tk::Exception& ) {
// exception thrown in DEBUG mode, test ok
}
#endif
}
//! Test vector extents
template<> template<>
void ContainerUtil_object::test< 4 >() {
set_test_name( "vector extents" );
std::vector< int > v{{ 1, 1, 2, 6, -2, 3, 5, 5 }};
auto e = tk::extents( v );
ensure( "vector extents incorrect", e == std::array< int, 2 >{{ -2, 6 }} );
std::vector< std::size_t > w{{ 1, 1, 2, 6, 2, 3, 5, 5 }};
auto f = tk::extents( w );
ensure( "vector extents incorrect",
f == std::array< std::size_t, 2 >{{ 1, 6 }} );
}
//! Test associative container (value) extents
template<> template<>
void ContainerUtil_object::test< 5 >() {
set_test_name( "[hash]map value extents" );
std::map< int, tk::real > m{ {1,34.2}, {2,12.3}, {4,-3.0} };
auto me = tk::extents( m );
ensure_equals( "map min incorrect", me[0], -3.0, precision );
ensure_equals( "map max incorrect", me[1], 34.2, precision );
std::unordered_map< int, tk::real > u{ {1,34.2}, {2,12.3}, {4,-3.0} };
auto mu = tk::extents( u );
ensure_equals( "hash map min incorrect", mu[0], -3.0, precision );
ensure_equals( "hash map max incorrect", mu[1], 34.2, precision );
}
//! Test operator += adding values of a std::vector to another one
template<> template<>
void ContainerUtil_object::test< 6 >() {
set_test_name( "operator+= std::vector" );
using tk::operator+=;
// add non-empty vector to empty one: copy src to dst (main intended use-case)
std::vector< tk::real > v1, v2{{ 4.0, 9.0, 2.0 }};
v1 += v2;
ensure_equals( "add non-empty vector to empty one, dst size incorrect",
v1.size(), 3UL );
ensure_equals( "add non-empty vector to empty one, src size incorrect",
v2.size(), 3UL );
ensure_equals( "add non-empty vector to empty one, src[0]==dst[0], incorrect",
v1[0], v2[0], precision );
ensure_equals( "add non-empty vector to empty one, src[1]==dst[1], incorrect",
v1[1], v2[1], precision );
ensure_equals( "add non-empty vector to empty one, src[2]==dst[2], incorrect",
v1[2], v2[2], precision );
// add empty vector to non-empty one
std::vector< tk::real > r1{{ 4.0, 9.0, 2.0 }}, r2;
r1 += r2;
ensure_equals( "add empty vector to non-empty one: no change in dst",
r1[0], 4.0, precision );
ensure_equals( "add empty vector to non-empty one: no change in dst",
r1[1], 9.0, precision );
ensure_equals( "add empty vector to non-empty one: no change in dst",
r1[2], 2.0, precision );
// add empty vector to empty one
std::vector< tk::real > q1, q2;
q1 += q2;
//fail( "should throw exception in DEBUG mode" );
ensure( "add empty vector to empty one, no change in src and dst",
q1.size() == q2.size() );
// add non-empty vector to non-empty one with src.size() == dst.size():
// dst += src for all components, leave src unchanged
std::vector< tk::real > s1{{ 4.0, 9.0, 2.0 }}, s2{{ 3.0, -3.0, 1.0 }};
s1 += s2;
ensure_equals( "add non-empty vector to non-empty one, dst size incorrect",
s1.size(), 3UL );
ensure_equals( "add non-empty vector to non-empty one, src size incorrect",
s2.size(), 3UL );
ensure_equals( "add non-empty vector to non-empty one, dst[0], incorrect",
s1[0], 7.0, precision );
ensure_equals( "add non-empty vector to non-empty one, dst[1], incorrect",
s1[1], 6.0, precision );
ensure_equals( "add non-empty vector to non-empty one, dst[2], incorrect",
s1[2], 3.0, precision );
ensure_equals( "add non-empty vector to non-empty one, src[0], incorrect",
s2[0], 3.0, precision );
ensure_equals( "add non-empty vector to non-empty one, src[1], incorrect",
s2[1], -3.0, precision );
ensure_equals( "add non-empty vector to non-empty one, src[2], incorrect",
s2[2], 1.0, precision );
// add non-empty vector to non-empty one with src.size() > dst.size(): grow
// dst to that of src.size() (padding with zeros), dst += src for all
// components, leave src unchanged
std::vector< tk::real > w1{{ 4.0, 9.0 }}, w2{{ 3.0, -3.0, 1.0 }};
w1 += w2;
ensure_equals( "add non-empty vector to non-empty one, dst size incorrect",
w1.size(), 3UL );
ensure_equals( "add non-empty vector to non-empty one, src size incorrect",
w2.size(), 3UL );
ensure_equals( "add non-empty vector to non-empty one, dst[0], incorrect",
w1[0], 7.0, precision );
ensure_equals( "add non-empty vector to non-empty one, dst[1], incorrect",
w1[1], 6.0, precision );
ensure_equals( "add non-empty vector to non-empty one, dst[2], incorrect",
w1[2], 1.0, precision );
ensure_equals( "add non-empty vector to non-empty one, src[0], incorrect",
w2[0], 3.0, precision );
ensure_equals( "add non-empty vector to non-empty one, src[1], incorrect",
w2[1], -3.0, precision );
ensure_equals( "add non-empty vector to non-empty one, src[2], incorrect",
w2[2], 1.0, precision );
// add non-empty vector to non-empty one with src.size() < dst.size(): thrown
// in DEBUG to warn on loosing data
// skipped in RELEASE mode, would yield segmentation fault
#ifndef NDEBUG // exception only thrown in DEBUG mode
try {
std::vector< tk::real > n1{{ 4.0, 9.0, 2.0 }}, n2{{ 3.0, -3.0 }};
n1 += n2;
fail( "should throw exception in DEBUG mode" );
}
catch ( tk::Exception& ) {
// exception thrown in DEBUG mode, test ok
}
#endif
}
//! Test keyEqual()
template<> template<>
void ContainerUtil_object::test< 7 >() {
set_test_name( "keyEqual" );
// Test if keys are equal
std::map< int, tk::real > t1{ {1,4.0}, {2,2.0} }, t2{ {1,4.0}, {2,3.0} };
ensure_equals( "keys are not equal", tk::keyEqual(t1,t2), true );
// Test if keys are unequal
std::map< int, tk::real > q1{ {3,4.0}, {2,2.0} }, q2{ {1,4.0}, {2,3.0} };
ensure_equals( "keys are equal", tk::keyEqual(q1,q2), false );
// test if throws in DEBUG to warn on unequal-size containers
// skipped in RELEASE mode, would yield segmentation fault
#ifndef NDEBUG // exception only thrown in DEBUG mode
try {
std::map< int, tk::real > r1{ {1,4.0}, {2,2.0} }, r2{ {1,4.0} };
tk::keyEqual( r1, r2 );
fail( "should throw exception in DEBUG mode" );
}
catch ( tk::Exception& ) {
// exception thrown in DEBUG mode, test ok
}
#endif
}
//! Test sumsize()
template<> template<>
void ContainerUtil_object::test< 8 >() {
set_test_name( "sumsize" );
// Test sum of the sizes of a vector of vectors
std::vector< std::vector< tk::real > > c{ {3,4.0}, {2,2.0} };
ensure_equals( "sum of sizes incorrect", tk::sumsize(c), 4UL );
}
//! Test destroy()
template<> template<>
void ContainerUtil_object::test< 9 >() {
set_test_name( "destroy" );
// Test destroying a vector of vectors
std::vector< std::vector< tk::real > > c{ {3.2,4.0}, {2.1,2.0} };
tk::destroy( c );
ensure_equals( "destroy yields empty vec of vec", c.empty(), true );
// Test destroying a map of vectors
std::map< int, std::vector< tk::real > > m{ {3,{4.0,5.0}}, {2,{1.0,2.0}} };
tk::destroy( m );
ensure_equals( "destroy yields empty map of vec", m.empty(), true );
}
//! Test numunique()
template<> template<>
void ContainerUtil_object::test< 10 >() {
set_test_name( "numunique" );
// Test a vector of vector of ints
std::vector< std::vector< int > > v{ {3,-4}, {2,3,6} };
ensure_equals(
"number of unique values in container of containers incorrect",
tk::numunique( v ), 4UL );
// Test a set of vector of std::size_ts
std::set< std::vector< std::size_t > > s{ {1,2}, {3,4,5} };
ensure_equals(
"number of unique values in container of containers incorrect",
tk::numunique( s ), 5UL );
// Test a vector of set of std::size_ts
std::vector< std::set< std::size_t > > q{ {1,2}, {1,4,5} };
ensure_equals(
"number of unique values in container of containers incorrect",
tk::numunique( q ), 4UL );
}
//! Test erase_if()
template<> template<>
void ContainerUtil_object::test< 11 >() {
set_test_name( "erase_if" );
std::vector< int > v{ 3,-4,2,3,6 };
tk::erase_if( v, []( int i ){ return i%2; } );
ensure( "erase_if on vector incorrect", v == std::vector<int>{-4,2,6} );
std::vector< int > w{ 3,-4,2,3,6 };
tk::erase_if( w, []( int i ){ return !(i%2); } );
ensure( "erase_if on vector incorrect", w == std::vector<int>{3,3} );
std::map< int, std::vector< std::size_t > > b{ {3,{4,5,6}}, {-2,{3,2,3}} };
std::map< int, std::vector< std::size_t > > correct_result{ {3,{4,5,6}} };
tk::erase_if( b, []( const decltype(b)::value_type& p ){return p.first<0;} );
ensure( "erase_if on map incorrect", b == correct_result );
}
//! Test operator += adding values of a std::vector to another one
template<> template<>
void ContainerUtil_object::test< 12 >() {
set_test_name( "operator+= std::array" );
using tk::operator+=;
// add array to other one, dst += src for all components, leave src unchanged
std::array< tk::real, 3 > s1{{ 4.0, 9.0, 2.0 }}, s2{{ 3.0, -3.0, 1.0 }};
s1 += s2;
ensure_equals( "add array to other one, dst[0], incorrect",
s1[0], 7.0, precision );
ensure_equals( "add array to other one, dst[1], incorrect",
s1[1], 6.0, precision );
ensure_equals( "add array to other one, dst[2], incorrect",
s1[2], 3.0, precision );
ensure_equals( "add array to other one, src[0], incorrect",
s2[0], 3.0, precision );
ensure_equals( "add array to other one, src[1], incorrect",
s2[1], -3.0, precision );
ensure_equals( "add array to other one, src[2], incorrect",
s2[2], 1.0, precision );
}
//! Test operator -= subtracting values of a std::vector from another one
template<> template<>
void ContainerUtil_object::test< 13 >() {
set_test_name( "operator-= std::vector" );
using tk::operator-=;
// subtract non-empty vector from empty one: copy src to dst (main intended
// use-case)
std::vector< tk::real > v1, v2{{ 4.0, 9.0, 2.0 }};
v1 -= v2;
ensure_equals( "subtract non-empty vector from empty one, dst size incorrect",
v1.size(), 3UL );
ensure_equals( "subtract non-empty vector from empty one, src size incorrect",
v2.size(), 3UL );
ensure_equals(
"subtract non-empty vector from empty one, src[0]==dst[0], incorrect",
v1[0], -v2[0], precision );
ensure_equals(
"subtract non-empty vector from empty one, src[1]==dst[1], incorrect",
v1[1], -v2[1], precision );
ensure_equals(
"subtract non-empty vector from empty one, src[2]==dst[2], incorrect",
v1[2], -v2[2], precision );
// subtract empty vector from non-empty one
std::vector< tk::real > r1{{ 4.0, 9.0, 2.0 }}, r2;
r1 -= r2;
ensure_equals( "subtract empty vector to non-empty one: no change in dst",
r1[0], 4.0, precision );
ensure_equals( "subtract empty vector to non-empty one: no change in dst",
r1[1], 9.0, precision );
ensure_equals( "subtract empty vector to non-empty one: no change in dst",
r1[2], 2.0, precision );
// subtract empty vector from empty one
std::vector< tk::real > q1, q2;
q1 -= q2;
ensure( "subtract empty vector from empty one, no change in src and dst",
q1.size() == q2.size() );
// subtract non-empty vector from non-empty one with src.size() == dst.size():
// dst -= src for all components, leave src unchanged
std::vector< tk::real > s1{{ 4.0, 9.0, 2.0 }}, s2{{ 3.0, -3.0, 1.0 }};
s1 -= s2;
ensure_equals(
"subtract non-empty vector from non-empty one, dst size incorrect",
s1.size(), 3UL );
ensure_equals(
"subtract non-empty vector from non-empty one, src size incorrect",
s2.size(), 3UL );
ensure_equals(
"subtract non-empty vector from non-empty one, dst[0], incorrect",
s1[0], 1.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, dst[1], incorrect",
s1[1], 12.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, dst[2], incorrect",
s1[2], 1.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, src[0], incorrect",
s2[0], 3.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, src[1], incorrect",
s2[1], -3.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, src[2], incorrect",
s2[2], 1.0, precision );
// subtract non-empty vector from non-empty one with src.size() > dst.size():
// grow dst to that of src.size() (padding with zeros), dst -= src for all
// components, leave src unchanged
std::vector< tk::real > w1{{ 4.0, 9.0 }}, w2{{ 3.0, -3.0, 1.0 }};
w1 -= w2;
ensure_equals(
"subtract non-empty vector from non-empty one, dst size incorrect",
w1.size(), 3UL );
ensure_equals(
"subtract non-empty vector from non-empty one, src size incorrect",
w2.size(), 3UL );
ensure_equals(
"subtract non-empty vector from non-empty one, dst[0], incorrect",
w1[0], 1.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, dst[1], incorrect",
w1[1], 12.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, dst[2], incorrect",
w1[2], -1.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, src[0], incorrect",
w2[0], 3.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, src[1], incorrect",
w2[1], -3.0, precision );
ensure_equals(
"subtract non-empty vector from non-empty one, src[2], incorrect",
w2[2], 1.0, precision );
// subtract non-empty vector from non-empty one with src.size() < dst.size():
// thrown in DEBUG to warn on loosing data
// skipped in RELEASE mode, would yield segmentation fault
#ifndef NDEBUG // exception only thrown in DEBUG mode
try {
std::vector< tk::real > n1{{ 4.0, 9.0, 2.0 }}, n2{{ 3.0, -3.0 }};
n1 -= n2;
fail( "should throw exception in DEBUG mode" );
}
catch ( tk::Exception& ) {
// exception thrown in DEBUG mode, test ok
}
#endif
}
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(STRICT_GNUC)
#pragma GCC diagnostic pop
#endif
} // tut::
#endif // DOXYGEN_GENERATING_OUTPUT
|