Path: blob/master/test/hotspot/gtest/metaprogramming/test_primitiveConversions.cpp
41145 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.hpp"26#include "metaprogramming/isSame.hpp"27#include "metaprogramming/primitiveConversions.hpp"28#include "unittest.hpp"29#include "utilities/debug.hpp"3031struct PrimitiveConversionsTestSupport: AllStatic {3233template<size_t byte_size> struct SignedTypeOfSize;34template<size_t byte_size> struct UnsignedTypeOfSize;3536template<typename T> struct Signed;37template<typename T> struct Unsigned;38};3940#define DEFINE_CANONICAL_SIGNED_TYPE(T) \41template<> \42struct PrimitiveConversionsTestSupport::SignedTypeOfSize<sizeof(T)> \43: public AllStatic \44{ \45typedef T type; \46};4748#define DEFINE_CANONICAL_UNSIGNED_TYPE(T) \49template<> \50struct PrimitiveConversionsTestSupport::UnsignedTypeOfSize<sizeof(T)> \51: public AllStatic \52{ \53typedef T type; \54};5556#define DEFINE_INTEGER_TYPES_OF_SIZE(NBITS) \57DEFINE_CANONICAL_SIGNED_TYPE(int ## NBITS ## _t) \58DEFINE_CANONICAL_UNSIGNED_TYPE(uint ## NBITS ## _t)5960DEFINE_INTEGER_TYPES_OF_SIZE(8)61DEFINE_INTEGER_TYPES_OF_SIZE(16)62DEFINE_INTEGER_TYPES_OF_SIZE(32)63DEFINE_INTEGER_TYPES_OF_SIZE(64)6465#undef DEFINE_INTEGER_TYPES_OF_SIZE66#undef DEFINE_CANONICAL_SIGNED_TYPE67#undef DEFINE_CANONICAL_UNSIGNED_TYPE6869template<typename T>70struct PrimitiveConversionsTestSupport::Signed71: public SignedTypeOfSize<sizeof(T)>72{};7374template<typename T>75struct PrimitiveConversionsTestSupport::Unsigned76: public UnsignedTypeOfSize<sizeof(T)>77{};7879TEST(PrimitiveConversionsTest, round_trip_int) {80int sfive = 5;81int mfive = -5;82uint ufive = 5u;8384typedef PrimitiveConversionsTestSupport::Signed<int>::type SI;85typedef PrimitiveConversionsTestSupport::Unsigned<int>::type UI;8687EXPECT_EQ(sfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<SI>(sfive)));88EXPECT_EQ(sfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<UI>(sfive)));8990EXPECT_EQ(mfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<SI>(mfive)));91EXPECT_EQ(mfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<UI>(mfive)));9293EXPECT_EQ(ufive, PrimitiveConversions::cast<uint>(PrimitiveConversions::cast<SI>(ufive)));94EXPECT_EQ(ufive, PrimitiveConversions::cast<uint>(PrimitiveConversions::cast<UI>(ufive)));95}9697TEST(PrimitiveConversionsTest, round_trip_int_constexpr) {98constexpr int sfive = 5;99constexpr int mfive = -5;100constexpr uint ufive = 5u;101102typedef PrimitiveConversionsTestSupport::Signed<int>::type SI;103typedef PrimitiveConversionsTestSupport::Unsigned<int>::type UI;104105{106constexpr SI i = PrimitiveConversions::cast<SI>(sfive);107constexpr int r = PrimitiveConversions::cast<int>(i);108EXPECT_EQ(sfive, r);109}110111{112constexpr UI i = PrimitiveConversions::cast<UI>(sfive);113constexpr int r = PrimitiveConversions::cast<int>(i);114EXPECT_EQ(sfive, r);115}116117{118constexpr SI i = PrimitiveConversions::cast<SI>(mfive);119constexpr int r = PrimitiveConversions::cast<int>(i);120EXPECT_EQ(mfive, r);121}122123{124constexpr UI i = PrimitiveConversions::cast<UI>(mfive);125constexpr int r = PrimitiveConversions::cast<int>(i);126EXPECT_EQ(mfive, r);127}128129{130constexpr SI i = PrimitiveConversions::cast<SI>(ufive);131constexpr uint r = PrimitiveConversions::cast<uint>(i);132EXPECT_EQ(ufive, r);133}134135{136constexpr UI i = PrimitiveConversions::cast<UI>(ufive);137constexpr uint r = PrimitiveConversions::cast<uint>(i);138EXPECT_EQ(ufive, r);139}140}141142TEST(PrimitiveConversionsTest, round_trip_float) {143float ffive = 5.0f;144double dfive = 5.0;145146typedef PrimitiveConversionsTestSupport::Signed<float>::type SF;147typedef PrimitiveConversionsTestSupport::Unsigned<float>::type UF;148149typedef PrimitiveConversionsTestSupport::Signed<double>::type SD;150typedef PrimitiveConversionsTestSupport::Unsigned<double>::type UD;151152EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<SF>(ffive)));153EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<UF>(ffive)));154155EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<SD>(dfive)));156EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<UD>(dfive)));157}158159TEST(PrimitiveConversionsTest, round_trip_ptr) {160int five = 5;161int* pfive = &five;162const int* cpfive = &five;163164typedef PrimitiveConversionsTestSupport::Signed<int*>::type SIP;165typedef PrimitiveConversionsTestSupport::Unsigned<int*>::type UIP;166167EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<SIP>(pfive)));168EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<UIP>(pfive)));169170EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<SIP>(cpfive)));171EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<UIP>(cpfive)));172}173174175