Loading [MathJax]/jax/input/TeX/config.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
containers.cpp
1#include <Core/Containers/Iterators.hpp>
2#include <algorithm>
3#include <catch2/catch_test_macros.hpp>
4#include <functional>
5#include <iterator>
6#include <random>
7
8TEST_CASE( "Core/Containers/Iterators", "[unittests][Core][Core/Containers][Iterators]" ) {
9 SECTION( "Test reversed Iterators" ) {
10 using namespace Ra::Core;
11
12 static constexpr int nbSamples = 1000;
13 static constexpr int randomMin = 1;
14 static constexpr int randomMax = 1000;
15
16 using Container = std::vector<int>;
17
18 // Init random number stuff
19 std::random_device rnd_device;
20 std::mt19937 mersenne_engine( rnd_device() );
21 std::uniform_int_distribution<int> dist( randomMin, randomMax );
22
23 // Generate array with random numbers
24 auto gen = std::bind( dist, mersenne_engine );
25 Container container( nbSamples );
26 std::generate( std::begin( container ), std::end( container ), gen );
27 std::shuffle( std::begin( container ), std::end( container ), mersenne_engine );
28
29 // check that Ra::Core::reversed produces same results than std::rbegin()
30 Container reverted1;
31 reverted1.reserve( nbSamples );
32 Container reverted2;
33 reverted2.reserve( nbSamples );
34
35 for ( auto v : Ra::Core::reversed( container ) )
36 reverted1.push_back( v );
37 for ( auto vIt = container.rbegin(); vIt != container.rend(); vIt++ )
38 reverted2.push_back( *vIt );
39
40 // Comparison with std::reverse_iterator
41 REQUIRE( reverted1 == reverted2 );
42 // Reversed array should be different from original
43 REQUIRE( container != reverted1 );
44
45 // check that reverse iterator produce same result than std::algorithm
46 Container reverted3( nbSamples );
47 std::reverse_copy( container.begin(), container.end(), reverted3.begin() );
48
49 // Reversed array equal std::reverse(original)
50 REQUIRE( reverted1 == reverted3 );
51
52 // check constant access
53 const Container& ccontainer = container;
54 Container reverted4;
55 reverted4.reserve( nbSamples );
56 for ( auto v : Ra::Core::reversed( ccontainer ) )
57 reverted4.push_back( v );
58
59 // Const qualifier should not change the loop behavior
60 REQUIRE( reverted1 == reverted4 );
61
62 // check we can also write in the containers
63 for ( auto& v : Ra::Core::reversed( reverted2 ) )
64 v++;
65 for ( auto& v : reverted1 )
66 v++;
67
68 // Writting is working
69 REQUIRE( reverted1 == reverted2 );
70 REQUIRE( reverted1 != reverted3 );
71 }
72}
T begin(T... args)
T bind(T... args)
T end(T... args)
T generate(T... args)
This namespace contains everything "low level", related to data, datastuctures, and computation.
Definition Cage.cpp:5
T shuffle(T... args)
T reverse_copy(T... args)