/* * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "precompiled.hpp" #include "memory/allocation.hpp" #include "metaprogramming/integerTypes.hpp" #include "metaprogramming/isSame.hpp" #include "unittest.hpp" #include "utilities/debug.hpp" class IntegerTypesTest: AllStatic { template class TestIntegers: AllStatic { static const bool _cts_unsigned_type_is_signed_type = IsSame::type, SignedType>::value; STATIC_ASSERT(_cts_unsigned_type_is_signed_type); static const bool _ctu_signed_type_is_unsigned_type = IsSame::type, UnsignedType>::value; STATIC_ASSERT(_ctu_signed_type_is_unsigned_type); }; const TestIntegers TestByte; const TestIntegers TestShort; const TestIntegers TestInt; const TestIntegers TestLong; typedef IntegerTypes::Canonical::type CanonicalIntPtr; typedef IntegerTypes::Canonical::type CanonicalUIntPtr; static const bool _cts_voidptr_is_intptrt = IsSame::type, CanonicalIntPtr>::value; STATIC_ASSERT(_cts_voidptr_is_intptrt); static const bool _ctu_voidptr_is_uintptrt = IsSame::type, CanonicalUIntPtr>::value; STATIC_ASSERT(_ctu_voidptr_is_uintptrt); class A VALUE_OBJ_CLASS_SPEC { intptr_t _value; public: operator void* () { return reinterpret_cast(_value); } A(void* arg) : _value(reinterpret_cast(arg)) {} }; static const bool _cts_cvptrcv_is_intptrt = IsSame::type, CanonicalIntPtr>::value; STATIC_ASSERT(_cts_cvptrcv_is_intptrt); static const bool _ctu_cvptrcv_is_uintptrt = IsSame::type, CanonicalUIntPtr>::value; STATIC_ASSERT(_ctu_cvptrcv_is_uintptrt); static const bool _cts_float_is_int = IsSame::type, int32_t>::value; STATIC_ASSERT(_cts_float_is_int); static const bool _ctu_float_is_uint = IsSame::type, uint32_t>::value; STATIC_ASSERT(_ctu_float_is_uint); static const bool _cts_double_is_long = IsSame::type, int64_t>::value; STATIC_ASSERT(_cts_double_is_long); static const bool _ctu_double_is_ulong = IsSame::type, uint64_t>::value; STATIC_ASSERT(_ctu_double_is_ulong); static void test_cast() { (void)IntegerTypes::cast(intptr_t(0)); (void)IntegerTypes::cast(int32_t(0)); (void)IntegerTypes::cast(int64_t(0)); (void)IntegerTypes::cast(int64_t(0)); } }; TEST(IntegerTypesRTest, round_trip_int) { int sfive = 5; int mfive = -5; uint ufive = 5u; EXPECT_EQ(sfive, IntegerTypes::cast(IntegerTypes::cast_to_signed(sfive))); EXPECT_EQ(sfive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(sfive))); EXPECT_EQ(mfive, IntegerTypes::cast(IntegerTypes::cast_to_signed(mfive))); EXPECT_EQ(mfive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(mfive))); EXPECT_EQ(ufive, IntegerTypes::cast(IntegerTypes::cast_to_signed(ufive))); EXPECT_EQ(ufive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(ufive))); } TEST(IntegerTypesRTest, round_trip_float) { float ffive = 5.0f; double dfive = 5.0; EXPECT_EQ(ffive, IntegerTypes::cast(IntegerTypes::cast_to_signed(ffive))); EXPECT_EQ(ffive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(ffive))); EXPECT_EQ(dfive, IntegerTypes::cast(IntegerTypes::cast_to_signed(dfive))); EXPECT_EQ(dfive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(dfive))); } TEST(IntegerTypesRTest, round_trip_ptr) { int five = 5; int* pfive = &five; const int* cpfive = &five; EXPECT_EQ(pfive, IntegerTypes::cast(IntegerTypes::cast_to_signed(pfive))); EXPECT_EQ(pfive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(pfive))); EXPECT_EQ(cpfive, IntegerTypes::cast(IntegerTypes::cast_to_signed(cpfive))); EXPECT_EQ(cpfive, IntegerTypes::cast(IntegerTypes::cast_to_unsigned(cpfive))); }