mfbt/tests/TestTypeTraits.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial