find_not_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (c) T. Zachary Laine 2018.
  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. For more information, see http://www.boost.org
  6. */
  7. #include <iostream>
  8. #include <boost/algorithm/find_not.hpp>
  9. #define BOOST_TEST_MAIN
  10. #include <boost/test/unit_test.hpp>
  11. #include <vector>
  12. #include <list>
  13. namespace ba = boost::algorithm;
  14. template <typename Container>
  15. struct dist_t
  16. {
  17. dist_t(Container & cont) : cont_(cont) {}
  18. template<typename Iter>
  19. std::ptrdiff_t operator()(Iter it) const
  20. {
  21. return std::distance(cont_.begin(), it);
  22. }
  23. Container & cont_;
  24. };
  25. BOOST_CXX14_CONSTEXPR bool check_constexpr()
  26. {
  27. int in_data[] = {2, 2, 3, 4, 5};
  28. bool res = true;
  29. const int* from = in_data;
  30. const int* to = in_data + 5;
  31. const int* start = ba::find_not(from, to, 1); // stops on first
  32. res = (res && start == from);
  33. start = ba::find_not(in_data, 1); // stops on first
  34. res = (res && start == from);
  35. int in_data_2[] = {6, 6, 6, 6, 6};
  36. const int* end = ba::find_not(in_data_2, in_data_2 + 5, 6); // stops on the end
  37. res = (res && end == in_data_2 + 5);
  38. end = ba::find_not(in_data_2, 6); // stops on the end
  39. res = (res && end == in_data_2 + 5);
  40. const int* three = ba::find_not(from, to, 2); // stops on third element
  41. res = (res && three == in_data + 2);
  42. three = ba::find_not(in_data, 2); // stops on third element
  43. res = (res && three == in_data + 2);
  44. return res;
  45. }
  46. void test_sequence()
  47. {
  48. {
  49. std::vector<int> v1;
  50. const dist_t<std::vector<int> > dist(v1);
  51. for (int i = 5; i < 15; ++i)
  52. v1.push_back(i);
  53. BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
  54. BOOST_CHECK_EQUAL(
  55. dist(ba::find_not(v1.begin(), v1.end(), v1.back())), 0);
  56. BOOST_CHECK_EQUAL(
  57. dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1);
  58. BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
  59. BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), 0);
  60. BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), 1);
  61. v1 = std::vector<int>(10, 2);
  62. BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
  63. BOOST_CHECK_EQUAL(
  64. dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size());
  65. BOOST_CHECK_EQUAL(
  66. dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size());
  67. BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
  68. BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), v1.size());
  69. BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), v1.size());
  70. }
  71. // With bidirectional iterators.
  72. {
  73. std::list<int> l1;
  74. const dist_t<std::list<int> > dist(l1);
  75. for (int i = 5; i < 15; ++i)
  76. l1.push_back(i);
  77. BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
  78. BOOST_CHECK_EQUAL(
  79. dist(ba::find_not(l1.begin(), l1.end(), l1.back())), 0);
  80. BOOST_CHECK_EQUAL(
  81. dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1);
  82. BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
  83. BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), 0);
  84. BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), 1);
  85. l1.clear();
  86. for (int i = 0; i < 10; ++i)
  87. l1.push_back(2);
  88. BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
  89. BOOST_CHECK_EQUAL(
  90. dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size());
  91. BOOST_CHECK_EQUAL(
  92. dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size());
  93. BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
  94. BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), l1.size());
  95. BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), l1.size());
  96. }
  97. BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr();
  98. BOOST_CHECK(ce_result);
  99. }
  100. BOOST_AUTO_TEST_CASE(test_main)
  101. {
  102. test_sequence();
  103. }