permutation_generator.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cds/opt/permutation.h>
  6. #include <cds_test/ext_gtest.h>
  7. namespace {
  8. class Permutations: public ::testing::Test
  9. {
  10. protected:
  11. static const size_t c_nMax = 1024;
  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. do {
  20. typename Gen::integer_type i = gen;
  21. ++arr[ i ];
  22. } while ( gen.next());
  23. for ( size_t i = 0; i < nLen; ++i )
  24. EXPECT_EQ( arr[i], 1u ) << "i=" << i;
  25. for ( size_t i = nLen; i < c_nMax; ++i )
  26. EXPECT_EQ( arr[i], 0u ) << "i=" << i;
  27. gen.reset();
  28. }
  29. }
  30. template <typename Gen>
  31. void test()
  32. {
  33. for ( size_t nLen = 2; nLen <= c_nMax; ++nLen ) {
  34. Gen gen( nLen );
  35. test_with( gen, nLen );
  36. }
  37. }
  38. template <typename Gen>
  39. void test2()
  40. {
  41. for ( size_t nLen = 2; nLen <= c_nMax; nLen *= 2 ) {
  42. Gen gen( nLen );
  43. test_with( gen, nLen );
  44. }
  45. }
  46. };
  47. TEST_F( Permutations, random_permutation )
  48. {
  49. test< cds::opt::v::random_permutation<> >();
  50. }
  51. TEST_F( Permutations, random2_permutation )
  52. {
  53. test2< cds::opt::v::random2_permutation<> >();
  54. }
  55. TEST_F( Permutations, random_shuffle_permutation )
  56. {
  57. test< cds::opt::v::random_shuffle_permutation<> >();
  58. }
  59. } // namespace