| 1 | /*============================================================================= |
| 2 | Copyright (c) 2005-2007 Dan Marsden |
| 3 | Copyright (c) 2005-2007 Joel de Guzman |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | ==============================================================================*/ |
| 8 | |
| 9 | #include <functional> |
| 10 | #include <boost/phoenix/core.hpp> |
| 11 | #include <boost/phoenix/stl/algorithm/transformation.hpp> |
| 12 | #include <boost/detail/lightweight_test.hpp> |
| 13 | |
| 14 | #include <list> |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | struct even |
| 19 | { |
| 20 | bool operator()(const int i) const |
| 21 | { |
| 22 | return i % 2 == 0; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | void rotate_test() |
| 27 | { |
| 28 | using boost::phoenix::rotate; |
| 29 | using boost::phoenix::arg_names::arg1; |
| 30 | int array[] = {1,2,3}; |
| 31 | rotate(a0: arg1, a1: array + 1)(array); |
| 32 | std::cout << array[0] << array[1] << array[2] << std::endl; |
| 33 | BOOST_TEST(array[0] == 2); |
| 34 | BOOST_TEST(array[1] == 3); |
| 35 | BOOST_TEST(array[2] == 1); |
| 36 | |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | void rotate_copy_test() |
| 41 | { |
| 42 | using boost::phoenix::rotate_copy; |
| 43 | using boost::phoenix::arg_names::arg1; |
| 44 | using boost::phoenix::arg_names::arg2; |
| 45 | int array[] = {1,2,3}; |
| 46 | int array2[3]; |
| 47 | rotate_copy(a0: arg1, a1: array + 1, a2: arg2)(array, array2); |
| 48 | BOOST_TEST(array2[0] == 2); |
| 49 | BOOST_TEST(array2[1] == 3); |
| 50 | BOOST_TEST(array2[2] == 1); |
| 51 | |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | void random_shuffle_test() |
| 56 | { |
| 57 | #ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE |
| 58 | using boost::phoenix::random_shuffle; |
| 59 | using boost::phoenix::arg_names::arg1; |
| 60 | int array[] = {1,2,3}; |
| 61 | random_shuffle(a0: arg1)(array); |
| 62 | const int first = array[0]; |
| 63 | BOOST_TEST(first == 1 || first == 2 || first == 3); |
| 64 | const int second = array[1]; |
| 65 | BOOST_TEST(second == 1 || second == 2 || second == 3); |
| 66 | BOOST_TEST(first != second); |
| 67 | const int third = array[2]; |
| 68 | BOOST_TEST(third == 1 || third == 2 || third == 3); |
| 69 | BOOST_TEST(first != third && second != third); |
| 70 | return; |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | void partition_test() |
| 75 | { |
| 76 | using boost::phoenix::partition; |
| 77 | using boost::phoenix::arg_names::arg1; |
| 78 | int array[] = {1,2,3}; |
| 79 | int* const end = partition(a0: arg1, a1: even())(array); |
| 80 | BOOST_TEST(end == array + 1); |
| 81 | BOOST_TEST(array[0] % 2 == 0); |
| 82 | BOOST_TEST(array[1] % 2 != 0); |
| 83 | BOOST_TEST(array[2] % 2 != 0); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | void stable_partition_test() |
| 88 | { |
| 89 | using boost::phoenix::stable_partition; |
| 90 | using boost::phoenix::arg_names::arg1; |
| 91 | int array[] = {1,2,3}; |
| 92 | int* const end = stable_partition(a0: arg1, a1: even())(array); |
| 93 | BOOST_TEST(end == array + 1); |
| 94 | BOOST_TEST(array[0] == 2); |
| 95 | BOOST_TEST(array[1] == 1); |
| 96 | BOOST_TEST(array[2] == 3); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | void sort_test() |
| 101 | { |
| 102 | using boost::phoenix::sort; |
| 103 | using boost::phoenix::arg_names::arg1; |
| 104 | int array[] = {3,1,2}; |
| 105 | std::list<int> test_list(array, array + 3); |
| 106 | sort(a0: arg1)(array); |
| 107 | BOOST_TEST(array[0] == 1); |
| 108 | BOOST_TEST(array[1] == 2); |
| 109 | BOOST_TEST(array[2] == 3); |
| 110 | |
| 111 | sort(a0: arg1)(test_list); |
| 112 | std::list<int>::const_iterator it(test_list.begin()); |
| 113 | BOOST_TEST(*it++ == 1); |
| 114 | BOOST_TEST(*it++ == 2); |
| 115 | BOOST_TEST(*it++ == 3); |
| 116 | |
| 117 | boost::phoenix::sort(a0: arg1, a1: std::greater<int>())(array); |
| 118 | BOOST_TEST(array[0] == 3); |
| 119 | BOOST_TEST(array[1] == 2); |
| 120 | BOOST_TEST(array[2] == 1); |
| 121 | |
| 122 | boost::phoenix::sort(a0: arg1, a1: std::greater<int>())(test_list); |
| 123 | std::list<int>::const_iterator jt(test_list.begin()); |
| 124 | BOOST_TEST(*jt++ == 3); |
| 125 | BOOST_TEST(*jt++ == 2); |
| 126 | BOOST_TEST(*jt++ == 1); |
| 127 | |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | void stable_sort_test() |
| 132 | { |
| 133 | using boost::phoenix::stable_sort; |
| 134 | using boost::phoenix::arg_names::arg1; |
| 135 | int array[] = {3,1,2}; |
| 136 | stable_sort(a0: arg1)(array); |
| 137 | BOOST_TEST(array[0] == 1); |
| 138 | BOOST_TEST(array[1] == 2); |
| 139 | BOOST_TEST(array[2] == 3); |
| 140 | |
| 141 | boost::phoenix::stable_sort(a0: arg1, a1: std::greater<int>())(array); |
| 142 | BOOST_TEST(array[0] == 3); |
| 143 | BOOST_TEST(array[1] == 2); |
| 144 | BOOST_TEST(array[2] == 1); |
| 145 | |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | void partial_sort_test() |
| 150 | { |
| 151 | using boost::phoenix::partial_sort; |
| 152 | using boost::phoenix::arg_names::arg1; |
| 153 | int array[] = {2,4,1,3}; |
| 154 | partial_sort(a0: arg1, a1: array + 2)(array); |
| 155 | BOOST_TEST(array[0] == 1); |
| 156 | BOOST_TEST(array[1] == 2); |
| 157 | |
| 158 | boost::phoenix::partial_sort(a0: arg1, a1: array + 2, a2: std::greater<int>())(array); |
| 159 | BOOST_TEST(array[0] == 4); |
| 160 | BOOST_TEST(array[1] == 3); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | void partial_sort_copy_test() |
| 165 | { |
| 166 | using boost::phoenix::partial_sort_copy; |
| 167 | using boost::phoenix::arg_names::arg1; |
| 168 | using boost::phoenix::arg_names::arg2; |
| 169 | int array[] = {2,4,1,3}; |
| 170 | int array2[2]; |
| 171 | partial_sort_copy(a0: arg1, a1: arg2)(array, array2); |
| 172 | BOOST_TEST(array2[0] == 1); |
| 173 | BOOST_TEST(array2[1] == 2); |
| 174 | |
| 175 | boost::phoenix::partial_sort_copy(a0: arg1, a1: arg2, a2: std::greater<int>())(array, array2); |
| 176 | BOOST_TEST(array2[0] == 4); |
| 177 | BOOST_TEST(array2[1] == 3); |
| 178 | |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | int main() |
| 184 | { |
| 185 | rotate_test(); |
| 186 | rotate_copy_test(); |
| 187 | random_shuffle_test(); |
| 188 | partition_test(); |
| 189 | stable_partition_test(); |
| 190 | sort_test(); |
| 191 | stable_sort_test(); |
| 192 | partial_sort_test(); |
| 193 | partial_sort_copy_test(); |
| 194 | return boost::report_errors(); |
| 195 | } |
| 196 | |