OSDN Git Service

a402bcf6b4800123eea51b70c913af899ee3bde7
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / util / testsuite_common_types.h
1 // -*- C++ -*-
2 // typelist for the C++ library testsuite. 
3 //
4 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3.  If not see
20 // <http://www.gnu.org/licenses/>.
21 //
22
23 #ifndef _TESTSUITE_COMMON_TYPES_H
24 #define _TESTSUITE_COMMON_TYPES_H 1
25
26 #include <ext/typelist.h>
27
28 #include <ext/new_allocator.h>
29 #include <ext/malloc_allocator.h>
30 #include <ext/mt_allocator.h>
31 #include <ext/bitmap_allocator.h>
32 #include <ext/pool_allocator.h>
33
34 #include <algorithm>
35
36 #include <vector>
37 #include <list>
38 #include <deque>
39 #include <string>
40
41 #include <map>
42 #include <set>
43 #include <tr1/functional>
44 #include <tr1/unordered_map>
45 #include <tr1/unordered_set>
46
47 #ifdef __GXX_EXPERIMENTAL_CXX0X__
48 #include <atomic>
49 #include <type_traits>
50 #endif
51
52 namespace __gnu_test
53 {
54   using __gnu_cxx::typelist::node;
55   using __gnu_cxx::typelist::transform;
56   using __gnu_cxx::typelist::append;
57
58   // All the allocators to test.
59   template<typename Tp, bool Thread>
60     struct allocator_policies
61     {
62       typedef Tp                                value_type;
63       typedef __gnu_cxx::new_allocator<Tp>              a1;
64       typedef __gnu_cxx::malloc_allocator<Tp>           a2;
65       typedef __gnu_cxx::__common_pool_policy<__gnu_cxx::__pool, Thread> pool_policy;
66       typedef __gnu_cxx::__mt_alloc<Tp, pool_policy>    a3;
67       typedef __gnu_cxx::bitmap_allocator<Tp>           a4;
68       typedef __gnu_cxx::__pool_alloc<Tp>               a5;
69       typedef node<_GLIBCXX_TYPELIST_CHAIN5(a1, a2, a3, a4, a5)> type;
70     };
71
72   // Typelists for vector, string, list, deque.
73   // XXX should just use template templates
74   template<typename Tp, bool Thread>
75     struct vectors
76     {
77       typedef Tp                                        value_type;
78
79       template<typename Tl>
80         struct vector_shell
81         {
82           typedef Tl                                    allocator_type;
83           typedef std::vector<value_type, allocator_type>       type;
84         };
85
86       typedef allocator_policies<value_type, Thread>    allocator_types;
87       typedef typename allocator_types::type            allocator_typelist;
88       typedef typename transform<allocator_typelist, vector_shell>::type type;
89     };
90
91   template<typename Tp, bool Thread>
92     struct lists
93     {
94       typedef Tp                                        value_type;
95
96       template<typename Tl>
97         struct list_shell
98         {
99           typedef Tl                                    allocator_type;
100           typedef std::list<value_type, allocator_type> type;
101         };
102
103       typedef allocator_policies<value_type, Thread>    allocator_types;
104       typedef typename allocator_types::type            allocator_typelist;
105       typedef typename transform<allocator_typelist, list_shell>::type type;
106     };
107
108   template<typename Tp, bool Thread>
109     struct deques
110     {
111       typedef Tp                                        value_type;
112
113       template<typename Tl>
114         struct deque_shell
115         {
116           typedef Tl                                    allocator_type;
117           typedef std::deque<value_type, allocator_type>        type;
118         };
119
120       typedef allocator_policies<value_type, Thread>    allocator_types;
121       typedef typename allocator_types::type            allocator_typelist;
122       typedef typename transform<allocator_typelist, deque_shell>::type type;
123     };
124
125   template<typename Tp, bool Thread>
126     struct strings
127     {
128       typedef Tp                                        value_type;
129
130       template<typename Tl>
131         struct string_shell
132         {
133           typedef Tl                                    allocator_type;
134           typedef std::char_traits<value_type>          traits_type;
135           typedef std::basic_string<value_type, traits_type, allocator_type>    type;
136         };
137
138       typedef allocator_policies<value_type, Thread>    allocator_types;
139       typedef typename allocator_types::type            allocator_typelist;
140       typedef typename transform<allocator_typelist, string_shell>::type type;
141     };
142
143   // A typelist of vector, list, deque, and string all instantiated
144   // with each of the allocator policies.
145   template<typename Tp, bool Thread>
146     struct sequence_containers
147     {
148       typedef Tp                                        value_type;
149
150       typedef typename vectors<value_type, Thread>::type vector_typelist;
151       typedef typename lists<value_type, Thread>::type   list_typelist;
152       typedef typename deques<value_type, Thread>::type  deque_typelist;
153       typedef typename strings<value_type, Thread>::type string_typelist;
154
155       typedef typename append<vector_typelist, list_typelist>::type a1;
156       typedef typename append<deque_typelist, string_typelist>::type a2;
157       typedef typename append<a1, a2>::type type;
158     };
159
160   // Typelists for map, set, unordered_set, unordered_map.
161   template<typename Tp, bool Thread>
162     struct maps
163     {
164       typedef Tp                                        value_type;
165       typedef Tp                                        key_type;
166       typedef std::pair<const key_type, value_type>     pair_type;
167       typedef std::less<key_type>                       compare_function;
168
169       template<typename Tl>
170         struct container
171         {
172           typedef Tl                                    allocator_type;
173           typedef std::map<key_type, value_type, compare_function, allocator_type>      type;
174         };
175
176       typedef allocator_policies<pair_type, Thread>     allocator_types;
177       typedef typename allocator_types::type            allocator_typelist;
178       typedef typename transform<allocator_typelist, container>::type type;
179     };
180
181   template<typename Tp, bool Thread>
182     struct unordered_maps
183     {
184       typedef Tp                                        value_type;
185       typedef Tp                                        key_type;
186       typedef std::pair<const key_type, value_type>     pair_type;
187       typedef std::tr1::hash<key_type>                  hash_function;
188       typedef std::equal_to<key_type>                   equality_function;
189
190       template<typename Tl>
191         struct container
192         {
193           typedef Tl                                    allocator_type;
194           typedef std::tr1::unordered_map<key_type, value_type, hash_function, equality_function, allocator_type>       type;
195         };
196
197       typedef allocator_policies<pair_type, Thread>     allocator_types;
198       typedef typename allocator_types::type            allocator_typelist;
199       typedef typename transform<allocator_typelist, container>::type type;
200     };
201
202   template<typename Tp, bool Thread>
203     struct sets
204     {
205       typedef Tp                                        value_type;
206       typedef Tp                                        key_type;
207       typedef std::less<key_type>                       compare_function;
208
209       template<typename Tl>
210         struct container
211         {
212           typedef Tl                                    allocator_type;
213           typedef std::set<key_type, compare_function, allocator_type>  type;
214         };
215
216       typedef allocator_policies<key_type, Thread>      allocator_types;
217       typedef typename allocator_types::type            allocator_typelist;
218       typedef typename transform<allocator_typelist, container>::type type;
219     };
220
221   template<typename Tp, bool Thread>
222     struct unordered_sets
223     {
224       typedef Tp                                        value_type;
225       typedef Tp                                        key_type;
226       typedef std::tr1::hash<key_type>                  hash_function;
227       typedef std::equal_to<key_type>                   equality_function;
228
229       template<typename Tl>
230         struct container
231         {
232           typedef Tl                                    allocator_type;
233           typedef std::tr1::unordered_set<key_type, hash_function, equality_function, allocator_type>   type;
234         };
235
236       typedef allocator_policies<key_type, Thread>      allocator_types;
237       typedef typename allocator_types::type            allocator_typelist;
238       typedef typename transform<allocator_typelist, container>::type type;
239     };
240
241
242   // A typelist  of all associated  container types, with each  of the
243   // allocator policies.
244   template<typename Tp, bool Thread>
245     struct associative_containers
246     {
247       typedef Tp                                        value_type;
248
249       typedef typename maps<value_type, Thread>::type map_typelist;
250       typedef typename sets<value_type, Thread>::type set_typelist;
251       typedef typename unordered_maps<value_type, Thread>::type unordered_map_typelist;
252       typedef typename unordered_sets<value_type, Thread>::type unordered_set_typelist;
253
254       typedef typename append<map_typelist, unordered_map_typelist>::type a1;
255       typedef typename append<set_typelist, unordered_set_typelist>::type a2;
256       typedef typename append<a1, a2>::type type;
257     };
258
259   // A typelist of all integral types.
260   struct integral_types
261   {
262     typedef bool                a1;
263     typedef char                a2;
264     typedef signed char         a3;
265     typedef unsigned char       a4;
266     typedef short               a5;
267     typedef unsigned short      a6;
268     typedef int                 a7;
269     typedef unsigned int        a8;
270     typedef long                a9;
271     typedef unsigned long       a10;
272     typedef long long           a11;
273     typedef unsigned long long  a12;
274     typedef wchar_t             a13;
275 #ifdef __GXX_EXPERIMENTAL_CXX0X__
276     typedef char16_t            a14;
277     typedef char32_t            a15;
278
279     typedef node<_GLIBCXX_TYPELIST_CHAIN15(a1, a2, a3, a4, a5, a6, a7, a8, a9, 
280                                            a10, a11, a12, a13, a14, a15)> type;
281 #else
282     typedef node<_GLIBCXX_TYPELIST_CHAIN13(a1, a2, a3, a4, a5, a6, a7, a8, a9, 
283                                            a10, a11, a12, a13)> type;
284 #endif
285   };
286
287 #ifdef __GXX_EXPERIMENTAL_CXX0X__
288   struct atomic_integrals_no_bool
289   {
290     typedef std::atomic_char            a2;
291     typedef std::atomic_schar           a3;
292     typedef std::atomic_uchar           a4;
293     typedef std::atomic_short           a5;
294     typedef std::atomic_ushort          a6;
295     typedef std::atomic_int             a7;
296     typedef std::atomic_uint            a8;
297     typedef std::atomic_long            a9;
298     typedef std::atomic_ulong           a10;
299     typedef std::atomic_llong           a11;
300     typedef std::atomic_ullong          a12;
301     typedef std::atomic_wchar_t         a13;
302     typedef std::atomic_char16_t        a14;
303     typedef std::atomic_char32_t        a15;
304     
305     typedef node<_GLIBCXX_TYPELIST_CHAIN14(a2, a3, a4, a5, a6, a7, a8, a9, 
306                                            a10, a11, a12, a13, a14, a15)> type;
307   };
308
309   struct atomic_integrals
310   {
311     typedef std::atomic_bool            a1;
312     typedef std::atomic_char            a2;
313     typedef std::atomic_schar           a3;
314     typedef std::atomic_uchar           a4;
315     typedef std::atomic_short           a5;
316     typedef std::atomic_ushort          a6;
317     typedef std::atomic_int             a7;
318     typedef std::atomic_uint            a8;
319     typedef std::atomic_long            a9;
320     typedef std::atomic_ulong           a10;
321     typedef std::atomic_llong           a11;
322     typedef std::atomic_ullong          a12;
323     typedef std::atomic_wchar_t         a13;
324     typedef std::atomic_char16_t        a14;
325     typedef std::atomic_char32_t        a15;
326     
327     typedef node<_GLIBCXX_TYPELIST_CHAIN15(a1, a2, a3, a4, a5, a6, a7, a8, a9, 
328                                            a10, a11, a12, a13, a14, a15)> type;
329   };
330
331
332   template<typename Tp>
333     struct atomics
334     {
335       typedef Tp                        value_type;
336       typedef std::atomic<value_type>   type;
337     };
338
339   typedef transform<integral_types::type, atomics>::type atomics_tl;
340 #endif
341
342   template<typename Tp>
343     struct numeric_limits
344     {
345       typedef Tp                        value_type;
346       typedef std::numeric_limits<value_type>   type;
347     };
348
349   typedef transform<integral_types::type, numeric_limits>::type limits_tl;
350
351   struct has_increment_operators
352   {
353     template<typename _Tp>
354       void 
355       operator()()
356       {
357         struct _Concept
358         {
359           void __constraint()
360           {
361             _Tp a; 
362             ++a; // prefix
363             a++; // postfix
364             a += a;
365           }
366         };
367
368         void (_Concept::*__x)() __attribute__((unused))
369           = &_Concept::__constraint;
370       }
371   };
372
373   struct has_decrement_operators
374   {
375     template<typename _Tp>
376       void 
377       operator()()
378       {
379         struct _Concept
380         {
381           void __constraint()
382           {
383             _Tp a; 
384             --a; // prefix
385             a--; // postfix
386             a -= a;
387           }
388         };
389
390         void (_Concept::*__x)() __attribute__((unused))
391           = &_Concept::__constraint;
392       }
393   };
394
395 #ifdef __GXX_EXPERIMENTAL_CXX0X__
396   template<typename _Tp>
397     void
398     constexpr_bitwise_operators()
399     {
400       constexpr _Tp a = _Tp();
401       constexpr _Tp b = _Tp();
402       constexpr _Tp c1 __attribute__((unused)) = a | b;
403       constexpr _Tp c2 __attribute__((unused)) = a & b;
404       constexpr _Tp c3 __attribute__((unused)) = a ^ b;
405       constexpr _Tp c4 __attribute__((unused)) = ~b;
406     }
407 #endif
408
409   template<typename _Tp>
410     void
411     bitwise_operators()
412     {
413       _Tp a = _Tp();
414       _Tp b = _Tp();
415       a | b;
416       a & b;
417       a ^ b;
418       ~b;
419     }
420
421   template<typename _Tp>
422     void
423     bitwise_assignment_operators()
424     {
425       _Tp a = _Tp();
426       _Tp b = _Tp();
427       a |= b; // set
428       a &= ~b; // clear
429       a ^= b;
430     }
431
432   // 17.3.2.1.2 - Bitmask types [lib.bitmask.types]
433   // bitmask_operators
434   template<typename _BitmTp>
435     void
436     bitmask_operators()
437     {
438       bitwise_operators<_BitmTp>();
439       bitwise_assignment_operators<_BitmTp>();
440     }
441
442   struct has_bitwise_operators
443   {
444     template<typename _Tp>
445       void 
446       operator()()
447       {
448         struct _Concept
449         {
450           void __constraint()
451           {
452             a |= b; // set
453             a &= ~b; // clear
454             a ^= b;
455           }
456           _Tp a;
457           _Tp b;
458         };
459
460         void (_Concept::*__x)() __attribute__((unused))
461           = &_Concept::__constraint;
462       }
463   };
464
465   // Generator to test standard layout
466 #ifdef __GXX_EXPERIMENTAL_CXX0X__
467   struct has_trivial_cons_dtor
468   {
469     template<typename _Tp>
470       void 
471       operator()()
472       {
473         struct _Concept
474         {
475           void __constraint()
476           {
477             typedef std::has_trivial_default_constructor<_Tp> ctor_p;
478             static_assert(ctor_p::value, "default constructor not trivial");
479
480             typedef std::has_trivial_destructor<_Tp> dtor_p;
481             static_assert(dtor_p::value, "destructor not trivial");
482           }
483         };
484
485         void (_Concept::*__x)() __attribute__((unused))
486           = &_Concept::__constraint;
487       }
488   };
489
490   struct standard_layout
491   {
492     template<typename _Tp>
493       void 
494       operator()()
495       {
496         struct _Concept
497         {
498           void __constraint()
499           {
500             typedef std::is_standard_layout<_Tp> standard_layout_p;
501             static_assert(standard_layout_p::value, "not standard_layout");
502           }
503         };
504
505         void (_Concept::*__x)() __attribute__((unused))
506           = &_Concept::__constraint;
507       }
508   };
509 #endif
510
511   // Generator to test base class
512   struct has_required_base_class
513   {
514     template<typename _TBase, typename _TDerived>
515       void 
516       operator()()
517       {
518         struct _Concept
519         {
520           void __constraint()
521           {
522             const _TDerived& obj = __a;
523             const _TBase* base __attribute__((unused)) = &obj;
524           }
525           
526           _TDerived __a;
527         };
528
529         void (_Concept::*__x)() __attribute__((unused))
530           = &_Concept::__constraint;
531       }
532   };
533
534   // Generator to test assignment operator.
535   struct assignable
536   {
537     template<typename _Tp>
538       void 
539       operator()()
540       {
541         struct _Concept
542         {
543           void __constraint()
544           { __v1 = __v2; }
545
546           _Tp __v1;
547           _Tp __v2;
548         };
549
550         void (_Concept::*__x)() __attribute__((unused))
551           = &_Concept::__constraint;
552       }
553   };
554
555   // Generator to test default constructor.
556   struct default_constructible
557   {
558     template<typename _Tp>
559       void 
560       operator()()
561       {
562         struct _Concept
563         {
564           void __constraint()
565           { _Tp __v __attribute__((unused)); }
566         };
567
568         void (_Concept::*__x)() __attribute__((unused))
569           = &_Concept::__constraint;
570       }
571   };
572
573   // Generator to test copy constructor.
574   struct copy_constructible
575   {
576     template<typename _Tp>
577       void 
578       operator()()
579       {
580         struct _Concept
581         {
582           void __constraint()
583           { _Tp __v2(__v1); }
584
585           _Tp __v1;
586         };
587
588         void (_Concept::*__x)() __attribute__((unused))
589           = &_Concept::__constraint;
590       }
591   };
592
593   // Generator to test direct initialization, single value constructor.
594   struct single_value_constructible
595   {
596     template<typename _Ttype, typename _Tvalue>
597       void 
598       operator()()
599       {
600         struct _Concept
601         {
602           void __constraint()
603           { _Ttype __v(__a); }
604           
605           _Tvalue __a;
606         };
607
608         void (_Concept::*__x)() __attribute__((unused))
609           = &_Concept::__constraint;
610       }
611   };
612
613   // Generator to test constexpr constructor
614 #ifdef __GXX_EXPERIMENTAL_CXX0X__
615   // Generator to test default constructor.
616   struct constexpr_default_constructible
617   {
618     template<typename _Tp>
619       void 
620       operator()()
621       {
622         struct _Concept
623         {
624           // Have to have user-defined default ctor for this to work.
625           void __constraint()
626           { constexpr _Tp __v; }
627         };
628
629         void (_Concept::*__x)() __attribute__((unused))
630           = &_Concept::__constraint;
631       }
632   };
633
634   struct constexpr_single_value_constructible
635   {
636     template<typename _Ttesttype, typename _Tbasetype>
637       void
638       operator()()
639       {
640         struct _Concept
641         {
642           // Additional constraint on _Tbasetype needed.
643           // Either assume user-defined default ctor as per
644           // constexpr_default_constructible and provide no
645           // initializer, provide an initializer, or assume empty-list
646           // init-able. Choose the latter.
647           void __constraint()
648           {
649             constexpr _Tbasetype __v { };
650             constexpr _Ttesttype __t(__v);
651           }
652         };
653
654         _Concept c;
655         c.__constraint();
656       }
657   };
658 #endif
659
660   // Generator to test direct list initialization
661 #ifdef __GXX_EXPERIMENTAL_CXX0X__
662   struct direct_list_initializable
663   {
664     template<typename _Ttype, typename _Tvalue>
665       void 
666       operator()()
667       {
668         struct _Concept
669         {
670           void __constraint()
671           { 
672             _Ttype __v1 { }; // default ctor
673             _Ttype __v2 { __a };  // single-argument ctor
674           }
675           
676           _Tvalue __a;
677         };
678
679         void (_Concept::*__x)() __attribute__((unused))
680           = &_Concept::__constraint;
681       }
682   };
683 #endif
684
685   // Generator to test copy list initialization, aggregate initialization
686   struct copy_list_initializable
687   {
688     template<typename _Ttype, typename _Tvalue>
689       void 
690       operator()()
691       {
692         struct _Concept
693         {
694           void __constraint()
695           { _Ttype __v __attribute__((unused)) = {__a}; }
696           
697           _Tvalue __a;
698         };
699
700         void (_Concept::*__x)() __attribute__((unused))
701           = &_Concept::__constraint;
702       }
703   };
704
705   // Generator to test integral conversion operator
706   struct integral_convertable
707   {
708     template<typename _Ttype, typename _Tvalue>
709       void 
710       operator()()
711       {
712         struct _Concept
713         {
714           void __constraint()
715           {
716             _Tvalue __v0(0);
717             _Tvalue __v1(1);
718             _Ttype __a(__v1);
719             __v0 = __a;
720
721             bool test __attribute__((unused)) = true;
722             VERIFY( __v1 == __v0 );
723           }
724         };
725
726         void (_Concept::*__x)() __attribute__((unused))
727           = &_Concept::__constraint;
728       }
729   };
730
731   // Generator to test integral assignment operator 
732   struct integral_assignable
733   {
734     template<typename _Ttype, typename _Tvalue>
735       void 
736       operator()()
737       {
738         struct _Concept
739         {
740           void __constraint()
741           {
742             _Tvalue __v0(0);
743             _Tvalue __v1(1);
744             _Ttype __a(__v0);
745             __a = __v1;
746             _Tvalue __vr = __a;
747
748             bool test __attribute__((unused)) = true;
749             VERIFY( __v1 == __vr );
750           }
751         };
752
753         void (_Concept::*__x)() __attribute__((unused))
754           = &_Concept::__constraint;
755       }
756   };
757 } // namespace __gnu_test
758 #endif