Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/metaprogramming/test_primitiveConversions.cpp
41145 views
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_int_constexpr) {
99
constexpr int sfive = 5;
100
constexpr int mfive = -5;
101
constexpr uint ufive = 5u;
102
103
typedef PrimitiveConversionsTestSupport::Signed<int>::type SI;
104
typedef PrimitiveConversionsTestSupport::Unsigned<int>::type UI;
105
106
{
107
constexpr SI i = PrimitiveConversions::cast<SI>(sfive);
108
constexpr int r = PrimitiveConversions::cast<int>(i);
109
EXPECT_EQ(sfive, r);
110
}
111
112
{
113
constexpr UI i = PrimitiveConversions::cast<UI>(sfive);
114
constexpr int r = PrimitiveConversions::cast<int>(i);
115
EXPECT_EQ(sfive, r);
116
}
117
118
{
119
constexpr SI i = PrimitiveConversions::cast<SI>(mfive);
120
constexpr int r = PrimitiveConversions::cast<int>(i);
121
EXPECT_EQ(mfive, r);
122
}
123
124
{
125
constexpr UI i = PrimitiveConversions::cast<UI>(mfive);
126
constexpr int r = PrimitiveConversions::cast<int>(i);
127
EXPECT_EQ(mfive, r);
128
}
129
130
{
131
constexpr SI i = PrimitiveConversions::cast<SI>(ufive);
132
constexpr uint r = PrimitiveConversions::cast<uint>(i);
133
EXPECT_EQ(ufive, r);
134
}
135
136
{
137
constexpr UI i = PrimitiveConversions::cast<UI>(ufive);
138
constexpr uint r = PrimitiveConversions::cast<uint>(i);
139
EXPECT_EQ(ufive, r);
140
}
141
}
142
143
TEST(PrimitiveConversionsTest, round_trip_float) {
144
float ffive = 5.0f;
145
double dfive = 5.0;
146
147
typedef PrimitiveConversionsTestSupport::Signed<float>::type SF;
148
typedef PrimitiveConversionsTestSupport::Unsigned<float>::type UF;
149
150
typedef PrimitiveConversionsTestSupport::Signed<double>::type SD;
151
typedef PrimitiveConversionsTestSupport::Unsigned<double>::type UD;
152
153
EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<SF>(ffive)));
154
EXPECT_EQ(ffive, PrimitiveConversions::cast<float>(PrimitiveConversions::cast<UF>(ffive)));
155
156
EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<SD>(dfive)));
157
EXPECT_EQ(dfive, PrimitiveConversions::cast<double>(PrimitiveConversions::cast<UD>(dfive)));
158
}
159
160
TEST(PrimitiveConversionsTest, round_trip_ptr) {
161
int five = 5;
162
int* pfive = &five;
163
const int* cpfive = &five;
164
165
typedef PrimitiveConversionsTestSupport::Signed<int*>::type SIP;
166
typedef PrimitiveConversionsTestSupport::Unsigned<int*>::type UIP;
167
168
EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<SIP>(pfive)));
169
EXPECT_EQ(pfive, PrimitiveConversions::cast<int*>(PrimitiveConversions::cast<UIP>(pfive)));
170
171
EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<SIP>(cpfive)));
172
EXPECT_EQ(cpfive, PrimitiveConversions::cast<const int*>(PrimitiveConversions::cast<UIP>(cpfive)));
173
}
174
175