]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/phoenix/test/algorithm/transformation2.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / phoenix / test / algorithm / transformation2.cpp
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(arg1, 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(arg1, array + 1, 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 using boost::phoenix::random_shuffle;
58 using boost::phoenix::arg_names::arg1;
59 int array[] = {1,2,3};
60 random_shuffle(arg1)(array);
61 const int first = array[0];
62 BOOST_TEST(first == 1 || first == 2 || first == 3);
63 const int second = array[1];
64 BOOST_TEST(second == 1 || second == 2 || second == 3);
65 BOOST_TEST(first != second);
66 const int third = array[2];
67 BOOST_TEST(third == 1 || third == 2 || third == 3);
68 BOOST_TEST(first != third && second != third);
69 return;
70 }
71
72 void partition_test()
73 {
74 using boost::phoenix::partition;
75 using boost::phoenix::arg_names::arg1;
76 int array[] = {1,2,3};
77 int* const end = partition(arg1, even())(array);
78 BOOST_TEST(end == array + 1);
79 BOOST_TEST(array[0] % 2 == 0);
80 BOOST_TEST(array[1] % 2 != 0);
81 BOOST_TEST(array[2] % 2 != 0);
82 return;
83 }
84
85 void stable_partition_test()
86 {
87 using boost::phoenix::stable_partition;
88 using boost::phoenix::arg_names::arg1;
89 int array[] = {1,2,3};
90 int* const end = stable_partition(arg1, even())(array);
91 BOOST_TEST(end == array + 1);
92 BOOST_TEST(array[0] == 2);
93 BOOST_TEST(array[1] == 1);
94 BOOST_TEST(array[2] == 3);
95 return;
96 }
97
98 void sort_test()
99 {
100 using boost::phoenix::sort;
101 using boost::phoenix::arg_names::arg1;
102 int array[] = {3,1,2};
103 std::list<int> test_list(array, array + 3);
104 sort(arg1)(array);
105 BOOST_TEST(array[0] == 1);
106 BOOST_TEST(array[1] == 2);
107 BOOST_TEST(array[2] == 3);
108
109 sort(arg1)(test_list);
110 std::list<int>::const_iterator it(test_list.begin());
111 BOOST_TEST(*it++ == 1);
112 BOOST_TEST(*it++ == 2);
113 BOOST_TEST(*it++ == 3);
114
115 boost::phoenix::sort(arg1, std::greater<int>())(array);
116 BOOST_TEST(array[0] == 3);
117 BOOST_TEST(array[1] == 2);
118 BOOST_TEST(array[2] == 1);
119
120 boost::phoenix::sort(arg1, std::greater<int>())(test_list);
121 std::list<int>::const_iterator jt(test_list.begin());
122 BOOST_TEST(*jt++ == 3);
123 BOOST_TEST(*jt++ == 2);
124 BOOST_TEST(*jt++ == 1);
125
126 return;
127 }
128
129 void stable_sort_test()
130 {
131 using boost::phoenix::stable_sort;
132 using boost::phoenix::arg_names::arg1;
133 int array[] = {3,1,2};
134 stable_sort(arg1)(array);
135 BOOST_TEST(array[0] == 1);
136 BOOST_TEST(array[1] == 2);
137 BOOST_TEST(array[2] == 3);
138
139 boost::phoenix::stable_sort(arg1, std::greater<int>())(array);
140 BOOST_TEST(array[0] == 3);
141 BOOST_TEST(array[1] == 2);
142 BOOST_TEST(array[2] == 1);
143
144 return;
145 }
146
147 void partial_sort_test()
148 {
149 using boost::phoenix::partial_sort;
150 using boost::phoenix::arg_names::arg1;
151 int array[] = {2,4,1,3};
152 partial_sort(arg1, array + 2)(array);
153 BOOST_TEST(array[0] == 1);
154 BOOST_TEST(array[1] == 2);
155
156 boost::phoenix::partial_sort(arg1, array + 2, std::greater<int>())(array);
157 BOOST_TEST(array[0] == 4);
158 BOOST_TEST(array[1] == 3);
159 return;
160 }
161
162 void partial_sort_copy_test()
163 {
164 using boost::phoenix::partial_sort_copy;
165 using boost::phoenix::arg_names::arg1;
166 using boost::phoenix::arg_names::arg2;
167 int array[] = {2,4,1,3};
168 int array2[2];
169 partial_sort_copy(arg1, arg2)(array, array2);
170 BOOST_TEST(array2[0] == 1);
171 BOOST_TEST(array2[1] == 2);
172
173 boost::phoenix::partial_sort_copy(arg1, arg2, std::greater<int>())(array, array2);
174 BOOST_TEST(array2[0] == 4);
175 BOOST_TEST(array2[1] == 3);
176
177 return;
178 }
179 }
180
181 int main()
182 {
183 rotate_test();
184 rotate_copy_test();
185 random_shuffle_test();
186 partition_test();
187 stable_partition_test();
188 sort_test();
189 stable_sort_test();
190 partial_sort_test();
191 partial_sort_copy_test();
192 return boost::report_errors();
193 }