1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "memory/allocation.hpp"
  27 #include "metaprogramming/integerTypes.hpp"
  28 #include "metaprogramming/isSame.hpp"
  29 #include "unittest.hpp"
  30 #include "utilities/debug.hpp"
  31 
  32 class IntegerTypesTest: AllStatic {
  33   template <typename SignedType, typename UnsignedType>
  34   class TestIntegers: AllStatic {
  35     static const bool _cts_unsigned_type_is_signed_type = IsSame<typename IntegerTypes::Signed<UnsignedType>::type, SignedType>::value;
  36     STATIC_ASSERT(_cts_unsigned_type_is_signed_type);
  37     static const bool _ctu_signed_type_is_unsigned_type = IsSame<typename IntegerTypes::Unsigned<SignedType>::type, UnsignedType>::value;
  38     STATIC_ASSERT(_ctu_signed_type_is_unsigned_type);
  39   };
  40 
  41   const TestIntegers<int8_t,  uint8_t>  TestByte;
  42   const TestIntegers<int16_t, uint16_t> TestShort;
  43   const TestIntegers<int32_t, uint32_t> TestInt;
  44   const TestIntegers<int64_t, uint64_t> TestLong;
  45 
  46   typedef IntegerTypes::Canonical<intptr_t>::type CanonicalIntPtr;
  47   typedef IntegerTypes::Canonical<uintptr_t>::type CanonicalUIntPtr;
  48 
  49   static const bool _cts_voidptr_is_intptrt = IsSame<IntegerTypes::Signed<void*>::type, CanonicalIntPtr>::value;
  50   STATIC_ASSERT(_cts_voidptr_is_intptrt);
  51   static const bool _ctu_voidptr_is_uintptrt = IsSame<IntegerTypes::Unsigned<void*>::type, CanonicalUIntPtr>::value;
  52   STATIC_ASSERT(_ctu_voidptr_is_uintptrt);
  53 
  54   class A VALUE_OBJ_CLASS_SPEC {
  55     intptr_t _value;
  56   public:
  57     operator void* () { return reinterpret_cast<void*>(_value); }
  58     A(void* arg) : _value(reinterpret_cast<intptr_t>(arg)) {}
  59   };
  60 
  61   static const bool _cts_cvptrcv_is_intptrt = IsSame<IntegerTypes::Signed<const volatile void* const volatile>::type, CanonicalIntPtr>::value;
  62   STATIC_ASSERT(_cts_cvptrcv_is_intptrt);
  63   static const bool _ctu_cvptrcv_is_uintptrt = IsSame<IntegerTypes::Unsigned<const volatile void* const volatile>::type, CanonicalUIntPtr>::value;
  64   STATIC_ASSERT(_ctu_cvptrcv_is_uintptrt);
  65 
  66   static const bool _cts_float_is_int = IsSame<IntegerTypes::Signed<float>::type, int32_t>::value;
  67   STATIC_ASSERT(_cts_float_is_int);
  68   static const bool _ctu_float_is_uint = IsSame<IntegerTypes::Unsigned<float>::type, uint32_t>::value;
  69   STATIC_ASSERT(_ctu_float_is_uint);
  70 
  71   static const bool _cts_double_is_long = IsSame<IntegerTypes::Signed<double>::type, int64_t>::value;
  72   STATIC_ASSERT(_cts_double_is_long);
  73   static const bool _ctu_double_is_ulong = IsSame<IntegerTypes::Unsigned<double>::type, uint64_t>::value;
  74   STATIC_ASSERT(_ctu_double_is_ulong);
  75 
  76   static void test_cast() {
  77     (void)IntegerTypes::cast<void*>(intptr_t(0));
  78     (void)IntegerTypes::cast<float>(int32_t(0));
  79     (void)IntegerTypes::cast<double>(int64_t(0));
  80     (void)IntegerTypes::cast<int64_t>(int64_t(0));
  81   }
  82 };
  83 
  84 TEST(IntegerTypesRTest, round_trip_int) {
  85   int  sfive = 5;
  86   int  mfive = -5;
  87   uint ufive = 5u;
  88 
  89   EXPECT_EQ(sfive, IntegerTypes::cast<int>(IntegerTypes::cast_to_signed(sfive)));
  90   EXPECT_EQ(sfive, IntegerTypes::cast<int>(IntegerTypes::cast_to_unsigned(sfive)));
  91 
  92   EXPECT_EQ(mfive, IntegerTypes::cast<int>(IntegerTypes::cast_to_signed(mfive)));
  93   EXPECT_EQ(mfive, IntegerTypes::cast<int>(IntegerTypes::cast_to_unsigned(mfive)));
  94 
  95   EXPECT_EQ(ufive, IntegerTypes::cast<uint>(IntegerTypes::cast_to_signed(ufive)));
  96   EXPECT_EQ(ufive, IntegerTypes::cast<uint>(IntegerTypes::cast_to_unsigned(ufive)));
  97 }
  98 
  99 TEST(IntegerTypesRTest, round_trip_float) {
 100   float  ffive = 5.0f;
 101   double dfive = 5.0;
 102 
 103   EXPECT_EQ(ffive, IntegerTypes::cast<float>(IntegerTypes::cast_to_signed(ffive)));
 104   EXPECT_EQ(ffive, IntegerTypes::cast<float>(IntegerTypes::cast_to_unsigned(ffive)));
 105 
 106   EXPECT_EQ(dfive, IntegerTypes::cast<double>(IntegerTypes::cast_to_signed(dfive)));
 107   EXPECT_EQ(dfive, IntegerTypes::cast<double>(IntegerTypes::cast_to_unsigned(dfive)));
 108 }
 109 
 110 TEST(IntegerTypesRTest, round_trip_ptr) {
 111   int five = 5;
 112   int* pfive = &five;
 113   const int* cpfive = &five;
 114 
 115   EXPECT_EQ(pfive, IntegerTypes::cast<int*>(IntegerTypes::cast_to_signed(pfive)));
 116   EXPECT_EQ(pfive, IntegerTypes::cast<int*>(IntegerTypes::cast_to_unsigned(pfive)));
 117 
 118   EXPECT_EQ(cpfive, IntegerTypes::cast<const int*>(IntegerTypes::cast_to_signed(cpfive)));
 119   EXPECT_EQ(cpfive, IntegerTypes::cast<const int*>(IntegerTypes::cast_to_unsigned(cpfive)));
 120 }