diff options
Diffstat (limited to 'libs/phoenix/test/algorithm')
-rw-r--r-- | libs/phoenix/test/algorithm/iteration.cpp | 57 | ||||
-rw-r--r-- | libs/phoenix/test/algorithm/querying.cpp | 289 | ||||
-rw-r--r-- | libs/phoenix/test/algorithm/querying2.cpp | 85 | ||||
-rw-r--r-- | libs/phoenix/test/algorithm/transformation1.cpp | 403 | ||||
-rw-r--r-- | libs/phoenix/test/algorithm/transformation2.cpp | 193 | ||||
-rw-r--r-- | libs/phoenix/test/algorithm/transformation3.cpp | 187 | ||||
-rw-r--r-- | libs/phoenix/test/algorithm/transformation4.cpp | 159 |
7 files changed, 1373 insertions, 0 deletions
diff --git a/libs/phoenix/test/algorithm/iteration.cpp b/libs/phoenix/test/algorithm/iteration.cpp new file mode 100644 index 000000000..a809b5eb1 --- /dev/null +++ b/libs/phoenix/test/algorithm/iteration.cpp @@ -0,0 +1,57 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + + 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) +==============================================================================*/ + +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/iteration.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <functional> + +namespace +{ + struct for_each_tester + { + int value_; + for_each_tester() : value_(0) { } + void operator()( + int& i) + { + value_ += i++; + return; + } + }; + + void for_each_test() + { + using boost::phoenix::for_each; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + BOOST_TEST(for_each(arg1, for_each_tester())(array).value_ == 6); + BOOST_TEST(array[0] == 2); + BOOST_TEST(array[1] == 3); + BOOST_TEST(array[2] == 4); + return; + } + + void accumulate_test() + { + using boost::phoenix::accumulate; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + BOOST_TEST(accumulate(arg1, 0)(array) == 6); + BOOST_TEST(boost::phoenix::accumulate(arg1, 0, std::minus<int>())(array) == -6); + return; + } +} + +int main() +{ + for_each_test(); + accumulate_test(); + boost::report_errors(); +} diff --git a/libs/phoenix/test/algorithm/querying.cpp b/libs/phoenix/test/algorithm/querying.cpp new file mode 100644 index 000000000..43f588c40 --- /dev/null +++ b/libs/phoenix/test/algorithm/querying.cpp @@ -0,0 +1,289 @@ +/*============================================================================= + Copyright (c) 2005 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + Copyright (c) 2007 Hartmut Kaiser + + 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) +==============================================================================*/ + +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/querying.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <boost/assign/list_of.hpp> + +#include <boost/config.hpp> + +#ifdef BOOST_HAS_HASH +#include BOOST_HASH_SET_HEADER +#include BOOST_HASH_MAP_HEADER +#define BOOST_PHOENIX_HAS_HASH +#define BOOST_PHOENIX_HASH_NAMESPACE BOOST_STD_EXTENSION_NAMESPACE +#elif defined(BOOST_DINKUMWARE_STDLIB) +#include <hash_set> +#include <hash_map> +#define BOOST_PHOENIX_HAS_HASH +#define BOOST_PHOENIX_HASH_NAMESPACE stdext +#endif + +#include <set> +#include <map> +#include <functional> + +namespace +{ + struct even + { + bool operator()(const int i) const + { + return i % 2 == 0; + } + }; + + struct mod_2_comparison + { + bool operator()( + const int lhs, + const int rhs) + { + return lhs % 2 == rhs % 2; + } + }; + + void find_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + BOOST_TEST(boost::phoenix::find(arg1,2)(array) == array + 1); + + std::set<int> s(array, array + 3); + BOOST_TEST(boost::phoenix::find(arg1, 2)(s) == s.find(2)); + + std::map<int, int> m = boost::assign::map_list_of(0, 1)(2, 3)(4, 5); + BOOST_TEST(boost::phoenix::find(arg1, 2)(m) == m.find(2)); + +#ifdef BOOST_PHOENIX_HAS_HASH + + BOOST_PHOENIX_HASH_NAMESPACE::hash_set<int> hs(array, array + 3); + BOOST_TEST(boost::phoenix::find(arg1, 2)(hs) == hs.find(2)); + + BOOST_PHOENIX_HASH_NAMESPACE::hash_map<int, int> hm = boost::assign::map_list_of(0, 1)(2, 3)(4, 5); + BOOST_TEST(boost::phoenix::find(arg1, 2)(hm) == hm.find(2)); + +#endif + + return; + } + + + void find_if_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + BOOST_TEST(boost::phoenix::find_if(arg1, even())(array) == array + 1); + return; + } + + void find_end_test() + { + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3,1,2,3,1}; + int pattern[] = {1,2,3}; + BOOST_TEST(boost::phoenix::find_end(arg1, arg2)(array, pattern) == array + 3); + int pattern2[] = {5,6,5}; + BOOST_TEST(boost::phoenix::find_end(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 3); + return; + } + + void find_first_of_test() + { + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int search_for[] = {2,3,4}; + BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2)(array, search_for) == array + 1); + + int search_for2[] = {0}; + BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2, mod_2_comparison())(array, search_for2) == array + 1); + return; + } + + void adjacent_find_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {0,1,3,4,4}; + BOOST_TEST(boost::phoenix::adjacent_find(arg1)(array) == array + 3); + BOOST_TEST(boost::phoenix::adjacent_find(arg1, mod_2_comparison())(array) == array + 1); + return; + } + + void count_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,1,0,1,1}; + BOOST_TEST(boost::phoenix::count(arg1, 1)(array) == 4); + return; + } + + void count_if_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3,4,5}; + BOOST_TEST(boost::phoenix::count_if(arg1, even())(array) == 2); + return; + } + + void distance_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,1,0,1,1}; + BOOST_TEST(boost::phoenix::distance(arg1)(array) == 5); + return; + } + + void mismatch_test() + { + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3,4,5}; + int search[] = {1,2,4}; + + BOOST_TEST( + boost::phoenix::mismatch(arg1, arg2)(array, search) == + std::make_pair(array + 2, search + 2)); + int search2[] = {1,2,1,1}; + BOOST_TEST( + boost::phoenix::mismatch(arg1, arg2, mod_2_comparison())(array, search2) + == std::make_pair(array + 3, search2 + 3)); + + return; + } + + void equal_test() + { + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[] = {1,2,3}; + int array3[] = {1,2,4}; + BOOST_TEST( + boost::phoenix::equal(arg1, arg2)(array, array2)); + BOOST_TEST( + !boost::phoenix::equal(arg1, arg2)(array, array3)); + + BOOST_TEST( + boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array2)); + BOOST_TEST( + !boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array3)); + return; + } + + void search_test() + { + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3,1,2,3}; + int pattern[] = {2,3}; + BOOST_TEST( + boost::phoenix::search(arg1, arg2)(array, pattern) == array + 1); + int pattern2[] = {1,1}; + BOOST_TEST( + boost::phoenix::search(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 2); + return; + } + + void lower_bound_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + const std::set<int> test_set(array, array + 3); + BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(array) == array + 1); + BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(test_set) == test_set.lower_bound(2)); + + int array2[] = {3,2,1}; + const std::set<int, std::greater<int> > test_set2(array2, array2 + 3); + BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(array2) == + array2 + 1); + BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(test_set2) == + test_set2.lower_bound(2)); + return; + } + + void upper_bound_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + const std::set<int> test_set(array, array + 3); + BOOST_TEST(upper_bound(arg1, 2)(array) == array + 2); + BOOST_TEST(upper_bound(arg1, 2)(test_set) == test_set.upper_bound(2)); + + int array2[] = {3,2,1}; + const std::set<int, std::greater<int> > test_set2(array2, array2 + 3); + BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(array2) == + array2 + 2); + BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(test_set2) == + test_set2.upper_bound(2)); + return; + } + + void equal_range_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,2,3}; + const std::set<int> test_set(array, array + 4); + BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).first == + array + 1); + BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).second == + array + 3); + + BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).first == + test_set.equal_range(2).first); + BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).second == + test_set.equal_range(2).second); + + int array2[] = {3,2,2,1}; + const std::set<int, std::greater<int> > test_set2(array2, array2 + 4); + BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).first == + array2 + 1); + BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).second == + array2 + 3); + + BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).first == + test_set2.equal_range(2).first); + BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).second == + test_set2.equal_range(2).second); + + return; + } + + void binary_search_test() + { + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + BOOST_TEST(boost::phoenix::binary_search(arg1, 2)(array)); + BOOST_TEST(!boost::phoenix::binary_search(arg1, 4)(array)); + return; + } + +} + +int main() +{ + find_test(); + find_if_test(); + find_end_test(); + find_first_of_test(); + adjacent_find_test(); + count_test(); + count_if_test(); + distance_test(); + mismatch_test(); + equal_test(); + search_test(); + lower_bound_test(); + upper_bound_test(); + equal_range_test(); + binary_search_test(); + return boost::report_errors(); +} diff --git a/libs/phoenix/test/algorithm/querying2.cpp b/libs/phoenix/test/algorithm/querying2.cpp new file mode 100644 index 000000000..c6641186d --- /dev/null +++ b/libs/phoenix/test/algorithm/querying2.cpp @@ -0,0 +1,85 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + + 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) +==============================================================================*/ + +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/querying.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <boost/range.hpp> + +#include <functional> + +namespace +{ + void includes_test() + { + using boost::phoenix::includes; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[] = {1,2}; + BOOST_TEST(includes(arg1, arg2)(array, array2)); + boost::iterator_range<int*> rng(array + 1, array + 3); + BOOST_TEST(!includes(arg1, arg2)(rng, array2)); + + int array3[] = {3,2,1}; + int array4[] = {2,1}; + BOOST_TEST(boost::phoenix::includes(arg1, arg2, std::greater<int>())(array3, array4)); + boost::iterator_range<int*> rng2(array3, array3 + 2); + BOOST_TEST(!boost::phoenix::includes(arg1, arg2, std::greater<int>())(rng2, array4)); + return; + } + + void min_element_test() + { + using boost::phoenix::min_element; + using boost::phoenix::arg_names::arg1; + int array[] = {1,3,2}; + BOOST_TEST(min_element(arg1)(array) == array); + BOOST_TEST(boost::phoenix::min_element(arg1, std::greater<int>())(array) == array + 1); + return; + } + + void max_element_test() + { + using boost::phoenix::max_element; + using boost::phoenix::arg_names::arg1; + int array[] = {1,3,2}; + BOOST_TEST(max_element(arg1)(array) == array + 1); + BOOST_TEST(boost::phoenix::max_element(arg1, std::greater<int>())(array) == array); + return; + } + + void lexicographical_compare_test() + { + using boost::phoenix::lexicographical_compare; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[] = {1,2,4}; + + BOOST_TEST(lexicographical_compare(arg1, arg2)(array, array2)); + BOOST_TEST(!lexicographical_compare(arg1, arg2)(array2, array)); + BOOST_TEST(!lexicographical_compare(arg1, arg2)(array, array)); + + BOOST_TEST(!boost::phoenix::lexicographical_compare(arg1, arg2, std::greater<int>())(array, array2)); + BOOST_TEST(boost::phoenix::lexicographical_compare(arg1, arg2, std::greater<int>())(array2, array)); + BOOST_TEST(!boost::phoenix::lexicographical_compare(arg1, arg2, std::greater<int>())(array, array)); + + return; + } +} + +int main() +{ + includes_test(); + min_element_test(); + max_element_test(); + lexicographical_compare_test(); + return boost::report_errors(); +} diff --git a/libs/phoenix/test/algorithm/transformation1.cpp b/libs/phoenix/test/algorithm/transformation1.cpp new file mode 100644 index 000000000..657b9fbe9 --- /dev/null +++ b/libs/phoenix/test/algorithm/transformation1.cpp @@ -0,0 +1,403 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + + 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) +==============================================================================*/ + +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/transformation.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <functional> +#include <list> + +namespace +{ + struct even + { + bool operator()(const int i) const + { + return i % 2 == 0; + } + }; + + struct mod_2_comparison + { + bool operator()( + const int lhs, + const int rhs) + { + return lhs % 2 == rhs % 2; + } + }; + + void swap_test() + { + using boost::phoenix::swap; + using boost::phoenix::ref; + using boost::phoenix::arg_names::_1; + using boost::phoenix::arg_names::_2; + int a = 123; + int b = 456; + swap(ref(a), ref(b))(); + BOOST_TEST(a == 456 && b == 123); + swap(ref(a), _1)(b); + BOOST_TEST(a == 123 && b == 456); + swap(_1, _2)(a, b); + BOOST_TEST(a == 456 && b == 123); + return; + } + + void copy_test() + { + using boost::phoenix::copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int output[4]; + BOOST_TEST( + copy(arg1, arg2)(array, output) == output + 3); + BOOST_TEST(output[0] == 1); + BOOST_TEST(output[1] == 2); + BOOST_TEST(output[2] == 3); + return; + } + + void copy_backward_test() + { + using boost::phoenix::copy_backward; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int output[4]; + int* output_end = output + 3; + BOOST_TEST( + copy_backward(arg1, arg2)(array, output_end) == output); + BOOST_TEST(output[0] == 1); + BOOST_TEST(output[1] == 2); + BOOST_TEST(output[2] == 3); + return; + } + + struct increment + { + int operator()( + int i) const + { + return i+1; + } + }; + + void transform_test() + { + using boost::phoenix::transform; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + using boost::phoenix::arg_names::arg3; + int array[] = {1,2,3}; + BOOST_TEST( + transform(arg1, arg2, increment())(array, array) == + array + 3); + BOOST_TEST(array[0] == 2); + BOOST_TEST(array[1] == 3); + BOOST_TEST(array[2] == 4); + + int array2[] = {1,2,3}; + BOOST_TEST( + boost::phoenix::transform(arg1, arg2, arg3, std::plus<int>())(array, array2, array) == + array +3); + BOOST_TEST(array[0] == 2 + 1); + BOOST_TEST(array[1] == 3 + 2); + BOOST_TEST(array[2] == 4 + 3); + return; + } + + void replace_test() + { + using boost::phoenix::replace; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + replace(arg1,2,4)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 4); + BOOST_TEST(array[2] == 3); + return; + } + + void replace_if_test() + { + using boost::phoenix::replace_if; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + replace_if(arg1, even(), 4)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 4); + BOOST_TEST(array[2] == 3); + return; + } + + void replace_copy_test() + { + using boost::phoenix::replace_copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int input[] = {1,2,3}; + int output[3]; + replace_copy(arg1, arg2, 2, 4)(input, output); + BOOST_TEST(output[0] == 1); + BOOST_TEST(output[1] == 4); + BOOST_TEST(output[2] == 3); + return; + } + + void replace_copy_if_test() + { + using boost::phoenix::replace_copy_if; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int input[] = {1,2,3}; + int output[3]; + replace_copy_if(arg1, arg2, even(), 4)(input, output); + BOOST_TEST(output[0] == 1); + BOOST_TEST(output[1] == 4); + BOOST_TEST(output[2] == 3); + return; + } + + void fill_test() + { + using boost::phoenix::fill; + using boost::phoenix::arg_names::arg1; + int array[] = {0,0,0}; + fill(arg1, 1)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 1); + BOOST_TEST(array[2] == 1); + return; + } + + void fill_n_test() + { + using boost::phoenix::fill_n; + using boost::phoenix::arg_names::arg1; + int array[] = {0,0,0}; + fill_n(arg1, 2, 1)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 1); + BOOST_TEST(array[2] == 0); + return; + } + + class int_seq + { + public: + int_seq() : val_(0) { } + + int operator()() + { + return val_++; + } + private: + int val_; + }; + + void generate_test() + { + using boost::phoenix::generate; + using boost::phoenix::arg_names::arg1; + int array[3]; + generate(arg1, int_seq())(array); + BOOST_TEST(array[0] == 0); + BOOST_TEST(array[1] == 1); + BOOST_TEST(array[2] == 2); + return; + } + + void generate_n_test() + { + using boost::phoenix::generate_n; + using boost::phoenix::arg_names::arg1; + int array[] = {0,0,1}; + generate_n(arg1, 2, int_seq())(array); + BOOST_TEST(array[0] == 0); + BOOST_TEST(array[1] == 1); + BOOST_TEST(array[2] == 1); + return; + } + + + void remove_test() + { + using boost::phoenix::remove; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + std::list<int> test_list(array, array + 3); + BOOST_TEST(boost::phoenix::remove(arg1, 2)(array) == array + 2); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 3); + BOOST_TEST(boost::phoenix::remove(arg1, 2)(test_list) == test_list.end()); + std::list<int>::const_iterator it(test_list.begin()); + BOOST_TEST(*it++ == 1); + BOOST_TEST(*it++ == 3); + return; + } + + void remove_if_test() + { + using boost::phoenix::remove_if; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + std::list<int> test_list(array, array + 3); + BOOST_TEST(boost::phoenix::remove_if(arg1, even())(array) == array + 2); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 3); + BOOST_TEST(boost::phoenix::remove_if(arg1, even())(test_list) == test_list.end()); + std::list<int>::const_iterator it(test_list.begin()); + BOOST_TEST(*it++ == 1); + BOOST_TEST(*it++ == 3); + return; + } + + void remove_copy_test() + { + using boost::phoenix::remove_copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[2]; + BOOST_TEST(boost::phoenix::remove_copy(arg1, arg2, 2)(array, array2) == array2 + 2); + BOOST_TEST(array2[0] == 1); + BOOST_TEST(array2[1] == 3); + return; + } + + void remove_copy_if_test() + { + using boost::phoenix::remove_copy_if; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[2]; + BOOST_TEST(boost::phoenix::remove_copy_if(arg1, arg2, even())(array, array2) == array2 + 2); + BOOST_TEST(array2[0] == 1); + BOOST_TEST(array2[1] == 3); + return; + } + + void unique_test() + { + using boost::phoenix::unique; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,2,3}; + std::list<int> test_list(array, array + 4); + BOOST_TEST(unique(arg1)(array) == array + 3); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 3); + + BOOST_TEST(unique(arg1)(test_list) == test_list.end()); + std::list<int>::const_iterator it(test_list.begin()); + BOOST_TEST(*it++ == 1); + BOOST_TEST(*it++ == 2); + BOOST_TEST(*it++ == 3); + + int array2[] = {1,3,2}; + std::list<int> test_list2(array2, array2 + 3); + BOOST_TEST(unique(arg1, mod_2_comparison())(array2) == array2 + 2); + BOOST_TEST(array2[0] == 1); + BOOST_TEST(array2[1] == 2); + + BOOST_TEST(unique(arg1, mod_2_comparison())(test_list2) == test_list2.end()); + std::list<int>::const_iterator jt(test_list2.begin()); + BOOST_TEST(*jt++ == 1); + BOOST_TEST(*jt++ == 2); + + return; + } + + void unique_copy_test() + { + using boost::phoenix::unique_copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,2,3}; + int out[3]; + BOOST_TEST(unique_copy(arg1, arg2)(array, out) == out + 3); + BOOST_TEST(out[0] == 1); + BOOST_TEST(out[1] == 2); + BOOST_TEST(out[2] == 3); + + int array2[] = {1,3,2}; + int out2[2]; + BOOST_TEST(unique_copy(arg1, arg2, mod_2_comparison())(array2, out2) == out2 + 2); + BOOST_TEST(out2[0] == 1); + BOOST_TEST(out2[1] == 2); + + return; + } + + void reverse_test() + { + using boost::phoenix::reverse; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + std::list<int> test_list(array, array + 3); + reverse(arg1)(array); + BOOST_TEST(array[0] == 3); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 1); + + reverse(arg1)(test_list); + std::list<int>::iterator it(test_list.begin()); + BOOST_TEST(*it++ == 3); + BOOST_TEST(*it++ == 2); + BOOST_TEST(*it++ == 1); + return; + } + + void reverse_copy_test() + { + using boost::phoenix::reverse_copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[3]; + reverse_copy(arg1, arg2)(array, array2); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 3); + + BOOST_TEST(array2[0] == 3); + BOOST_TEST(array2[1] == 2); + BOOST_TEST(array2[2] == 1); + + return; + } +} + +int main() +{ + swap_test(); + copy_test(); + copy_backward_test(); + transform_test(); + replace_test(); + replace_if_test(); + replace_copy_test(); + replace_copy_if_test(); + fill_test(); + fill_n_test(); + generate_test(); + generate_n_test(); + remove_test(); + remove_if_test(); + remove_copy_test(); + remove_copy_if_test(); + unique_test(); + unique_copy_test(); + reverse_test(); + reverse_copy_test(); + boost::report_errors(); +} diff --git a/libs/phoenix/test/algorithm/transformation2.cpp b/libs/phoenix/test/algorithm/transformation2.cpp new file mode 100644 index 000000000..87249ae6e --- /dev/null +++ b/libs/phoenix/test/algorithm/transformation2.cpp @@ -0,0 +1,193 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + + 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) +==============================================================================*/ + +#include <functional> +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/transformation.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <list> + +namespace +{ + struct even + { + bool operator()(const int i) const + { + return i % 2 == 0; + } + }; + + void rotate_test() + { + using boost::phoenix::rotate; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + rotate(arg1, array + 1)(array); + std::cout << array[0] << array[1] << array[2] << std::endl; + BOOST_TEST(array[0] == 2); + BOOST_TEST(array[1] == 3); + BOOST_TEST(array[2] == 1); + + return; + } + + void rotate_copy_test() + { + using boost::phoenix::rotate_copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int array2[3]; + rotate_copy(arg1, array + 1, arg2)(array, array2); + BOOST_TEST(array2[0] == 2); + BOOST_TEST(array2[1] == 3); + BOOST_TEST(array2[2] == 1); + + return; + } + + void random_shuffle_test() + { + using boost::phoenix::random_shuffle; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + random_shuffle(arg1)(array); + const int first = array[0]; + BOOST_TEST(first == 1 || first == 2 || first == 3); + const int second = array[1]; + BOOST_TEST(second == 1 || second == 2 || second == 3); + BOOST_TEST(first != second); + const int third = array[2]; + BOOST_TEST(third == 1 || third == 2 || third == 3); + BOOST_TEST(first != third && second != third); + return; + } + + void partition_test() + { + using boost::phoenix::partition; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + int* const end = partition(arg1, even())(array); + BOOST_TEST(end == array + 1); + BOOST_TEST(array[0] % 2 == 0); + BOOST_TEST(array[1] % 2 != 0); + BOOST_TEST(array[2] % 2 != 0); + return; + } + + void stable_partition_test() + { + using boost::phoenix::stable_partition; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + int* const end = stable_partition(arg1, even())(array); + BOOST_TEST(end == array + 1); + BOOST_TEST(array[0] == 2); + BOOST_TEST(array[1] == 1); + BOOST_TEST(array[2] == 3); + return; + } + + void sort_test() + { + using boost::phoenix::sort; + using boost::phoenix::arg_names::arg1; + int array[] = {3,1,2}; + std::list<int> test_list(array, array + 3); + sort(arg1)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 3); + + sort(arg1)(test_list); + std::list<int>::const_iterator it(test_list.begin()); + BOOST_TEST(*it++ == 1); + BOOST_TEST(*it++ == 2); + BOOST_TEST(*it++ == 3); + + boost::phoenix::sort(arg1, std::greater<int>())(array); + BOOST_TEST(array[0] == 3); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 1); + + boost::phoenix::sort(arg1, std::greater<int>())(test_list); + std::list<int>::const_iterator jt(test_list.begin()); + BOOST_TEST(*jt++ == 3); + BOOST_TEST(*jt++ == 2); + BOOST_TEST(*jt++ == 1); + + return; + } + + void stable_sort_test() + { + using boost::phoenix::stable_sort; + using boost::phoenix::arg_names::arg1; + int array[] = {3,1,2}; + stable_sort(arg1)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 3); + + boost::phoenix::stable_sort(arg1, std::greater<int>())(array); + BOOST_TEST(array[0] == 3); + BOOST_TEST(array[1] == 2); + BOOST_TEST(array[2] == 1); + + return; + } + + void partial_sort_test() + { + using boost::phoenix::partial_sort; + using boost::phoenix::arg_names::arg1; + int array[] = {2,4,1,3}; + partial_sort(arg1, array + 2)(array); + BOOST_TEST(array[0] == 1); + BOOST_TEST(array[1] == 2); + + boost::phoenix::partial_sort(arg1, array + 2, std::greater<int>())(array); + BOOST_TEST(array[0] == 4); + BOOST_TEST(array[1] == 3); + return; + } + + void partial_sort_copy_test() + { + using boost::phoenix::partial_sort_copy; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {2,4,1,3}; + int array2[2]; + partial_sort_copy(arg1, arg2)(array, array2); + BOOST_TEST(array2[0] == 1); + BOOST_TEST(array2[1] == 2); + + boost::phoenix::partial_sort_copy(arg1, arg2, std::greater<int>())(array, array2); + BOOST_TEST(array2[0] == 4); + BOOST_TEST(array2[1] == 3); + + return; + } +} + +int main() +{ + rotate_test(); + rotate_copy_test(); + random_shuffle_test(); + partition_test(); + stable_partition_test(); + sort_test(); + stable_sort_test(); + partial_sort_test(); + partial_sort_copy_test(); + return boost::report_errors(); +} diff --git a/libs/phoenix/test/algorithm/transformation3.cpp b/libs/phoenix/test/algorithm/transformation3.cpp new file mode 100644 index 000000000..8a1787480 --- /dev/null +++ b/libs/phoenix/test/algorithm/transformation3.cpp @@ -0,0 +1,187 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + + 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) +==============================================================================*/ + +#include <functional> +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/transformation.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <list> + +namespace +{ + void nth_element_test() + { + using boost::phoenix::nth_element; + using boost::phoenix::arg_names::arg1; + int array[] = {5,1,4,3,2}; + nth_element(arg1, array + 2)(array); + BOOST_TEST(array[0] < 3); + BOOST_TEST(array[1] < 3); + BOOST_TEST(array[2] == 3); + BOOST_TEST(array[3] > 3); + BOOST_TEST(array[4] > 3); + + boost::phoenix::nth_element(arg1, array + 2, std::greater<int>())(array); + BOOST_TEST(array[0] > 3); + BOOST_TEST(array[1] > 3); + BOOST_TEST(array[2] == 3); + BOOST_TEST(array[3] < 3); + BOOST_TEST(array[4] < 3); + + return; + } + + void merge_test() + { + using boost::phoenix::merge; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + using boost::phoenix::arg_names::arg3; + int array[] = {1,2,3}; + int array2[] = {2,3,4}; + int output[6]; + + BOOST_TEST(merge(arg1, arg2, arg3)(array, array2, output) == output + 6); + int expected_result[] = {1,2,2,3,3,4}; + BOOST_TEST(std::equal(output, output + 6, expected_result)); + + int array3[] = {5,4,3}; + int array4[] = {3,2,1}; + int output2[6]; + BOOST_TEST(boost::phoenix::merge(arg1, arg2, arg3, std::greater<int>())(array3, array4, output2) == + output2 + 6); + int expected_result2[] = {5,4,3,3,2,1}; + BOOST_TEST(std::equal(output2, output2 + 6, expected_result2)); + return; + } + + void inplace_merge_test() + { + using boost::phoenix::inplace_merge; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3,2,3,4}; + inplace_merge(arg1, array + 3)(array); + int expected_result[] = {1,2,2,3,3,4}; + BOOST_TEST(std::equal(array, array + 6, expected_result)); + + int array2[] = {5,4,3,4,3,2}; + boost::phoenix::inplace_merge(arg1, array2 + 3, std::greater<int>())(array2); + int expected_result2[] = {5,4,4,3,3,2}; + BOOST_TEST(std::equal(array2, array2 + 6, expected_result2)); + return; + } + + void set_union_test() + { + using boost::phoenix::set_union; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + using boost::phoenix::arg_names::arg3; + int array[] = {1,2,3}; + int array2[] = {2,3,4}; + int output[4]; + BOOST_TEST(set_union(arg1, arg2, arg3)(array, array2, output) == output + 4); + int expected_result[] = {1,2,3,4}; + BOOST_TEST(std::equal(output, output + 4, expected_result)); + + int array3[] = {3,2,1}; + int array4[] = {4,3,2}; + int output2[4]; + BOOST_TEST(boost::phoenix::set_union(arg1, arg2, arg3, std::greater<int>()) + (array3, array4, output2) == + output2 + 4); + int expected_result2[] = {4,3,2,1}; + BOOST_TEST(std::equal(output2, output2 + 4, expected_result2)); + return; + } + + void set_intersection_test() + { + using boost::phoenix::set_intersection; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + using boost::phoenix::arg_names::arg3; + int array[] = {1,2,3}; + int array2[] = {2,3,4}; + int output[2]; + BOOST_TEST(set_intersection(arg1, arg2, arg3)(array, array2, output) == output + 2); + int expected_result[] = {2,3}; + BOOST_TEST(std::equal(output, output + 2, expected_result)); + + int array3[] = {3,2,1}; + int array4[] = {4,3,2}; + int output2[2]; + BOOST_TEST(boost::phoenix::set_intersection(arg1, arg2, arg3, std::greater<int>()) + (array3, array4, output2) == + output2 + 2); + int expected_result2[] = {3,2}; + BOOST_TEST(std::equal(output2, output2 + 2, expected_result2)); + return; + } + + void set_difference_test() + { + using boost::phoenix::set_difference; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + using boost::phoenix::arg_names::arg3; + int array[] = {1,2,3}; + int array2[] = {2,3,4}; + int output[1]; + BOOST_TEST(set_difference(arg1, arg2, arg3)(array, array2, output) == output + 1); + int expected_result[] = {1}; + BOOST_TEST(std::equal(output, output + 1, expected_result)); + + int array3[] = {3,2,1}; + int array4[] = {4,3,2}; + int output2[1]; + BOOST_TEST(boost::phoenix::set_difference(arg1, arg2, arg3, std::greater<int>()) + (array3, array4, output2) == + output2 + 1); + int expected_result2[] = {1}; + BOOST_TEST(std::equal(output2, output2 + 1, expected_result2)); + return; + } + + void set_symmetric_difference_test() + { + using boost::phoenix::set_symmetric_difference; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + using boost::phoenix::arg_names::arg3; + int array[] = {1,2,3}; + int array2[] = {2,3,4}; + int output[2]; + BOOST_TEST(set_symmetric_difference(arg1, arg2, arg3)(array, array2, output) == output + 2); + int expected_result[] = {1,4}; + BOOST_TEST(std::equal(output, output + 2, expected_result)); + + int array3[] = {3,2,1}; + int array4[] = {4,3,2}; + int output2[2]; + BOOST_TEST(boost::phoenix::set_symmetric_difference(arg1, arg2, arg3, std::greater<int>()) + (array3, array4, output2) == + output2 + 2); + int expected_result2[] = {4,1}; + BOOST_TEST(std::equal(output2, output2 + 2, expected_result2)); + return; + } +} + +int main() +{ + nth_element_test(); + merge_test(); + inplace_merge_test(); + set_union_test(); + set_intersection_test(); + set_difference_test(); + set_symmetric_difference_test(); + return boost::report_errors(); +} diff --git a/libs/phoenix/test/algorithm/transformation4.cpp b/libs/phoenix/test/algorithm/transformation4.cpp new file mode 100644 index 000000000..f278a4c6f --- /dev/null +++ b/libs/phoenix/test/algorithm/transformation4.cpp @@ -0,0 +1,159 @@ +/*============================================================================= + Copyright (c) 2005-2007 Dan Marsden + Copyright (c) 2005-2007 Joel de Guzman + + 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) +==============================================================================*/ + +#include <boost/phoenix/core.hpp> +#include <boost/phoenix/stl/algorithm/transformation.hpp> +#include <boost/detail/lightweight_test.hpp> + +#include <vector> +#include <functional> +#include <algorithm> + +namespace +{ + void heap_test() + { + using boost::phoenix::make_heap; + using boost::phoenix::pop_heap; + using boost::phoenix::push_heap; + using boost::phoenix::sort_heap; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2,3}; + std::vector<int> vec(array, array + 3); + boost::phoenix::make_heap(arg1)(vec); + vec.push_back(5); + boost::phoenix::push_heap(arg1)(vec); + vec.push_back(4); + boost::phoenix::push_heap(arg1)(vec); + boost::phoenix::pop_heap(arg1)(vec); + BOOST_TEST(vec.back() == 5); + vec.pop_back(); + boost::phoenix::sort_heap(arg1)(vec); + int expected_result[] = {1,2,3,4}; + BOOST_TEST(std::equal(vec.begin(), vec.end(), expected_result)); + + int array2[] = {3,2,1}; + std::vector<int> vec2(array2, array2 + 3); + boost::phoenix::make_heap(arg1, std::greater<int>())(vec2); + vec2.push_back(5); + boost::phoenix::push_heap(arg1, std::greater<int>())(vec2); + vec2.push_back(4); + boost::phoenix::push_heap(arg1, std::greater<int>())(vec2); + boost::phoenix::pop_heap(arg1, std::greater<int>())(vec2); + BOOST_TEST(vec2.back() == 1); + vec2.pop_back(); + boost::phoenix::sort_heap(arg1, std::greater<int>())(vec2); + int expected_result2[] = {5,4,3,2}; + BOOST_TEST(std::equal(vec2.begin(), vec2.end(), expected_result2)); + + return; + } + + void next_permutation_test() + { + using boost::phoenix::next_permutation; + using boost::phoenix::arg_names::arg1; + int array[] = {1,2}; + int expected_result[] = {2,1}; + int expected_result2[] = {1,2}; + + BOOST_TEST(next_permutation(arg1)(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result)); + BOOST_TEST(!next_permutation(arg1)(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result2)); + + std::reverse(array, array + 2); + BOOST_TEST(boost::phoenix::next_permutation(arg1, std::greater<int>())(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result2)); + BOOST_TEST(!boost::phoenix::next_permutation(arg1, std::greater<int>())(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result)); + return; + } + + void prev_permutation_test() + { + using boost::phoenix::prev_permutation; + using boost::phoenix::arg_names::arg1; + int array[] = {2,1}; + int expected_result[] = {1,2}; + int expected_result2[] = {2,1}; + + BOOST_TEST(prev_permutation(arg1)(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result)); + BOOST_TEST(!prev_permutation(arg1)(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result2)); + + std::reverse(array, array + 2); + BOOST_TEST(boost::phoenix::prev_permutation(arg1, std::greater<int>())(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result2)); + BOOST_TEST(!boost::phoenix::prev_permutation(arg1, std::greater<int>())(array)); + BOOST_TEST(std::equal(array, array + 2, expected_result)); + return; + } + + void inner_product_test() + { + using boost::phoenix::inner_product; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int lhs[] = {1,2,3}; + int rhs[] = {4,5,6}; + BOOST_TEST(inner_product(arg1, arg2, 0) + (lhs, rhs) == 1*4 + 2*5 + 3*6); + BOOST_TEST(boost::phoenix::inner_product(arg1, arg2, 1, std::multiplies<int>(), std::minus<int>()) + (lhs, rhs) == (1 - 4) * (2 - 5) * (3 - 6)); + return; + } + + void partial_sum_test() + { + using boost::phoenix::partial_sum; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int output[3]; + BOOST_TEST(partial_sum(arg1, arg2)(array, output) == output + 3); + int expected_result[] = {1, 3, 6}; + BOOST_TEST(std::equal(output, output + 3, expected_result)); + + BOOST_TEST(boost::phoenix::partial_sum(arg1, arg2, std::multiplies<int>()) + (array, output) == output + 3); + int expected_result2[] = {1, 2, 6}; + BOOST_TEST(std::equal(output, output + 3, expected_result2)); + return; + } + + void adjacent_difference_test() + { + using boost::phoenix::adjacent_difference; + using boost::phoenix::arg_names::arg1; + using boost::phoenix::arg_names::arg2; + int array[] = {1,2,3}; + int output[3]; + BOOST_TEST(adjacent_difference(arg1, arg2)(array, output) == output + 3); + int expected_result[] = {1, 1, 1}; + BOOST_TEST(std::equal(output, output + 3, expected_result)); + BOOST_TEST(boost::phoenix::adjacent_difference(arg1, arg2, std::plus<int>()) + (array, output) == output + 3); + int expected_result2[] = {1, 3, 5}; + BOOST_TEST(std::equal(output, output + 3, expected_result2)); + return; + } + +} + +int main() +{ + heap_test(); + next_permutation_test(); + prev_permutation_test(); + inner_product_test(); + partial_sum_test(); + adjacent_difference_test(); + return boost::report_errors(); +} |