integer_sort_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Boost Sort library int_test.cpp file ------------------------------------//
  2. // Copyright Steven Ross 2009-2014. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/sort for library home page.
  7. #include <boost/cstdint.hpp>
  8. #include <boost/sort/spreadsort/spreadsort.hpp>
  9. // Include unit test framework
  10. #include <boost/test/included/test_exec_monitor.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <vector>
  13. #include <iostream>
  14. using namespace std;
  15. using namespace boost::sort::spreadsort;
  16. struct rightshift {
  17. int operator()(int x, unsigned offset) { return x >> offset; }
  18. };
  19. struct rightshift_max {
  20. boost::intmax_t operator()(const boost::intmax_t &x, unsigned offset) {
  21. return x >> offset;
  22. }
  23. };
  24. struct negrightshift {
  25. int operator()(const int &x, const unsigned offset) { return -(x >> offset); }
  26. };
  27. struct negrightshift_max {
  28. boost::intmax_t operator()(const boost::intmax_t &x, const unsigned offset) {
  29. return -(x >> offset);
  30. }
  31. };
  32. boost::int32_t
  33. rand_32(bool sign = true) {
  34. boost::int32_t result = rand() | (rand()<< 16);
  35. if (rand() % 2)
  36. result |= 1 << 15;
  37. //Adding the sign bit
  38. if (sign && (rand() % 2))
  39. result *= -1;
  40. return result;
  41. }
  42. void int_test()
  43. {
  44. // Prepare inputs
  45. vector<int> base_vec;
  46. unsigned count = 100000;
  47. srand(1);
  48. //Generating semirandom numbers
  49. for (unsigned u = 0; u < count; ++u)
  50. base_vec.push_back(rand_32());
  51. vector<int> sorted_vec = base_vec;
  52. vector<int> test_vec = base_vec;
  53. std::sort(sorted_vec.begin(), sorted_vec.end());
  54. //Testing basic call
  55. integer_sort(test_vec.begin(), test_vec.end());
  56. BOOST_CHECK(test_vec == sorted_vec);
  57. //boost::sort::spreadsort variant
  58. test_vec = base_vec;
  59. boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());
  60. BOOST_CHECK(test_vec == sorted_vec);
  61. //One functor
  62. test_vec = base_vec;
  63. integer_sort(test_vec.begin(), test_vec.end(), rightshift());
  64. BOOST_CHECK(test_vec == sorted_vec);
  65. //Both functors
  66. test_vec = base_vec;
  67. integer_sort(test_vec.begin(), test_vec.end(), rightshift(), less<int>());
  68. BOOST_CHECK(test_vec == sorted_vec);
  69. //reverse order
  70. std::sort(sorted_vec.begin(), sorted_vec.end(), greater<int>());
  71. integer_sort(test_vec.begin(), test_vec.end(), negrightshift(),
  72. greater<int>());
  73. BOOST_CHECK(test_vec == sorted_vec);
  74. //Making sure we're correctly sorting boost::intmax_ts; should use std::sort
  75. vector<boost::intmax_t> long_base_vec;
  76. for (unsigned u = 0; u < base_vec.size(); ++u)
  77. long_base_vec.push_back((((boost::intmax_t)rand_32()) <<
  78. ((8 * sizeof(int)) -1)) + rand_32(false));
  79. vector<boost::intmax_t> long_sorted_vec = long_base_vec;
  80. vector<boost::intmax_t> long_test_vec = long_base_vec;
  81. integer_sort(long_test_vec.begin(), long_test_vec.end());
  82. std::sort(long_sorted_vec.begin(), long_sorted_vec.end());
  83. BOOST_CHECK(long_test_vec == long_sorted_vec);
  84. //One functor
  85. long_test_vec = long_base_vec;
  86. integer_sort(long_test_vec.begin(), long_test_vec.end(), rightshift_max());
  87. BOOST_CHECK(long_test_vec == long_sorted_vec);
  88. //Both functors
  89. long_test_vec = long_base_vec;
  90. integer_sort(long_test_vec.begin(), long_test_vec.end(), rightshift_max(),
  91. less<boost::intmax_t>());
  92. BOOST_CHECK(long_test_vec == long_sorted_vec);
  93. //reverse order
  94. std::sort(long_sorted_vec.begin(), long_sorted_vec.end(),
  95. greater<boost::intmax_t>());
  96. integer_sort(long_test_vec.begin(), long_test_vec.end(), negrightshift_max(),
  97. greater<boost::intmax_t>());
  98. BOOST_CHECK(long_test_vec == long_sorted_vec);
  99. }
  100. // Verify that 0 and 1 elements work correctly.
  101. void corner_test() {
  102. vector<int> test_vec;
  103. boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());
  104. const int test_value = 42;
  105. test_vec.push_back(test_value);
  106. boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());
  107. BOOST_CHECK(test_vec.size() == 1);
  108. BOOST_CHECK(test_vec[0] == test_value);
  109. }
  110. // test main
  111. int test_main( int, char*[] )
  112. {
  113. int_test();
  114. corner_test();
  115. return 0;
  116. }