]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /* |
b32b8144 FG |
2 | Copyright 2014 Glen Joseph Fernandes |
3 | (glenjofe@gmail.com) | |
7c673cae | 4 | |
b32b8144 FG |
5 | Distributed under the Boost Software License, Version 1.0. |
6 | (http://www.boost.org/LICENSE_1_0.txt) | |
7c673cae FG |
7 | */ |
8 | #include <boost/align/alignment_of.hpp> | |
9 | #include <boost/core/lightweight_test.hpp> | |
10 | #include <boost/config.hpp> | |
b32b8144 | 11 | #include <cstddef> |
7c673cae FG |
12 | |
13 | template<class T> | |
14 | struct remove_reference { | |
15 | typedef T type; | |
16 | }; | |
17 | ||
18 | template<class T> | |
19 | struct remove_reference<T&> { | |
20 | typedef T type; | |
21 | }; | |
22 | ||
23 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
24 | template<class T> | |
25 | struct remove_reference<T&&> { | |
26 | typedef T type; | |
27 | }; | |
28 | #endif | |
29 | ||
30 | template<class T> | |
31 | struct remove_all_extents { | |
32 | typedef T type; | |
33 | }; | |
34 | ||
35 | template<class T> | |
36 | struct remove_all_extents<T[]> { | |
37 | typedef typename remove_all_extents<T>::type type; | |
38 | }; | |
39 | ||
40 | template<class T, std::size_t N> | |
41 | struct remove_all_extents<T[N]> { | |
42 | typedef typename remove_all_extents<T>::type type; | |
43 | }; | |
44 | ||
45 | template<class T> | |
b32b8144 | 46 | struct remove_cv { |
7c673cae FG |
47 | typedef T type; |
48 | }; | |
49 | ||
50 | template<class T> | |
b32b8144 | 51 | struct remove_cv<const T> { |
7c673cae FG |
52 | typedef T type; |
53 | }; | |
54 | ||
55 | template<class T> | |
b32b8144 | 56 | struct remove_cv<volatile T> { |
7c673cae FG |
57 | typedef T type; |
58 | }; | |
59 | ||
60 | template<class T> | |
b32b8144 | 61 | struct remove_cv<const volatile T> { |
7c673cae FG |
62 | typedef T type; |
63 | }; | |
64 | ||
65 | template<class T> | |
b32b8144 | 66 | struct offset_value { |
7c673cae FG |
67 | char value; |
68 | typename remove_cv<typename remove_all_extents<typename | |
69 | remove_reference<T>::type>::type>::type object; | |
70 | }; | |
71 | ||
7c673cae FG |
72 | template<class T> |
73 | void test_type() | |
74 | { | |
b32b8144 FG |
75 | enum { |
76 | N = boost::alignment::alignment_of<T>::value | |
77 | }; | |
78 | BOOST_TEST(offsetof(offset_value<T>, object) == N); | |
7c673cae FG |
79 | } |
80 | ||
81 | template<class T> | |
82 | void test_reference() | |
83 | { | |
84 | test_type<T>(); | |
85 | test_type<T&>(); | |
86 | ||
87 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) | |
88 | test_type<T&&>(); | |
89 | #endif | |
90 | } | |
91 | ||
92 | template<class T> | |
93 | void test_array() | |
94 | { | |
95 | test_reference<T>(); | |
96 | test_reference<T[2]>(); | |
97 | test_type<T[]>(); | |
98 | } | |
99 | ||
100 | template<class T> | |
101 | void test_cv() | |
102 | { | |
103 | test_array<T>(); | |
104 | test_array<const T>(); | |
105 | test_array<volatile T>(); | |
106 | test_array<const volatile T>(); | |
107 | } | |
108 | ||
109 | template<class T> | |
b32b8144 | 110 | struct Struct { |
7c673cae FG |
111 | T t; |
112 | }; | |
113 | ||
114 | template<class T> | |
b32b8144 | 115 | union Union { |
7c673cae FG |
116 | T t; |
117 | }; | |
118 | ||
119 | template<class T> | |
120 | void test() | |
121 | { | |
122 | test_cv<T>(); | |
b32b8144 FG |
123 | test_cv<Struct<T> >(); |
124 | test_cv<Union<T> >(); | |
7c673cae FG |
125 | } |
126 | ||
127 | void test_integral() | |
128 | { | |
129 | test<bool>(); | |
130 | test<char>(); | |
131 | test<signed char>(); | |
132 | test<unsigned char>(); | |
133 | test<wchar_t>(); | |
134 | ||
135 | #if !defined(BOOST_NO_CXX11_CHAR16_T) | |
136 | test<char16_t>(); | |
137 | #endif | |
138 | ||
139 | #if !defined(BOOST_NO_CXX11_CHAR32_T) | |
140 | test<char32_t>(); | |
141 | #endif | |
142 | ||
143 | test<short>(); | |
144 | test<unsigned short>(); | |
145 | test<int>(); | |
146 | test<unsigned int>(); | |
147 | test<long>(); | |
148 | test<unsigned long>(); | |
149 | ||
150 | #if !defined(BOOST_NO_LONG_LONG) | |
151 | test<long long>(); | |
152 | test<unsigned long long>(); | |
153 | #endif | |
154 | } | |
155 | ||
156 | void test_floating_point() | |
157 | { | |
158 | test<float>(); | |
159 | test<double>(); | |
160 | test<long double>(); | |
161 | } | |
162 | ||
163 | void test_nullptr_t() | |
164 | { | |
165 | #if !defined(BOOST_NO_CXX11_NULLPTR) && \ | |
166 | !defined(BOOST_NO_CXX11_DECLTYPE) | |
167 | test<decltype(nullptr)>(); | |
168 | #endif | |
169 | } | |
170 | ||
171 | class X; | |
172 | ||
173 | void test_pointer() | |
174 | { | |
175 | test<void*>(); | |
176 | test<char*>(); | |
177 | test<int*>(); | |
178 | test<X*>(); | |
179 | test<void(*)()>(); | |
180 | } | |
181 | ||
182 | void test_member_pointer() | |
183 | { | |
184 | test<int X::*>(); | |
185 | test<int(X::*)()>(); | |
186 | } | |
187 | ||
188 | enum E { | |
189 | V = 1 | |
190 | }; | |
191 | ||
192 | void test_enum() | |
193 | { | |
194 | test<E>(); | |
195 | } | |
196 | ||
197 | struct S { }; | |
198 | class C { }; | |
199 | union U { }; | |
200 | ||
201 | void test_class() | |
202 | { | |
203 | test<S>(); | |
204 | test<C>(); | |
205 | test<U>(); | |
206 | } | |
207 | ||
208 | int main() | |
209 | { | |
210 | test_integral(); | |
211 | test_floating_point(); | |
212 | test_nullptr_t(); | |
213 | test_pointer(); | |
214 | test_member_pointer(); | |
215 | test_enum(); | |
216 | test_class(); | |
217 | ||
218 | return boost::report_errors(); | |
219 | } |