PageRenderTime 73ms CodeModel.GetById 33ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/libs/phoenix/test/algorithm/transformation2.cpp

http://hadesmem.googlecode.com/
C++ | 192 lines | 161 code | 24 blank | 7 comment | 60 complexity | 207c3c0bfd87ce805bdb4103595a0225 MD5 | raw file
  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 <list>
 14
 15namespace
 16{
 17    struct even
 18    {
 19        bool operator()(const int i) const
 20        {
 21            return i % 2 == 0;
 22        }
 23    };
 24
 25    void rotate_test()
 26    {
 27        using boost::phoenix::rotate;
 28        using boost::phoenix::arg_names::arg1;
 29        int array[] = {1,2,3};
 30        rotate(arg1, array + 1)(array);
 31        std::cout << array[0] << array[1] << array[2] << std::endl;
 32        BOOST_TEST(array[0] == 2);
 33        BOOST_TEST(array[1] == 3);
 34        BOOST_TEST(array[2] == 1);
 35        
 36        return;
 37    }
 38
 39    void rotate_copy_test()
 40    {
 41        using boost::phoenix::rotate_copy;
 42        using boost::phoenix::arg_names::arg1;
 43        using boost::phoenix::arg_names::arg2;
 44        int array[] = {1,2,3};
 45        int array2[3];
 46        rotate_copy(arg1, array + 1, arg2)(array, array2);
 47        BOOST_TEST(array2[0] == 2);
 48        BOOST_TEST(array2[1] == 3);
 49        BOOST_TEST(array2[2] == 1);
 50        
 51        return;
 52    }
 53
 54    void random_shuffle_test()
 55    {
 56        using boost::phoenix::random_shuffle;
 57        using boost::phoenix::arg_names::arg1;
 58        int array[] = {1,2,3};
 59        random_shuffle(arg1)(array);
 60        const int first = array[0];
 61        BOOST_TEST(first == 1 || first == 2 || first == 3);
 62        const int second = array[1];
 63        BOOST_TEST(second == 1 || second == 2 || second == 3);
 64        BOOST_TEST(first != second);
 65        const int third = array[2];
 66        BOOST_TEST(third == 1 || third == 2 || third == 3);
 67        BOOST_TEST(first != third && second != third);
 68        return;
 69    }
 70    
 71    void partition_test()
 72    {
 73        using boost::phoenix::partition;
 74        using boost::phoenix::arg_names::arg1;
 75        int array[] = {1,2,3};
 76        int* const end = partition(arg1, even())(array);
 77        BOOST_TEST(end == array + 1);
 78        BOOST_TEST(array[0] % 2 == 0);
 79        BOOST_TEST(array[1] % 2 != 0);
 80        BOOST_TEST(array[2] % 2 != 0);
 81        return;
 82    }
 83
 84    void stable_partition_test()
 85    {
 86        using boost::phoenix::stable_partition;
 87        using boost::phoenix::arg_names::arg1;
 88        int array[] = {1,2,3};
 89        int* const end = stable_partition(arg1, even())(array);
 90        BOOST_TEST(end == array + 1);
 91        BOOST_TEST(array[0] == 2);
 92        BOOST_TEST(array[1] == 1);
 93        BOOST_TEST(array[2] == 3);
 94        return;
 95    }
 96
 97    void sort_test()
 98    {
 99        using boost::phoenix::sort;
100        using boost::phoenix::arg_names::arg1;
101        int array[] = {3,1,2};
102        std::list<int> test_list(array, array + 3);
103        sort(arg1)(array);
104        BOOST_TEST(array[0] == 1);
105        BOOST_TEST(array[1] == 2);
106        BOOST_TEST(array[2] == 3);
107
108        sort(arg1)(test_list);
109        std::list<int>::const_iterator it(test_list.begin());
110        BOOST_TEST(*it++ == 1);
111        BOOST_TEST(*it++ == 2);
112        BOOST_TEST(*it++ == 3);
113
114        boost::phoenix::sort(arg1, std::greater<int>())(array);
115        BOOST_TEST(array[0] == 3);
116        BOOST_TEST(array[1] == 2);
117        BOOST_TEST(array[2] == 1);
118
119        boost::phoenix::sort(arg1, std::greater<int>())(test_list);
120        std::list<int>::const_iterator jt(test_list.begin());
121        BOOST_TEST(*jt++ == 3);
122        BOOST_TEST(*jt++ == 2);
123        BOOST_TEST(*jt++ == 1);
124
125        return;
126    }
127
128    void stable_sort_test()
129    {
130        using boost::phoenix::stable_sort;
131        using boost::phoenix::arg_names::arg1;
132        int array[] = {3,1,2};
133        stable_sort(arg1)(array);
134        BOOST_TEST(array[0] == 1);
135        BOOST_TEST(array[1] == 2);
136        BOOST_TEST(array[2] == 3);
137
138        boost::phoenix::stable_sort(arg1, std::greater<int>())(array);
139        BOOST_TEST(array[0] == 3);
140        BOOST_TEST(array[1] == 2);
141        BOOST_TEST(array[2] == 1);
142        
143        return;
144    }
145
146    void partial_sort_test()
147    {
148        using boost::phoenix::partial_sort;
149        using boost::phoenix::arg_names::arg1;
150        int array[] = {2,4,1,3};
151        partial_sort(arg1, array + 2)(array);
152        BOOST_TEST(array[0] == 1);
153        BOOST_TEST(array[1] == 2);
154
155        boost::phoenix::partial_sort(arg1, array + 2, std::greater<int>())(array);
156        BOOST_TEST(array[0] == 4);
157        BOOST_TEST(array[1] == 3);
158        return;
159    }
160
161    void partial_sort_copy_test()
162    {
163        using boost::phoenix::partial_sort_copy;
164        using boost::phoenix::arg_names::arg1;
165        using boost::phoenix::arg_names::arg2;
166        int array[] = {2,4,1,3};
167        int array2[2];
168        partial_sort_copy(arg1, arg2)(array, array2);
169        BOOST_TEST(array2[0] == 1);
170        BOOST_TEST(array2[1] == 2);
171
172        boost::phoenix::partial_sort_copy(arg1, arg2, std::greater<int>())(array, array2);
173        BOOST_TEST(array2[0] == 4);
174        BOOST_TEST(array2[1] == 3);
175
176        return;
177    }
178}
179
180int main()
181{
182    rotate_test();
183    rotate_copy_test();
184    random_shuffle_test();
185    partition_test();
186    stable_partition_test();
187    sort_test();
188    stable_sort_test();
189    partial_sort_test();
190    partial_sort_copy_test();
191    return boost::report_errors();
192}