Move libcds 1.6.0 from SVN
[libcds.git] / tests / test-hdr / misc / permutation_generator.cpp
1 //$$CDS-header$$
2
3 #include <cds/opt/permutation.h>
4 #include "cppunit/cppunit_proxy.h"
5
6 namespace misc {
7
8     class Permutations: public CppUnitMini::TestCase
9     {
10         static const size_t c_nMax = 1024;
11
12         template <typename Gen>
13         void test_with( Gen& gen, size_t nLen )
14         {
15             unsigned int arr[c_nMax];
16             for ( size_t nPass = 0; nPass < 10; ++nPass ) {
17                 for ( size_t i = 0; i < c_nMax; ++i )
18                     arr[i] = 0;
19
20                 do {
21                     typename Gen::integer_type i = gen;
22                     ++arr[ i ];
23                 } while ( gen.next() );
24
25                 for ( size_t i = 0; i < nLen; ++i )
26                     CPPUNIT_CHECK_EX( arr[i] == 1, "arr[" << i << "]=" << arr[i] );
27                 for ( size_t i = nLen; i < c_nMax; ++i )
28                     CPPUNIT_CHECK_EX( arr[i] == 0, "arr[" << i << "]=" << arr[i] );
29
30                 gen.reset();
31             }
32         }
33
34         template <typename Gen>
35         void test()
36         {
37             for ( size_t nLen = 2; nLen <= c_nMax; ++nLen ) {
38                 Gen gen( nLen );
39                 test_with( gen, nLen );
40             }
41         }
42
43         template <typename Gen>
44         void test2()
45         {
46             for ( size_t nLen = 2; nLen <= c_nMax; nLen *= 2 ) {
47                 Gen gen( nLen );
48                 test_with( gen, nLen );
49             }
50         }
51
52         void test_random_permutation()
53         {
54             test< cds::opt::v::random_permutation<> >();
55         }
56
57         void test_random2_permutation()
58         {
59             test2< cds::opt::v::random2_permutation<> >();
60         }
61
62         void test_random_shuffle_permutation()
63         {
64             test< cds::opt::v::random_shuffle_permutation<> >();
65         }
66
67     public:
68         CPPUNIT_TEST_SUITE(Permutations)
69             CPPUNIT_TEST( test_random_permutation )
70             CPPUNIT_TEST( test_random2_permutation )
71             CPPUNIT_TEST( test_random_shuffle_permutation )
72         CPPUNIT_TEST_SUITE_END()
73     };
74
75 } // namespace misc
76
77 CPPUNIT_TEST_SUITE_REGISTRATION(misc::Permutations);