is_sorted_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*==============================================================================
  2. Copyright (c) 2010-2011 Bryce Lelbach
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <boost/config.hpp>
  8. #include <boost/array.hpp>
  9. #include <boost/detail/is_sorted.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. template<class T>
  12. struct tracking_less {
  13. typedef bool result_type;
  14. typedef T first_argument_type;
  15. typedef T second_argument_type;
  16. #if defined(__PATHSCALE__)
  17. tracking_less (void) { }
  18. ~tracking_less (void) { }
  19. #endif
  20. bool operator() (T const& x, T const& y) const {
  21. std::cout << x << " < " << y << " == " << (x < y) << "\n";
  22. return x < y;
  23. }
  24. };
  25. template<class T>
  26. struct tracking_less_equal {
  27. typedef bool result_type;
  28. typedef T first_argument_type;
  29. typedef T second_argument_type;
  30. #if defined(__PATHSCALE__)
  31. tracking_less_equal (void) { }
  32. ~tracking_less_equal (void) { }
  33. #endif
  34. bool operator() (T const& x, T const& y) const {
  35. std::cout << x << " <= " << y << " == " << (x <= y) << "\n";
  36. return x <= y;
  37. }
  38. };
  39. template<class T>
  40. struct tracking_greater {
  41. typedef bool result_type;
  42. typedef T first_argument_type;
  43. typedef T second_argument_type;
  44. #if defined(__PATHSCALE__)
  45. tracking_greater (void) { }
  46. ~tracking_greater (void) { }
  47. #endif
  48. bool operator() (T const& x, T const& y) const {
  49. std::cout << x << " > " << y << " == " << (x > y) << "\n";
  50. return x > y;
  51. }
  52. };
  53. template<class T>
  54. struct tracking_greater_equal {
  55. typedef bool result_type;
  56. typedef T first_argument_type;
  57. typedef T second_argument_type;
  58. #if defined(__PATHSCALE__)
  59. tracking_greater_equal (void) { }
  60. ~tracking_greater_equal (void) { }
  61. #endif
  62. bool operator() (T const& x, T const& y) const {
  63. std::cout << x << " >= " << y << " == " << (x >= y) << "\n";
  64. return x >= y;
  65. }
  66. };
  67. int main (void) {
  68. #define IS_SORTED ::boost::detail::is_sorted
  69. #define IS_SORTED_UNTIL ::boost::detail::is_sorted_until
  70. using boost::array;
  71. using boost::report_errors;
  72. std::cout << std::boolalpha;
  73. array<int, 10> a = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
  74. array<int, 10> b = { { 0, 1, 1, 2, 5, 8, 13, 34, 55, 89 } };
  75. array<int, 10> c = { { 0, 1, -1, 2, -3, 5, -8, 13, -21, 34 } };
  76. tracking_less<int> lt;
  77. tracking_less_equal<int> lte;
  78. tracking_greater<int> gt;
  79. tracking_greater_equal<int> gte;
  80. BOOST_TEST_EQ(IS_SORTED_UNTIL(a.begin(), a.end()), a.end());
  81. BOOST_TEST_EQ(IS_SORTED_UNTIL(a.begin(), a.end(), lt), a.end());
  82. BOOST_TEST_EQ(IS_SORTED_UNTIL(a.begin(), a.end(), lte), a.end());
  83. BOOST_TEST_EQ(IS_SORTED_UNTIL(a.rbegin(), a.rend(), gt).base(), a.rend().base());
  84. BOOST_TEST_EQ(IS_SORTED_UNTIL(a.rbegin(), a.rend(), gte).base(), a.rend().base());
  85. BOOST_TEST_EQ(IS_SORTED(a.begin(), a.end()), true);
  86. BOOST_TEST_EQ(IS_SORTED(a.begin(), a.end(), lt), true);
  87. BOOST_TEST_EQ(IS_SORTED(a.begin(), a.end(), lte), true);
  88. BOOST_TEST_EQ(IS_SORTED(a.rbegin(), a.rend(), gt), true);
  89. BOOST_TEST_EQ(IS_SORTED(a.rbegin(), a.rend(), gte), true);
  90. BOOST_TEST_EQ(IS_SORTED_UNTIL(b.begin(), b.end()), b.end());
  91. BOOST_TEST_EQ(IS_SORTED_UNTIL(b.begin(), b.end(), lt), b.end());
  92. BOOST_TEST_EQ(IS_SORTED_UNTIL(b.begin(), b.end(), lte), &b[2]);
  93. BOOST_TEST_EQ(IS_SORTED_UNTIL(b.rbegin(), b.rend(), gt).base(), b.rend().base());
  94. BOOST_TEST_EQ(IS_SORTED_UNTIL(b.rbegin(), b.rend(), gte).base(), &b[2]);
  95. BOOST_TEST_EQ(IS_SORTED(b.begin(), b.end()), true);
  96. BOOST_TEST_EQ(IS_SORTED(b.begin(), b.end(), lt), true);
  97. BOOST_TEST_EQ(IS_SORTED(b.begin(), b.end(), lte), false);
  98. BOOST_TEST_EQ(IS_SORTED(b.rbegin(), b.rend(), gt), true);
  99. BOOST_TEST_EQ(IS_SORTED(b.rbegin(), b.rend(), gte), false);
  100. BOOST_TEST_EQ(IS_SORTED_UNTIL(c.begin(), c.end()), &c[2]);
  101. BOOST_TEST_EQ(IS_SORTED_UNTIL(c.begin(), c.end(), lt), &c[2]);
  102. BOOST_TEST_EQ(IS_SORTED_UNTIL(c.begin(), c.end(), lte), &c[2]);
  103. BOOST_TEST_EQ(IS_SORTED_UNTIL(c.rbegin(), c.rend(), gt).base(), &c[8]);
  104. BOOST_TEST_EQ(IS_SORTED_UNTIL(c.rbegin(), c.rend(), gte).base(), &c[8]);
  105. BOOST_TEST_EQ(IS_SORTED(c.begin(), c.end()), false);
  106. BOOST_TEST_EQ(IS_SORTED(c.begin(), c.end(), lt), false);
  107. BOOST_TEST_EQ(IS_SORTED(c.begin(), c.end(), lte), false);
  108. BOOST_TEST_EQ(IS_SORTED(c.rbegin(), c.rend(), gt), false);
  109. BOOST_TEST_EQ(IS_SORTED(c.rbegin(), c.rend(), gte), false);
  110. return report_errors();
  111. }