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/isSame.hpp" |
28 | #include "metaprogramming/primitiveConversions.hpp" |
29 | #include "unittest.hpp" |
30 | #include "utilities/debug.hpp" |
31 | |
32 | struct PrimitiveConversionsTestSupport: AllStatic { |
33 | |
34 | template<size_t byte_size> struct SignedTypeOfSize; |
35 | template<size_t byte_size> struct UnsignedTypeOfSize; |
36 | |
37 | template<typename T> struct Signed; |
38 | template<typename T> struct Unsigned; |
39 | }; |
40 | |
41 | #define DEFINE_CANONICAL_SIGNED_TYPE(T) \ |
42 | template<> \ |
43 | struct PrimitiveConversionsTestSupport::SignedTypeOfSize<sizeof(T)> \ |
44 | : public AllStatic \ |
45 | { \ |
46 | typedef T type; \ |
47 | }; |
48 | |
49 | #define DEFINE_CANONICAL_UNSIGNED_TYPE(T) \ |
50 | template<> \ |
51 | struct PrimitiveConversionsTestSupport::UnsignedTypeOfSize<sizeof(T)> \ |
52 | : public AllStatic \ |
53 | { \ |
54 | typedef T type; \ |
55 | }; |
56 | |
57 | #define DEFINE_INTEGER_TYPES_OF_SIZE(NBITS) \ |
58 | DEFINE_CANONICAL_SIGNED_TYPE(int ## NBITS ## _t) \ |
59 | DEFINE_CANONICAL_UNSIGNED_TYPE(uint ## NBITS ## _t) |
60 | |
61 | DEFINE_INTEGER_TYPES_OF_SIZE(8) |
62 | DEFINE_INTEGER_TYPES_OF_SIZE(16) |
63 | DEFINE_INTEGER_TYPES_OF_SIZE(32) |
64 | DEFINE_INTEGER_TYPES_OF_SIZE(64) |
65 | |
66 | #undef DEFINE_INTEGER_TYPES_OF_SIZE |
67 | #undef DEFINE_CANONICAL_SIGNED_TYPE |
68 | #undef DEFINE_CANONICAL_UNSIGNED_TYPE |
69 | |
70 | template<typename T> |
71 | struct PrimitiveConversionsTestSupport::Signed |
72 | : public SignedTypeOfSize<sizeof(T)> |
73 | {}; |
74 | |
75 | template<typename T> |
76 | struct PrimitiveConversionsTestSupport::Unsigned |
77 | : public UnsignedTypeOfSize<sizeof(T)> |
78 | {}; |
79 | |
80 | TEST(PrimitiveConversionsTest, round_trip_int) { |
81 | int sfive = 5; |
82 | int mfive = -5; |
83 | uint ufive = 5u; |
84 | |
85 | typedef PrimitiveConversionsTestSupport::Signed<int>::type SI; |
86 | typedef PrimitiveConversionsTestSupport::Unsigned<int>::type UI; |
87 | |
88 | EXPECT_EQ(sfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<SI>(sfive))); |
89 | EXPECT_EQ(sfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<UI>(sfive))); |
90 | |
91 | EXPECT_EQ(mfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<SI>(mfive))); |
92 | EXPECT_EQ(mfive, PrimitiveConversions::cast<int>(PrimitiveConversions::cast<UI>(mfive))); |
93 | |
94 | EXPECT_EQ(ufive, PrimitiveConversions::cast<uint>(PrimitiveConversions::cast<SI>(ufive))); |
95 | EXPECT_EQ(ufive, PrimitiveConversions::cast<uint>(PrimitiveConversions::cast<UI>(ufive))); |
96 | } |
97 | |
98 | TEST(PrimitiveConversionsTest, round_trip_float) { |
99 | float ffive = 5.0f; |
100 | double dfive = 5.0; |
101 | |
102 | typedef PrimitiveConversionsTestSupport::Signed<float>::type SF; |
103 | typedef PrimitiveConversionsTestSupport::Unsigned<float>::type UF; |
104 | |
105 | typedef PrimitiveConversionsTestSupport::Signed<double>::type SD; |
106 | typedef PrimitiveConversionsTestSupport::Unsigned<double>::type UD; |
107 | |
108 | EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<SF>(ffive))); |
109 | EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<UF>(ffive))); |
110 | |
111 | EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<SD>(dfive))); |
112 | EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<UD>(dfive))); |
113 | } |
114 | |
115 | TEST(PrimitiveConversionsTest, round_trip_ptr) { |
116 | int five = 5; |
117 | int* pfive = &five; |
118 | const int* cpfive = &five; |
119 | |
120 | typedef PrimitiveConversionsTestSupport::Signed<int*>::type SIP; |
121 | typedef PrimitiveConversionsTestSupport::Unsigned<int*>::type UIP; |
122 | |
123 | EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<SIP>(pfive))); |
124 | EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<UIP>(pfive))); |
125 | |
126 | EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<SIP>(cpfive))); |
127 | EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<UIP>(cpfive))); |
128 | } |
129 | |