1 
2 // Copyright Aleksey Gurtovoy 2003-2007
3 // Copyright David Abrahams 2003-2004
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/mpl for documentation.
10 
11 // $Id$
12 // $Date$
13 // $Revision$
14 
15 #include <boost/mpl/set.hpp>
16 #include <boost/mpl/contains.hpp>
17 #include <boost/mpl/find.hpp>
18 #include <boost/mpl/deref.hpp>
19 #include <boost/mpl/next.hpp>
20 #include <boost/mpl/insert.hpp>
21 #include <boost/mpl/erase.hpp>
22 #include <boost/mpl/erase_key.hpp>
23 #include <boost/mpl/at.hpp>
24 #include <boost/mpl/clear.hpp>
25 #include <boost/mpl/has_key.hpp>
26 #include <boost/mpl/order.hpp>
27 #include <boost/mpl/size.hpp>
28 #include <boost/mpl/distance.hpp>
29 #include <boost/mpl/empty.hpp>
30 #include <boost/mpl/begin_end.hpp>
31 
32 #include <boost/mpl/aux_/test.hpp>
33 
34 
35 // Use templates for testing so that GCC will show us the actual types involved
36 
37 template< typename s >
empty_set_test()38 void empty_set_test()
39 {
40     MPL_ASSERT_RELATION( size<s>::value, ==, 0 );
41     MPL_ASSERT(( empty<s> ));
42 
43     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
44     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, void_ > ));
45     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, void_ > ));
46     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,long>::type, void_ > ));
47 
48     MPL_ASSERT_NOT(( has_key<s,int> ));
49     MPL_ASSERT_NOT(( has_key<s,char> ));
50     MPL_ASSERT_NOT(( has_key<s,long> ));
51 
52     typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
53     typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
54     typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
55     MPL_ASSERT(( is_same< o1, void_ > ));
56     MPL_ASSERT(( is_same< o2, void_ > ));
57     MPL_ASSERT(( is_same< o3, void_ > ));
58 
59     typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
60     typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
61 
62     MPL_ASSERT(( is_same<first, last> ));
63     MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 0 );
64 }
65 
66 
67 template< typename s >
int_set_test()68 void int_set_test()
69 {
70     MPL_ASSERT_RELATION( size<s>::value, ==, 1 );
71     MPL_ASSERT_NOT(( empty<s> ));
72 
73     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
74     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, int > ));
75     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, void_ > ));
76     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,long>::type, void_ > ));
77 
78     MPL_ASSERT(( has_key<s,int> ));
79     MPL_ASSERT_NOT(( has_key<s,char> ));
80     MPL_ASSERT_NOT(( has_key<s,long> ));
81 
82     typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
83     typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
84     typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
85     MPL_ASSERT_NOT(( is_same< o1, void_ > ));
86     MPL_ASSERT(( is_same< o2, void_ > ));
87     MPL_ASSERT(( is_same< o3, void_ > ));
88 
89     typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
90     typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
91 
92     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME deref<first>::type, int > ));
93     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME next<first>::type, last > ));
94 
95     MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 1 );
96     MPL_ASSERT(( contains< s, int > ));
97 }
98 
99 
100 template< typename s >
int_char_set_test()101 void int_char_set_test()
102 {
103     MPL_ASSERT_RELATION( size<s>::value, ==, 2 );
104     MPL_ASSERT_NOT(( empty<s> ));
105     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
106     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, int > ));
107     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, char > ));
108 
109     MPL_ASSERT(( has_key<s,char> ));
110     MPL_ASSERT_NOT(( has_key<s,long> ));
111 
112     typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
113     typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
114     typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
115     MPL_ASSERT_NOT(( is_same< o1, void_ > ));
116     MPL_ASSERT_NOT(( is_same< o2, void_ > ));
117     MPL_ASSERT(( is_same< o3, void_ > ));
118     MPL_ASSERT_NOT(( is_same< o1, o2 > ));
119 
120     typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
121     typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
122 
123     MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 2 );
124 
125     MPL_ASSERT(( contains< s, int > ));
126     MPL_ASSERT(( contains< s, char > ));
127 }
128 
129 template< typename s >
int_char_long_set_test()130 void int_char_long_set_test()
131 {
132     MPL_ASSERT_RELATION( size<s>::value, ==, 3 );
133     MPL_ASSERT_NOT(( empty<s> ));
134     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME clear<s>::type, set0<> > ));
135     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,int>::type, int > ));
136     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,char>::type, char > ));
137     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME at<s,long>::type, long > ));
138 
139     MPL_ASSERT(( has_key<s,long> ));
140     MPL_ASSERT(( has_key<s,int> ));
141     MPL_ASSERT(( has_key<s,char> ));
142 
143     typedef BOOST_DEDUCED_TYPENAME order<s,int>::type o1;
144     typedef BOOST_DEDUCED_TYPENAME order<s,char>::type o2;
145     typedef BOOST_DEDUCED_TYPENAME order<s,long>::type o3;
146     MPL_ASSERT_NOT(( is_same< o1, void_ > ));
147     MPL_ASSERT_NOT(( is_same< o2, void_ > ));
148     MPL_ASSERT_NOT(( is_same< o3, void_ > ));
149     MPL_ASSERT_NOT(( is_same< o1, o2 > ));
150     MPL_ASSERT_NOT(( is_same< o1, o3 > ));
151     MPL_ASSERT_NOT(( is_same< o2, o3 > ));
152 
153     typedef BOOST_DEDUCED_TYPENAME begin<s>::type first;
154     typedef BOOST_DEDUCED_TYPENAME end<s>::type last;
155     MPL_ASSERT_RELATION( (distance<first, last>::value), ==, 3 );
156 
157     MPL_ASSERT(( contains< s, int > ));
158     MPL_ASSERT(( contains< s, char > ));
159     MPL_ASSERT(( contains< s, long > ));
160 }
161 
162 template< typename S0, typename S1, typename S2, typename S3 >
basic_set_test()163 void basic_set_test()
164 {
165     empty_set_test<S0>();
166     empty_set_test< BOOST_DEDUCED_TYPENAME erase_key<S1,int>::type  >();
167     empty_set_test< BOOST_DEDUCED_TYPENAME erase_key<
168           BOOST_DEDUCED_TYPENAME erase_key<S2,char>::type
169         , int
170         >::type >();
171 
172     empty_set_test< BOOST_DEDUCED_TYPENAME erase_key<
173           BOOST_DEDUCED_TYPENAME erase_key<
174                BOOST_DEDUCED_TYPENAME erase_key<S3,char>::type
175               , long
176               >::type
177         , int
178         >::type >();
179 
180 
181     int_set_test<S1>();
182     int_set_test< BOOST_DEDUCED_TYPENAME insert<S0,int>::type >();
183 
184     int_set_test< BOOST_DEDUCED_TYPENAME erase_key<S2,char>::type >();
185     int_set_test< BOOST_DEDUCED_TYPENAME erase_key<
186           BOOST_DEDUCED_TYPENAME erase_key<S3,char>::type
187         , long
188         >::type >();
189 
190     int_char_set_test<S2>();
191     int_char_set_test< BOOST_DEDUCED_TYPENAME insert<
192           BOOST_DEDUCED_TYPENAME insert<S0,char>::type
193         , int
194         >::type >();
195 
196     int_char_set_test< BOOST_DEDUCED_TYPENAME insert<S1,char>::type  >();
197     int_char_set_test< BOOST_DEDUCED_TYPENAME erase_key<S3,long>::type >();
198 
199     int_char_long_set_test<S3>();
200     int_char_long_set_test< BOOST_DEDUCED_TYPENAME insert<
201           BOOST_DEDUCED_TYPENAME insert<
202                BOOST_DEDUCED_TYPENAME insert<S0,char>::type
203               , long
204               >::type
205         , int
206         >::type >();
207 
208     int_char_long_set_test< BOOST_DEDUCED_TYPENAME insert<
209           BOOST_DEDUCED_TYPENAME insert<S1,long>::type
210         , char
211         >::type >();
212 
213     int_char_long_set_test< BOOST_DEDUCED_TYPENAME insert<S2,long>::type  >();
214 }
215 
216 
217 template< typename S1, typename S2 >
numbered_vs_variadic_set_test()218 void numbered_vs_variadic_set_test()
219 {
220     MPL_ASSERT(( is_same< S1, BOOST_DEDUCED_TYPENAME S1::type > ));
221     MPL_ASSERT(( is_same< BOOST_DEDUCED_TYPENAME S2::type, S1 > ));
222 }
223 
224 
MPL_TEST_CASE()225 MPL_TEST_CASE()
226 {
227     typedef mpl::set0<> s01;
228     typedef mpl::set<>  s02;
229     typedef mpl::set1<int> s11;
230     typedef mpl::set<int>  s12;
231     typedef mpl::set2<int,char> s21;
232     typedef mpl::set<int,char>  s22;
233     typedef mpl::set<char,int>  s23;
234     typedef mpl::set3<int,char,long> s31;
235     typedef mpl::set<int,char,long>  s32;
236     typedef mpl::set<int,long,char>  s33;
237     typedef mpl::set<long,char,int>  s34;
238 
239     numbered_vs_variadic_set_test<s01,s02>();
240     numbered_vs_variadic_set_test<s11,s12>();
241     numbered_vs_variadic_set_test<s21,s22>();
242     numbered_vs_variadic_set_test<s31,s32>();
243 
244     basic_set_test<s01,s11,s21,s31>();
245     basic_set_test<s02,s12,s22,s32>();
246     basic_set_test<s01,s11,s23,s31>();
247     basic_set_test<s01,s11,s23,s33>();
248     basic_set_test<s01,s11,s23,s34>();
249 }
250 
251 
252 template< typename s >
empty_set_types_variety_test()253 void empty_set_types_variety_test()
254 {
255     MPL_ASSERT_NOT(( has_key<s,char> ));
256     MPL_ASSERT_NOT(( has_key<s,int> ));
257     MPL_ASSERT_NOT(( has_key<s,UDT> ));
258     MPL_ASSERT_NOT(( has_key<s,incomplete> ));
259 
260     MPL_ASSERT_NOT(( has_key<s,char const> ));
261     MPL_ASSERT_NOT(( has_key<s,int const> ));
262     MPL_ASSERT_NOT(( has_key<s,UDT const> ));
263     MPL_ASSERT_NOT(( has_key<s,incomplete const> ));
264 
265     MPL_ASSERT_NOT(( has_key<s,int*> ));
266     MPL_ASSERT_NOT(( has_key<s,UDT*> ));
267     MPL_ASSERT_NOT(( has_key<s,incomplete*> ));
268 
269     MPL_ASSERT_NOT(( has_key<s,int&> ));
270     MPL_ASSERT_NOT(( has_key<s,UDT&> ));
271     MPL_ASSERT_NOT(( has_key<s,incomplete&> ));
272 }
273 
274 template< typename s >
set_types_variety_test()275 void set_types_variety_test()
276 {
277     MPL_ASSERT_RELATION( size<s>::value, ==, 8 );
278 
279     MPL_ASSERT(( has_key<s,char> ));
280     MPL_ASSERT(( has_key<s,int const> ));
281     MPL_ASSERT(( has_key<s,long*> ));
282     MPL_ASSERT(( has_key<s,UDT* const> ));
283     MPL_ASSERT(( has_key<s,incomplete> ));
284     MPL_ASSERT(( has_key<s,abstract> ));
285     MPL_ASSERT(( has_key<s,incomplete volatile&> ));
286     MPL_ASSERT(( has_key<s,abstract const&> ));
287 
288     MPL_ASSERT_NOT(( has_key<s,char const> ));
289     MPL_ASSERT_NOT(( has_key<s,int> ));
290     MPL_ASSERT_NOT(( has_key<s,long* const> ));
291     MPL_ASSERT_NOT(( has_key<s,UDT*> ));
292     MPL_ASSERT_NOT(( has_key<s,incomplete const> ));
293     MPL_ASSERT_NOT(( has_key<s,abstract volatile> ));
294     MPL_ASSERT_NOT(( has_key<s,incomplete&> ));
295     MPL_ASSERT_NOT(( has_key<s,abstract&> ));
296 }
297 
298 
MPL_TEST_CASE()299 MPL_TEST_CASE()
300 {
301     empty_set_types_variety_test< set<> >();
302     empty_set_types_variety_test< set<>::type >();
303 
304     typedef set<
305           char,int const,long*,UDT* const,incomplete,abstract
306         , incomplete volatile&,abstract const&
307         > s;
308 
309     set_types_variety_test<s>();
310     set_types_variety_test<s::type>();
311 }
312 
313 
314 template <class S>
find_test()315 void find_test()
316 {
317     MPL_ASSERT_RELATION( size<S>::value, ==, 3 );
318 
319     typedef typename end<S>::type not_found;
320     BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<S,int>::type,not_found> ));
321     BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<S,long>::type,not_found> ));
322     BOOST_MPL_ASSERT_NOT(( is_same<BOOST_DEDUCED_TYPENAME find<S,char>::type,not_found> ));
323     BOOST_MPL_ASSERT(( is_same<BOOST_DEDUCED_TYPENAME find<S,char*>::type,not_found> ));
324 }
325 
MPL_TEST_CASE()326 MPL_TEST_CASE()
327 {
328     typedef mpl::set<int,long,char> s;
329     find_test<s>();
330     find_test<s::type>();
331 }
332 
MPL_TEST_CASE()333 MPL_TEST_CASE()
334 {
335     typedef insert< set<>, int >::type little_set;
336 
337     MPL_ASSERT_RELATION(size<little_set>::value, ==, 1);
338     MPL_ASSERT_RELATION(size<little_set::type>::value, ==, 1);
339 }
340 
MPL_TEST_CASE()341 MPL_TEST_CASE()
342 {
343     typedef erase_key< set< float, int >, float >::type little_set;
344 
345     MPL_ASSERT_RELATION(size<little_set>::value, ==, 1);
346     MPL_ASSERT_RELATION(size<little_set::type>::value, ==, 1);
347 }
348