mfbt/tests/TestTypeTraits.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/Assertions.h"
michael@0 7 #include "mozilla/TypeTraits.h"
michael@0 8
michael@0 9 using mozilla::IsArray;
michael@0 10 using mozilla::IsBaseOf;
michael@0 11 using mozilla::IsClass;
michael@0 12 using mozilla::IsConvertible;
michael@0 13 using mozilla::IsEmpty;
michael@0 14 using mozilla::IsLvalueReference;
michael@0 15 using mozilla::IsReference;
michael@0 16 using mozilla::IsRvalueReference;
michael@0 17 using mozilla::IsSame;
michael@0 18 using mozilla::IsSigned;
michael@0 19 using mozilla::IsUnsigned;
michael@0 20 using mozilla::MakeSigned;
michael@0 21 using mozilla::MakeUnsigned;
michael@0 22
michael@0 23 static_assert(!IsArray<bool>::value, "bool not an array");
michael@0 24 static_assert(IsArray<bool[]>::value, "bool[] is an array");
michael@0 25 static_assert(IsArray<bool[5]>::value, "bool[5] is an array");
michael@0 26
michael@0 27 static_assert(!IsLvalueReference<bool>::value, "bool not an lvalue reference");
michael@0 28 static_assert(!IsLvalueReference<bool*>::value, "bool* not an lvalue reference");
michael@0 29 static_assert(IsLvalueReference<bool&>::value, "bool& is an lvalue reference");
michael@0 30 static_assert(!IsLvalueReference<bool&&>::value, "bool&& not an lvalue reference");
michael@0 31
michael@0 32 static_assert(!IsLvalueReference<void>::value, "void not an lvalue reference");
michael@0 33 static_assert(!IsLvalueReference<void*>::value, "void* not an lvalue reference");
michael@0 34
michael@0 35 static_assert(!IsLvalueReference<int>::value, "int not an lvalue reference");
michael@0 36 static_assert(!IsLvalueReference<int*>::value, "int* not an lvalue reference");
michael@0 37 static_assert(IsLvalueReference<int&>::value, "int& is an lvalue reference");
michael@0 38 static_assert(!IsLvalueReference<int&&>::value, "int&& not an lvalue reference");
michael@0 39
michael@0 40 static_assert(!IsRvalueReference<bool>::value, "bool not an rvalue reference");
michael@0 41 static_assert(!IsRvalueReference<bool*>::value, "bool* not an rvalue reference");
michael@0 42 static_assert(!IsRvalueReference<bool&>::value, "bool& not an rvalue reference");
michael@0 43 static_assert(IsRvalueReference<bool&&>::value, "bool&& is an rvalue reference");
michael@0 44
michael@0 45 static_assert(!IsRvalueReference<void>::value, "void not an rvalue reference");
michael@0 46 static_assert(!IsRvalueReference<void*>::value, "void* not an rvalue reference");
michael@0 47
michael@0 48 static_assert(!IsRvalueReference<int>::value, "int not an rvalue reference");
michael@0 49 static_assert(!IsRvalueReference<int*>::value, "int* not an rvalue reference");
michael@0 50 static_assert(!IsRvalueReference<int&>::value, "int& not an rvalue reference");
michael@0 51 static_assert(IsRvalueReference<int&&>::value, "int&& is an rvalue reference");
michael@0 52
michael@0 53 static_assert(!IsReference<bool>::value, "bool not a reference");
michael@0 54 static_assert(!IsReference<bool*>::value, "bool* not a reference");
michael@0 55 static_assert(IsReference<bool&>::value, "bool& is a reference");
michael@0 56 static_assert(IsReference<bool&&>::value, "bool&& is a reference");
michael@0 57
michael@0 58 static_assert(!IsReference<void>::value, "void not a reference");
michael@0 59 static_assert(!IsReference<void*>::value, "void* not a reference");
michael@0 60
michael@0 61 static_assert(!IsReference<int>::value, "int not a reference");
michael@0 62 static_assert(!IsReference<int*>::value, "int* not a reference");
michael@0 63 static_assert(IsReference<int&>::value, "int& is a reference");
michael@0 64 static_assert(IsReference<int&&>::value, "int&& is a reference");
michael@0 65
michael@0 66 struct S1 {};
michael@0 67 union U1 { int x; };
michael@0 68
michael@0 69 static_assert(!IsClass<int>::value, "int isn't a class");
michael@0 70 static_assert(IsClass<const S1>::value, "S is a class");
michael@0 71 static_assert(!IsClass<U1>::value, "U isn't a class");
michael@0 72
michael@0 73 static_assert(!mozilla::IsEmpty<int>::value, "not a class => not empty");
michael@0 74 static_assert(!mozilla::IsEmpty<bool[5]>::value, "not a class => not empty");
michael@0 75
michael@0 76 static_assert(!mozilla::IsEmpty<U1>::value, "not a class => not empty");
michael@0 77
michael@0 78 struct E1 {};
michael@0 79 struct E2 { int : 0; };
michael@0 80 struct E3 : E1 {};
michael@0 81 struct E4 : E2 {};
michael@0 82
michael@0 83 static_assert(IsEmpty<const volatile S1>::value, "S should be empty");
michael@0 84
michael@0 85 static_assert(mozilla::IsEmpty<E1>::value &&
michael@0 86 mozilla::IsEmpty<E2>::value &&
michael@0 87 mozilla::IsEmpty<E3>::value &&
michael@0 88 mozilla::IsEmpty<E4>::value,
michael@0 89 "all empty");
michael@0 90
michael@0 91 union U2 { E1 e1; };
michael@0 92 static_assert(!mozilla::IsEmpty<U2>::value, "not a class => not empty");
michael@0 93
michael@0 94 struct NE1 { int x; };
michael@0 95 struct NE2 : virtual E1 {};
michael@0 96 struct NE3 : E2 { virtual ~NE3() {} };
michael@0 97 struct NE4 { virtual void f() {} };
michael@0 98
michael@0 99 static_assert(!mozilla::IsEmpty<NE1>::value &&
michael@0 100 !mozilla::IsEmpty<NE2>::value &&
michael@0 101 !mozilla::IsEmpty<NE3>::value &&
michael@0 102 !mozilla::IsEmpty<NE4>::value,
michael@0 103 "all empty");
michael@0 104
michael@0 105 static_assert(!IsSigned<bool>::value, "bool shouldn't be signed");
michael@0 106 static_assert(IsUnsigned<bool>::value, "bool should be unsigned");
michael@0 107
michael@0 108 static_assert(!IsSigned<const bool>::value, "const bool shouldn't be signed");
michael@0 109 static_assert(IsUnsigned<const bool>::value, "const bool should be unsigned");
michael@0 110
michael@0 111 static_assert(!IsSigned<volatile bool>::value, "volatile bool shouldn't be signed");
michael@0 112 static_assert(IsUnsigned<volatile bool>::value, "volatile bool should be unsigned");
michael@0 113
michael@0 114 static_assert(!IsSigned<unsigned char>::value, "unsigned char shouldn't be signed");
michael@0 115 static_assert(IsUnsigned<unsigned char>::value, "unsigned char should be unsigned");
michael@0 116 static_assert(IsSigned<signed char>::value, "signed char should be signed");
michael@0 117 static_assert(!IsUnsigned<signed char>::value, "signed char shouldn't be unsigned");
michael@0 118
michael@0 119 static_assert(!IsSigned<unsigned short>::value, "unsigned short shouldn't be signed");
michael@0 120 static_assert(IsUnsigned<unsigned short>::value, "unsigned short should be unsigned");
michael@0 121 static_assert(IsSigned<short>::value, "short should be signed");
michael@0 122 static_assert(!IsUnsigned<short>::value, "short shouldn't be unsigned");
michael@0 123
michael@0 124 static_assert(!IsSigned<unsigned int>::value, "unsigned int shouldn't be signed");
michael@0 125 static_assert(IsUnsigned<unsigned int>::value, "unsigned int should be unsigned");
michael@0 126 static_assert(IsSigned<int>::value, "int should be signed");
michael@0 127 static_assert(!IsUnsigned<int>::value, "int shouldn't be unsigned");
michael@0 128
michael@0 129 static_assert(!IsSigned<unsigned long>::value, "unsigned long shouldn't be signed");
michael@0 130 static_assert(IsUnsigned<unsigned long>::value, "unsigned long should be unsigned");
michael@0 131 static_assert(IsSigned<long>::value, "long should be signed");
michael@0 132 static_assert(!IsUnsigned<long>::value, "long shouldn't be unsigned");
michael@0 133
michael@0 134 static_assert(IsSigned<float>::value, "float should be signed");
michael@0 135 static_assert(!IsUnsigned<float>::value, "float shouldn't be unsigned");
michael@0 136
michael@0 137 static_assert(IsSigned<const float>::value, "const float should be signed");
michael@0 138 static_assert(!IsUnsigned<const float>::value, "const float shouldn't be unsigned");
michael@0 139
michael@0 140 static_assert(IsSigned<double>::value, "double should be signed");
michael@0 141 static_assert(!IsUnsigned<double>::value, "double shouldn't be unsigned");
michael@0 142
michael@0 143 static_assert(IsSigned<volatile double>::value, "volatile double should be signed");
michael@0 144 static_assert(!IsUnsigned<volatile double>::value, "volatile double shouldn't be unsigned");
michael@0 145
michael@0 146 static_assert(IsSigned<long double>::value, "long double should be signed");
michael@0 147 static_assert(!IsUnsigned<long double>::value, "long double shouldn't be unsigned");
michael@0 148
michael@0 149 static_assert(IsSigned<const volatile long double>::value,
michael@0 150 "const volatile long double should be signed");
michael@0 151 static_assert(!IsUnsigned<const volatile long double>::value,
michael@0 152 "const volatile long double shouldn't be unsigned");
michael@0 153
michael@0 154 namespace CPlusPlus11IsBaseOf {
michael@0 155
michael@0 156 // Adapted from C++11 ยง 20.9.6.
michael@0 157 struct B {};
michael@0 158 struct B1 : B {};
michael@0 159 struct B2 : B {};
michael@0 160 struct D : private B1, private B2 {};
michael@0 161
michael@0 162 static void
michael@0 163 StandardIsBaseOfTests()
michael@0 164 {
michael@0 165 static_assert((IsBaseOf<B, D>::value) == true, "IsBaseOf fails on diamond");
michael@0 166 static_assert((IsBaseOf<const B, D>::value) == true, "IsBaseOf fails on diamond plus constness change");
michael@0 167 static_assert((IsBaseOf<B, const D>::value) == true, "IsBaseOf fails on diamond plus constness change");
michael@0 168 static_assert((IsBaseOf<B, const B>::value) == true, "IsBaseOf fails on constness change");
michael@0 169 static_assert((IsBaseOf<D, B>::value) == false, "IsBaseOf got the direction of inheritance wrong");
michael@0 170 static_assert((IsBaseOf<B&, D&>::value) == false, "IsBaseOf should return false on references");
michael@0 171 static_assert((IsBaseOf<B[3], D[3]>::value) == false, "IsBaseOf should return false on arrays");
michael@0 172 // We fail at the following test. To fix it, we need to specialize IsBaseOf
michael@0 173 // for all built-in types.
michael@0 174 // static_assert((IsBaseOf<int, int>::value) == false);
michael@0 175 }
michael@0 176
michael@0 177 } /* namespace CPlusPlus11IsBaseOf */
michael@0 178
michael@0 179 class A { };
michael@0 180 class B : public A { };
michael@0 181 class C : private A { };
michael@0 182 class D { };
michael@0 183 class E : public A { };
michael@0 184 class F : public B, public E { };
michael@0 185
michael@0 186 static void
michael@0 187 TestIsBaseOf()
michael@0 188 {
michael@0 189 static_assert((IsBaseOf<A, B>::value),
michael@0 190 "A is a base of B");
michael@0 191 static_assert((!IsBaseOf<B, A>::value),
michael@0 192 "B is not a base of A");
michael@0 193 static_assert((IsBaseOf<A, C>::value),
michael@0 194 "A is a base of C");
michael@0 195 static_assert((!IsBaseOf<C, A>::value),
michael@0 196 "C is not a base of A");
michael@0 197 static_assert((IsBaseOf<A, F>::value),
michael@0 198 "A is a base of F");
michael@0 199 static_assert((!IsBaseOf<F, A>::value),
michael@0 200 "F is not a base of A");
michael@0 201 static_assert((!IsBaseOf<A, D>::value),
michael@0 202 "A is not a base of D");
michael@0 203 static_assert((!IsBaseOf<D, A>::value),
michael@0 204 "D is not a base of A");
michael@0 205 static_assert((IsBaseOf<B, B>::value),
michael@0 206 "B is the same as B (and therefore, a base of B)");
michael@0 207 }
michael@0 208
michael@0 209 static void
michael@0 210 TestIsConvertible()
michael@0 211 {
michael@0 212 // Pointer type convertibility
michael@0 213 static_assert((IsConvertible<A*, A*>::value),
michael@0 214 "A* should convert to A*");
michael@0 215 static_assert((IsConvertible<B*, A*>::value),
michael@0 216 "B* should convert to A*");
michael@0 217 static_assert((!IsConvertible<A*, B*>::value),
michael@0 218 "A* shouldn't convert to B*");
michael@0 219 static_assert((!IsConvertible<A*, C*>::value),
michael@0 220 "A* shouldn't convert to C*");
michael@0 221 static_assert((!IsConvertible<A*, D*>::value),
michael@0 222 "A* shouldn't convert to unrelated D*");
michael@0 223 static_assert((!IsConvertible<D*, A*>::value),
michael@0 224 "D* shouldn't convert to unrelated A*");
michael@0 225
michael@0 226 // Instance type convertibility
michael@0 227 static_assert((IsConvertible<A, A>::value),
michael@0 228 "A is A");
michael@0 229 static_assert((IsConvertible<B, A>::value),
michael@0 230 "B converts to A");
michael@0 231 static_assert((!IsConvertible<D, A>::value),
michael@0 232 "D and A are unrelated");
michael@0 233 static_assert((!IsConvertible<A, D>::value),
michael@0 234 "A and D are unrelated");
michael@0 235
michael@0 236 // These cases seem to require C++11 support to properly implement them, so
michael@0 237 // for now just disable them.
michael@0 238 //static_assert((!IsConvertible<C*, A*>::value),
michael@0 239 // "C* shouldn't convert to A* (private inheritance)");
michael@0 240 //static_assert((!IsConvertible<C, A>::value),
michael@0 241 // "C doesn't convert to A (private inheritance)");
michael@0 242 }
michael@0 243
michael@0 244 static_assert(IsSame<MakeSigned<const unsigned char>::Type, const signed char>::value,
michael@0 245 "const unsigned char won't signify correctly");
michael@0 246 static_assert(IsSame<MakeSigned<volatile unsigned short>::Type, volatile signed short>::value,
michael@0 247 "volatile unsigned short won't signify correctly");
michael@0 248 static_assert(IsSame<MakeSigned<const volatile unsigned int>::Type, const volatile signed int>::value,
michael@0 249 "const volatile unsigned int won't signify correctly");
michael@0 250 static_assert(IsSame<MakeSigned<unsigned long>::Type, signed long>::value,
michael@0 251 "unsigned long won't signify correctly");
michael@0 252 static_assert(IsSame<MakeSigned<const signed char>::Type, const signed char>::value,
michael@0 253 "const signed char won't signify correctly");
michael@0 254
michael@0 255 static_assert(IsSame<MakeSigned<volatile signed short>::Type, volatile signed short>::value,
michael@0 256 "volatile signed short won't signify correctly");
michael@0 257 static_assert(IsSame<MakeSigned<const volatile signed int>::Type, const volatile signed int>::value,
michael@0 258 "const volatile signed int won't signify correctly");
michael@0 259 static_assert(IsSame<MakeSigned<signed long>::Type, signed long>::value,
michael@0 260 "signed long won't signify correctly");
michael@0 261
michael@0 262 static_assert(IsSame<MakeSigned<char>::Type, signed char>::value,
michael@0 263 "char won't signify correctly");
michael@0 264 static_assert(IsSame<MakeSigned<volatile char>::Type, volatile signed char>::value,
michael@0 265 "volatile char won't signify correctly");
michael@0 266 static_assert(IsSame<MakeSigned<const char>::Type, const signed char>::value,
michael@0 267 "const char won't signify correctly");
michael@0 268
michael@0 269 static_assert(IsSame<MakeUnsigned<const signed char>::Type, const unsigned char>::value,
michael@0 270 "const signed char won't unsignify correctly");
michael@0 271 static_assert(IsSame<MakeUnsigned<volatile signed short>::Type, volatile unsigned short>::value,
michael@0 272 "volatile signed short won't unsignify correctly");
michael@0 273 static_assert(IsSame<MakeUnsigned<const volatile signed int>::Type, const volatile unsigned int>::value,
michael@0 274 "const volatile signed int won't unsignify correctly");
michael@0 275 static_assert(IsSame<MakeUnsigned<signed long>::Type, unsigned long>::value,
michael@0 276 "signed long won't unsignify correctly");
michael@0 277
michael@0 278 static_assert(IsSame<MakeUnsigned<const unsigned char>::Type, const unsigned char>::value,
michael@0 279 "const unsigned char won't unsignify correctly");
michael@0 280
michael@0 281 static_assert(IsSame<MakeUnsigned<volatile unsigned short>::Type, volatile unsigned short>::value,
michael@0 282 "volatile unsigned short won't unsignify correctly");
michael@0 283 static_assert(IsSame<MakeUnsigned<const volatile unsigned int>::Type, const volatile unsigned int>::value,
michael@0 284 "const volatile unsigned int won't unsignify correctly");
michael@0 285 static_assert(IsSame<MakeUnsigned<unsigned long>::Type, unsigned long>::value,
michael@0 286 "signed long won't unsignify correctly");
michael@0 287
michael@0 288 static_assert(IsSame<MakeUnsigned<char>::Type, unsigned char>::value,
michael@0 289 "char won't unsignify correctly");
michael@0 290 static_assert(IsSame<MakeUnsigned<volatile char>::Type, volatile unsigned char>::value,
michael@0 291 "volatile char won't unsignify correctly");
michael@0 292 static_assert(IsSame<MakeUnsigned<const char>::Type, const unsigned char>::value,
michael@0 293 "const char won't unsignify correctly");
michael@0 294
michael@0 295 int
michael@0 296 main()
michael@0 297 {
michael@0 298 CPlusPlus11IsBaseOf::StandardIsBaseOfTests();
michael@0 299 TestIsBaseOf();
michael@0 300 TestIsConvertible();
michael@0 301 }

mercurial