test_flat_stable_sort.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //----------------------------------------------------------------------------
  2. /// @file test_flat_stable_sort.cpp
  3. /// @brief test program of the flat_stable_sort algorithm
  4. ///
  5. /// @author Copyright (c) 2017 Francisco José Tapia (fjtapia@gmail.com )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanying file LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #include <algorithm>
  14. #include <iostream>
  15. #include <random>
  16. #include <vector>
  17. #include <ciso646>
  18. #include <boost/sort/flat_stable_sort/flat_stable_sort.hpp>
  19. #include <boost/test/included/test_exec_monitor.hpp>
  20. #include <boost/test/test_tools.hpp>
  21. using namespace boost::sort;
  22. void test1 ( );
  23. void test2 ( );
  24. void test3 ( );
  25. //---------------- stability test -----------------------------------
  26. struct xk
  27. {
  28. unsigned tail : 4;
  29. unsigned num : 28;
  30. xk ( uint32_t n =0 , uint32_t t =0): tail (t), num(n){};
  31. bool operator< (xk A) const { return (num < A.num); };
  32. };
  33. void test1 ( )
  34. {
  35. typedef typename std::vector< xk >::iterator iter_t;
  36. typedef std::less< xk > compare_t;
  37. std::mt19937_64 my_rand (0);
  38. const uint32_t NMAX = 100000;
  39. std::vector< xk > V1, V2;
  40. V1.reserve (NMAX);
  41. for (uint32_t i = 0; i < 8; ++i) {
  42. for (uint32_t k = 0; k < NMAX; ++k) {
  43. uint32_t NM = my_rand ( );
  44. xk G;
  45. G.num = NM >> 3;
  46. G.tail = i;
  47. V1.push_back (G);
  48. };
  49. };
  50. V2 = V1;
  51. flat_stable_sort< iter_t, compare_t > (V1.begin ( ), V1.end ( ), compare_t ( ));
  52. std::stable_sort (V2.begin ( ), V2.end ( ));
  53. BOOST_CHECK (V1.size ( ) == V2.size ( ));
  54. for (uint32_t i = 0; i < V1.size ( ); ++i) {
  55. BOOST_CHECK (V1[ i ].num == V2[ i ].num and
  56. V1[ i ].tail == V2[ i ].tail);
  57. };
  58. };
  59. void test2 (void)
  60. {
  61. typedef std::less< uint64_t > compare_t;
  62. const uint32_t NElem = 100000;
  63. std::vector< uint64_t > V1,V2;
  64. std::mt19937_64 my_rand (0);
  65. compare_t comp;
  66. // ------------------------ random elements -------------------------------
  67. for (uint32_t i = 0; i < NElem; ++i) V1.push_back (my_rand ( ) % NElem);
  68. V2 = V1;
  69. flat_stable_sort (V1.begin ( ), V1.end ( ), comp);
  70. std::stable_sort (V2.begin ( ), V2.end ( ), comp);
  71. for (unsigned i = 0; i < NElem; i++) {
  72. BOOST_CHECK (V2[ i ] == V1[ i ]);
  73. };
  74. // --------------------------- sorted elements ----------------------------
  75. V1.clear ( );
  76. for (uint32_t i = 0; i < NElem; ++i) V1.push_back (i);
  77. flat_stable_sort (V1.begin ( ), V1.end ( ), comp);
  78. for (unsigned i = 1; i < NElem; i++) {
  79. BOOST_CHECK (V1[ i - 1 ] <= V1[ i ]);
  80. };
  81. //-------------------------- reverse sorted elements ----------------------
  82. V1.clear ( );
  83. for (uint32_t i = 0; i < NElem; ++i) V1.push_back (NElem - i);
  84. flat_stable_sort (V1.begin ( ), V1.end ( ), comp);
  85. for (unsigned i = 1; i < NElem; i++) {
  86. BOOST_CHECK (V1[ i - 1 ] <= V1[ i ]);
  87. };
  88. //---------------------------- equal elements ----------------------------
  89. V1.clear ( );
  90. for (uint32_t i = 0; i < NElem; ++i) V1.push_back (1000);
  91. flat_stable_sort (V1.begin ( ), V1.end ( ), comp);
  92. for (unsigned i = 1; i < NElem; i++) {
  93. BOOST_CHECK (V1[ i - 1 ] == V1[ i ]);
  94. };
  95. };
  96. void test3 (void)
  97. {
  98. typedef typename std::vector<xk>::iterator iter_t;
  99. typedef std::less<xk> compare_t;
  100. std::mt19937 my_rand (0);
  101. std::vector<xk> V ;
  102. const uint32_t NELEM = 100000;
  103. V.reserve(NELEM * 10);
  104. for (uint32_t k =0 ; k < 10 ; ++k)
  105. { for ( uint32_t i =0 ; i < NELEM ; ++i)
  106. { V.emplace_back(i , k);
  107. };
  108. iter_t first = V.begin() + (k * NELEM);
  109. iter_t last = first + NELEM ;
  110. std::shuffle( first, last, my_rand);
  111. };
  112. flat_stable_sort( V.begin() , V.end(), compare_t());
  113. for ( uint32_t i =0 ; i < ( NELEM * 10); ++i)
  114. { BOOST_CHECK ( V[i].num == (i / 10) and V[i].tail == (i %10) );
  115. };
  116. }
  117. int test_main (int, char *[])
  118. {
  119. test1 ( );
  120. test2 ( );
  121. test3 ( );
  122. return 0;
  123. };