Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_globalDefinitions.cpp
41144 views
1
/*
2
* Copyright (c) 2016, 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
#include "precompiled.hpp"
25
#include "runtime/os.hpp"
26
#include "utilities/align.hpp"
27
#include "utilities/globalDefinitions.hpp"
28
#include <type_traits>
29
#include "unittest.hpp"
30
31
static ::testing::AssertionResult testPageAddress(
32
const char* expected_addr_expr,
33
const char* addr_expr,
34
const char* page_addr_expr,
35
const char* page_size_expr,
36
const char* actual_addr_expr,
37
address expected_addr,
38
address addr,
39
address page_addr,
40
intptr_t page_size,
41
address actual_addr) {
42
if (expected_addr == actual_addr) {
43
return ::testing::AssertionSuccess();
44
}
45
46
return ::testing::AssertionFailure()
47
<< actual_addr_expr << " returned unexpected address " << (void*) actual_addr << std::endl
48
<< "Expected " << expected_addr_expr << ": " << (void*) expected_addr << std::endl
49
<< "where" << std::endl
50
<< addr_expr << ": " << (void*) addr << std::endl
51
<< page_addr_expr << ": " << (void*) page_addr << std::endl
52
<< page_size_expr << ": " << page_size;
53
}
54
55
TEST_VM(globalDefinitions, clamp_address_in_page) {
56
const intptr_t page_sizes[] = {os::vm_page_size(), 4096, 8192, 65536, 2 * 1024 * 1024};
57
const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]);
58
59
for (int i = 0; i < num_page_sizes; i++) {
60
intptr_t page_size = page_sizes[i];
61
address page_address = (address) (10 * page_size);
62
63
const intptr_t within_page_offsets[] = {0, 128, page_size - 1};
64
const int num_within_page_offsets = sizeof(within_page_offsets) / sizeof(within_page_offsets[0]);
65
66
for (int k = 0; k < num_within_page_offsets; ++k) {
67
address addr = page_address + within_page_offsets[k];
68
address expected_address = addr;
69
EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
70
clamp_address_in_page(addr, page_address, page_size))
71
<< "Expect that address within page is returned as is";
72
}
73
74
const intptr_t above_page_offsets[] = {page_size, page_size + 1, 5 * page_size + 1};
75
const int num_above_page_offsets = sizeof(above_page_offsets) / sizeof(above_page_offsets[0]);
76
77
for (int k = 0; k < num_above_page_offsets; ++k) {
78
address addr = page_address + above_page_offsets[k];
79
address expected_address = page_address + page_size;
80
EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
81
clamp_address_in_page(addr, page_address, page_size))
82
<< "Expect that address above page returns start of next page";
83
}
84
85
const intptr_t below_page_offsets[] = {1, 2 * page_size + 1, 5 * page_size + 1};
86
const int num_below_page_offsets = sizeof(below_page_offsets) / sizeof(below_page_offsets[0]);
87
88
for (int k = 0; k < num_below_page_offsets; ++k) {
89
address addr = page_address - below_page_offsets[k];
90
address expected_address = page_address;
91
EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
92
clamp_address_in_page(addr, page_address, page_size))
93
<< "Expect that address below page returns start of page";
94
}
95
}
96
}
97
98
TEST(globalDefinitions, proper_unit) {
99
EXPECT_EQ(0u, byte_size_in_proper_unit(0u));
100
EXPECT_STREQ("B", proper_unit_for_byte_size(0u));
101
102
EXPECT_EQ(1u, byte_size_in_proper_unit(1u));
103
EXPECT_STREQ("B", proper_unit_for_byte_size(1u));
104
105
EXPECT_EQ(1023u, byte_size_in_proper_unit(K - 1));
106
EXPECT_STREQ("B", proper_unit_for_byte_size(K - 1));
107
108
EXPECT_EQ(1024u, byte_size_in_proper_unit(K));
109
EXPECT_STREQ("B", proper_unit_for_byte_size(K));
110
111
EXPECT_EQ(1025u, byte_size_in_proper_unit(K + 1));
112
EXPECT_STREQ("B", proper_unit_for_byte_size(K + 1));
113
114
EXPECT_EQ(51200u, byte_size_in_proper_unit(50*K));
115
EXPECT_STREQ("B", proper_unit_for_byte_size(50*K));
116
117
EXPECT_EQ(1023u, byte_size_in_proper_unit(M - 1));
118
EXPECT_STREQ("K", proper_unit_for_byte_size(M - 1));
119
120
EXPECT_EQ(1024u, byte_size_in_proper_unit(M));
121
EXPECT_STREQ("K", proper_unit_for_byte_size(M));
122
123
EXPECT_EQ(1024u, byte_size_in_proper_unit(M + 1));
124
EXPECT_STREQ("K", proper_unit_for_byte_size(M + 1));
125
126
EXPECT_EQ(1025u, byte_size_in_proper_unit(M + K));
127
EXPECT_STREQ("K", proper_unit_for_byte_size(M + K));
128
129
EXPECT_EQ(51200u, byte_size_in_proper_unit(50*M));
130
EXPECT_STREQ("K", proper_unit_for_byte_size(50*M));
131
132
#ifdef _LP64
133
EXPECT_EQ(1023u, byte_size_in_proper_unit(G - 1));
134
EXPECT_STREQ("M", proper_unit_for_byte_size(G - 1));
135
136
EXPECT_EQ(1024u, byte_size_in_proper_unit(G));
137
EXPECT_STREQ("M", proper_unit_for_byte_size(G));
138
139
EXPECT_EQ(1024u, byte_size_in_proper_unit(G + 1));
140
EXPECT_STREQ("M", proper_unit_for_byte_size(G + 1));
141
142
EXPECT_EQ(1024u, byte_size_in_proper_unit(G + K));
143
EXPECT_STREQ("M", proper_unit_for_byte_size(G + K));
144
145
EXPECT_EQ(1025u, byte_size_in_proper_unit(G + M));
146
EXPECT_STREQ("M", proper_unit_for_byte_size(G + M));
147
148
EXPECT_EQ(51200u, byte_size_in_proper_unit(50*G));
149
EXPECT_STREQ("M", proper_unit_for_byte_size(50*G));
150
#endif
151
}
152
153
TEST(globalDefinitions, exact_unit_for_byte_size) {
154
EXPECT_STREQ("B", exact_unit_for_byte_size(0));
155
EXPECT_STREQ("B", exact_unit_for_byte_size(1));
156
EXPECT_STREQ("B", exact_unit_for_byte_size(K - 1));
157
EXPECT_STREQ("K", exact_unit_for_byte_size(K));
158
EXPECT_STREQ("B", exact_unit_for_byte_size(K + 1));
159
EXPECT_STREQ("B", exact_unit_for_byte_size(M - 1));
160
EXPECT_STREQ("M", exact_unit_for_byte_size(M));
161
EXPECT_STREQ("B", exact_unit_for_byte_size(M + 1));
162
EXPECT_STREQ("K", exact_unit_for_byte_size(M + K));
163
#ifdef _LP64
164
EXPECT_STREQ("B", exact_unit_for_byte_size(G - 1));
165
EXPECT_STREQ("G", exact_unit_for_byte_size(G));
166
EXPECT_STREQ("B", exact_unit_for_byte_size(G + 1));
167
EXPECT_STREQ("K", exact_unit_for_byte_size(G + K));
168
EXPECT_STREQ("M", exact_unit_for_byte_size(G + M));
169
EXPECT_STREQ("K", exact_unit_for_byte_size(G + M + K));
170
#endif
171
}
172
173
TEST(globalDefinitions, byte_size_in_exact_unit) {
174
EXPECT_EQ(0u, byte_size_in_exact_unit(0));
175
EXPECT_EQ(1u, byte_size_in_exact_unit(1));
176
EXPECT_EQ(K - 1, byte_size_in_exact_unit(K - 1));
177
EXPECT_EQ(1u, byte_size_in_exact_unit(K));
178
EXPECT_EQ(K + 1, byte_size_in_exact_unit(K + 1));
179
EXPECT_EQ(M - 1, byte_size_in_exact_unit(M - 1));
180
EXPECT_EQ(1u, byte_size_in_exact_unit(M));
181
EXPECT_EQ(M + 1, byte_size_in_exact_unit(M + 1));
182
EXPECT_EQ(K + 1, byte_size_in_exact_unit(M + K));
183
#ifdef _LP64
184
EXPECT_EQ(G - 1, byte_size_in_exact_unit(G - 1));
185
EXPECT_EQ(1u, byte_size_in_exact_unit(G));
186
EXPECT_EQ(G + 1, byte_size_in_exact_unit(G + 1));
187
EXPECT_EQ(M + 1, byte_size_in_exact_unit(G + K));
188
EXPECT_EQ(K + 1, byte_size_in_exact_unit(G + M));
189
EXPECT_EQ(M + K + 1, byte_size_in_exact_unit(G + M + K));
190
#endif
191
}
192
193
TEST(globalDefinitions, array_size) {
194
const size_t test_size = 10;
195
196
{
197
int test_array[test_size] = {};
198
static_assert(test_size == ARRAY_SIZE(test_array), "must be");
199
}
200
201
{
202
double test_array[test_size] = {};
203
static_assert(test_size == ARRAY_SIZE(test_array), "must be");
204
}
205
206
struct ArrayElt { int x; };
207
208
{
209
ArrayElt test_array[test_size] = {};
210
static_assert(test_size == ARRAY_SIZE(test_array), "must be");
211
}
212
213
{
214
const ArrayElt test_array[] = { {0}, {1}, {2}, {3}, {4}, {5} };
215
static_assert(6 == ARRAY_SIZE(test_array), "must be");
216
}
217
218
}
219
220