Boost.RangeEx merged into Boost.Range

[SVN r60897]
This commit is contained in:
Neil Groves
2010-03-28 16:08:35 +00:00
parent 1461479a17
commit b0d1db7c2e
471 changed files with 48610 additions and 2065 deletions
+110
View File
@@ -0,0 +1,110 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/adjacent_filtered.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <string>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void adjacent_filtered_test_impl( Container& c )
{
using namespace boost::adaptors;
typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
// This is my preferred syntax using the | operator.
std::vector< value_t > test_result1
= boost::copy_range< std::vector< value_t > >(
c | adjacent_filtered(std::not_equal_to< value_t >()));
// This is an alternative syntax preferred by some.
std::vector< value_t > test_result2
= boost::copy_range< std::vector< value_t > >(
adaptors::adjacent_filter(c, std::not_equal_to< value_t >()));
// Calculate the reference result.
std::vector< value_t > reference_result;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
value_t prev_v = value_t();
for (iter_t it = c.begin(); it != c.end(); ++it)
{
if (it == c.begin())
{
reference_result.push_back(*it);
}
else if (*it != prev_v)
{
reference_result.push_back(*it);
}
prev_v = *it;
}
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
reference_result.end(),
test_result1.begin(),
test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
reference_result.end(),
test_result2.begin(),
test_result2.end() );
}
template< class Collection >
void adjacent_filtered_test_impl()
{
using namespace boost::assign;
Collection c;
// test empty collection
adjacent_filtered_test_impl(c);
// test one element;
c += 1;
adjacent_filtered_test_impl(c);
// test many elements;
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
adjacent_filtered_test_impl(c);
}
void adjacent_filtered_test()
{
adjacent_filtered_test_impl< std::vector< int > >();
adjacent_filtered_test_impl< std::list< int > >();
adjacent_filtered_test_impl< std::set< int > >();
adjacent_filtered_test_impl< std::multiset< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.adjacent_filtered" );
test->add( BOOST_TEST_CASE( &boost::adjacent_filtered_test ) );
return test;
}
@@ -0,0 +1,58 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/adjacent_filtered.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
void adjacent_filtered_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 1,1,2,2,2,3,4,5,6;
boost::copy(
input | adjacent_filtered(std::not_equal_to<int>()),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 1,2,3,4,5,6;
std::vector<int> test;
boost::push_back(test, input | adjacent_filtered(std::not_equal_to<int>()));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.adjacent_filtered_example" );
test->add( BOOST_TEST_CASE( &adjacent_filtered_example_test ) );
return test;
}
+84
View File
@@ -0,0 +1,84 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/copied.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <deque>
#include <string>
#include <vector>
#include <boost/range/algorithm_ext.hpp>
namespace boost
{
namespace
{
template< class Container >
void copied_test_impl( Container& c )
{
using namespace boost::adaptors;
// This is my preferred syntax using the | operator.
std::vector< int > test_result1;
boost::push_back(test_result1, c | copied(0u, c.size()));
// This is the alternative syntax preferred by some.
std::vector< int > test_result2;
boost::push_back(test_result2, adaptors::copy(c, 0u, c.size()));
BOOST_CHECK_EQUAL_COLLECTIONS( test_result1.begin(), test_result1.end(),
c.begin(), c.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( test_result2.begin(), test_result2.end(),
c.begin(), c.end() );
}
template< class Container >
void copied_test_impl()
{
using namespace boost::assign;
Container c;
// test empty collection
copied_test_impl(c);
// test one element
c += 1;
copied_test_impl(c);
// test many elements
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
copied_test_impl(c);
}
void copied_test()
{
copied_test_impl< std::vector< int > >();
copied_test_impl< std::deque< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.copied" );
test->add( BOOST_TEST_CASE( &boost::copied_test ) );
return test;
}
+58
View File
@@ -0,0 +1,58 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/copied.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
void copied_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9,10;
boost::copy(
input | copied(1, 5),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 2,3,4,5;
std::vector<int> test;
boost::push_back(test, input | copied(1, 5));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.copied_example" );
test->add( BOOST_TEST_CASE( &copied_example_test ) );
return test;
}
+135
View File
@@ -0,0 +1,135 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/filtered.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <string>
#include <vector>
namespace boost
{
namespace
{
struct always_false_pred
{
template< class T1 >
bool operator()(T1) const { return false; }
};
struct always_true_pred
{
template< class T1 >
bool operator()(T1) const { return true; }
};
struct is_even
{
template< class IntegerT >
bool operator()( IntegerT x ) const { return x % 2 == 0; }
};
struct is_odd
{
template< class IntegerT >
bool operator()( IntegerT x ) const { return x % 2 != 0; }
};
template< class Container, class Pred >
void filtered_test_impl( Container& c, Pred pred )
{
using namespace boost::adaptors;
typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
// This is my preferred syntax using the | operator.
std::vector< value_t > test_result1;
boost::push_back(test_result1, c | filtered(pred));
// This is an alternative syntax preferred by some.
std::vector< value_t > test_result2;
boost::push_back(test_result2, adaptors::filter(c, pred));
// Calculate the reference result.
std::vector< value_t > reference_result;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
for (iter_t it = c.begin(); it != c.end(); ++it)
{
if (pred(*it))
reference_result.push_back(*it);
}
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
reference_result.end(),
test_result1.begin(),
test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
reference_result.end(),
test_result2.begin(),
test_result2.end() );
}
template< class Container, class Pred >
void filtered_test_impl()
{
using namespace boost::assign;
Container c;
// test empty container
filtered_test_impl(c, Pred());
// test one element
c += 1;
filtered_test_impl(c, Pred());
// test many elements
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
filtered_test_impl(c, Pred());
}
template< class Container >
void filtered_test_all_predicates()
{
filtered_test_impl< Container, always_false_pred >();
filtered_test_impl< Container, always_true_pred >();
filtered_test_impl< Container, is_odd >();
filtered_test_impl< Container, is_even >();
}
void filtered_test()
{
filtered_test_all_predicates< std::vector< int > >();
filtered_test_all_predicates< std::list< int > >();
filtered_test_all_predicates< std::set< int > >();
filtered_test_all_predicates< std::multiset< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.filtered" );
test->add( BOOST_TEST_CASE( &boost::filtered_test ) );
return test;
}
+62
View File
@@ -0,0 +1,62 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
struct is_even
{
bool operator()( int x ) const { return x % 2 == 0; }
};
void filtered_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;
boost::copy(
input | filtered(is_even()),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 2,4,6,8;
std::vector<int> test;
boost::push_back(test, input | filtered(is_even()));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.filtered_example" );
test->add( BOOST_TEST_CASE( &filtered_example_test ) );
return test;
}
+99
View File
@@ -0,0 +1,99 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/indexed.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void indexed_test_impl( Container& c )
{
using namespace boost::adaptors;
typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
// This is my preferred syntax using the | operator.
std::vector< value_t > test_result1;
boost::push_back(test_result1, c | indexed(0));
// This is an alternative syntax preferred by some.
std::vector< value_t > test_result2;
boost::push_back(test_result2, adaptors::index(c, 0));
BOOST_CHECK_EQUAL_COLLECTIONS( c.begin(), c.end(),
test_result1.begin(), test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( c.begin(), c.end(),
test_result2.begin(), test_result2.end() );
boost::indexed_range< Container > test_result3
= c | indexed(0);
typedef BOOST_DEDUCED_TYPENAME boost::range_const_iterator<
boost::indexed_range< Container > >::type iter_t;
iter_t it = test_result3.begin();
for (std::size_t i = 0, count = c.size(); i < count; ++i)
{
BOOST_CHECK_EQUAL( i, static_cast<std::size_t>(it.index()) );
++it;
}
}
template< class Container >
void indexed_test_impl()
{
using namespace boost::assign;
Container c;
// test empty container
indexed_test_impl(c);
// test one element
c += 1;
indexed_test_impl(c);
// test many elements
c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
indexed_test_impl(c);
}
void indexed_test()
{
indexed_test_impl< std::vector< int > >();
indexed_test_impl< std::list< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indexed" );
test->add( BOOST_TEST_CASE( &boost::indexed_test ) );
return test;
}
+95
View File
@@ -0,0 +1,95 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
template<class Iterator>
void display_element_and_index(Iterator first, Iterator last)
{
for (Iterator it = first; it != last; ++it)
{
std::cout << "Element = " << *it << " Index = " << it.index() << std::endl;
}
}
template<class SinglePassRange>
void display_element_and_index(const SinglePassRange& rng)
{
display_element_and_index(boost::begin(rng), boost::end(rng));
}
template<class Iterator1, class Iterator2>
void check_element_and_index(
Iterator1 test_first,
Iterator1 test_last,
Iterator2 reference_first,
Iterator2 reference_last)
{
BOOST_CHECK_EQUAL( std::distance(test_first, test_last),
std::distance(reference_first, reference_last) );
int reference_index = 0;
Iterator1 test_it = test_first;
Iterator2 reference_it = reference_first;
for (; test_it != test_last; ++test_it, ++reference_it, ++reference_index)
{
BOOST_CHECK_EQUAL( *test_it, *reference_it );
BOOST_CHECK_EQUAL( test_it.index(), reference_index );
}
}
template<class SinglePassRange1, class SinglePassRange2>
void check_element_and_index(
const SinglePassRange1& test_rng,
const SinglePassRange2& reference_rng)
{
check_element_and_index(boost::begin(test_rng), boost::end(test_rng),
boost::begin(reference_rng), boost::end(reference_rng));
}
void indexed_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
display_element_and_index( input | indexed(0) );
check_element_and_index(
input | indexed(0),
input);
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indexed_example" );
test->add( BOOST_TEST_CASE( &indexed_example_test ) );
return test;
}
+99
View File
@@ -0,0 +1,99 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/indirected.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void indirected_test_impl( Container& c )
{
using namespace boost::adaptors;
// This is my preferred syntax using the | operator.
std::vector< int > test_result1;
boost::push_back(test_result1, c | indirected);
// This is an alternative syntax preferred by some.
std::vector< int > test_result2;
boost::push_back(test_result2, adaptors::indirect(c));
// Calculate the reference result.
std::vector< int > reference_result;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
for (iter_t it = c.begin(); it != c.end(); ++it)
{
reference_result.push_back(**it);
}
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
reference_result.end(),
test_result1.begin(),
test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
reference_result.end(),
test_result2.begin(),
test_result2.end() );
}
template< class Container >
void indirected_test_impl()
{
using namespace boost::assign;
Container c;
indirected_test_impl(c);
c += boost::shared_ptr<int>(new int(1));
indirected_test_impl(c);
std::vector<int> v;
v += 1,1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
for (std::vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
{
c += boost::shared_ptr<int>(new int(*it));
}
indirected_test_impl(c);
}
void indirected_test()
{
indirected_test_impl< std::vector< boost::shared_ptr< int > > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indirected" );
test->add( BOOST_TEST_CASE( &boost::indirected_test ) );
return test;
}
+60
View File
@@ -0,0 +1,60 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/shared_ptr.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void indirected_example_test()
{
using namespace boost::adaptors;
std::vector<boost::shared_ptr<int> > input;
for (int i = 0; i < 10; ++i)
input.push_back(boost::shared_ptr<int>(new int(i)));
boost::copy(
input | indirected,
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
for (int i = 0; i < 10; ++i)
reference.push_back(i);
std::vector<int> test;
boost::push_back(test, input | indirected);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.indirected_example" );
test->add( BOOST_TEST_CASE( &indirected_example_test ) );
return test;
}
+127
View File
@@ -0,0 +1,127 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/map.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <map>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void map_test_keys( Container& c )
{
using namespace boost::adaptors;
std::vector<int> keys;
boost::push_back(keys, c | map_keys);
std::vector<int> keys2;
boost::push_back(keys2, adaptors::keys(c));
std::vector<int> reference_keys;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
for (iter_t it = c.begin(); it != c.end(); ++it)
{
reference_keys.push_back(it->first);
}
BOOST_CHECK_EQUAL_COLLECTIONS( reference_keys.begin(),
reference_keys.end(),
keys.begin(),
keys.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference_keys.begin(),
reference_keys.end(),
keys2.begin(),
keys2.end() );
}
template< class Container >
void map_test_values( Container& c )
{
using namespace boost::adaptors;
std::vector<int> values;
boost::push_back(values, c | map_values);
std::vector<int> values2;
boost::push_back(values2, adaptors::values(c));
std::vector<int> reference_values;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
for (iter_t it = c.begin(); it != c.end(); ++it)
{
reference_values.push_back(it->second);
}
BOOST_CHECK_EQUAL_COLLECTIONS( reference_values.begin(),
reference_values.end(),
values.begin(),
values.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference_values.begin(),
reference_values.end(),
values2.begin(),
values2.end() );
}
template< class Container >
void map_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
map_test_keys(c);
map_test_values(c);
// Test one element
c.insert(std::make_pair(1,2));
map_test_keys(c);
map_test_values(c);
// Test many elements
for (int x = 2; x < 10; ++x)
{
c.insert(std::make_pair(x, x * 2));
}
map_test_keys(c);
map_test_values(c);
}
void map_test()
{
map_test_impl< std::map<int,int> >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.map" );
test->add( BOOST_TEST_CASE( &boost::map_test ) );
return test;
}
+60
View File
@@ -0,0 +1,60 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void map_keys_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::map<int,int> input;
for (int i = 0; i < 10; ++i)
input.insert(std::make_pair(i, i * 10));
boost::copy(
input | map_keys,
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 0,1,2,3,4,5,6,7,8,9;
std::vector<int> test;
boost::push_back(test, input | map_keys);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.map_keys_example" );
test->add( BOOST_TEST_CASE( &map_keys_example_test ) );
return test;
}
+60
View File
@@ -0,0 +1,60 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void map_values_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::map<int,int> input;
for (int i = 0; i < 10; ++i)
input.insert(std::make_pair(i, i * 10));
boost::copy(
input | map_values,
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 0,10,20,30,40,50,60,70,80,90;
std::vector<int> test;
boost::push_back(test, input | map_values);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.map_values_example" );
test->add( BOOST_TEST_CASE( &map_values_example_test ) );
return test;
}
+87
View File
@@ -0,0 +1,87 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/replaced.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void replaced_test_impl( Container& c )
{
using namespace boost::adaptors;
const int value_to_replace = 1;
const int replacement_value = 0;
std::vector< int > test_result1;
boost::push_back(test_result1, c | replaced(value_to_replace, replacement_value));
std::vector< int > test_result2;
boost::push_back(test_result2, adaptors::replace(c, value_to_replace, replacement_value));
std::vector< int > reference( c.begin(), c.end() );
std::replace(reference.begin(), reference.end(), value_to_replace, replacement_value);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result1.begin(), test_result1.end() );
}
template< class Container >
void replaced_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
replaced_test_impl(c);
// Test one
c += 1;
replaced_test_impl(c);
// Test many
c += 1,1,1,2,2,2,3,3,3,3,3,4,5,6,6,6,7,8,9;
replaced_test_impl(c);
}
void replaced_test()
{
replaced_test_impl< std::vector< int > >();
replaced_test_impl< std::list< int > >();
replaced_test_impl< std::set< int > >();
replaced_test_impl< std::multiset< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.replaced" );
test->add( BOOST_TEST_CASE( &boost::replaced_test ) );
return test;
}
+58
View File
@@ -0,0 +1,58 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/replaced.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void replaced_example_test()
{
using namespace boost::adaptors;
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,2,5,2,7,2,9;
boost::copy(
input | replaced(2, 10),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 1,10,3,10,5,10,7,10,9;
std::vector<int> test;
boost::push_back(test, input | replaced(2, 10));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.replaced_example" );
test->add( BOOST_TEST_CASE( &replaced_example_test ) );
return test;
}
+96
View File
@@ -0,0 +1,96 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/replaced_if.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>
namespace boost
{
namespace
{
struct if_value_is_one
{
bool operator()(int x) const { return x == 1; }
};
template< class Container >
void replaced_if_test_impl( Container& c )
{
using namespace boost::adaptors;
if_value_is_one pred;
const int replacement_value = 0;
std::vector< int > test_result1;
boost::push_back(test_result1, c | replaced_if(pred, replacement_value));
std::vector< int > test_result2;
boost::push_back(test_result2, boost::adaptors::replace_if(c, pred, replacement_value));
std::vector< int > reference( c.begin(), c.end() );
std::replace_if(reference.begin(), reference.end(), pred, replacement_value);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result1.begin(), test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result2.begin(), test_result2.end() );
}
template< class Container >
void replaced_if_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
replaced_if_test_impl(c);
// Test one
c += 1;
replaced_if_test_impl(c);
// Test many
c += 1,1,1,2,2,2,3,3,3,3,3,4,5,6,6,6,7,8,9;
replaced_if_test_impl(c);
}
void replaced_if_test()
{
replaced_if_test_impl< std::vector< int > >();
replaced_if_test_impl< std::list< int > >();
replaced_if_test_impl< std::set< int > >();
replaced_if_test_impl< std::multiset< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.replaced_if" );
test->add( BOOST_TEST_CASE( &boost::replaced_if_test ) );
return test;
}
+62
View File
@@ -0,0 +1,62 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/replaced_if.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
struct is_even
{
bool operator()(int x) const { return x % 2 == 0; }
};
void replaced_if_example_test()
{
using namespace boost::adaptors;
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;
boost::copy(
input | replaced_if(is_even(), 10),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 1,10,3,10,5,10,7,10,9;
std::vector<int> test;
boost::push_back(test, input | replaced_if(is_even(), 10));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.replaced_if_example" );
test->add( BOOST_TEST_CASE( &replaced_if_example_test ) );
return test;
}
+84
View File
@@ -0,0 +1,84 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/reversed.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>
namespace boost
{
template< class Container >
void reversed_test_impl( Container& c )
{
using namespace boost::adaptors;
std::vector< int > test_result1;
boost::push_back(test_result1, c | reversed);
std::vector< int > test_result2;
boost::push_back(test_result2, adaptors::reverse(c));
std::vector< int > reference( c.rbegin(), c.rend() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result1.begin(), test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result2.begin(), test_result2.end() );
}
template< class Container >
void reversed_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
reversed_test_impl(c);
// Test one
c += 1;
reversed_test_impl(c);
// Test many
c += 1,1,1,2,2,2,2,2,3,3,3,3,3,3,4,5,6,7,8,9;
reversed_test_impl(c);
}
void reversed_test()
{
reversed_test_impl< std::vector< int > >();
reversed_test_impl< std::list< int > >();
reversed_test_impl< std::set< int > >();
reversed_test_impl< std::multiset< int > >();
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.reversed" );
test->add( BOOST_TEST_CASE( &boost::reversed_test ) );
return test;
}
+55
View File
@@ -0,0 +1,55 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void reversed_example_test()
{
using namespace boost::adaptors;
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;
boost::copy(
input | reversed,
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> test;
boost::push_back(test, input | reversed);
BOOST_CHECK_EQUAL_COLLECTIONS( input.rbegin(), input.rend(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.reversed_example" );
test->add( BOOST_TEST_CASE( &reversed_example_test ) );
return test;
}
+99
View File
@@ -0,0 +1,99 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/sliced.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <deque>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void sliced_test_impl( Container& c )
{
using namespace boost::adaptors;
typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
std::vector< value_t > test_result1;
boost::push_back(test_result1, c | sliced(0u,c.size()));
BOOST_CHECK_EQUAL_COLLECTIONS( test_result1.begin(), test_result1.end(),
c.begin(), c.end() );
std::vector< value_t > test_alt_result1;
boost::push_back(test_alt_result1, adaptors::slice(c, 0u, c.size()));
BOOST_CHECK_EQUAL_COLLECTIONS( test_alt_result1.begin(), test_alt_result1.end(),
c.begin(), c.end() );
BOOST_CHECK( boost::empty(c | sliced(0u, 0u)) );
const std::size_t half_count = c.size() / 2u;
if (half_count > 0u)
{
std::vector< value_t > test_result2;
boost::push_back(test_result2, c | sliced(0u, half_count));
BOOST_CHECK_EQUAL_COLLECTIONS( test_result2.begin(), test_result2.end(),
c.begin(), c.begin() + half_count );
std::vector< value_t > test_alt_result2;
boost::push_back(test_alt_result2, adaptors::slice(c, 0u, half_count));
BOOST_CHECK_EQUAL_COLLECTIONS( test_alt_result2.begin(), test_alt_result2.end(),
c.begin(), c.begin() + half_count );
}
}
template< class Container >
void sliced_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
sliced_test_impl(c);
// Test one element
c += 1;
sliced_test_impl(c);
// Test many elements
c += 1,1,1,2,2,3,4,5,6,6,6,7,8,9;
sliced_test_impl(c);
}
void sliced_test()
{
sliced_test_impl< std::vector< int > >();
sliced_test_impl< std::deque< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.sliced" );
test->add( BOOST_TEST_CASE( &boost::sliced_test ) );
return test;
}
+58
View File
@@ -0,0 +1,58 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void sliced_example_test()
{
using namespace boost::adaptors;
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;
boost::copy(
input | sliced(2, 5),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 3,4,5;
std::vector<int> test;
boost::push_back(test, input | sliced(2, 5));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.sliced_example" );
test->add( BOOST_TEST_CASE( &sliced_example_test ) );
return test;
}
+111
View File
@@ -0,0 +1,111 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/strided.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <deque>
#include <vector>
namespace boost
{
namespace
{
const int STRIDE_SIZE = 2;
template< class Container >
void strided_test_impl( Container& c )
{
using namespace boost::adaptors;
BOOST_ASSERT( c.size() % STRIDE_SIZE == 0 );
Container reference;
{
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iterator_t;
iterator_t it = c.begin();
for (iterator_t last = c.end();
it != last; it += STRIDE_SIZE)
{
reference.push_back(*it);
}
}
Container test;
boost::push_back( test, c | strided(2) );
BOOST_CHECK_EQUAL_COLLECTIONS( test.begin(), test.end(),
reference.begin(), reference.end() );
Container test2;
boost::push_back( test2, adaptors::stride(c, 2) );
BOOST_CHECK_EQUAL_COLLECTIONS( test2.begin(), test2.end(),
reference.begin(), reference.end() );
// Test the const versions:
const Container& cc = c;
Container test3;
boost::push_back( test3, cc | strided(2) );
BOOST_CHECK_EQUAL_COLLECTIONS( test3.begin(), test3.end(),
reference.begin(), reference.end() );
Container test4;
boost::push_back( test4, adaptors::stride(cc, 2) );
BOOST_CHECK_EQUAL_COLLECTIONS( test4.begin(), test4.end(),
reference.begin(), reference.end() );
}
template< class Container >
void strided_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
strided_test_impl(c);
// Test two elements
c += 1,2;
strided_test_impl(c);
// Test many elements
c += 1,1,1,2,2,3,4,5,6,6,6,7,8,9;
strided_test_impl(c);
}
void strided_test()
{
strided_test_impl< std::vector< int > >();
strided_test_impl< std::deque< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.strided" );
test->add( BOOST_TEST_CASE( &boost::strided_test ) );
return test;
}
+58
View File
@@ -0,0 +1,58 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/strided.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
void strided_example_test()
{
using namespace boost::adaptors;
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9,10;
boost::copy(
input | strided(2),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 1,3,5,7,9;
std::vector<int> test;
boost::push_back(test, input | strided(2));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.strided_example" );
test->add( BOOST_TEST_CASE( &strided_example_test ) );
return test;
}
+79
View File
@@ -0,0 +1,79 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/tokenized.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <algorithm>
#include <string>
#include <vector>
namespace boost
{
namespace
{
template< class Iterator, class Container >
void tokenized_test_impl( Container& c, std::size_t expected_result )
{
using namespace boost::adaptors;
std::vector< boost::sub_match< Iterator > > test_result1;
boost::push_back(test_result1, c | tokenized(boost::regex("\\b")));
BOOST_CHECK_EQUAL( test_result1.size(), expected_result );
// std::vector< boost::sub_match< Iterator > > test_result2;
// boost::push_back(test_result2, adaptors::tokenize(c, boost::regex("\\b")));
// BOOST_CHECK_EQUAL( test_result2.size(), expected_result );
}
template< class Container1, class Container2 >
void tokenized_test_impl()
{
Container1 c;
Container2& r = c;
typedef typename boost::range_iterator<Container2>::type It;
// Test empty
tokenized_test_impl<It, Container2>(r, 0u);
// Test one element
c = "a";
tokenized_test_impl<It, Container2>(r, 2u);
// Test many elements
c = "a b c d e f g hijlmnopqrstuvwxyz";
tokenized_test_impl<It, Container2>(r, 16u);
}
void tokenized_test()
{
// tokenized_test_impl<std::string, const std::string>();
tokenized_test_impl<std::string, std::string>();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.tokenized" );
test->add( BOOST_TEST_CASE( &boost::tokenized_test ) );
return test;
}
+107
View File
@@ -0,0 +1,107 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/transformed.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>
namespace boost
{
namespace
{
struct double_x
{
typedef int result_type;
int operator()(int x) const { return x * 2; }
};
struct halve_x
{
typedef int result_type;
int operator()(int x) const { return x / 2; }
};
template< class Container, class TransformFn >
void transformed_test_impl_core( Container& c, TransformFn fn )
{
using namespace boost::adaptors;
std::vector< int > test_result1;
boost::push_back(test_result1, c | transformed(fn));
std::vector< int > test_result2;
boost::push_back(test_result2, adaptors::transform(c, fn));
std::vector< int > reference;
std::transform(c.begin(), c.end(), std::back_inserter(reference), fn);
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result1.begin(), test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result2.begin(), test_result2.end() );
}
template< class Container, class TransformFn >
void transformed_test_fn_impl()
{
using namespace boost::assign;
Container c;
TransformFn fn;
// Test empty
transformed_test_impl_core(c, fn);
// Test one element
c += 1;
transformed_test_impl_core(c, fn);
// Test many elements
c += 1,1,1,2,2,2,2,2,3,4,5,6,7,8,9;
transformed_test_impl_core(c, fn);
}
template< class Container >
void transformed_test_impl()
{
transformed_test_fn_impl< Container, double_x >();
transformed_test_fn_impl< Container, halve_x >();
}
void transformed_test()
{
transformed_test_impl< std::vector< int > >();
transformed_test_impl< std::list< int > >();
transformed_test_impl< std::set< int > >();
transformed_test_impl< std::multiset< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.transformed" );
test->add( BOOST_TEST_CASE( &boost::transformed_test ) );
return test;
}
+64
View File
@@ -0,0 +1,64 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
struct double_int
{
typedef int result_type;
int operator()(int x) const { return x * 2; }
};
void transformed_example_test()
{
using namespace boost::adaptors;
using namespace boost::assign;
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9,10;
boost::copy(
input | transformed(double_int()),
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 2,4,6,8,10,12,14,16,18,20;
std::vector<int> test;
boost::push_back(test, input | transformed(double_int()));
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.transformed_example" );
test->add( BOOST_TEST_CASE( &transformed_example_test ) );
return test;
}
+89
View File
@@ -0,0 +1,89 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/uniqued.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/assign.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <algorithm>
#include <list>
#include <set>
#include <vector>
namespace boost
{
namespace
{
template< class Container >
void uniqued_test_impl( Container& c )
{
using namespace boost::adaptors;
std::vector< int > test_result1;
boost::push_back(test_result1, c | uniqued);
std::vector< int > test_result2;
boost::push_back(test_result2, adaptors::unique(c));
std::vector< int > reference(c.begin(), c.end());
reference.erase(
std::unique(reference.begin(), reference.end()),
reference.end());
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result1.begin(), test_result1.end() );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test_result2.begin(), test_result2.end() );
}
template< class Container >
void uniqued_test_impl()
{
using namespace boost::assign;
Container c;
// Test empty
uniqued_test_impl(c);
// Test one
c += 1;
uniqued_test_impl(c);
// Test many
c += 1,1,1,2,2,2,2,2,3,3,3,3,4,5,6,6,6,7,7,7,8,8,9,9,9,9,9,10;
uniqued_test_impl(c);
}
void uniqued_test()
{
uniqued_test_impl< std::vector< int > >();
uniqued_test_impl< std::list< int > >();
uniqued_test_impl< std::set< int > >();
uniqued_test_impl< std::multiset< int > >();
}
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.uniqued" );
test->add( BOOST_TEST_CASE( &boost::uniqued_test ) );
return test;
}
+57
View File
@@ -0,0 +1,57 @@
// Boost.Range library
//
// Copyright Neil Groves 2009. Use, modification and
// distribution is subject to 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/libs/range/
//
#include <boost/range/adaptor/uniqued.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/range/algorithm_ext/push_back.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
namespace
{
void uniqued_example_test()
{
using namespace boost::assign;
using namespace boost::adaptors;
std::vector<int> input;
input += 1,1,2,2,2,3,4,5,6;
boost::copy(
input | uniqued,
std::ostream_iterator<int>(std::cout, ","));
std::vector<int> reference;
reference += 1,2,3,4,5,6;
std::vector<int> test;
boost::push_back( test, input | uniqued );
BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
test.begin(), test.end() );
}
}
boost::unit_test::test_suite*
init_unit_test_suite(int argc, char* argv[])
{
boost::unit_test::test_suite* test
= BOOST_TEST_SUITE( "RangeTestSuite.adaptor.uniqued_example" );
test->add( BOOST_TEST_CASE( &uniqued_example_test ) );
return test;
}