/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "mozilla/Assertions.h" #include "mozilla/TypeTraits.h" #define TEST_CV_QUALIFIERS(test, type, ...) \ test(type, __VA_ARGS__) \ test(const type, __VA_ARGS__) \ test(volatile type, __VA_ARGS__) \ test(const volatile type, __VA_ARGS__) using mozilla::AddLvalueReference; using mozilla::AddPointer; using mozilla::AddRvalueReference; using mozilla::Decay; using mozilla::DeclVal; using mozilla::IsFunction; using mozilla::IsArray; using mozilla::IsBaseOf; using mozilla::IsClass; using mozilla::IsConvertible; using mozilla::IsEmpty; using mozilla::IsLvalueReference; using mozilla::IsPointer; using mozilla::IsReference; using mozilla::IsRvalueReference; using mozilla::IsSame; using mozilla::IsSigned; using mozilla::IsUnsigned; using mozilla::IsDestructible; using mozilla::MakeSigned; using mozilla::MakeUnsigned; using mozilla::RemoveExtent; using mozilla::RemovePointer; static_assert(!IsFunction::value, "int is not a function type"); static_assert(IsFunction::value, "void(int) is a function type"); static_assert(!IsFunction::value, "void(*)(int) is not a function type"); static_assert(!IsArray::value, "bool not an array"); static_assert(IsArray::value, "bool[] is an array"); static_assert(IsArray::value, "bool[5] is an array"); static_assert(!IsPointer::value, "bool not a pointer"); static_assert(IsPointer::value, "bool* is a pointer"); static_assert(IsPointer::value, "bool* const is a pointer"); static_assert(IsPointer::value, "bool* volatile is a pointer"); static_assert(IsPointer::value, "bool* const volatile is a pointer"); static_assert(IsPointer::value, "bool** is a pointer"); static_assert(IsPointer::value, "void (*)(void) is a pointer"); struct IsPointerTest { bool m; void f(); }; static_assert(!IsPointer::value, "IsPointerTest not a pointer"); static_assert(IsPointer::value, "IsPointerTest* is a pointer"); static_assert(!IsPointer::value, "bool(IsPointerTest::*)() not a pointer"); static_assert(!IsPointer::value, "void(IsPointerTest::*)(void) not a pointer"); static_assert(!IsLvalueReference::value, "bool not an lvalue reference"); static_assert(!IsLvalueReference::value, "bool* not an lvalue reference"); static_assert(IsLvalueReference::value, "bool& is an lvalue reference"); static_assert(!IsLvalueReference::value, "bool&& not an lvalue reference"); static_assert(!IsLvalueReference::value, "void not an lvalue reference"); static_assert(!IsLvalueReference::value, "void* not an lvalue reference"); static_assert(!IsLvalueReference::value, "int not an lvalue reference"); static_assert(!IsLvalueReference::value, "int* not an lvalue reference"); static_assert(IsLvalueReference::value, "int& is an lvalue reference"); static_assert(!IsLvalueReference::value, "int&& not an lvalue reference"); static_assert(!IsRvalueReference::value, "bool not an rvalue reference"); static_assert(!IsRvalueReference::value, "bool* not an rvalue reference"); static_assert(!IsRvalueReference::value, "bool& not an rvalue reference"); static_assert(IsRvalueReference::value, "bool&& is an rvalue reference"); static_assert(!IsRvalueReference::value, "void not an rvalue reference"); static_assert(!IsRvalueReference::value, "void* not an rvalue reference"); static_assert(!IsRvalueReference::value, "int not an rvalue reference"); static_assert(!IsRvalueReference::value, "int* not an rvalue reference"); static_assert(!IsRvalueReference::value, "int& not an rvalue reference"); static_assert(IsRvalueReference::value, "int&& is an rvalue reference"); static_assert(!IsReference::value, "bool not a reference"); static_assert(!IsReference::value, "bool* not a reference"); static_assert(IsReference::value, "bool& is a reference"); static_assert(IsReference::value, "bool&& is a reference"); static_assert(!IsReference::value, "void not a reference"); static_assert(!IsReference::value, "void* not a reference"); static_assert(!IsReference::value, "int not a reference"); static_assert(!IsReference::value, "int* not a reference"); static_assert(IsReference::value, "int& is a reference"); static_assert(IsReference::value, "int&& is a reference"); namespace CPlusPlus11IsMemberPointer { using mozilla::IsMemberPointer; struct S {}; union U {}; #define ASSERT_IS_MEMBER_POINTER(type, msg) \ static_assert(IsMemberPointer::value, #type msg); #define TEST_IS_MEMBER_POINTER(type) \ TEST_CV_QUALIFIERS(ASSERT_IS_MEMBER_POINTER, type, \ " is a member pointer type") TEST_IS_MEMBER_POINTER(int S::*) TEST_IS_MEMBER_POINTER(int U::*) #undef TEST_IS_MEMBER_POINTER #undef ASSERT_IS_MEMBER_POINTER #define ASSERT_IS_NOT_MEMBER_POINTER(type, msg) \ static_assert(!IsMemberPointer::value, #type msg); #define TEST_IS_NOT_MEMBER_POINTER(type) \ TEST_CV_QUALIFIERS(ASSERT_IS_NOT_MEMBER_POINTER, type, \ " is not a member pointer type") TEST_IS_NOT_MEMBER_POINTER(int*) #undef TEST_IS_NOT_MEMBER_POINTER #undef ASSERT_IS_NOT_MEMBER_POINTER } // CPlusPlus11IsMemberPointer namespace CPlusPlus11IsScalar { using mozilla::IsScalar; enum E {}; enum class EC {}; class C {}; struct S {}; union U {}; #define ASSERT_IS_SCALAR(type, msg) \ static_assert(IsScalar::value, #type msg); #define TEST_IS_SCALAR(type) \ TEST_CV_QUALIFIERS(ASSERT_IS_SCALAR, type, " is a scalar type") TEST_IS_SCALAR(int) TEST_IS_SCALAR(float) TEST_IS_SCALAR(E) TEST_IS_SCALAR(EC) TEST_IS_SCALAR(S*) TEST_IS_SCALAR(int S::*) #undef TEST_IS_SCALAR #undef ASSERT_IS_SCALAR #define ASSERT_IS_NOT_SCALAR(type, msg) \ static_assert(!IsScalar::value, #type msg); #define TEST_IS_NOT_SCALAR(type) \ TEST_CV_QUALIFIERS(ASSERT_IS_NOT_SCALAR, type, " is not a scalar type") TEST_IS_NOT_SCALAR(C) TEST_IS_NOT_SCALAR(S) TEST_IS_NOT_SCALAR(U) #undef TEST_IS_NOT_SCALAR #undef ASSERT_IS_NOT_SCALAR } // CPlusPlus11IsScalar struct S1 {}; union U1 { int mX; }; static_assert(!IsClass::value, "int isn't a class"); static_assert(IsClass::value, "S is a class"); static_assert(!IsClass::value, "U isn't a class"); static_assert(!mozilla::IsEmpty::value, "not a class => not empty"); static_assert(!mozilla::IsEmpty::value, "not a class => not empty"); static_assert(!mozilla::IsEmpty::value, "not a class => not empty"); struct E1 {}; struct E2 { int : 0; }; struct E3 : E1 {}; struct E4 : E2 {}; static_assert(IsEmpty::value, "S should be empty"); static_assert(mozilla::IsEmpty::value && mozilla::IsEmpty::value && mozilla::IsEmpty::value && mozilla::IsEmpty::value, "all empty"); union U2 { E1 e1; }; static_assert(!mozilla::IsEmpty::value, "not a class => not empty"); struct NE1 { int mX; }; struct NE2 : virtual E1 {}; struct NE3 : E2 { virtual ~NE3() {} }; struct NE4 { virtual void f() {} }; static_assert(!mozilla::IsEmpty::value && !mozilla::IsEmpty::value && !mozilla::IsEmpty::value && !mozilla::IsEmpty::value, "all empty"); static_assert(!IsSigned::value, "bool shouldn't be signed"); static_assert(IsUnsigned::value, "bool should be unsigned"); static_assert(!IsSigned::value, "const bool shouldn't be signed"); static_assert(IsUnsigned::value, "const bool should be unsigned"); static_assert(!IsSigned::value, "volatile bool shouldn't be signed"); static_assert(IsUnsigned::value, "volatile bool should be unsigned"); static_assert(!IsSigned::value, "unsigned char shouldn't be signed"); static_assert(IsUnsigned::value, "unsigned char should be unsigned"); static_assert(IsSigned::value, "signed char should be signed"); static_assert(!IsUnsigned::value, "signed char shouldn't be unsigned"); static_assert(!IsSigned::value, "unsigned short shouldn't be signed"); static_assert(IsUnsigned::value, "unsigned short should be unsigned"); static_assert(IsSigned::value, "short should be signed"); static_assert(!IsUnsigned::value, "short shouldn't be unsigned"); static_assert(!IsSigned::value, "unsigned int shouldn't be signed"); static_assert(IsUnsigned::value, "unsigned int should be unsigned"); static_assert(IsSigned::value, "int should be signed"); static_assert(!IsUnsigned::value, "int shouldn't be unsigned"); static_assert(!IsSigned::value, "unsigned long shouldn't be signed"); static_assert(IsUnsigned::value, "unsigned long should be unsigned"); static_assert(IsSigned::value, "long should be signed"); static_assert(!IsUnsigned::value, "long shouldn't be unsigned"); static_assert(IsSigned::value, "float should be signed"); static_assert(!IsUnsigned::value, "float shouldn't be unsigned"); static_assert(IsSigned::value, "const float should be signed"); static_assert(!IsUnsigned::value, "const float shouldn't be unsigned"); static_assert(IsSigned::value, "double should be signed"); static_assert(!IsUnsigned::value, "double shouldn't be unsigned"); static_assert(IsSigned::value, "volatile double should be signed"); static_assert(!IsUnsigned::value, "volatile double shouldn't be unsigned"); static_assert(IsSigned::value, "long double should be signed"); static_assert(!IsUnsigned::value, "long double shouldn't be unsigned"); static_assert(IsSigned::value, "const volatile long double should be signed"); static_assert(!IsUnsigned::value, "const volatile long double shouldn't be unsigned"); class NotIntConstructible { NotIntConstructible(int) = delete; }; static_assert(!IsSigned::value, "non-arithmetic types are not signed"); static_assert(!IsUnsigned::value, "non-arithmetic types are not unsigned"); class PublicDestructible { public: ~PublicDestructible(); }; class PrivateDestructible { private: ~PrivateDestructible(); }; class TrivialDestructible { }; static_assert(IsDestructible::value, "public destructible class is destructible"); static_assert(!IsDestructible::value, "private destructible class is not destructible"); static_assert(IsDestructible::value, "trivial destructible class is destructible"); namespace CPlusPlus11IsBaseOf { // Adapted from C++11 ยง 20.9.6. struct B {}; struct B1 : B {}; struct B2 : B {}; struct D : private B1, private B2 {}; static void StandardIsBaseOfTests() { static_assert((IsBaseOf::value) == true, "IsBaseOf fails on diamond"); static_assert((IsBaseOf::value) == true, "IsBaseOf fails on diamond plus constness change"); static_assert((IsBaseOf::value) == true, "IsBaseOf fails on diamond plus constness change"); static_assert((IsBaseOf::value) == true, "IsBaseOf fails on constness change"); static_assert((IsBaseOf::value) == false, "IsBaseOf got the direction of inheritance wrong"); static_assert((IsBaseOf::value) == false, "IsBaseOf should return false on references"); static_assert((IsBaseOf::value) == false, "IsBaseOf should return false on arrays"); // We fail at the following test. To fix it, we need to specialize IsBaseOf // for all built-in types. // static_assert((IsBaseOf::value) == false); } } /* namespace CPlusPlus11IsBaseOf */ class A { }; class B : public A { }; class C : private A { }; class D { }; class E : public A { }; class F : public B, public E { }; static void TestIsBaseOf() { static_assert((IsBaseOf::value), "A is a base of B"); static_assert((!IsBaseOf::value), "B is not a base of A"); static_assert((IsBaseOf::value), "A is a base of C"); static_assert((!IsBaseOf::value), "C is not a base of A"); static_assert((IsBaseOf::value), "A is a base of F"); static_assert((!IsBaseOf::value), "F is not a base of A"); static_assert((!IsBaseOf::value), "A is not a base of D"); static_assert((!IsBaseOf::value), "D is not a base of A"); static_assert((IsBaseOf::value), "B is the same as B (and therefore, a base of B)"); } class ExplicitCopyConstructor { explicit ExplicitCopyConstructor(const ExplicitCopyConstructor&) = default; }; static void TestIsConvertible() { // Pointer type convertibility static_assert((IsConvertible::value), "A* should convert to A*"); static_assert((IsConvertible::value), "B* should convert to A*"); static_assert((!IsConvertible::value), "A* shouldn't convert to B*"); static_assert((!IsConvertible::value), "A* shouldn't convert to C*"); static_assert((!IsConvertible::value), "A* shouldn't convert to unrelated D*"); static_assert((!IsConvertible::value), "D* shouldn't convert to unrelated A*"); // Instance type convertibility static_assert((IsConvertible::value), "A is A"); static_assert((IsConvertible::value), "B converts to A"); static_assert((!IsConvertible::value), "D and A are unrelated"); static_assert((!IsConvertible::value), "A and D are unrelated"); static_assert(IsConvertible::value, "void is void"); static_assert(!IsConvertible::value, "A shouldn't convert to void"); static_assert(!IsConvertible::value, "void shouldn't convert to B"); static_assert(!IsConvertible::value, "IsConvertible should test for implicit convertibility"); // These cases seem to require C++11 support to properly implement them, so // for now just disable them. //static_assert((!IsConvertible::value), // "C* shouldn't convert to A* (private inheritance)"); //static_assert((!IsConvertible::value), // "C doesn't convert to A (private inheritance)"); } static_assert(IsSame::Type, int&>::value, "not adding & to int correctly"); static_assert(IsSame::Type, volatile int&>::value, "not adding & to volatile int& correctly"); static_assert(IsSame::Type, void*&>::value, "not adding & to void* correctly"); static_assert(IsSame::Type, void>::value, "void shouldn't be transformed by AddLvalueReference"); static_assert(IsSame::Type, struct S1&>::value, "not reference-collapsing struct S1&& & to struct S1& correctly"); static_assert(IsSame::Type, int&&>::value, "not adding && to int correctly"); static_assert(IsSame::Type, volatile int&>::value, "not adding && to volatile int& correctly"); static_assert(IsSame::Type, const int&&>::value, "not adding && to volatile int& correctly"); static_assert(IsSame::Type, void*&&>::value, "not adding && to void* correctly"); static_assert(IsSame::Type, void>::value, "void shouldn't be transformed by AddRvalueReference"); static_assert(IsSame::Type, struct S1&>::value, "not reference-collapsing struct S1& && to struct S1& correctly"); struct TestWithDefaultConstructor { int foo() const { return 0; } }; struct TestWithNoDefaultConstructor { explicit TestWithNoDefaultConstructor(int) {} int foo() const { return 1; } }; static_assert(IsSame::value, "decltype should work using a struct with a default constructor"); static_assert(IsSame().foo()), int>::value, "decltype should work using a DeclVal'd struct with a default constructor"); static_assert(IsSame().foo()), int>::value, "decltype should work using a DeclVal'd struct without a default constructor"); static_assert(IsSame::Type, const signed char>::value, "const unsigned char won't signify correctly"); static_assert(IsSame::Type, volatile signed short>::value, "volatile unsigned short won't signify correctly"); static_assert(IsSame::Type, const volatile signed int>::value, "const volatile unsigned int won't signify correctly"); static_assert(IsSame::Type, signed long>::value, "unsigned long won't signify correctly"); static_assert(IsSame::Type, const signed char>::value, "const signed char won't signify correctly"); static_assert(IsSame::Type, volatile signed short>::value, "volatile signed short won't signify correctly"); static_assert(IsSame::Type, const volatile signed int>::value, "const volatile signed int won't signify correctly"); static_assert(IsSame::Type, signed long>::value, "signed long won't signify correctly"); static_assert(IsSame::Type, signed char>::value, "char won't signify correctly"); static_assert(IsSame::Type, volatile signed char>::value, "volatile char won't signify correctly"); static_assert(IsSame::Type, const signed char>::value, "const char won't signify correctly"); static_assert(IsSame::Type, const unsigned char>::value, "const signed char won't unsignify correctly"); static_assert(IsSame::Type, volatile unsigned short>::value, "volatile signed short won't unsignify correctly"); static_assert(IsSame::Type, const volatile unsigned int>::value, "const volatile signed int won't unsignify correctly"); static_assert(IsSame::Type, unsigned long>::value, "signed long won't unsignify correctly"); static_assert(IsSame::Type, const unsigned char>::value, "const unsigned char won't unsignify correctly"); static_assert(IsSame::Type, volatile unsigned short>::value, "volatile unsigned short won't unsignify correctly"); static_assert(IsSame::Type, const volatile unsigned int>::value, "const volatile unsigned int won't unsignify correctly"); static_assert(IsSame::Type, unsigned long>::value, "signed long won't unsignify correctly"); static_assert(IsSame::Type, unsigned char>::value, "char won't unsignify correctly"); static_assert(IsSame::Type, volatile unsigned char>::value, "volatile char won't unsignify correctly"); static_assert(IsSame::Type, const unsigned char>::value, "const char won't unsignify correctly"); static_assert(IsSame::Type, int>::value, "removing extent from non-array must return the non-array"); static_assert(IsSame::Type, const int>::value, "removing extent from unknown-bound array must return element type"); static_assert(IsSame::Type, volatile int>::value, "removing extent from known-bound array must return element type"); static_assert(IsSame::Type, long[17]>::value, "removing extent from multidimensional array must return element type"); struct TestRemovePointer { bool m; void f(); }; static_assert(IsSame::Type, int>::value, "removing pointer from int must return int"); static_assert(IsSame::Type, int>::value, "removing pointer from int* must return int"); static_assert(IsSame::Type, int>::value, "removing pointer from int* const must return int"); static_assert(IsSame::Type, int>::value, "removing pointer from int* volatile must return int"); static_assert(IsSame::Type, const long>::value, "removing pointer from const long* must return const long"); static_assert(IsSame::Type, void>::value, "removing pointer from void* const must return void"); static_assert(IsSame::Type, void (TestRemovePointer::*)()>::value, "removing pointer from void (S::*)() must return void (S::*)()"); static_assert(IsSame::Type, void()>::value, "removing pointer from void (*)() must return void()"); static_assert(IsSame::Type, bool TestRemovePointer::*>::value, "removing pointer from bool S::* must return bool S::*"); static_assert(IsSame::Type, int*>::value, "adding pointer to int must return int*"); static_assert(IsSame::Type, int**>::value, "adding pointer to int* must return int**"); static_assert(IsSame::Type, int*>::value, "adding pointer to int& must return int*"); static_assert(IsSame::Type, int* const*>::value, "adding pointer to int* const must return int* const*"); static_assert(IsSame::Type, int* volatile*>::value, "adding pointer to int* volatile must return int* volatile*"); static_assert(IsSame::Type, int>::value, "decaying int must return int"); static_assert(IsSame::Type, int*>::value, "decaying int* must return int*"); static_assert(IsSame::Type, int*>::value, "decaying int* const must return int*"); static_assert(IsSame::Type, int*>::value, "decaying int* volatile must return int*"); static_assert(IsSame::Type, int>::value, "decaying int& must return int"); static_assert(IsSame::Type, int>::value, "decaying const int& must return int"); static_assert(IsSame::Type, int>::value, "decaying int&& must return int"); static_assert(IsSame::Type, int*>::value, "decaying int[1] must return int*"); static_assert(IsSame::Type, void(*)(int)>::value, "decaying void(int) must return void(*)(int)"); /* * Android's broken [u]intptr_t inttype macros are broken because its PRI*PTR * macros are defined as "ld", but sizeof(long) is 8 and sizeof(intptr_t) * is 4 on 32-bit Android. We redefine Android's PRI*PTR macros in * IntegerPrintfMacros.h and assert here that our new definitions match the * actual type sizes seen at compile time. */ #if defined(ANDROID) && !defined(__LP64__) static_assert(mozilla::IsSame::value, "emulated PRI[di]PTR definitions will be wrong"); static_assert(mozilla::IsSame::value, "emulated PRI[ouxX]PTR definitions will be wrong"); #endif int main() { CPlusPlus11IsBaseOf::StandardIsBaseOfTests(); TestIsBaseOf(); TestIsConvertible(); return 0; }