Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
20.meta.trans.sign.cpp
Go to the documentation of this file.
1// -*- C++ -*-
2
30#include <rw_driver.h>
31#include <rw/_defs.h>
32
33// compile out all test code if extensions disabled
34#ifndef _RWSTD_NO_EXT_CXX_0X
35
36#include <type_traits>
37
38#include <stddef.h>
39
40
42template <class T, class U>
43struct test_is_same
44{
45 enum { value = 0 };
46};
47
48template <class T>
49struct test_is_same<T,T>
50{
51 enum { value = 1 };
52};
53
54template <class T>
56 return 0;
57}
58
59template <class T>
60int test_cv_qual (const T&) {
61 return 1;
62}
63
64template <class T>
65int test_cv_qual (volatile T&) {
66 return 2;
67}
68
69template <class T>
70int test_cv_qual (const volatile T&) {
71 return 3;
72}
73
74enum enum_SS { Ess_v = _RWSTD_SCHAR_MAX };
75enum enum_MS { Ems_v = _RWSTD_INT_MAX };
76enum enum_LS { Els_v = _RWSTD_LONG_MAX };
77
78enum enum_SU { Esu_v = _RWSTD_UCHAR_MAX };
79enum enum_MU { Emu_v = _RWSTD_UINT_MAX };
80enum enum_LU { Elu_v = _RWSTD_ULONG_MAX };
81
82
84const char* get_type_name (const volatile void*)
85{
86 return "unknown";
87}
88
89#define _RETURN_TYPE_NAME(T) \
90 const char* get_type_name (T*) { return #T; }
91
92#define _RETURN_TYPE_NAME_CV(T) \
93 _RETURN_TYPE_NAME(T) \
94 _RETURN_TYPE_NAME(const T) \
95 _RETURN_TYPE_NAME(volatile T) \
96 _RETURN_TYPE_NAME(const volatile T)
97
100
101#ifndef _RWSTD_NO_WCHAR_T
103#endif // _RWSTD_NO_WCHAR_T
104
105_RETURN_TYPE_NAME_CV( signed char)
106_RETURN_TYPE_NAME_CV(unsigned char)
107
108_RETURN_TYPE_NAME_CV( signed short)
109_RETURN_TYPE_NAME_CV(unsigned short)
110
111_RETURN_TYPE_NAME_CV( signed int)
112_RETURN_TYPE_NAME_CV(unsigned int)
113
114_RETURN_TYPE_NAME_CV( signed long)
115_RETURN_TYPE_NAME_CV(unsigned long)
116
117#ifndef _RWSTD_NO_LONG_LONG
118_RETURN_TYPE_NAME_CV( signed long long)
119_RETURN_TYPE_NAME_CV(unsigned long long)
120#endif // _RWSTD_NO_LONG_LONG
121
122void test_trait (int line, bool same,
123 const char* trait,
124 const char* typeT,
125 const char* typeU,
126 const char* typeV)
127{
128 rw_assert (same, 0, line,
129 "%s<%s>::type is%{?}n't%{;} %s; got %s",
130 trait, typeT, !same, typeU, typeV);
131}
132
133#define _TEST(Trait,TypeT,TypeU) \
134 test_trait(__LINE__, \
135 std::is_same<Trait<TypeT>::type, TypeU>::value, \
136 #Trait, #TypeT, #TypeU, get_type_name((Trait<TypeT>::type*)0))
137
138#define TEST(Trait,TypeT,TypeU) \
139 _TEST(Trait, TypeT, TypeU); \
140 _TEST(Trait, const TypeT, const TypeU); \
141 _TEST(Trait, volatile TypeT, volatile TypeU); \
142 _TEST(Trait, const volatile TypeT, const volatile TypeU)
143
144
145
147 const char* trait_name,
148 const char* type_name,
149 size_t got_size, size_t exp_size,
150 bool got_sign, bool exp_sign,
151 bool got_c, bool exp_c,
152 bool got_v, bool exp_v)
153{
154 const char* qualifiers[] = {
155 "unqualified", "const", "volatile", "cv-qualified"
156 };
157
158 const unsigned got_cv = got_c + (got_v << 1);
159 const unsigned exp_cv = exp_c + (exp_v << 1);
160
161 const bool success = got_size == exp_size
162 && got_cv == exp_cv
163 && got_sign == exp_sign;
164
165 rw_assert (success, 0, line,
166 "%s<%s>::type is a %zu-byte %s %{?}un%{;}signed type, "
167 "expected a %zu-byte %s %{?}un%{;}signed type.",
168 trait_name, type_name,
169 got_size, qualifiers[got_cv], !got_sign,
170 exp_size, qualifiers[exp_cv], !exp_sign);
171}
172
173#define _TEST_SIGN_AND_CV(Trait,Type,Signed) \
174 test_sign_and_cv(__LINE__, #Trait, #Type, \
175 sizeof (Trait<Type>::type), \
176 sizeof (Type), \
177 std::is_signed<Trait<Type>::type>::value, \
178 Signed, \
179 std::is_const<Trait<Type>::type>::value, \
180 std::is_const<Type>::value, \
181 std::is_volatile<Trait<Type>::type>::value, \
182 std::is_volatile<Type>::value)
183
184#define TEST_MAKE_SIGNED(Type) \
185 _TEST_SIGN_AND_CV (std::make_signed, Type, true); \
186 _TEST_SIGN_AND_CV (std::make_signed, const Type, true); \
187 _TEST_SIGN_AND_CV (std::make_signed, volatile Type, true); \
188 _TEST_SIGN_AND_CV (std::make_signed, const volatile Type, true)
189
190#define TEST_MAKE_UNSIGNED(Type) \
191 _TEST_SIGN_AND_CV (std::make_unsigned, Type, false); \
192 _TEST_SIGN_AND_CV (std::make_unsigned, const Type, false); \
193 _TEST_SIGN_AND_CV (std::make_unsigned, volatile Type, false); \
194 _TEST_SIGN_AND_CV (std::make_unsigned, const volatile Type, false)
195
196
198static void test_make_signed ()
199{
200#if (_RWSTD_CHAR_MIN < 0)
201 TEST (std::make_signed, char, char);
202#else
203 TEST (std::make_signed, char, signed char);
204#endif
205
206 TEST (std::make_signed, signed char, signed char);
207 TEST (std::make_signed, unsigned char, signed char); // unsure
208
209 TEST (std::make_signed, signed short, signed short);
210 TEST (std::make_signed, unsigned short, signed short);
211
212 TEST (std::make_signed, signed int, signed int);
213 TEST (std::make_signed, unsigned int, signed int);
214
215 TEST (std::make_signed, signed long, signed long);
216 TEST (std::make_signed, unsigned long, signed long);
217
218#ifndef _RWSTD_NO_LONG_LONG
219 TEST (std::make_signed, signed long long, signed long long);
220 TEST (std::make_signed, unsigned long long, signed long long);
221#endif // _RWSTD_NO_LONG_LONG
222
223 TEST_MAKE_SIGNED (char);
224
225#ifndef _RWSTD_NO_NATIVE_WCHAR_T
226 TEST_MAKE_SIGNED (wchar_t);
227#endif // _RWSTD_NO_NATIVE_WCHAR_T
228
235}
236
237static void test_make_unsigned ()
238{
239#if (_RWSTD_CHAR_MIN < 0)
240 TEST (std::make_unsigned, char, unsigned char);
241#else
242 TEST (std::make_unsigned, char, char);
243#endif
244
245 TEST (std::make_unsigned, signed char, unsigned char); // unsure
246 TEST (std::make_unsigned, unsigned char, unsigned char);
247
248 TEST (std::make_unsigned, signed short, unsigned short);
249 TEST (std::make_unsigned, unsigned short, unsigned short);
250
251 TEST (std::make_unsigned, signed int, unsigned int);
252 TEST (std::make_unsigned, unsigned int, unsigned int);
253
254 TEST (std::make_unsigned, signed long, unsigned long);
255 TEST (std::make_unsigned, unsigned long, unsigned long);
256
257#ifndef _RWSTD_NO_LONG_LONG
258 TEST (std::make_unsigned, signed long long, unsigned long long);
259 TEST (std::make_unsigned, unsigned long long, unsigned long long);
260#endif // _RWSTD_NO_LONG_LONG
261
262 TEST_MAKE_UNSIGNED (char);
263
264#ifndef _RWSTD_NO_NATIVE_WCHAR_T
265 TEST_MAKE_UNSIGNED (wchar_t);
266#endif // _RWSTD_NO_NATIVE_WCHAR_T
267
274}
275
276
278static int run_test (int, char*[])
279{
280 test_make_signed ();
281 test_make_unsigned ();
282
283 return 0;
284}
285
286
288#else // _RWSTD_NO_EXT_CXX_0X
289
290
292static int run_test (int, char*[])
293{
294 rw_warn (0, 0, __LINE__,
295 "test disabled because _RWSTD_NO_EXT_CXX_0X is defined");
296 return 0;
297}
298
299#endif // !_RWSTD_NO_EXT_CXX_0X
300
301
303int main (int argc, char *argv[])
304{
305 return rw_test (argc, argv, __FILE__,
306 "meta.trans.sign",
307 0 /* no comment */,
308 run_test,
309 0);
310}
#define T
Definition 17.names.cpp:163
#define line
Definition 17.names.cpp:490
void test_trait(int line, bool same, const char *trait, const char *typeT, const char *typeU, const char *typeV)
#define TEST_MAKE_UNSIGNED(Type)
int test_cv_qual(T &)
#define TEST_MAKE_SIGNED(Type)
void test_sign_and_cv(int line, const char *trait_name, const char *type_name, size_t got_size, size_t exp_size, bool got_sign, bool exp_sign, bool got_c, bool exp_c, bool got_v, bool exp_v)
const char * get_type_name(const volatile void *)
#define TEST(Trait, TypeT, TypeU)
#define _RETURN_TYPE_NAME_CV(T)
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
const char * type_name()
#define main
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
_TEST_EXPORT int rw_warn(int, const char *, int, const char *,...)
Definition driver.cpp:1647