1#include <Core/Utils/IndexMap.hpp>
2#include <catch2/catch_test_macros.hpp>
3#include <unittestUtils.hpp>
5using Ra::Core::Utils::Index;
10 explicit Foo(
int x ) : value( x ) {}
14TEST_CASE(
"Core/Utils/IndexMap",
"[unittests][Core][Core/Utils][IndexMap]" ) {
16 SECTION(
"Sanity checks" ) {
19 REQUIRE( map1.
empty() );
21 REQUIRE( map1.size() == 0 );
23 REQUIRE( !map1.
full() );
26 SECTION(
"Test Copyable" ) {
29 i1 = map1.
insert( Foo( 12 ) );
30 REQUIRE( i1.isValid() );
33 REQUIRE( !map1.
empty() );
34 REQUIRE( map1.size() == 1 );
37 REQUIRE( map1.
at( i1 ).value == 12 );
38 REQUIRE( map1[i1].value == 12 );
40 map1.
access( i1 ).value = 24;
41 REQUIRE( map1.
at( i1 ).value == 24 );
44 REQUIRE( map1.
at( i1 ).value == 32 );
46 i2 = map1.
insert( Foo( 42 ) );
47 REQUIRE( map1.
at( i2 ).value == 42 );
48 REQUIRE( map1.size() == 2 );
53 for (
const Foo& f : map1 ) {
54 REQUIRE( ( f.value == 32 || f.value == 42 ) );
57 REQUIRE( counter == 2 );
61 for ( Foo& f : map1 ) {
62 f.value = 2 * f.value;
63 REQUIRE( ( f.value == 2 * 32 || f.value == 2 * 42 ) );
66 REQUIRE( counter == 2 );
70 for (
auto it = map1.cbegin_index(); it != map1.cend_index(); ++it ) {
71 REQUIRE( ( *it == i1 || *it == i2 ) );
78 REQUIRE( map1.contains( i1 ) );
79 REQUIRE( map1.contains( i2 ) );
80 REQUIRE( !map1.contains( Index( 12000 ) ) );
83 bool result = map1.remove( i1 );
85 REQUIRE( map1.size() == 1 );
88 result = map1.remove( i1 );
92 result = map1.remove( Index( 1000 ) );
95 result = map1.remove( i2 );
98 REQUIRE( map1.size() == 0 );
99 REQUIRE( map1.empty() );
102 SECTION(
"Test Non-Copyable" ) {
103 using Ra::Core::Utils::Index;
110 REQUIRE( i1.isValid() );
114 REQUIRE( i2.isValid() );
117 REQUIRE( map2[i1].value == 12 );
118 REQUIRE( map2[i2].value == 42 );
122 REQUIRE( map2.size() == 0 );
123 REQUIRE( map2.
empty() );
135 REQUIRE( idx.isInvalid() );
139TEST_CASE(
"Core/Utils/Index/Ctor",
"[unittests][Core][Core/Utils][Index]" ) {
142 REQUIRE( idxInvalid.isInvalid() );
145 for ( Index::IntegerType i = 0; i < std::numeric_limits<Index::IntegerType>::max() - 2 * step;
148 REQUIRE( idx.isValid() );
149 auto idxUl = Index {
static_cast<unsigned long int>( i ) };
150 auto idxL = Index {
static_cast<long int>( i ) };
151 auto idxU = Index {
static_cast<unsigned int>( i ) };
152 REQUIRE( idxUl.isValid() );
153 REQUIRE( idxU.isValid() );
154 REQUIRE( idxL.isValid() );
155 REQUIRE( idx == idxUl );
156 REQUIRE( idx == idxU );
157 REQUIRE( idx == idxL );
160 testType<unsigned long int>();
162 testType<unsigned int>();
const T & at(const Index &idx) const
Return a read-only ref to object with the given index. Crashes if index does not exist.
void clear()
Return the size of the IndexMap ( number of object contained ).
Index emplace(const Args &&... args)
T & access(const Index &idx)
Return a reference to the object with the given index. Crash if index does not exist.
Index insert(const T &obj)
Destructor.
bool empty() const
Clear the IndexMap.
bool full() const
Return true if the IndexMap is empty.