/* * 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" struct IntegerTypesTestSupport: AllStatic { template struct SignedTypeOfSize; template struct UnsignedTypeOfSize; template struct Signed; template struct Unsigned; }; #define DEFINE_CANONICAL_SIGNED_TYPE(T) \ template<> \ struct IntegerTypesTestSupport::SignedTypeOfSize \ : public AllStatic \ { \ typedef T type; \ }; #define DEFINE_CANONICAL_UNSIGNED_TYPE(T) \ template<> \ struct IntegerTypesTestSupport::UnsignedTypeOfSize \ : public AllStatic \ { \ typedef T type; \ }; #define DEFINE_INTEGER_TYPES_OF_SIZE(NBITS) \ DEFINE_CANONICAL_SIGNED_TYPE(int ## NBITS ## _t) \ DEFINE_CANONICAL_UNSIGNED_TYPE(uint ## NBITS ## _t) DEFINE_INTEGER_TYPES_OF_SIZE(8) DEFINE_INTEGER_TYPES_OF_SIZE(16) DEFINE_INTEGER_TYPES_OF_SIZE(32) DEFINE_INTEGER_TYPES_OF_SIZE(64) #undef DEFINE_INTEGER_TYPES_OF_SIZE #undef DEFINE_CANONICAL_SIGNED_TYPE #undef DEFINE_CANONICAL_UNSIGNED_TYPE template struct IntegerTypesTestSupport::Signed : public SignedTypeOfSize {}; template struct IntegerTypesTestSupport::Unsigned : public UnsignedTypeOfSize {}; TEST(IntegerTypesTest, round_trip_int) { int sfive = 5; int mfive = -5; uint ufive = 5u; typedef IntegerTypesTestSupport::Signed::type SI; typedef IntegerTypesTestSupport::Unsigned::type UI; EXPECT_EQ(sfive, IntegerTypes::cast(IntegerTypes::cast(sfive))); EXPECT_EQ(sfive, IntegerTypes::cast(IntegerTypes::cast(sfive))); EXPECT_EQ(mfive, IntegerTypes::cast(IntegerTypes::cast(mfive))); EXPECT_EQ(mfive, IntegerTypes::cast(IntegerTypes::cast(mfive))); EXPECT_EQ(ufive, IntegerTypes::cast(IntegerTypes::cast(ufive))); EXPECT_EQ(ufive, IntegerTypes::cast(IntegerTypes::cast(ufive))); } TEST(IntegerTypesTest, round_trip_float) { float ffive = 5.0f; double dfive = 5.0; typedef IntegerTypesTestSupport::Signed::type SF; typedef IntegerTypesTestSupport::Unsigned::type UF; typedef IntegerTypesTestSupport::Signed::type SD; typedef IntegerTypesTestSupport::Unsigned::type UD; EXPECT_EQ(ffive, IntegerTypes::cast(IntegerTypes::cast(ffive))); EXPECT_EQ(ffive, IntegerTypes::cast(IntegerTypes::cast(ffive))); EXPECT_EQ(dfive, IntegerTypes::cast(IntegerTypes::cast(dfive))); EXPECT_EQ(dfive, IntegerTypes::cast(IntegerTypes::cast(dfive))); } TEST(IntegerTypesTest, round_trip_ptr) { int five = 5; int* pfive = &five; const int* cpfive = &five; typedef IntegerTypesTestSupport::Signed::type SIP; typedef IntegerTypesTestSupport::Unsigned::type UIP; EXPECT_EQ(pfive, IntegerTypes::cast(IntegerTypes::cast(pfive))); EXPECT_EQ(pfive, IntegerTypes::cast(IntegerTypes::cast(pfive))); EXPECT_EQ(cpfive, IntegerTypes::cast(IntegerTypes::cast(cpfive))); EXPECT_EQ(cpfive, IntegerTypes::cast(IntegerTypes::cast(cpfive))); }