- added Iterate.h header with nice little helpers + unit-tests
- moved around unit-tests into more matching source files + their relevant Eir SDK definitions
@@ -0,0 +1,186 @@ | ||
1 | +/***************************************************************************** | |
2 | +* | |
3 | +* PROJECT: Eir SDK | |
4 | +* LICENSE: See LICENSE in the top level directory | |
5 | +* FILE: eirrepo/sdk/Iterate.h | |
6 | +* PURPOSE: Iteration over lists and arguments. | |
7 | +* | |
8 | +* Find the Eir SDK at: https://osdn.net/projects/eirrepo/ | |
9 | +* Multi Theft Auto is available from http://www.multitheftauto.com/ | |
10 | +* | |
11 | +*****************************************************************************/ | |
12 | + | |
13 | +#ifndef _EIRREPO_ITERATION_HEADER_ | |
14 | +#define _EIRREPO_ITERATION_HEADER_ | |
15 | + | |
16 | +#include "MetaHelpers.h" | |
17 | +#include "Vector.h" | |
18 | +#include "Set.h" | |
19 | +#include "MultiValueStore.h" | |
20 | + | |
21 | +namespace eir | |
22 | +{ | |
23 | + | |
24 | +// Iterates over a single item/container. | |
25 | +template <typename itemType, typename containerType, typename callbackType> | |
26 | + requires ( eir::constructible_from <itemType, containerType&&> && std::is_invocable <callbackType, itemType&&>::value || | |
27 | + std::is_invocable <callbackType, itemType&>::value ) | |
28 | +AINLINE void iterateOver( containerType&& container, callbackType&& cb ) | |
29 | +{ | |
30 | + if constexpr ( eir::constructible_from <itemType, containerType&&> ) | |
31 | + { | |
32 | + cb( std::forward <containerType> ( container ) ); | |
33 | + } | |
34 | + else | |
35 | + { | |
36 | + for ( itemType& item : container ) | |
37 | + { | |
38 | + cb( item ); | |
39 | + } | |
40 | + } | |
41 | +} | |
42 | + | |
43 | +// Iterates over multiple containers, non-uniquely. | |
44 | +template <typename itemType, typename... containerTypes, typename callbackType> | |
45 | + requires ( std::is_invocable <callbackType, itemType&>::value ) | |
46 | +AINLINE void iterateOverMulti( callbackType&& cb, containerTypes&&... containers ) | |
47 | +{ | |
48 | + ( iterateOver <itemType> ( containers, cb ), ... ); | |
49 | +} | |
50 | + | |
51 | +// Queries a single container and sees if the data exist in that container. | |
52 | +template <typename containerType, typename itemType> | |
53 | +AINLINE bool containerContains( const containerType& container, const itemType& item ) | |
54 | +{ | |
55 | + if constexpr ( is_set <containerType>::value || is_vector <containerType>::value || is_multivaluestore <containerType>::value ) | |
56 | + { | |
57 | + return ( container.Find( item ) ); | |
58 | + } | |
59 | + else if constexpr ( EqualityComparableValues <containerType, itemType> ) | |
60 | + { | |
61 | + return ( container == item ); | |
62 | + } | |
63 | + else | |
64 | + { | |
65 | + for ( const itemType& ai : container ) | |
66 | + { | |
67 | + if ( ai == item ) | |
68 | + { | |
69 | + return true; | |
70 | + } | |
71 | + } | |
72 | + return false; | |
73 | + } | |
74 | +} | |
75 | + | |
76 | +// Quick helper for checking multiple containers related to item presence. | |
77 | +template <typename itemType, typename... containerTypes> | |
78 | + requires ( sizeof...( containerTypes ) > 0 ) | |
79 | +AINLINE bool isInContainer( const itemType& item, const containerTypes&... containers ) | |
80 | +{ | |
81 | + return ( containerContains( containers, item ) || ... ); | |
82 | +} | |
83 | + | |
84 | +template <typename findItemType, typename putItemType, typename... containerTypes> | |
85 | + requires ( sizeof...( containerTypes ) > 0 && ( std::is_const <containerTypes>::value || ... ) == false ) | |
86 | +AINLINE void replaceInContainer( const findItemType& oldval, const putItemType& newval, containerTypes&&... containers ) | |
87 | +{ | |
88 | + auto _replace_lambda = [&]( auto& container ) LAINLINE | |
89 | + { | |
90 | + if constexpr ( is_set <decltype(container)>::value ) | |
91 | + { | |
92 | + if ( auto *foundNode = container.Find( oldval ) ) | |
93 | + { | |
94 | + auto& upd = container.BeginNodeUpdate( foundNode ); | |
95 | + | |
96 | + upd = newval; | |
97 | + | |
98 | + container.EndNodeUpdate( foundNode ); | |
99 | + } | |
100 | + } | |
101 | + else if constexpr ( is_multivaluestore <decltype(container)>::value ) | |
102 | + { | |
103 | + container.Replace( oldval, newval ); | |
104 | + } | |
105 | + else if constexpr ( is_vector <decltype(container)>::value ) | |
106 | + { | |
107 | + container.ReplaceValues( oldval, newval ); | |
108 | + } | |
109 | + else if constexpr ( | |
110 | + std::is_reference <decltype(container)>::value && | |
111 | + std::convertible_to <putItemType, typename std::remove_reference <decltype(container)>::type> | |
112 | + ) | |
113 | + { | |
114 | + if ( container == oldval ) | |
115 | + { | |
116 | + container = newval; | |
117 | + } | |
118 | + } | |
119 | + else | |
120 | + { | |
121 | + for ( auto& ival : container ) | |
122 | + { | |
123 | + if ( ival == oldval ) | |
124 | + { | |
125 | + ival = newval; | |
126 | + } | |
127 | + } | |
128 | + } | |
129 | + }; | |
130 | + | |
131 | + ( _replace_lambda( containers ), ... ); | |
132 | +} | |
133 | + | |
134 | +enum class eUniqueVectorRemovalPolicy | |
135 | +{ | |
136 | + REMOVE, | |
137 | + REPLACE_WITH_DEFAULT | |
138 | +}; | |
139 | + | |
140 | +template <typename itemType, eUniqueVectorRemovalPolicy vecRemPolicy = eUniqueVectorRemovalPolicy::REMOVE, typename... containerTypes> | |
141 | + requires ( sizeof...( containerTypes ) > 0 && std::is_const <itemType>::value == false && ( std::is_const <containerTypes>::value || ... ) == false ) | |
142 | +AINLINE void removeFromContainer( const itemType& findval, containerTypes&&... containers ) | |
143 | +{ | |
144 | + auto _remove_lambda = [&]( auto& container ) LAINLINE | |
145 | + { | |
146 | + if constexpr ( is_set <decltype(container)>::value ) | |
147 | + { | |
148 | + container.Remove( findval ); | |
149 | + } | |
150 | + else if constexpr ( is_multivaluestore <decltype(container)>::value ) | |
151 | + { | |
152 | + container.RemoveAll( findval ); | |
153 | + } | |
154 | + else if constexpr ( is_vector <decltype(container)>::value && vecRemPolicy == eUniqueVectorRemovalPolicy::REMOVE ) | |
155 | + { | |
156 | + // If the policy is not equal to REMOVE, then we fall down to the replacement logic below. | |
157 | + container.RemoveByValue( findval ); | |
158 | + } | |
159 | + else if constexpr ( std::same_as <decltype(container), itemType&> ) | |
160 | + { | |
161 | + if ( findval != itemType() && container == findval ) | |
162 | + { | |
163 | + container = {}; | |
164 | + } | |
165 | + } | |
166 | + else | |
167 | + { | |
168 | + if ( findval != itemType() ) | |
169 | + { | |
170 | + for ( auto& ival : container ) | |
171 | + { | |
172 | + if ( ival == findval ) | |
173 | + { | |
174 | + ival = {}; | |
175 | + } | |
176 | + } | |
177 | + } | |
178 | + } | |
179 | + }; | |
180 | + | |
181 | + ( _remove_lambda( containers ), ... ); | |
182 | +} | |
183 | + | |
184 | +} // namespace eir | |
185 | + | |
186 | +#endif //_EIRREPO_ITERATION_HEADER_ | |
\ No newline at end of file |
@@ -13,10 +13,7 @@ | ||
13 | 13 | #ifndef _EIRSDK_UNIQUE_ITER_HEADER_ |
14 | 14 | #define _EIRSDK_UNIQUE_ITER_HEADER_ |
15 | 15 | |
16 | -#include "MetaHelpers.h" | |
17 | -#include "Vector.h" | |
18 | -#include "Set.h" | |
19 | -#include "MultiValueStore.h" | |
16 | +#include "Iterate.h" | |
20 | 17 | |
21 | 18 | #include <type_traits> |
22 | 19 | #include <optional> |
@@ -130,30 +127,6 @@ | ||
130 | 127 | } |
131 | 128 | } |
132 | 129 | |
133 | -template <typename containerType, typename itemType> | |
134 | -AINLINE bool containerContains( const containerType& container, const itemType& item ) | |
135 | -{ | |
136 | - if constexpr ( is_set <containerType>::value || is_vector <containerType>::value || is_multivaluestore <containerType>::value ) | |
137 | - { | |
138 | - return ( container.Find( item ) ); | |
139 | - } | |
140 | - else if constexpr ( EqualityComparableValues <containerType, itemType> ) | |
141 | - { | |
142 | - return ( container == item ); | |
143 | - } | |
144 | - else | |
145 | - { | |
146 | - for ( const itemType& ai : container ) | |
147 | - { | |
148 | - if ( ai == item ) | |
149 | - { | |
150 | - return true; | |
151 | - } | |
152 | - } | |
153 | - return false; | |
154 | - } | |
155 | -} | |
156 | - | |
157 | 130 | // Helper. |
158 | 131 | template <typename itemType, typename optionalStore> |
159 | 132 | AINLINE itemType _fetch_from_optional( const optionalStore& store ) |
@@ -366,114 +339,6 @@ | ||
366 | 339 | uniqueIterateTuple <itemType> ( std::forward <callbackType> ( cb ), std::tuple <const containerTypes&...> ( containers... ) ); |
367 | 340 | } |
368 | 341 | |
369 | -// Quick helper for checking multiple containers related to item presence. | |
370 | -template <typename itemType, typename... containerTypes> | |
371 | - requires ( sizeof...( containerTypes ) > 0 ) | |
372 | -AINLINE bool isInContainer( const itemType& item, const containerTypes&... containers ) | |
373 | -{ | |
374 | - return ( containerContains( containers, item ) || ... ); | |
375 | -} | |
376 | - | |
377 | -template <typename findItemType, typename putItemType, typename... containerTypes> | |
378 | - requires ( sizeof...( containerTypes ) > 0 && ( std::is_const <containerTypes>::value || ... ) == false ) | |
379 | -AINLINE void replaceInContainer( const findItemType& oldval, const putItemType& newval, containerTypes&&... containers ) | |
380 | -{ | |
381 | - auto _replace_lambda = [&]( auto& container ) LAINLINE | |
382 | - { | |
383 | - if constexpr ( is_set <decltype(container)>::value ) | |
384 | - { | |
385 | - if ( auto *foundNode = container.Find( oldval ) ) | |
386 | - { | |
387 | - auto& upd = container.BeginNodeUpdate( foundNode ); | |
388 | - | |
389 | - upd = newval; | |
390 | - | |
391 | - container.EndNodeUpdate( foundNode ); | |
392 | - } | |
393 | - } | |
394 | - else if constexpr ( is_multivaluestore <decltype(container)>::value ) | |
395 | - { | |
396 | - container.Replace( oldval, newval ); | |
397 | - } | |
398 | - else if constexpr ( is_vector <decltype(container)>::value ) | |
399 | - { | |
400 | - container.ReplaceValues( oldval, newval ); | |
401 | - } | |
402 | - else if constexpr ( | |
403 | - std::is_reference <decltype(container)>::value && | |
404 | - std::convertible_to <putItemType, typename std::remove_reference <decltype(container)>::type> | |
405 | - ) | |
406 | - { | |
407 | - if ( container == oldval ) | |
408 | - { | |
409 | - container = newval; | |
410 | - } | |
411 | - } | |
412 | - else | |
413 | - { | |
414 | - for ( auto& ival : container ) | |
415 | - { | |
416 | - if ( ival == oldval ) | |
417 | - { | |
418 | - ival = newval; | |
419 | - } | |
420 | - } | |
421 | - } | |
422 | - }; | |
423 | - | |
424 | - ( _replace_lambda( containers ), ... ); | |
425 | -} | |
426 | - | |
427 | -enum class eUniqueVectorRemovalPolicy | |
428 | -{ | |
429 | - REMOVE, | |
430 | - REPLACE_WITH_DEFAULT | |
431 | -}; | |
432 | - | |
433 | -template <typename itemType, eUniqueVectorRemovalPolicy vecRemPolicy = eUniqueVectorRemovalPolicy::REMOVE, typename... containerTypes> | |
434 | - requires ( sizeof...( containerTypes ) > 0 && std::is_const <itemType>::value == false && ( std::is_const <containerTypes>::value || ... ) == false ) | |
435 | -AINLINE void removeFromContainer( const itemType& findval, containerTypes&&... containers ) | |
436 | -{ | |
437 | - auto _remove_lambda = [&]( auto& container ) LAINLINE | |
438 | - { | |
439 | - if constexpr ( is_set <decltype(container)>::value ) | |
440 | - { | |
441 | - container.Remove( findval ); | |
442 | - } | |
443 | - else if constexpr ( is_multivaluestore <decltype(container)>::value ) | |
444 | - { | |
445 | - container.RemoveAll( findval ); | |
446 | - } | |
447 | - else if constexpr ( is_vector <decltype(container)>::value && vecRemPolicy == eUniqueVectorRemovalPolicy::REMOVE ) | |
448 | - { | |
449 | - // If the policy is not equal to REMOVE, then we fall down to the replacement logic below. | |
450 | - container.RemoveByValue( findval ); | |
451 | - } | |
452 | - else if constexpr ( std::same_as <decltype(container), itemType&> ) | |
453 | - { | |
454 | - if ( findval != itemType() && container == findval ) | |
455 | - { | |
456 | - container = {}; | |
457 | - } | |
458 | - } | |
459 | - else | |
460 | - { | |
461 | - if ( findval != itemType() ) | |
462 | - { | |
463 | - for ( auto& ival : container ) | |
464 | - { | |
465 | - if ( ival == findval ) | |
466 | - { | |
467 | - ival = {}; | |
468 | - } | |
469 | - } | |
470 | - } | |
471 | - } | |
472 | - }; | |
473 | - | |
474 | - ( _remove_lambda( containers ), ... ); | |
475 | -} | |
476 | - | |
477 | 342 | } // namespace eir |
478 | 343 | |
479 | 344 | #endif //_EIRSDK_UNIQUE_ITER_HEADER_ |
\ No newline at end of file |
@@ -0,0 +1,264 @@ | ||
1 | +#include <sdk/Iterate.h> | |
2 | + | |
3 | +void ITERATE_TESTS( void ) | |
4 | +{ | |
5 | + printf( "testing iterateOver (with vector)..." ); | |
6 | + { | |
7 | + eir::Vector <int, CRTHeapAllocator> vec = std::array{ 1, 2, 1, 4, 7 }; | |
8 | + | |
9 | + size_t idx = 0; | |
10 | + | |
11 | + eir::iterateOver <int> ( vec, | |
12 | + [&]( int& v ) | |
13 | + { | |
14 | + assert( idx != 0 || v == 1 ); | |
15 | + assert( idx != 1 || v == 2 ); | |
16 | + assert( idx != 2 || v == 1 ); | |
17 | + assert( idx != 3 || v == 4 ); | |
18 | + assert( idx != 4 || v == 7 ); | |
19 | + | |
20 | + idx++; | |
21 | + } | |
22 | + ); | |
23 | + | |
24 | + assert( idx == 5 ); | |
25 | + } | |
26 | + printf( "ok.\n" ); | |
27 | + | |
28 | + printf( "testing iterateOver (with single value)..." ); | |
29 | + { | |
30 | + eir::iterateOver <int> ( 901, | |
31 | + [&]( int&& v ) | |
32 | + { | |
33 | + assert( v == 901 ); | |
34 | + } | |
35 | + ); | |
36 | + | |
37 | + eir::iterateOver <int> ( 77, | |
38 | + [&]( const int& v ) | |
39 | + { | |
40 | + assert( v == 77 ); | |
41 | + } | |
42 | + ); | |
43 | + } | |
44 | + printf( "ok.\n" ); | |
45 | + | |
46 | + printf( "testing iterateOverMulti (with multi values)..." ); | |
47 | + { | |
48 | + size_t idx = 0; | |
49 | + | |
50 | + eir::iterateOverMulti <int> ( | |
51 | + [&]( int& v ) | |
52 | + { | |
53 | + assert( idx != 0 || v == 4 ); | |
54 | + assert( idx != 1 || v == 3 ); | |
55 | + assert( idx != 2 || v == 8 ); | |
56 | + assert( idx != 3 || v == 9 ); | |
57 | + assert( idx != 4 || v == -20 ); | |
58 | + | |
59 | + idx++; | |
60 | + }, | |
61 | + 4, 3, 8, 9, -20 | |
62 | + ); | |
63 | + | |
64 | + assert( idx == 5 ); | |
65 | + } | |
66 | + printf( "ok.\n" ); | |
67 | + | |
68 | + printf( "testing iterateOverMulti (with vector and set)..." ); | |
69 | + { | |
70 | + eir::Vector <int, CRTHeapAllocator> cont1 = std::array{ 5, 3, 9 }; | |
71 | + eir::Set <int, CRTHeapAllocator> cont2 = std::array{ 99, 4145, 4 }; | |
72 | + | |
73 | + size_t idx = 0; | |
74 | + | |
75 | + eir::iterateOverMulti <const int> ( | |
76 | + [&]( const int& v ) | |
77 | + { | |
78 | + assert( idx != 0 || v == 5 ); | |
79 | + assert( idx != 1 || v == 3 ); | |
80 | + assert( idx != 2 || v == 9 ); | |
81 | + assert( idx != 3 || v == 4 ); | |
82 | + assert( idx != 4 || v == 99 ); | |
83 | + assert( idx != 5 || v == 4145 ); | |
84 | + | |
85 | + idx++; | |
86 | + }, | |
87 | + cont1, cont2 | |
88 | + ); | |
89 | + | |
90 | + assert( idx == 6 ); | |
91 | + } | |
92 | + printf( "ok.\n" ); | |
93 | + | |
94 | + printf( "testing isInContainer..." ); | |
95 | + { | |
96 | + assert( eir::isInContainer( 1, std::array{ 0, 1, 2 } ) == true ); | |
97 | + assert( eir::isInContainer( 6, std::array{ 3, 2, 7 } ) == false ); | |
98 | + assert( eir::isInContainer( 5, eir::Set <int, CRTHeapAllocator> ( std::array{ 1, 3, 5 } ) ) == true ); | |
99 | + assert( eir::isInContainer( 4, eir::Vector <int, CRTHeapAllocator> ( std::array{ 2, 6, 0 } ) ) == false ); | |
100 | + assert( eir::isInContainer( 42, std::array{ 1, 2, 3 }, std::array{ 5, 9, 12 }, eir::Vector <int, CRTHeapAllocator> ( std::array{ 31, 42, 53 } ) ) == true ); | |
101 | + assert( eir::isInContainer( 99, 11, 22, 33, 44, 55, 66, 77, 88, 99 ) == true ); | |
102 | + assert( eir::isInContainer( 1, 0, -42, 88 ) == false ); | |
103 | + } | |
104 | + printf( "ok.\n" ); | |
105 | + | |
106 | + printf( "testing replaceInContainer (with array)..." ); | |
107 | + { | |
108 | + auto arr = std::array{ 3, 5, 9 }; | |
109 | + auto arr2 = std::array{ 66, 5 }; | |
110 | + | |
111 | + eir::replaceInContainer <int> ( 5, 11, arr, arr2 ); | |
112 | + | |
113 | + assert( arr[0] == 3 ); | |
114 | + assert( arr[1] == 11 ); | |
115 | + assert( arr[2] == 9 ); | |
116 | + assert( arr2[0] == 66 ); | |
117 | + assert( arr2[1] == 11 ); | |
118 | + } | |
119 | + printf( "ok.\n" ); | |
120 | + | |
121 | + printf( "testing replaceInContainer (with vector)..." ); | |
122 | + { | |
123 | + eir::Vector <int, CRTHeapAllocator> vec = std::array{ 331, 45, 824, 42, 42 }; | |
124 | + eir::Vector <int, CRTHeapAllocator> vec2 = std::array{ 1 }; | |
125 | + | |
126 | + eir::replaceInContainer <int> ( 42, 84, vec, vec2 ); | |
127 | + | |
128 | + assert( vec[0] == 331 ); | |
129 | + assert( vec[1] == 45 ); | |
130 | + assert( vec[2] == 824 ); | |
131 | + assert( vec[3] == 84 ); | |
132 | + assert( vec[4] == 84 ); | |
133 | + assert( vec2[0] == 1 ); | |
134 | + } | |
135 | + printf( "ok.\n" ); | |
136 | + | |
137 | + printf( "testing replaceInContainer (with set)..." ); | |
138 | + { | |
139 | + eir::Set <int, CRTHeapAllocator> set = std::array{ 0, 11, 22, 33, 44, 55 }; | |
140 | + eir::Set <int, CRTHeapAllocator> set2 = std::array{ 6, 7, 8, 33 }; | |
141 | + | |
142 | + eir::replaceInContainer <int> ( 33, 0, set, set2 ); | |
143 | + | |
144 | + assert( set.Find( 33 ) == nullptr ); | |
145 | + assert( set.Find( 0 ) != nullptr ); | |
146 | + assert( set2.Find( 33 ) == nullptr ); | |
147 | + assert( set2.Find( 0 ) != nullptr ); | |
148 | + } | |
149 | + printf( "ok.\n" ); | |
150 | + | |
151 | + printf( "testing replaceInContainer (with multi-value-store)..." ); | |
152 | + { | |
153 | + eir::MultiValueStore <int, CRTHeapAllocator> mvs = std::array{ 1, 1, 2, 2, 3, 3 }; | |
154 | + eir::MultiValueStore <int, CRTHeapAllocator> mvs2 = std::array{ 99 }; | |
155 | + | |
156 | + eir::replaceInContainer( 1, 7, mvs, mvs2 ); | |
157 | + | |
158 | + assert( mvs.GetRefCount( 7 ) == 2 ); | |
159 | + assert( mvs.GetRefCount( 1 ) == 0 ); | |
160 | + | |
161 | + eir::replaceInContainer( 2, 3, mvs, mvs2 ); | |
162 | + | |
163 | + assert( mvs.GetRefCount( 3 ) == 4 ); | |
164 | + assert( mvs.GetRefCount( 2 ) == 0 ); | |
165 | + | |
166 | + eir::replaceInContainer( 99, 0, mvs, mvs2 ); | |
167 | + | |
168 | + assert( mvs2.GetRefCount( 0 ) == 1 ); | |
169 | + assert( mvs2.GetRefCount( 99 ) == 0 ); | |
170 | + } | |
171 | + printf( "ok.\n" ); | |
172 | + | |
173 | + printf( "testing replaceInContainer (with references)..." ); | |
174 | + { | |
175 | + int a = 1; | |
176 | + int b = 2; | |
177 | + int c = 1; | |
178 | + | |
179 | + eir::replaceInContainer( 1, 9, a, b, c ); | |
180 | + | |
181 | + assert( a == 9 ); | |
182 | + assert( b == 2 ); | |
183 | + assert( c == 9 ); | |
184 | + } | |
185 | + printf( "ok.\n" ); | |
186 | + | |
187 | + printf( "testing removeFromContainer (with array)..." ); | |
188 | + { | |
189 | + auto arr = std::array{ 1, 2, 3 }; | |
190 | + auto arr2 = std::array{ 3, 4, 5 }; | |
191 | + | |
192 | + eir::removeFromContainer <int> ( 3, arr, arr2 ); | |
193 | + | |
194 | + assert( arr[2] == 0 ); | |
195 | + assert( arr2[0] == 0 ); | |
196 | + } | |
197 | + printf( "ok.\n" ); | |
198 | + | |
199 | + printf( "testing removeFromContainer (with vector)..." ); | |
200 | + { | |
201 | + eir::Vector <int, CRTHeapAllocator> vec = std::array{ 2, 4, 6 }; | |
202 | + eir::Vector <int, CRTHeapAllocator> vec2 = std::array{ 6, 8, 10 }; | |
203 | + | |
204 | + eir::removeFromContainer <int> ( 6, vec, vec2 ); | |
205 | + | |
206 | + assert( vec.GetCount() == 2 ); | |
207 | + assert( vec2.GetCount() == 2 ); | |
208 | + } | |
209 | + printf( "ok.\n" ); | |
210 | + | |
211 | + printf( "testing removeFromContainer (with set)..." ); | |
212 | + { | |
213 | + eir::Set <int, CRTHeapAllocator> set = std::array{ 442, 563, 857 }; | |
214 | + eir::Set <int, CRTHeapAllocator> set2 = std::array{ 0, 1, 2 }; | |
215 | + | |
216 | + eir::removeFromContainer <int> ( 563, set, set2 ); | |
217 | + | |
218 | + assert( set.Find( 563 ) == nullptr ); | |
219 | + assert( set.GetValueCount() == 2 ); | |
220 | + } | |
221 | + printf( "ok.\n" ); | |
222 | + | |
223 | + printf( "testing removeFromContainer (with multi-value-store)..." ); | |
224 | + { | |
225 | + eir::MultiValueStore <int, CRTHeapAllocator> mvs = std::array{ 66, 99, 111 }; | |
226 | + eir::MultiValueStore <int, CRTHeapAllocator> mvs2 = std::array{ 1, 1, 2 }; | |
227 | + | |
228 | + eir::removeFromContainer( 1, mvs, mvs2 ); | |
229 | + | |
230 | + assert( mvs2.Find( 1 ) == false ); | |
231 | + | |
232 | + eir::removeFromContainer( 99, mvs, mvs2 ); | |
233 | + | |
234 | + assert( mvs.Find( 99 ) == false ); | |
235 | + } | |
236 | + printf( "ok.\n" ); | |
237 | + | |
238 | + printf( "testing removeFromContainer (with references)..." ); | |
239 | + { | |
240 | + int a = 1; | |
241 | + int b = 2; | |
242 | + int c = 1; | |
243 | + | |
244 | + eir::removeFromContainer( 1, a, b, c ); | |
245 | + | |
246 | + assert( a == 0 ); | |
247 | + assert( b == 2 ); | |
248 | + assert( c == 0 ); | |
249 | + } | |
250 | + printf( "ok.\n" ); | |
251 | + | |
252 | + printf( "testing removeFromContainer (replace-default removal policy)..." ); | |
253 | + { | |
254 | + eir::Vector <int, CRTHeapAllocator> vec = std::array{ 1, 2, 3 }; | |
255 | + | |
256 | + eir::removeFromContainer <int, eir::eUniqueVectorRemovalPolicy::REPLACE_WITH_DEFAULT> ( 2, vec ); | |
257 | + | |
258 | + assert( vec.GetCount() == 3 ); | |
259 | + assert( vec[0] == 1 ); | |
260 | + assert( vec[1] == 0 ); | |
261 | + assert( vec[2] == 3 ); | |
262 | + } | |
263 | + printf( "ok.\n" ); | |
264 | +} | |
\ No newline at end of file |
@@ -31,6 +31,7 @@ | ||
31 | 31 | extern void STACKEDALLOC_TESTS( void ); |
32 | 32 | extern void BITVECTOR_TESTS( void ); |
33 | 33 | extern void EMPTYVECTOR_TESTS( void ); |
34 | +extern void ITERATE_TESTS( void ); | |
34 | 35 | extern void UNIQUEITER_TESTS( void ); |
35 | 36 | extern void MULTIVALUESTORE_TESTS( void ); |
36 | 37 |
@@ -64,6 +65,7 @@ | ||
64 | 65 | STACKEDALLOC_TESTS(); |
65 | 66 | BITVECTOR_TESTS(); |
66 | 67 | EMPTYVECTOR_TESTS(); |
68 | + ITERATE_TESTS(); | |
67 | 69 | UNIQUEITER_TESTS(); |
68 | 70 | MULTIVALUESTORE_TESTS(); |
69 | 71 |
@@ -222,175 +222,4 @@ | ||
222 | 222 | assert( cnt == 6 ); |
223 | 223 | } |
224 | 224 | printf( "ok.\n" ); |
225 | - | |
226 | - printf( "testing isInContainer..." ); | |
227 | - { | |
228 | - assert( eir::isInContainer( 1, std::array{ 0, 1, 2 } ) == true ); | |
229 | - assert( eir::isInContainer( 6, std::array{ 3, 2, 7 } ) == false ); | |
230 | - assert( eir::isInContainer( 5, eir::Set <int, CRTHeapAllocator> ( std::array{ 1, 3, 5 } ) ) == true ); | |
231 | - assert( eir::isInContainer( 4, eir::Vector <int, CRTHeapAllocator> ( std::array{ 2, 6, 0 } ) ) == false ); | |
232 | - assert( eir::isInContainer( 42, std::array{ 1, 2, 3 }, std::array{ 5, 9, 12 }, eir::Vector <int, CRTHeapAllocator> ( std::array{ 31, 42, 53 } ) ) == true ); | |
233 | - assert( eir::isInContainer( 99, 11, 22, 33, 44, 55, 66, 77, 88, 99 ) == true ); | |
234 | - assert( eir::isInContainer( 1, 0, -42, 88 ) == false ); | |
235 | - } | |
236 | - printf( "ok.\n" ); | |
237 | - | |
238 | - printf( "testing replaceInContainer (with array)..." ); | |
239 | - { | |
240 | - auto arr = std::array{ 3, 5, 9 }; | |
241 | - auto arr2 = std::array{ 66, 5 }; | |
242 | - | |
243 | - eir::replaceInContainer <int> ( 5, 11, arr, arr2 ); | |
244 | - | |
245 | - assert( arr[0] == 3 ); | |
246 | - assert( arr[1] == 11 ); | |
247 | - assert( arr[2] == 9 ); | |
248 | - assert( arr2[0] == 66 ); | |
249 | - assert( arr2[1] == 11 ); | |
250 | - } | |
251 | - printf( "ok.\n" ); | |
252 | - | |
253 | - printf( "testing replaceInContainer (with vector)..." ); | |
254 | - { | |
255 | - eir::Vector <int, CRTHeapAllocator> vec = std::array{ 331, 45, 824, 42, 42 }; | |
256 | - eir::Vector <int, CRTHeapAllocator> vec2 = std::array{ 1 }; | |
257 | - | |
258 | - eir::replaceInContainer <int> ( 42, 84, vec, vec2 ); | |
259 | - | |
260 | - assert( vec[0] == 331 ); | |
261 | - assert( vec[1] == 45 ); | |
262 | - assert( vec[2] == 824 ); | |
263 | - assert( vec[3] == 84 ); | |
264 | - assert( vec[4] == 84 ); | |
265 | - assert( vec2[0] == 1 ); | |
266 | - } | |
267 | - printf( "ok.\n" ); | |
268 | - | |
269 | - printf( "testing replaceInContainer (with set)..." ); | |
270 | - { | |
271 | - eir::Set <int, CRTHeapAllocator> set = std::array{ 0, 11, 22, 33, 44, 55 }; | |
272 | - eir::Set <int, CRTHeapAllocator> set2 = std::array{ 6, 7, 8, 33 }; | |
273 | - | |
274 | - eir::replaceInContainer <int> ( 33, 0, set, set2 ); | |
275 | - | |
276 | - assert( set.Find( 33 ) == nullptr ); | |
277 | - assert( set.Find( 0 ) != nullptr ); | |
278 | - assert( set2.Find( 33 ) == nullptr ); | |
279 | - assert( set2.Find( 0 ) != nullptr ); | |
280 | - } | |
281 | - printf( "ok.\n" ); | |
282 | - | |
283 | - printf( "testing replaceInContainer (with multi-value-store)..." ); | |
284 | - { | |
285 | - eir::MultiValueStore <int, CRTHeapAllocator> mvs = std::array{ 1, 1, 2, 2, 3, 3 }; | |
286 | - eir::MultiValueStore <int, CRTHeapAllocator> mvs2 = std::array{ 99 }; | |
287 | - | |
288 | - eir::replaceInContainer( 1, 7, mvs, mvs2 ); | |
289 | - | |
290 | - assert( mvs.GetRefCount( 7 ) == 2 ); | |
291 | - assert( mvs.GetRefCount( 1 ) == 0 ); | |
292 | - | |
293 | - eir::replaceInContainer( 2, 3, mvs, mvs2 ); | |
294 | - | |
295 | - assert( mvs.GetRefCount( 3 ) == 4 ); | |
296 | - assert( mvs.GetRefCount( 2 ) == 0 ); | |
297 | - | |
298 | - eir::replaceInContainer( 99, 0, mvs, mvs2 ); | |
299 | - | |
300 | - assert( mvs2.GetRefCount( 0 ) == 1 ); | |
301 | - assert( mvs2.GetRefCount( 99 ) == 0 ); | |
302 | - } | |
303 | - printf( "ok.\n" ); | |
304 | - | |
305 | - printf( "testing replaceInContainer (with references)..." ); | |
306 | - { | |
307 | - int a = 1; | |
308 | - int b = 2; | |
309 | - int c = 1; | |
310 | - | |
311 | - eir::replaceInContainer( 1, 9, a, b, c ); | |
312 | - | |
313 | - assert( a == 9 ); | |
314 | - assert( b == 2 ); | |
315 | - assert( c == 9 ); | |
316 | - } | |
317 | - printf( "ok.\n" ); | |
318 | - | |
319 | - printf( "testing removeFromContainer (with array)..." ); | |
320 | - { | |
321 | - auto arr = std::array{ 1, 2, 3 }; | |
322 | - auto arr2 = std::array{ 3, 4, 5 }; | |
323 | - | |
324 | - eir::removeFromContainer <int> ( 3, arr, arr2 ); | |
325 | - | |
326 | - assert( arr[2] == 0 ); | |
327 | - assert( arr2[0] == 0 ); | |
328 | - } | |
329 | - printf( "ok.\n" ); | |
330 | - | |
331 | - printf( "testing removeFromContainer (with vector)..." ); | |
332 | - { | |
333 | - eir::Vector <int, CRTHeapAllocator> vec = std::array{ 2, 4, 6 }; | |
334 | - eir::Vector <int, CRTHeapAllocator> vec2 = std::array{ 6, 8, 10 }; | |
335 | - | |
336 | - eir::removeFromContainer <int> ( 6, vec, vec2 ); | |
337 | - | |
338 | - assert( vec.GetCount() == 2 ); | |
339 | - assert( vec2.GetCount() == 2 ); | |
340 | - } | |
341 | - printf( "ok.\n" ); | |
342 | - | |
343 | - printf( "testing removeFromContainer (with set)..." ); | |
344 | - { | |
345 | - eir::Set <int, CRTHeapAllocator> set = std::array{ 442, 563, 857 }; | |
346 | - eir::Set <int, CRTHeapAllocator> set2 = std::array{ 0, 1, 2 }; | |
347 | - | |
348 | - eir::removeFromContainer <int> ( 563, set, set2 ); | |
349 | - | |
350 | - assert( set.Find( 563 ) == nullptr ); | |
351 | - assert( set.GetValueCount() == 2 ); | |
352 | - } | |
353 | - printf( "ok.\n" ); | |
354 | - | |
355 | - printf( "testing removeFromContainer (with multi-value-store)..." ); | |
356 | - { | |
357 | - eir::MultiValueStore <int, CRTHeapAllocator> mvs = std::array{ 66, 99, 111 }; | |
358 | - eir::MultiValueStore <int, CRTHeapAllocator> mvs2 = std::array{ 1, 1, 2 }; | |
359 | - | |
360 | - eir::removeFromContainer( 1, mvs, mvs2 ); | |
361 | - | |
362 | - assert( mvs2.Find( 1 ) == false ); | |
363 | - | |
364 | - eir::removeFromContainer( 99, mvs, mvs2 ); | |
365 | - | |
366 | - assert( mvs.Find( 99 ) == false ); | |
367 | - } | |
368 | - printf( "ok.\n" ); | |
369 | - | |
370 | - printf( "testing removeFromContainer (with references)..." ); | |
371 | - { | |
372 | - int a = 1; | |
373 | - int b = 2; | |
374 | - int c = 1; | |
375 | - | |
376 | - eir::removeFromContainer( 1, a, b, c ); | |
377 | - | |
378 | - assert( a == 0 ); | |
379 | - assert( b == 2 ); | |
380 | - assert( c == 0 ); | |
381 | - } | |
382 | - printf( "ok.\n" ); | |
383 | - | |
384 | - printf( "testing removeFromContainer (replace-default removal policy)..." ); | |
385 | - { | |
386 | - eir::Vector <int, CRTHeapAllocator> vec = std::array{ 1, 2, 3 }; | |
387 | - | |
388 | - eir::removeFromContainer <int, eir::eUniqueVectorRemovalPolicy::REPLACE_WITH_DEFAULT> ( 2, vec ); | |
389 | - | |
390 | - assert( vec.GetCount() == 3 ); | |
391 | - assert( vec[0] == 1 ); | |
392 | - assert( vec[1] == 0 ); | |
393 | - assert( vec[2] == 3 ); | |
394 | - } | |
395 | - printf( "ok.\n" ); | |
396 | 225 | } |
\ No newline at end of file |