ordered_test.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2010 Nuovation System Designs, LLC
  2. // Grant Erickson <gerickson@nuovations.com>
  3. //
  4. // Reworked by Marshall Clow; August 2010
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/ for latest version.
  11. #include <algorithm>
  12. #include <iostream>
  13. #include <boost/algorithm/cxx11/is_sorted.hpp>
  14. #define BOOST_TEST_MAIN
  15. #include <boost/test/unit_test.hpp>
  16. using namespace boost;
  17. /* Preprocessor Defines */
  18. #define elementsof(v) (sizeof (v) / sizeof (v[0]))
  19. #define a_begin(v) (&v[0])
  20. #define a_end(v) (v + elementsof (v))
  21. #define a_range(v) v
  22. #define b_e(v) a_begin(v),a_end(v)
  23. namespace ba = boost::algorithm;
  24. BOOST_CXX14_CONSTEXPR bool less( int x, int y ) { return x < y; }
  25. static void
  26. test_ordered(void)
  27. {
  28. BOOST_CXX14_CONSTEXPR const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
  29. BOOST_CXX14_CONSTEXPR const int randomValues[] = { 3, 6, 1, 2, 7 };
  30. const int constantValues[] = { 1, 2, 2, 2, 5 };
  31. int nonConstantArray[] = { 1, 2, 2, 2, 5 };
  32. const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 };
  33. // Begin/end checks
  34. BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues)));
  35. BOOST_CHECK ( !ba::is_sorted (b_e(randomValues)));
  36. BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues), std::less<int>()));
  37. BOOST_CHECK ( !ba::is_sorted (b_e(strictlyIncreasingValues), std::greater<int>()));
  38. // Range checks
  39. BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues)));
  40. BOOST_CHECK ( !ba::is_sorted (a_range(randomValues)));
  41. BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues), std::less<int>()));
  42. BOOST_CHECK ( !ba::is_sorted (a_range(strictlyIncreasingValues), std::greater<int>()));
  43. BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues)) == a_end(strictlyIncreasingValues));
  44. BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less<int>()) == a_end(strictlyIncreasingValues));
  45. BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues)) == boost::end(strictlyIncreasingValues));
  46. BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues));
  47. // Check for const and non-const arrays
  48. BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less<int>()) == a_end(constantValues));
  49. BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less<int>()) == boost::end(constantValues));
  50. BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) == a_end(nonConstantArray));
  51. BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) == boost::end(nonConstantArray));
  52. BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
  53. BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues)) == &randomValues[2] );
  54. BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
  55. BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues)) == &randomValues[2] );
  56. BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less<int>()) == &inOrderUntilTheEnd[8] );
  57. BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd)) == &inOrderUntilTheEnd[8] );
  58. // For zero and one element collections, the comparison predicate should never be called
  59. BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to<int>()) == a_begin(randomValues));
  60. BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues)) == a_begin(randomValues));
  61. BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1);
  62. BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 ) == a_begin(randomValues) + 1);
  63. BOOST_CXX14_CONSTEXPR bool constexpr_res = (
  64. ba::is_sorted ( boost::begin(strictlyIncreasingValues), boost::end(strictlyIncreasingValues) )
  65. && !ba::is_sorted (a_range(randomValues))
  66. && ba::is_sorted_until ( boost::begin(strictlyIncreasingValues), boost::end(strictlyIncreasingValues), less) == a_end(strictlyIncreasingValues)
  67. && ba::is_sorted_until ( randomValues, less) == &randomValues[2]
  68. );
  69. BOOST_CHECK ( constexpr_res );
  70. }
  71. static void
  72. test_increasing_decreasing(void)
  73. {
  74. BOOST_CXX14_CONSTEXPR const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
  75. BOOST_CXX14_CONSTEXPR const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 };
  76. BOOST_CXX14_CONSTEXPR const int increasingValues[] = { 1, 2, 2, 2, 5 };
  77. BOOST_CXX14_CONSTEXPR const int decreasingValues[] = { 9, 7, 7, 7, 5 };
  78. BOOST_CXX14_CONSTEXPR const int randomValues[] = { 3, 6, 1, 2, 7 };
  79. BOOST_CXX14_CONSTEXPR const int constantValues[] = { 7, 7, 7, 7, 7 };
  80. // Test a strictly increasing sequence
  81. BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues)));
  82. BOOST_CHECK ( ba::is_increasing (b_e(strictlyIncreasingValues)));
  83. BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(strictlyIncreasingValues)));
  84. BOOST_CHECK ( !ba::is_decreasing (b_e(strictlyIncreasingValues)));
  85. BOOST_CHECK ( ba::is_strictly_increasing (a_range(strictlyIncreasingValues)));
  86. BOOST_CHECK ( ba::is_increasing (a_range(strictlyIncreasingValues)));
  87. BOOST_CHECK ( !ba::is_strictly_decreasing (a_range(strictlyIncreasingValues)));
  88. BOOST_CHECK ( !ba::is_decreasing (a_range(strictlyIncreasingValues)));
  89. // Test a strictly decreasing sequence
  90. BOOST_CHECK ( !ba::is_strictly_increasing (b_e(strictlyDecreasingValues)));
  91. BOOST_CHECK ( !ba::is_increasing (b_e(strictlyDecreasingValues)));
  92. BOOST_CHECK ( ba::is_strictly_decreasing (b_e(strictlyDecreasingValues)));
  93. BOOST_CHECK ( ba::is_decreasing (b_e(strictlyDecreasingValues)));
  94. // Test an increasing sequence
  95. BOOST_CHECK ( !ba::is_strictly_increasing (b_e(increasingValues)));
  96. BOOST_CHECK ( ba::is_increasing (b_e(increasingValues)));
  97. BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(increasingValues)));
  98. BOOST_CHECK ( !ba::is_decreasing (b_e(increasingValues)));
  99. // Test a decreasing sequence
  100. BOOST_CHECK ( !ba::is_strictly_increasing (b_e(decreasingValues)));
  101. BOOST_CHECK ( !ba::is_increasing (b_e(decreasingValues)));
  102. BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(decreasingValues)));
  103. BOOST_CHECK ( ba::is_decreasing (b_e(decreasingValues)));
  104. // Test a random sequence
  105. BOOST_CHECK ( !ba::is_strictly_increasing (b_e(randomValues)));
  106. BOOST_CHECK ( !ba::is_increasing (b_e(randomValues)));
  107. BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(randomValues)));
  108. BOOST_CHECK ( !ba::is_decreasing (b_e(randomValues)));
  109. // Test a constant sequence
  110. BOOST_CHECK ( !ba::is_strictly_increasing (b_e(constantValues)));
  111. BOOST_CHECK ( ba::is_increasing (b_e(constantValues)));
  112. BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(constantValues)));
  113. BOOST_CHECK ( ba::is_decreasing (b_e(constantValues)));
  114. // Test an empty sequence
  115. BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues));
  116. BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues));
  117. BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues));
  118. BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues));
  119. // Test a one-element sequence
  120. BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
  121. BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
  122. BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
  123. BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
  124. // Test a two-element sequence
  125. BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
  126. BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
  127. BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
  128. BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
  129. BOOST_CXX14_CONSTEXPR bool constexpr_res = (
  130. ba::is_increasing (boost::begin(increasingValues), boost::end(increasingValues))
  131. && ba::is_decreasing (boost::begin(decreasingValues), boost::end(decreasingValues))
  132. && ba::is_strictly_increasing (boost::begin(strictlyIncreasingValues), boost::end(strictlyIncreasingValues))
  133. && ba::is_strictly_decreasing (boost::begin(strictlyDecreasingValues), boost::end(strictlyDecreasingValues))
  134. && !ba::is_strictly_increasing (boost::begin(increasingValues), boost::end(increasingValues))
  135. && !ba::is_strictly_decreasing (boost::begin(decreasingValues), boost::end(decreasingValues))
  136. );
  137. BOOST_CHECK ( constexpr_res );
  138. }
  139. BOOST_AUTO_TEST_CASE( test_main )
  140. {
  141. test_ordered ();
  142. test_increasing_decreasing ();
  143. }