1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*=============================================================================
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()
{
#ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
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;
#endif
}
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();
}
|