1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/Assertions.h"
7 #include "mozilla/TypeTraits.h"
9 using mozilla::IsBaseOf
;
10 using mozilla::IsConvertible
;
11 using mozilla::IsSame
;
12 using mozilla::IsSigned
;
13 using mozilla::IsUnsigned
;
14 using mozilla::MakeSigned
;
15 using mozilla::MakeUnsigned
;
17 MOZ_STATIC_ASSERT(!IsSigned
<bool>::value
, "bool shouldn't be signed");
18 MOZ_STATIC_ASSERT(IsUnsigned
<bool>::value
, "bool should be unsigned");
20 MOZ_STATIC_ASSERT(!IsSigned
<const bool>::value
, "const bool shouldn't be signed");
21 MOZ_STATIC_ASSERT(IsUnsigned
<const bool>::value
, "const bool should be unsigned");
23 MOZ_STATIC_ASSERT(!IsSigned
<volatile bool>::value
, "volatile bool shouldn't be signed");
24 MOZ_STATIC_ASSERT(IsUnsigned
<volatile bool>::value
, "volatile bool should be unsigned");
26 MOZ_STATIC_ASSERT(!IsSigned
<unsigned char>::value
, "unsigned char shouldn't be signed");
27 MOZ_STATIC_ASSERT(IsUnsigned
<unsigned char>::value
, "unsigned char should be unsigned");
28 MOZ_STATIC_ASSERT(IsSigned
<signed char>::value
, "signed char should be signed");
29 MOZ_STATIC_ASSERT(!IsUnsigned
<signed char>::value
, "signed char shouldn't be unsigned");
31 MOZ_STATIC_ASSERT(!IsSigned
<unsigned short>::value
, "unsigned short shouldn't be signed");
32 MOZ_STATIC_ASSERT(IsUnsigned
<unsigned short>::value
, "unsigned short should be unsigned");
33 MOZ_STATIC_ASSERT(IsSigned
<short>::value
, "short should be signed");
34 MOZ_STATIC_ASSERT(!IsUnsigned
<short>::value
, "short shouldn't be unsigned");
36 MOZ_STATIC_ASSERT(!IsSigned
<unsigned int>::value
, "unsigned int shouldn't be signed");
37 MOZ_STATIC_ASSERT(IsUnsigned
<unsigned int>::value
, "unsigned int should be unsigned");
38 MOZ_STATIC_ASSERT(IsSigned
<int>::value
, "int should be signed");
39 MOZ_STATIC_ASSERT(!IsUnsigned
<int>::value
, "int shouldn't be unsigned");
41 MOZ_STATIC_ASSERT(!IsSigned
<unsigned long>::value
, "unsigned long shouldn't be signed");
42 MOZ_STATIC_ASSERT(IsUnsigned
<unsigned long>::value
, "unsigned long should be unsigned");
43 MOZ_STATIC_ASSERT(IsSigned
<long>::value
, "long should be signed");
44 MOZ_STATIC_ASSERT(!IsUnsigned
<long>::value
, "long shouldn't be unsigned");
46 MOZ_STATIC_ASSERT(IsSigned
<float>::value
, "float should be signed");
47 MOZ_STATIC_ASSERT(!IsUnsigned
<float>::value
, "float shouldn't be unsigned");
49 MOZ_STATIC_ASSERT(IsSigned
<const float>::value
, "const float should be signed");
50 MOZ_STATIC_ASSERT(!IsUnsigned
<const float>::value
, "const float shouldn't be unsigned");
52 MOZ_STATIC_ASSERT(IsSigned
<double>::value
, "double should be signed");
53 MOZ_STATIC_ASSERT(!IsUnsigned
<double>::value
, "double shouldn't be unsigned");
55 MOZ_STATIC_ASSERT(IsSigned
<volatile double>::value
, "volatile double should be signed");
56 MOZ_STATIC_ASSERT(!IsUnsigned
<volatile double>::value
, "volatile double shouldn't be unsigned");
58 MOZ_STATIC_ASSERT(IsSigned
<long double>::value
, "long double should be signed");
59 MOZ_STATIC_ASSERT(!IsUnsigned
<long double>::value
, "long double shouldn't be unsigned");
61 MOZ_STATIC_ASSERT(IsSigned
<const volatile long double>::value
,
62 "const volatile long double should be signed");
63 MOZ_STATIC_ASSERT(!IsUnsigned
<const volatile long double>::value
,
64 "const volatile long double shouldn't be unsigned");
66 namespace CPlusPlus11IsBaseOf
{
68 // Adapted from C++11 ยง 20.9.6.
72 struct D
: private B1
, private B2
{};
75 StandardIsBaseOfTests()
77 MOZ_ASSERT((IsBaseOf
<B
, D
>::value
) == true);
78 MOZ_ASSERT((IsBaseOf
<const B
, D
>::value
) == true);
79 MOZ_ASSERT((IsBaseOf
<B
, const D
>::value
) == true);
80 MOZ_ASSERT((IsBaseOf
<B
, const B
>::value
) == true);
81 MOZ_ASSERT((IsBaseOf
<D
, B
>::value
) == false);
82 MOZ_ASSERT((IsBaseOf
<B
&, D
&>::value
) == false);
83 MOZ_ASSERT((IsBaseOf
<B
[3], D
[3]>::value
) == false);
84 // We fail at the following test. To fix it, we need to specialize IsBaseOf
85 // for all built-in types.
86 // MOZ_ASSERT((IsBaseOf<int, int>::value) == false);
89 } /* namespace CPlusPlus11IsBaseOf */
92 class B
: public A
{ };
93 class C
: private A
{ };
95 class E
: public A
{ };
96 class F
: public B
, public E
{ };
101 MOZ_ASSERT((IsBaseOf
<A
, B
>::value
),
103 MOZ_ASSERT((!IsBaseOf
<B
, A
>::value
),
104 "B is not a base of A");
105 MOZ_ASSERT((IsBaseOf
<A
, C
>::value
),
107 MOZ_ASSERT((!IsBaseOf
<C
, A
>::value
),
108 "C is not a base of A");
109 MOZ_ASSERT((IsBaseOf
<A
, F
>::value
),
111 MOZ_ASSERT((!IsBaseOf
<F
, A
>::value
),
112 "F is not a base of A");
113 MOZ_ASSERT((!IsBaseOf
<A
, D
>::value
),
114 "A is not a base of D");
115 MOZ_ASSERT((!IsBaseOf
<D
, A
>::value
),
116 "D is not a base of A");
117 MOZ_ASSERT((IsBaseOf
<B
, B
>::value
),
118 "B is the same as B (and therefore, a base of B)");
124 // Pointer type convertibility
125 MOZ_ASSERT((IsConvertible
<A
*, A
*>::value
),
126 "A* should convert to A*");
127 MOZ_ASSERT((IsConvertible
<B
*, A
*>::value
),
128 "B* should convert to A*");
129 MOZ_ASSERT((!IsConvertible
<A
*, B
*>::value
),
130 "A* shouldn't convert to B*");
131 MOZ_ASSERT((!IsConvertible
<A
*, C
*>::value
),
132 "A* shouldn't convert to C*");
133 MOZ_ASSERT((!IsConvertible
<A
*, D
*>::value
),
134 "A* shouldn't convert to unrelated D*");
135 MOZ_ASSERT((!IsConvertible
<D
*, A
*>::value
),
136 "D* shouldn't convert to unrelated A*");
138 // Instance type convertibility
139 MOZ_ASSERT((IsConvertible
<A
, A
>::value
),
141 MOZ_ASSERT((IsConvertible
<B
, A
>::value
),
143 MOZ_ASSERT((!IsConvertible
<D
, A
>::value
),
144 "D and A are unrelated");
145 MOZ_ASSERT((!IsConvertible
<A
, D
>::value
),
146 "A and D are unrelated");
148 // These cases seem to require C++11 support to properly implement them, so
149 // for now just disable them.
150 //MOZ_ASSERT((!IsConvertible<C*, A*>::value),
151 // "C* shouldn't convert to A* (private inheritance)");
152 //MOZ_ASSERT((!IsConvertible<C, A>::value),
153 // "C doesn't convert to A (private inheritance)");
156 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<const unsigned char>::Type
, const signed char>::value
),
157 "const unsigned char won't signify correctly");
158 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<volatile unsigned short>::Type
, volatile signed short>::value
),
159 "volatile unsigned short won't signify correctly");
160 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<const volatile unsigned int>::Type
, const volatile signed int>::value
),
161 "const volatile unsigned int won't signify correctly");
162 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<unsigned long>::Type
, signed long>::value
),
163 "unsigned long won't signify correctly");
164 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<const signed char>::Type
, const signed char>::value
),
165 "const signed char won't signify correctly");
167 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<volatile signed short>::Type
, volatile signed short>::value
),
168 "volatile signed short won't signify correctly");
169 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<const volatile signed int>::Type
, const volatile signed int>::value
),
170 "const volatile signed int won't signify correctly");
171 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<signed long>::Type
, signed long>::value
),
172 "signed long won't signify correctly");
174 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<char>::Type
, signed char>::value
),
175 "char won't signify correctly");
176 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<volatile char>::Type
, volatile signed char>::value
),
177 "volatile char won't signify correctly");
178 MOZ_STATIC_ASSERT((IsSame
<MakeSigned
<const char>::Type
, const signed char>::value
),
179 "const char won't signify correctly");
181 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<const signed char>::Type
, const unsigned char>::value
),
182 "const signed char won't unsignify correctly");
183 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<volatile signed short>::Type
, volatile unsigned short>::value
),
184 "volatile signed short won't unsignify correctly");
185 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<const volatile signed int>::Type
, const volatile unsigned int>::value
),
186 "const volatile signed int won't unsignify correctly");
187 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<signed long>::Type
, unsigned long>::value
),
188 "signed long won't unsignify correctly");
190 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<const unsigned char>::Type
, const unsigned char>::value
),
191 "const unsigned char won't unsignify correctly");
193 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<volatile unsigned short>::Type
, volatile unsigned short>::value
),
194 "volatile unsigned short won't unsignify correctly");
195 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<const volatile unsigned int>::Type
, const volatile unsigned int>::value
),
196 "const volatile unsigned int won't unsignify correctly");
197 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<unsigned long>::Type
, unsigned long>::value
),
198 "signed long won't unsignify correctly");
200 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<char>::Type
, unsigned char>::value
),
201 "char won't unsignify correctly");
202 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<volatile char>::Type
, volatile unsigned char>::value
),
203 "volatile char won't unsignify correctly");
204 MOZ_STATIC_ASSERT((IsSame
<MakeUnsigned
<const char>::Type
, const unsigned char>::value
),
205 "const char won't unsignify correctly");
210 CPlusPlus11IsBaseOf::StandardIsBaseOfTests();