| 1 | /* |
| 2 | Copyright (c) Marshall Clow 2011-2012. |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | For more information, see http://www.boost.org |
| 8 | */ |
| 9 | |
| 10 | #include <iostream> |
| 11 | |
| 12 | #include <boost/config.hpp> |
| 13 | #include <boost/algorithm/cxx11/is_partitioned.hpp> |
| 14 | |
| 15 | #define BOOST_TEST_MAIN |
| 16 | #include <boost/test/unit_test.hpp> |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | #include <list> |
| 21 | |
| 22 | namespace ba = boost::algorithm; |
| 23 | // namespace ba = boost; |
| 24 | |
| 25 | template <typename T> |
| 26 | struct less_than { |
| 27 | public: |
| 28 | BOOST_CXX14_CONSTEXPR less_than ( T foo ) : val ( foo ) {} |
| 29 | BOOST_CXX14_CONSTEXPR less_than ( const less_than &rhs ) : val ( rhs.val ) {} |
| 30 | |
| 31 | BOOST_CXX14_CONSTEXPR bool operator () ( const T &v ) const { return v < val; } |
| 32 | private: |
| 33 | less_than (); |
| 34 | less_than operator = ( const less_than &rhs ); |
| 35 | T val; |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | BOOST_CXX14_CONSTEXPR bool test_constexpr() { |
| 40 | int v[] = { 4, 5, 6, 7, 8, 9, 10 }; |
| 41 | bool res = true; |
| 42 | res = ( res && ba::is_partitioned ( r: v, p: less_than<int>(3))); // no elements |
| 43 | res = ( res && ba::is_partitioned ( r: v, p: less_than<int>(5))); // only the first element |
| 44 | res = ( res && ba::is_partitioned ( r: v, p: less_than<int>(8))); // in the middle somewhere |
| 45 | res = ( res && ba::is_partitioned ( r: v, p: less_than<int>(99))); // all elements |
| 46 | return res; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void test_sequence1 () { |
| 51 | std::vector<int> v; |
| 52 | |
| 53 | v.clear (); |
| 54 | for ( int i = 5; i < 15; ++i ) |
| 55 | v.push_back ( x: i ); |
| 56 | BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(3))); // no elements |
| 57 | BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(6))); // only the first element |
| 58 | BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(10))); // in the middle somewhere |
| 59 | BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(99))); // all elements satisfy |
| 60 | |
| 61 | // With bidirectional iterators. |
| 62 | std::list<int> l; |
| 63 | for ( int i = 5; i < 15; ++i ) |
| 64 | l.push_back ( x: i ); |
| 65 | BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(3))); // no elements |
| 66 | BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(6))); // only the first element |
| 67 | BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(10))); // in the middle somewhere |
| 68 | BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(99))); // all elements satisfy |
| 69 | } |
| 70 | |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE( test_main ) |
| 73 | { |
| 74 | test_sequence1 (); |
| 75 | BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr (); |
| 76 | BOOST_CHECK ( constexpr_res ); |
| 77 | } |
| 78 | |