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 <boost/phoenix/core.hpp>
10 #include <boost/phoenix/stl/algorithm/transformation.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12
13 #include <vector>
14 #include <functional>
15 #include <algorithm>
16
17 namespace
18 {
heap_test()19 void heap_test()
20 {
21 using boost::phoenix::make_heap;
22 using boost::phoenix::pop_heap;
23 using boost::phoenix::push_heap;
24 using boost::phoenix::sort_heap;
25 using boost::phoenix::arg_names::arg1;
26 int array[] = {1,2,3};
27 std::vector<int> vec(array, array + 3);
28 boost::phoenix::make_heap(arg1)(vec);
29 vec.push_back(5);
30 boost::phoenix::push_heap(arg1)(vec);
31 vec.push_back(4);
32 boost::phoenix::push_heap(arg1)(vec);
33 boost::phoenix::pop_heap(arg1)(vec);
34 BOOST_TEST(vec.back() == 5);
35 vec.pop_back();
36 boost::phoenix::sort_heap(arg1)(vec);
37 int expected_result[] = {1,2,3,4};
38 BOOST_TEST(std::equal(vec.begin(), vec.end(), expected_result));
39
40 int array2[] = {3,2,1};
41 std::vector<int> vec2(array2, array2 + 3);
42 boost::phoenix::make_heap(arg1, std::greater<int>())(vec2);
43 vec2.push_back(5);
44 boost::phoenix::push_heap(arg1, std::greater<int>())(vec2);
45 vec2.push_back(4);
46 boost::phoenix::push_heap(arg1, std::greater<int>())(vec2);
47 boost::phoenix::pop_heap(arg1, std::greater<int>())(vec2);
48 BOOST_TEST(vec2.back() == 1);
49 vec2.pop_back();
50 boost::phoenix::sort_heap(arg1, std::greater<int>())(vec2);
51 int expected_result2[] = {5,4,3,2};
52 BOOST_TEST(std::equal(vec2.begin(), vec2.end(), expected_result2));
53
54 return;
55 }
56
next_permutation_test()57 void next_permutation_test()
58 {
59 using boost::phoenix::next_permutation;
60 using boost::phoenix::arg_names::arg1;
61 int array[] = {1,2};
62 int expected_result[] = {2,1};
63 int expected_result2[] = {1,2};
64
65 BOOST_TEST(next_permutation(arg1)(array));
66 BOOST_TEST(std::equal(array, array + 2, expected_result));
67 BOOST_TEST(!next_permutation(arg1)(array));
68 BOOST_TEST(std::equal(array, array + 2, expected_result2));
69
70 std::reverse(array, array + 2);
71 BOOST_TEST(boost::phoenix::next_permutation(arg1, std::greater<int>())(array));
72 BOOST_TEST(std::equal(array, array + 2, expected_result2));
73 BOOST_TEST(!boost::phoenix::next_permutation(arg1, std::greater<int>())(array));
74 BOOST_TEST(std::equal(array, array + 2, expected_result));
75 return;
76 }
77
prev_permutation_test()78 void prev_permutation_test()
79 {
80 using boost::phoenix::prev_permutation;
81 using boost::phoenix::arg_names::arg1;
82 int array[] = {2,1};
83 int expected_result[] = {1,2};
84 int expected_result2[] = {2,1};
85
86 BOOST_TEST(prev_permutation(arg1)(array));
87 BOOST_TEST(std::equal(array, array + 2, expected_result));
88 BOOST_TEST(!prev_permutation(arg1)(array));
89 BOOST_TEST(std::equal(array, array + 2, expected_result2));
90
91 std::reverse(array, array + 2);
92 BOOST_TEST(boost::phoenix::prev_permutation(arg1, std::greater<int>())(array));
93 BOOST_TEST(std::equal(array, array + 2, expected_result2));
94 BOOST_TEST(!boost::phoenix::prev_permutation(arg1, std::greater<int>())(array));
95 BOOST_TEST(std::equal(array, array + 2, expected_result));
96 return;
97 }
98
inner_product_test()99 void inner_product_test()
100 {
101 using boost::phoenix::inner_product;
102 using boost::phoenix::arg_names::arg1;
103 using boost::phoenix::arg_names::arg2;
104 int lhs[] = {1,2,3};
105 int rhs[] = {4,5,6};
106 BOOST_TEST(inner_product(arg1, arg2, 0)
107 (lhs, rhs) == 1*4 + 2*5 + 3*6);
108 BOOST_TEST(boost::phoenix::inner_product(arg1, arg2, 1, std::multiplies<int>(), std::minus<int>())
109 (lhs, rhs) == (1 - 4) * (2 - 5) * (3 - 6));
110 return;
111 }
112
partial_sum_test()113 void partial_sum_test()
114 {
115 using boost::phoenix::partial_sum;
116 using boost::phoenix::arg_names::arg1;
117 using boost::phoenix::arg_names::arg2;
118 int array[] = {1,2,3};
119 int output[3];
120 BOOST_TEST(partial_sum(arg1, arg2)(array, output) == output + 3);
121 int expected_result[] = {1, 3, 6};
122 BOOST_TEST(std::equal(output, output + 3, expected_result));
123
124 BOOST_TEST(boost::phoenix::partial_sum(arg1, arg2, std::multiplies<int>())
125 (array, output) == output + 3);
126 int expected_result2[] = {1, 2, 6};
127 BOOST_TEST(std::equal(output, output + 3, expected_result2));
128 return;
129 }
130
adjacent_difference_test()131 void adjacent_difference_test()
132 {
133 using boost::phoenix::adjacent_difference;
134 using boost::phoenix::arg_names::arg1;
135 using boost::phoenix::arg_names::arg2;
136 int array[] = {1,2,3};
137 int output[3];
138 BOOST_TEST(adjacent_difference(arg1, arg2)(array, output) == output + 3);
139 int expected_result[] = {1, 1, 1};
140 BOOST_TEST(std::equal(output, output + 3, expected_result));
141 BOOST_TEST(boost::phoenix::adjacent_difference(arg1, arg2, std::plus<int>())
142 (array, output) == output + 3);
143 int expected_result2[] = {1, 3, 5};
144 BOOST_TEST(std::equal(output, output + 3, expected_result2));
145 return;
146 }
147
148 }
149
main()150 int main()
151 {
152 heap_test();
153 next_permutation_test();
154 prev_permutation_test();
155 inner_product_test();
156 partial_sum_test();
157 adjacent_difference_test();
158 return boost::report_errors();
159 }
160