Added c++11 algorithms to Boost.Algorithm

[SVN r77060]
This commit is contained in:
Marshall Clow
2012-02-18 07:17:39 +00:00
parent 8bfaa6dad3
commit 43c01ff2bc
26 changed files with 2380 additions and 0 deletions

View File

@ -22,6 +22,23 @@ import testing ;
# Clamp tests
[ run clamp_test.cpp : : : : clamp_test ]
# Cxx11 tests
[ run all_of_test.cpp : : : : all_of_test ]
[ run any_of_test.cpp : : : : any_of_test ]
[ run none_of_test.cpp : : : : none_of_test ]
[ run one_of_test.cpp : : : : one_of_test ]
[ run ordered_test.cpp : : : : ordered_test ]
[ run find_if_not_test1.cpp : : : : find_if_not_test1 ]
[ run copy_n_test1.cpp : : : : copy_n_test1 ]
[ run iota_test1.cpp : : : : iota_test1 ]
[ run is_permutation_test1.cpp : : : : is_permutation_test1 ]
[ run partition_point_test1.cpp : : : : partition_point_test1 ]
[ run is_partitioned_test1.cpp : : : : is_partitioned_test1 ]
[ run partition_copy_test1.cpp : : : : partition_copy_test1 ]
;
}

86
test/all_of_test.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
Copyright (c) Marshall Clow 2010-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <functional>
#include <vector>
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
T val_;
};
namespace ba = boost::algorithm;
void test_all ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 1, 1, 18, 10 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK (!ba::all_of_equal ( vi, 1 ));
BOOST_CHECK (!ba::all_of ( vi, is_<int> ( 1 )));
BOOST_CHECK (!ba::all_of_equal ( vi.begin(), vi.end(), 1 ));
BOOST_CHECK (!ba::all_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
BOOST_CHECK (!ba::all_of_equal ( vi, 0 ));
BOOST_CHECK (!ba::all_of ( vi, is_<int> ( 0 )));
BOOST_CHECK (!ba::all_of_equal ( vi.begin(), vi.end(), 0 ));
BOOST_CHECK (!ba::all_of ( vi.begin(), vi.end(), is_<int> ( 0 )));
BOOST_CHECK ( ba::all_of_equal ( vi.end(), vi.end(), 0 ));
BOOST_CHECK ( ba::all_of ( vi.end(), vi.end(), is_<int> ( 0 )));
BOOST_CHECK ( ba::all_of_equal ( vi.begin(), vi.begin () + 3, 1 ));
BOOST_CHECK ( ba::all_of ( vi.begin(), vi.begin () + 3, is_<int> ( 1 )));
BOOST_CHECK ( ba::all_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' ));
BOOST_CHECK ( ba::all_of ( vc.begin() + 1, vc.begin() + 2, is_<char> ( 'q' )));
BOOST_CHECK (!ba::all_of_equal ( vc, '!' ));
BOOST_CHECK (!ba::all_of ( vc, is_<char> ( '!' )));
BOOST_CHECK ( ba::all_of_equal ( vi.begin(), vi.begin(), 1 ));
BOOST_CHECK ( ba::all_of_equal ( vc.begin(), vc.begin(), 'a' ));
BOOST_CHECK ( ba::all_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
BOOST_CHECK ( ba::all_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
BOOST_CHECK (!ba::all_of_equal ( li, 1 ));
BOOST_CHECK (!ba::all_of ( li, is_<int> ( 1 )));
BOOST_CHECK (!ba::all_of_equal ( li.begin(), li.end(), 1 ));
BOOST_CHECK (!ba::all_of ( li.begin(), li.end(), is_<int> ( 1 )));
std::list<int>::iterator l_iter = li.begin ();
l_iter++; l_iter++; l_iter++;
BOOST_CHECK ( ba::all_of_equal ( li.begin(), l_iter, 1 ));
BOOST_CHECK ( ba::all_of ( li.begin(), l_iter, is_<int> ( 1 )));
}
int test_main( int , char* [] )
{
test_all ();
return 0;
}

105
test/any_of_test.cpp Normal file
View File

@ -0,0 +1,105 @@
/*
Copyright (c) Marshall Clow 2010-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <functional>
#include <vector>
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
T val_;
};
namespace ba = boost::algorithm;
void test_any ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 10 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK ( ba::any_of_equal ( vi, 1 ));
BOOST_CHECK ( ba::any_of ( vi, is_<int> ( 1 )));
BOOST_CHECK ( ba::any_of_equal ( vi.begin(), vi.end(), 1 ));
BOOST_CHECK ( ba::any_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
BOOST_CHECK (!ba::any_of_equal ( vi, 9 ));
BOOST_CHECK (!ba::any_of ( vi, is_<int> ( 9 )));
BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.end(), 9 ));
BOOST_CHECK (!ba::any_of ( vi.begin(), vi.end(), is_<int> ( 9 )));
BOOST_CHECK ( ba::any_of_equal ( vi, 10 ));
BOOST_CHECK ( ba::any_of ( vi, is_<int> ( 10 )));
BOOST_CHECK (!ba::any_of_equal ( vi, 4 ));
BOOST_CHECK (!ba::any_of ( vi, is_<int> ( 4 )));
BOOST_CHECK (!ba::any_of_equal ( vi.end(), vi.end(), 0 ));
BOOST_CHECK (!ba::any_of ( vi.end(), vi.end(), is_<int> ( 0 )));
// 5 is not in { 0, 18, 10 }, but 10 is
BOOST_CHECK ( ba::any_of_equal ( vi.begin() + 2, vi.end(), 10 ));
BOOST_CHECK ( ba::any_of ( vi.begin() + 2, vi.end(), is_<int> ( 10 )));
BOOST_CHECK (!ba::any_of_equal ( vi.begin() + 2, vi.end(), 5 ));
BOOST_CHECK (!ba::any_of ( vi.begin() + 2, vi.end(), is_<int> ( 5 )));
// 18 is not in { 1, 5, 0 }, but 5 is
BOOST_CHECK ( ba::any_of_equal ( vi.begin(), vi.begin() + 3, 5 ));
BOOST_CHECK ( ba::any_of ( vi.begin(), vi.begin() + 3, is_<int> ( 5 )));
BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.begin() + 3, 18 ));
BOOST_CHECK (!ba::any_of ( vi.begin(), vi.begin() + 3, is_<int> ( 18 )));
BOOST_CHECK ( ba::any_of_equal ( vc, 'q' ));
BOOST_CHECK ( ba::any_of ( vc, is_<char> ( 'q' )));
BOOST_CHECK (!ba::any_of_equal ( vc, '!' ));
BOOST_CHECK (!ba::any_of ( vc, is_<char> ( '!' )));
BOOST_CHECK ( ba::any_of_equal ( vc, 'n' ));
BOOST_CHECK ( ba::any_of ( vc, is_<char> ( 'n' )));
BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.begin(), 1 ));
BOOST_CHECK (!ba::any_of_equal ( vc.begin(), vc.begin(), 'a' ));
BOOST_CHECK (!ba::any_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
BOOST_CHECK (!ba::any_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
BOOST_CHECK ( ba::any_of_equal ( li, 1 ));
BOOST_CHECK ( ba::any_of ( li, is_<int> ( 1 )));
BOOST_CHECK ( ba::any_of_equal ( li.begin(), li.end(), 1 ));
BOOST_CHECK ( ba::any_of ( li.begin(), li.end(), is_<int> ( 1 )));
std::list<int>::iterator l_iter = li.begin ();
l_iter++; l_iter++; l_iter++;
BOOST_CHECK ( ba::any_of_equal ( li.begin(), l_iter, 5 ));
BOOST_CHECK ( ba::any_of ( li.begin(), l_iter, is_<int> ( 5 )));
BOOST_CHECK (!ba::any_of_equal ( li.begin(), l_iter, 18 ));
BOOST_CHECK (!ba::any_of ( li.begin(), l_iter, is_<int> ( 18 )));
}
int test_main( int , char* [] )
{
test_any ();
return 0;
}

85
test/copy_n_test1.cpp Normal file
View File

@ -0,0 +1,85 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/copy_n.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <iostream>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
template <typename Container>
void test_sequence ( Container const &c ) {
typedef typename Container::value_type value_type;
std::vector<value_type> v;
// Copy zero elements
v.clear ();
ba::copy_n ( c.begin (), 0, back_inserter ( v ));
BOOST_CHECK ( v.size () == 0 );
ba::copy_n ( c.begin (), 0U, back_inserter ( v ));
BOOST_CHECK ( v.size () == 0 );
if ( c.size () > 0 ) {
// Just one element
v.clear ();
ba::copy_n ( c.begin (), 1, back_inserter ( v ));
BOOST_CHECK ( v.size () == 1 );
BOOST_CHECK ( v[0] == *c.begin ());
v.clear ();
ba::copy_n ( c.begin (), 1U, back_inserter ( v ));
BOOST_CHECK ( v.size () == 1 );
BOOST_CHECK ( v[0] == *c.begin ());
// Half the elements
v.clear ();
ba::copy_n ( c.begin (), c.size () / 2, back_inserter ( v ));
BOOST_CHECK ( v.size () == c.size () / 2);
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
// Half the elements + 1
v.clear ();
ba::copy_n ( c.begin (), c.size () / 2 + 1, back_inserter ( v ));
BOOST_CHECK ( v.size () == c.size () / 2 + 1 );
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
// All the elements
v.clear ();
ba::copy_n ( c.begin (), c.size (), back_inserter ( v ));
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
}
}
void test_sequence1 () {
std::vector<int> v;
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
test_sequence ( v );
std::list<int> l;
for ( int i = 25; i > 15; --i )
l.push_back ( i );
test_sequence ( l );
}
int test_main( int , char* [] )
{
test_sequence1 ();
return 0;
}

View File

@ -0,0 +1,90 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/find_if_not.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
template <typename Container>
typename Container::iterator offset_to_iter ( Container &v, int offset ) {
typename Container::iterator retval;
if ( offset >= 0 ) {
retval = v.begin ();
std::advance ( retval, offset );
}
else {
retval = v.end ();
std::advance ( retval, offset + 1 );
}
return retval;
}
template <typename Container, typename Predicate>
void test_sequence ( Container &v, Predicate comp, int expected ) {
typename Container::iterator res, exp;
res = ba::find_if_not ( v.begin (), v.end (), comp );
exp = offset_to_iter ( v, expected );
std::cout << "Expected(1): " << std::distance ( v.begin (), exp )
<< ", got: " << std::distance ( v.begin (), res ) << std::endl;
BOOST_CHECK ( exp == res );
}
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
T val;
};
void test_sequence1 () {
std::vector<int> v;
v.clear ();
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
test_sequence ( v, less_than<int>(3), 0 ); // no elements
test_sequence ( v, less_than<int>(6), 1 ); // only the first element
test_sequence ( v, less_than<int>(10), 5 );
test_sequence ( v, less_than<int>(99), -1 ); // all elements satisfy
// With bidirectional iterators.
std::list<int> l;
for ( int i = 5; i < 15; ++i )
l.push_back ( i );
test_sequence ( l, less_than<int>(3), 0 ); // no elements
test_sequence ( l, less_than<int>(6), 1 ); // only the first element
test_sequence ( l, less_than<int>(10), 5 );
test_sequence ( l, less_than<int>(99), -1 ); // all elements satisfy
}
int test_main( int , char* [] )
{
test_sequence1 ();
return 0;
}

79
test/iota_test1.cpp Normal file
View File

@ -0,0 +1,79 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <list>
// Test to make sure a sequence is "correctly formed"; i.e, ascending by one
template <typename Iterator, typename T>
bool test_iota_results ( Iterator first, Iterator last, T initial_value ) {
if ( first == last ) return true;
if ( initial_value != *first ) return false;
Iterator prev = first;
while ( ++first != last ) {
if (( *first - *prev ) != 1 )
return false;
prev = first;
}
return true;
}
template <typename Range, typename T>
bool test_iota_results ( const Range &r, T initial_value ) {
return test_iota_results (boost::begin (r), boost::end (r), initial_value );
}
void test_ints () {
std::vector<int> v;
std::list<int> l;
v.clear (); v.reserve ( 10 );
boost::algorithm::iota ( v.begin (), v.end (), 23 );
BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 ));
v.clear (); v.reserve ( 19 );
boost::algorithm::iota ( v, 18 );
BOOST_CHECK ( test_iota_results ( v, 18 ));
v.clear ();
boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 );
BOOST_CHECK ( test_iota_results ( v, 99 ));
/*
l.clear (); l.reserve ( 5 );
boost::algorithm::iota ( l.begin (), l.end (), 123 );
BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 123 ));
l.clear (); l.reserve ( 9 );
boost::algorithm::iota ( l.begin (), l.end (), 87 );
BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 87 ));
*/
l.clear ();
boost::algorithm::iota_n ( std::back_inserter(l), 99, 20 );
BOOST_CHECK ( test_iota_results ( l, 99 ));
l.clear ();
boost::algorithm::iota_n ( std::front_inserter(l), 123, 20 );
BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 ));
}
int test_main( int , char* [] )
{
test_ints ();
return 0;
}

View File

@ -0,0 +1,63 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
T val;
};
void test_sequence1 () {
std::vector<int> v;
v.clear ();
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(3))); // no elements
BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(6))); // only the first element
BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(10))); // in the middle somewhere
BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(99))); // all elements satisfy
// With bidirectional iterators.
std::list<int> l;
for ( int i = 5; i < 15; ++i )
l.push_back ( i );
BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(3))); // no elements
BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(6))); // only the first element
BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(10))); // in the middle somewhere
BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(99))); // all elements satisfy
}
int test_main( int , char* [] )
{
test_sequence1 ();
return 0;
}

View File

@ -0,0 +1,49 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/is_permutation.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
void test_sequence1 () {
std::vector<int> v, v1;
v.clear ();
for ( std::size_t i = 5; i < 15; ++i )
v.push_back ( i );
v1 = v;
BOOST_CHECK ( ba::is_permutation ( v.begin (), v.end (), v.begin ())); // better be a permutation of itself!
BOOST_CHECK ( ba::is_permutation ( v.begin (), v.end (), v1.begin ()));
// With bidirectional iterators.
std::list<int> l;
std::copy ( v.begin (), v.end (), std::back_inserter ( l ));
BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), l.begin ())); // better be a permutation of itself!
BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), v1.begin ()));
for ( std::size_t i = 0; i < l.size (); ++i ) {
l.push_back ( *l.begin ()); l.pop_front (); // rotation
BOOST_CHECK ( ba::is_permutation ( l.begin (), l.end (), v1.begin ()));
}
}
int test_main( int , char* [] )
{
test_sequence1 ();
return 0;
}

96
test/none_of_test.cpp Normal file
View File

@ -0,0 +1,96 @@
/*
Copyright (c) Marshall Clow 2010-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <functional>
#include <vector>
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
T val_;
};
namespace ba = boost::algorithm;
void test_none()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 1 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK ( ba::none_of_equal ( vi, 100 ));
BOOST_CHECK ( ba::none_of ( vi, is_<int> ( 100 )));
BOOST_CHECK ( ba::none_of_equal ( vi.begin(), vi.end(), 100 ));
BOOST_CHECK ( ba::none_of ( vi.begin(), vi.end(), is_<int> ( 100 )));
BOOST_CHECK (!ba::none_of_equal ( vi, 1 ));
BOOST_CHECK (!ba::none_of ( vi, is_<int> ( 1 )));
BOOST_CHECK (!ba::none_of_equal ( vi.begin(), vi.end(), 1 ));
BOOST_CHECK (!ba::none_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
BOOST_CHECK ( ba::none_of_equal ( vi.end(), vi.end(), 0 ));
BOOST_CHECK ( ba::none_of ( vi.end(), vi.end(), is_<int> ( 0 )));
// 5 is not in { 0, 18, 1 }, but 1 is
BOOST_CHECK ( ba::none_of_equal ( vi.begin() + 2, vi.end(), 5 ));
BOOST_CHECK ( ba::none_of ( vi.begin() + 2, vi.end(), is_<int> ( 5 )));
BOOST_CHECK (!ba::none_of_equal ( vi.begin() + 2, vi.end(), 1 ));
BOOST_CHECK (!ba::none_of ( vi.begin() + 2, vi.end(), is_<int> ( 1 )));
// 18 is not in { 1, 5, 0 }, but 5 is
BOOST_CHECK ( ba::none_of_equal ( vi.begin(), vi.begin() + 3, 18 ));
BOOST_CHECK ( ba::none_of ( vi.begin(), vi.begin() + 3, is_<int> ( 18 )));
BOOST_CHECK (!ba::none_of_equal ( vi.begin(), vi.begin() + 3, 5 ));
BOOST_CHECK (!ba::none_of ( vi.begin(), vi.begin() + 3, is_<int> ( 5 )));
BOOST_CHECK ( ba::none_of_equal ( vc, 'z' ));
BOOST_CHECK ( ba::none_of ( vc, is_<char> ( 'z' )));
BOOST_CHECK (!ba::none_of_equal ( vc, 'a' ));
BOOST_CHECK (!ba::none_of ( vc, is_<char> ( 'a' )));
BOOST_CHECK (!ba::none_of_equal ( vc, 'n' ));
BOOST_CHECK (!ba::none_of ( vc, is_<char> ( 'n' )));
BOOST_CHECK ( ba::none_of_equal ( vi.begin(), vi.begin(), 1 ));
BOOST_CHECK ( ba::none_of_equal ( vc.begin(), vc.begin(), 'a' ));
BOOST_CHECK ( ba::none_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
BOOST_CHECK ( ba::none_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
BOOST_CHECK ( ba::none_of_equal ( li, 100 ));
BOOST_CHECK ( ba::none_of ( li, is_<int> ( 100 )));
BOOST_CHECK ( ba::none_of_equal ( li.begin(), li.end(), 100 ));
BOOST_CHECK ( ba::none_of ( li.begin(), li.end(), is_<int> ( 100 )));
std::list<int>::iterator l_iter = li.begin ();
l_iter++; l_iter++; l_iter++;
BOOST_CHECK ( ba::none_of_equal ( li.begin(), l_iter, 18 ));
BOOST_CHECK ( ba::none_of ( li.begin(), l_iter, is_<int> ( 18 )));
BOOST_CHECK (!ba::none_of ( li.begin(), l_iter, is_<int> ( 5 )));
}
int test_main( int , char* [] )
{
test_none();
return 0;
}

101
test/one_of_test.cpp Normal file
View File

@ -0,0 +1,101 @@
/*
Copyright (c) Marshall Clow 2008-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/one_of.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <functional>
#include <vector>
#include <list>
template<typename T>
struct is_ : public std::unary_function<T, bool> {
is_ ( T v ) : val_ ( v ) {}
~is_ () {}
bool operator () ( T comp ) const { return val_ == comp; }
private:
is_ (); // need a value
T val_;
};
namespace ba = boost::algorithm;
void test_one ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 1, 2, 3, 5 };
std::vector<int> vi(some_numbers, some_numbers + 5);
std::list<int> li(vi.begin(), vi.end ());
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK (!ba::one_of_equal ( vi, 1 ));
BOOST_CHECK (!ba::one_of ( vi, is_<int> ( 1 )));
BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 1 ));
BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
BOOST_CHECK (!ba::one_of_equal ( vi, 0 ));
BOOST_CHECK (!ba::one_of ( vi, is_<int> ( 0 )));
BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 0 ));
BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_<int> ( 0 )));
BOOST_CHECK ( ba::one_of_equal ( vi, 2 ));
BOOST_CHECK ( ba::one_of ( vi, is_<int> ( 2 )));
BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 2 ));
BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_<int> ( 2 )));
// Check for a match at the end
BOOST_CHECK ( ba::one_of_equal ( vi, 5 ));
BOOST_CHECK ( ba::one_of ( vi, is_<int> ( 5 )));
BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 5 ));
BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_<int> ( 5 )));
BOOST_CHECK ( ba::one_of_equal ( vi.begin() + 1, vi.end(), 1 ));
BOOST_CHECK ( ba::one_of ( vi.begin() + 1, vi.end(), is_<int> ( 1 )));
BOOST_CHECK ( ba::one_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' ));
BOOST_CHECK ( ba::one_of ( vc.begin() + 1, vc.begin() + 2, is_<char> ( 'q' )));
BOOST_CHECK (!ba::one_of_equal ( vc, '!' ));
BOOST_CHECK (!ba::one_of ( vc, is_<char> ( '!' )));
BOOST_CHECK (!ba::one_of_equal ( vc, 'n' ));
BOOST_CHECK (!ba::one_of ( vc, is_<char> ( 'n' )));
// Empty range check
BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.begin(), 1 ));
BOOST_CHECK (!ba::one_of_equal ( vc.begin(), vc.begin(), 'a' ));
BOOST_CHECK (!ba::one_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
BOOST_CHECK (!ba::one_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
BOOST_CHECK (!ba::one_of_equal ( li, 1 ));
BOOST_CHECK (!ba::one_of ( li, is_<int> ( 1 )));
BOOST_CHECK (!ba::one_of_equal ( li.begin(), li.end(), 1 ));
BOOST_CHECK (!ba::one_of ( li.begin(), li.end(), is_<int> ( 1 )));
std::list<int>::iterator l_iter = li.begin ();
l_iter++; l_iter++; l_iter++;
BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 1 ));
BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 1 )));
BOOST_CHECK ( ba::one_of_equal ( li.begin(), l_iter, 2 ));
BOOST_CHECK ( ba::one_of ( li.begin(), l_iter, is_<int> ( 2 )));
BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 3 ));
BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 3 )));
}
int test_main( int , char* [] )
{
test_one ();
return 0;
}

127
test/ordered_test.cpp Normal file
View File

@ -0,0 +1,127 @@
// Copyright (c) 2010 Nuovation System Designs, LLC
// Grant Erickson <gerickson@nuovations.com>
//
// Reworked by Marshall Clow; August 2010
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/ for latest version.
#include <algorithm>
#include <iostream>
#include <boost/algorithm/cxx11/ordered.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
using namespace boost;
/* Preprocessor Defines */
#define elementsof(v) (sizeof (v) / sizeof (v[0]))
#define a_begin(v) (&v[0])
#define a_end(v) (v + elementsof (v))
#define a_range(v) v
#define b_e(v) a_begin(v),a_end(v)
namespace ba = boost::algorithm;
static void
test_ordered(void)
{
const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 };
const int increasingValues[] = { 1, 2, 2, 2, 5 };
const int decreasingValues[] = { 9, 7, 7, 7, 5 };
const int randomValues[] = { 3, 6, 1, 2, 7 };
const int constantValues[] = { 7, 7, 7, 7, 7 };
int nonConstantArray[] = { 7, 7, 7, 7, 7 };
const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 };
// Test a strictly increasing sequence
BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues)));
BOOST_CHECK ( ba::is_increasing (b_e(strictlyIncreasingValues)));
BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(strictlyIncreasingValues)));
BOOST_CHECK ( !ba::is_decreasing (b_e(strictlyIncreasingValues)));
BOOST_CHECK ( ba::is_strictly_increasing (a_range(strictlyIncreasingValues)));
BOOST_CHECK ( ba::is_increasing (a_range(strictlyIncreasingValues)));
BOOST_CHECK ( !ba::is_strictly_decreasing (a_range(strictlyIncreasingValues)));
BOOST_CHECK ( !ba::is_decreasing (a_range(strictlyIncreasingValues)));
// Test a strictly decreasing sequence
BOOST_CHECK ( !ba::is_strictly_increasing (b_e(strictlyDecreasingValues)));
BOOST_CHECK ( !ba::is_increasing (b_e(strictlyDecreasingValues)));
BOOST_CHECK ( ba::is_strictly_decreasing (b_e(strictlyDecreasingValues)));
BOOST_CHECK ( ba::is_decreasing (b_e(strictlyDecreasingValues)));
// Test an increasing sequence
BOOST_CHECK ( !ba::is_strictly_increasing (b_e(increasingValues)));
BOOST_CHECK ( ba::is_increasing (b_e(increasingValues)));
BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(increasingValues)));
BOOST_CHECK ( !ba::is_decreasing (b_e(increasingValues)));
// Test a decreasing sequence
BOOST_CHECK ( !ba::is_strictly_increasing (b_e(decreasingValues)));
BOOST_CHECK ( !ba::is_increasing (b_e(decreasingValues)));
BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(decreasingValues)));
BOOST_CHECK ( ba::is_decreasing (b_e(decreasingValues)));
// Test a random sequence
BOOST_CHECK ( !ba::is_strictly_increasing (b_e(randomValues)));
BOOST_CHECK ( !ba::is_increasing (b_e(randomValues)));
BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(randomValues)));
BOOST_CHECK ( !ba::is_decreasing (b_e(randomValues)));
// Test a constant sequence
BOOST_CHECK ( !ba::is_strictly_increasing (b_e(constantValues)));
BOOST_CHECK ( ba::is_increasing (b_e(constantValues)));
BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(constantValues)));
BOOST_CHECK ( ba::is_decreasing (b_e(constantValues)));
// Test an empty sequence
BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues));
BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues));
BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues));
BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues));
// Test a one-element sequence
BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
// Test a two-element sequence
BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
// Test underlying routines
BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less<int>()) == a_end(strictlyIncreasingValues));
BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues));
BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) != a_end(nonConstantArray));
BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) != boost::end(nonConstantArray));
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less<int>()) == &inOrderUntilTheEnd[8] );
// For zero and one element collections, the comparison predicate should never be called
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to<int>()) == a_begin(randomValues));
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1);
}
int test_main( int, char * [] )
{
test_ordered ();
return 0;
}

View File

@ -0,0 +1,87 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/partition_copy.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
#include <string>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
template <typename Container, typename Predicate>
void test_sequence ( const Container &c, Predicate comp ) {
std::vector<typename Container::value_type> v1, v2;
v1.clear (); v2.clear ();
ba::partition_copy ( c.begin (), c.end (),
std::back_inserter (v1), std::back_inserter (v2), comp );
// std::cout << "Sizes(1): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl;
BOOST_CHECK ( v1.size () + v2.size () == c.size ());
BOOST_CHECK ( ba::all_of ( v1.begin (), v1.end (), comp ));
BOOST_CHECK ( ba::none_of ( v2.begin (), v2.end (), comp ));
v1.clear (); v2.clear ();
ba::partition_copy ( c, std::back_inserter (v1), std::back_inserter ( v2 ), comp );
// std::cout << "Sizes(2): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl;
BOOST_CHECK ( v1.size () + v2.size () == c.size ());
BOOST_CHECK ( ba::all_of ( v1, comp ));
BOOST_CHECK ( ba::none_of ( v2, comp ));
}
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
T val;
};
bool is_even ( int v ) { return v % 2 == 0; }
void test_sequence1 () {
std::vector<int> v;
v.clear ();
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
test_sequence ( v, less_than<int>(3)); // no elements
test_sequence ( v, less_than<int>(6)); // only the first element
test_sequence ( v, less_than<int>(10));
test_sequence ( v, less_than<int>(99)); // all elements satisfy
// With bidirectional iterators.
std::list<int> l;
for ( int i = 5; i < 16; ++i )
l.push_back ( i );
test_sequence ( l, less_than<int>(3)); // no elements
test_sequence ( l, less_than<int>(6)); // only the first element
test_sequence ( l, less_than<int>(10));
test_sequence ( l, less_than<int>(99)); // all elements satisfy
}
int test_main( int , char* [] )
{
test_sequence1 ();
return 0;
}

View File

@ -0,0 +1,98 @@
/*
Copyright (c) Marshall Clow 2011-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <iostream>
#include <boost/config.hpp>
#include <boost/algorithm/cxx11/partition_point.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <string>
#include <vector>
#include <list>
namespace ba = boost::algorithm;
// namespace ba = boost;
template <typename Container>
typename Container::iterator offset_to_iter ( Container &v, int offset ) {
typename Container::iterator retval;
if ( offset >= 0 ) {
retval = v.begin ();
std::advance ( retval, offset );
}
else {
retval = v.end ();
std::advance ( retval, offset + 1 );
}
return retval;
}
template <typename Container, typename Predicate>
void test_sequence ( Container &v, Predicate comp, int expected ) {
typename Container::iterator res, exp;
res = ba::partition_point ( v.begin (), v.end (), comp );
exp = offset_to_iter ( v, expected );
std::cout << "Expected(1): " << std::distance ( v.begin (), exp )
<< ", got: " << std::distance ( v.begin (), res ) << std::endl;
BOOST_CHECK ( exp == res );
// Duplicate the last element; this checks for any even/odd problems
v.push_back ( * v.rbegin ());
res = ba::partition_point ( v.begin (), v.end (), comp );
exp = offset_to_iter ( v, expected );
std::cout << "Expected(2): " << std::distance ( v.begin (), exp )
<< ", got: " << std::distance ( v.begin (), res ) << std::endl;
BOOST_CHECK ( exp == res );
}
template <typename T>
struct less_than {
public:
less_than ( T foo ) : val ( foo ) {}
less_than ( const less_than &rhs ) : val ( rhs.val ) {}
bool operator () ( const T &v ) const { return v < val; }
private:
less_than ();
less_than operator = ( const less_than &rhs );
T val;
};
void test_sequence1 () {
std::vector<int> v;
v.clear ();
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
test_sequence ( v, less_than<int>(3), 0 ); // no elements
test_sequence ( v, less_than<int>(6), 1 ); // only the first element
test_sequence ( v, less_than<int>(10), 5 );
test_sequence ( v, less_than<int>(99), -1 ); // all elements satisfy
// With bidirectional iterators.
std::list<int> l;
for ( int i = 5; i < 15; ++i )
l.push_back ( i );
test_sequence ( l, less_than<int>(3), 0 ); // no elements
test_sequence ( l, less_than<int>(6), 1 ); // only the first element
test_sequence ( l, less_than<int>(10), 5 );
test_sequence ( l, less_than<int>(99), -1 ); // all elements satisfy
}
int test_main( int , char* [] )
{
test_sequence1 ();
return 0;
}