test/hotspot/gtest/metaprogramming/test_primitiveConversions.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46958 hotspot/test/native/metaprogramming/test_primitiveConversions.cpp@a13bd8c6b7a2
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     1
/*
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     4
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     8
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    13
 * accompanied this code).
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    14
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    18
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    21
 * questions.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    22
 *
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    23
 */
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    24
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    25
#include "precompiled.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    26
#include "memory/allocation.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    27
#include "metaprogramming/isSame.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    28
#include "metaprogramming/primitiveConversions.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    29
#include "unittest.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    30
#include "utilities/debug.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    31
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    32
struct PrimitiveConversionsTestSupport: AllStatic {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    33
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    34
  template<size_t byte_size> struct SignedTypeOfSize;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    35
  template<size_t byte_size> struct UnsignedTypeOfSize;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    36
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    37
  template<typename T> struct Signed;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    38
  template<typename T> struct Unsigned;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    39
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    40
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    41
#define DEFINE_CANONICAL_SIGNED_TYPE(T)                                 \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    42
  template<>                                                            \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    43
  struct PrimitiveConversionsTestSupport::SignedTypeOfSize<sizeof(T)>   \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    44
    : public AllStatic                                                  \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    45
  {                                                                     \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    46
    typedef T type;                                                     \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    47
  };
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    48
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    49
#define DEFINE_CANONICAL_UNSIGNED_TYPE(T)                               \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    50
  template<>                                                            \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    51
  struct PrimitiveConversionsTestSupport::UnsignedTypeOfSize<sizeof(T)> \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    52
    : public AllStatic                                                  \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    53
  {                                                                     \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    54
    typedef T type;                                                     \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    55
  };
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    56
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    57
#define DEFINE_INTEGER_TYPES_OF_SIZE(NBITS)            \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    58
  DEFINE_CANONICAL_SIGNED_TYPE(int ## NBITS ## _t)     \
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    59
  DEFINE_CANONICAL_UNSIGNED_TYPE(uint ## NBITS ## _t)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    60
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    61
DEFINE_INTEGER_TYPES_OF_SIZE(8)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    62
DEFINE_INTEGER_TYPES_OF_SIZE(16)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    63
DEFINE_INTEGER_TYPES_OF_SIZE(32)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    64
DEFINE_INTEGER_TYPES_OF_SIZE(64)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    65
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    66
#undef DEFINE_INTEGER_TYPES_OF_SIZE
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    67
#undef DEFINE_CANONICAL_SIGNED_TYPE
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    68
#undef DEFINE_CANONICAL_UNSIGNED_TYPE
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    69
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    70
template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    71
struct PrimitiveConversionsTestSupport::Signed
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    72
  : public SignedTypeOfSize<sizeof(T)>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    73
{};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    74
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    75
template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    76
struct PrimitiveConversionsTestSupport::Unsigned
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    77
  : public UnsignedTypeOfSize<sizeof(T)>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    78
{};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    79
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    80
TEST(PrimitiveConversionsTest, round_trip_int) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    81
  int  sfive = 5;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    82
  int  mfive = -5;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    83
  uint ufive = 5u;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    84
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    85
  typedef PrimitiveConversionsTestSupport::Signed<int>::type SI;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    86
  typedef PrimitiveConversionsTestSupport::Unsigned<int>::type UI;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    87
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    88
  EXPECT_EQ(sfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<SI>(sfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    89
  EXPECT_EQ(sfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<UI>(sfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    90
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    91
  EXPECT_EQ(mfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<SI>(mfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    92
  EXPECT_EQ(mfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<UI>(mfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    93
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    94
  EXPECT_EQ(ufive, PrimitiveConversions::cast<uint>(PrimitiveConversions::cast<SI>(ufive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    95
  EXPECT_EQ(ufive, PrimitiveConversions::cast<uint>(PrimitiveConversions::cast<UI>(ufive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    96
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    97
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    98
TEST(PrimitiveConversionsTest, round_trip_float) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
    99
  float  ffive = 5.0f;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   100
  double dfive = 5.0;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   101
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   102
  typedef PrimitiveConversionsTestSupport::Signed<float>::type SF;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   103
  typedef PrimitiveConversionsTestSupport::Unsigned<float>::type UF;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   104
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   105
  typedef PrimitiveConversionsTestSupport::Signed<double>::type SD;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   106
  typedef PrimitiveConversionsTestSupport::Unsigned<double>::type UD;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   107
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   108
  EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<SF>(ffive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   109
  EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<UF>(ffive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   110
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   111
  EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<SD>(dfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   112
  EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<UD>(dfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   113
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   114
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   115
TEST(PrimitiveConversionsTest, round_trip_ptr) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   116
  int five = 5;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   117
  int* pfive = &five;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   118
  const int* cpfive = &five;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   119
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   120
  typedef PrimitiveConversionsTestSupport::Signed<int*>::type SIP;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   121
  typedef PrimitiveConversionsTestSupport::Unsigned<int*>::type UIP;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   122
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   123
  EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<SIP>(pfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   124
  EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<UIP>(pfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   125
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   126
  EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<SIP>(cpfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   127
  EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<UIP>(cpfive)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents:
diff changeset
   128
}