/* -*- Mode: C++; tab-width: 2; 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" using mozilla::IsBaseOf; using mozilla::IsConvertible; using mozilla::IsSigned; using mozilla::IsUnsigned; MOZ_STATIC_ASSERT(!IsSigned::value, "bool shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "bool should be unsigned"); MOZ_STATIC_ASSERT(!IsSigned::value, "const bool shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "const bool should be unsigned"); MOZ_STATIC_ASSERT(!IsSigned::value, "volatile bool shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "volatile bool should be unsigned"); MOZ_STATIC_ASSERT(!IsSigned::value, "unsigned char shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "unsigned char should be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "signed char should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "signed char shouldn't be unsigned"); MOZ_STATIC_ASSERT(!IsSigned::value, "unsigned short shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "unsigned short should be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "short should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "short shouldn't be unsigned"); MOZ_STATIC_ASSERT(!IsSigned::value, "unsigned int shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "unsigned int should be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "int should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "int shouldn't be unsigned"); MOZ_STATIC_ASSERT(!IsSigned::value, "unsigned long shouldn't be signed"); MOZ_STATIC_ASSERT(IsUnsigned::value, "unsigned long should be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "long should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "long shouldn't be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "float should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "float shouldn't be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "const float should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "const float shouldn't be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "double should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "double shouldn't be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "volatile double should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "volatile double shouldn't be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "long double should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "long double shouldn't be unsigned"); MOZ_STATIC_ASSERT(IsSigned::value, "const volatile long double should be signed"); MOZ_STATIC_ASSERT(!IsUnsigned::value, "const volatile long double shouldn't be unsigned"); 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() { MOZ_ASSERT((IsBaseOf::value) == true); MOZ_ASSERT((IsBaseOf::value) == true); MOZ_ASSERT((IsBaseOf::value) == true); MOZ_ASSERT((IsBaseOf::value) == true); MOZ_ASSERT((IsBaseOf::value) == false); MOZ_ASSERT((IsBaseOf::value) == false); MOZ_ASSERT((IsBaseOf::value) == false); // We fail at the following test. To fix it, we need to specialize IsBaseOf // for all built-in types. // MOZ_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() { MOZ_ASSERT((IsBaseOf::value), "A is a base of B"); MOZ_ASSERT((!IsBaseOf::value), "B is not a base of A"); MOZ_ASSERT((IsBaseOf::value), "A is a base of C"); MOZ_ASSERT((!IsBaseOf::value), "C is not a base of A"); MOZ_ASSERT((IsBaseOf::value), "A is a base of F"); MOZ_ASSERT((!IsBaseOf::value), "F is not a base of A"); MOZ_ASSERT((!IsBaseOf::value), "A is not a base of D"); MOZ_ASSERT((!IsBaseOf::value), "D is not a base of A"); MOZ_ASSERT((IsBaseOf::value), "B is the same as B (and therefore, a base of B)"); } static void TestIsConvertible() { // Pointer type convertibility MOZ_ASSERT((IsConvertible::value), "A* should convert to A*"); MOZ_ASSERT((IsConvertible::value), "B* should convert to A*"); MOZ_ASSERT((!IsConvertible::value), "A* shouldn't convert to B*"); MOZ_ASSERT((!IsConvertible::value), "A* shouldn't convert to C*"); MOZ_ASSERT((!IsConvertible::value), "A* shouldn't convert to unrelated D*"); MOZ_ASSERT((!IsConvertible::value), "D* shouldn't convert to unrelated A*"); // Instance type convertibility MOZ_ASSERT((IsConvertible::value), "A is A"); MOZ_ASSERT((IsConvertible::value), "B converts to A"); MOZ_ASSERT((!IsConvertible::value), "D and A are unrelated"); MOZ_ASSERT((!IsConvertible::value), "A and D are unrelated"); // These cases seem to require C++11 support to properly implement them, so // for now just disable them. //MOZ_ASSERT((!IsConvertible::value), // "C* shouldn't convert to A* (private inheritance)"); //MOZ_ASSERT((!IsConvertible::value), // "C doesn't convert to A (private inheritance)"); } int main() { CPlusPlus11IsBaseOf::StandardIsBaseOfTests(); TestIsBaseOf(); TestIsConvertible(); }