Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_align.cpp
41144 views
1
/*
2
* Copyright (c) 2017, 2020, 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 "utilities/align.hpp"
26
#include "utilities/formatBuffer.hpp"
27
#include "utilities/globalDefinitions.hpp"
28
#include <limits>
29
#include "unittest.hpp"
30
31
// A few arbitrarily chosen values to test the align functions on.
32
static constexpr uint64_t values[] = {1, 3, 10, 345, 1023, 1024, 1025, 23909034, INT_MAX, uint64_t(-1) / 2, uint64_t(-1) / 2 + 100, uint64_t(-1)};
33
34
template <typename T>
35
static constexpr T max_alignment() {
36
T max = std::numeric_limits<T>::max();
37
return max ^ (max >> 1);
38
}
39
40
#define log(...) SCOPED_TRACE(err_msg(__VA_ARGS__).buffer())
41
42
struct StaticTestAlignmentsResult {
43
uint64_t _value;
44
uint64_t _alignment;
45
int _status; // 0: success, > 0 indicates which failure case
46
constexpr StaticTestAlignmentsResult(uint64_t value, uint64_t alignment, int status) :
47
_value(value), _alignment(alignment), _status(status) {}
48
};
49
50
// Structure copied from test_alignments runtime test (below).
51
template<typename T, typename A>
52
static constexpr StaticTestAlignmentsResult
53
static_test_alignments_aux(A alignment) {
54
using Result = StaticTestAlignmentsResult;
55
56
for ( ; alignment > 0; alignment >>= 1) {
57
for (size_t i = 0; i < ARRAY_SIZE(values); ++i) {
58
// Test align up
59
uint64_t up = align_up(values[i], alignment);
60
if (0 < up && up < uint64_t(std::numeric_limits<T>::max())) {
61
T value = T(values[i]);
62
if (align_up(uint64_t(value), alignment) != up) {
63
return Result(values[i], alignment, 1);
64
} else if (align_up(value, alignment) < value) {
65
return Result(values[i], alignment, 2);
66
}
67
}
68
69
// Test align down
70
uint64_t down = align_down(values[i], alignment);
71
if (down <= uint64_t(std::numeric_limits<T>::max())) {
72
T value = T(values[i]);
73
if (uint64_t(align_down(value, alignment)) != down) {
74
return Result(values[i], alignment, 3);
75
} else if (align_down(value, alignment) > value) {
76
return Result(values[i], alignment, 4);
77
}
78
}
79
80
// Test is aligned
81
bool is = is_aligned(values[i], alignment);
82
if (values[i] <= uint64_t(std::numeric_limits<T>::max())) {
83
T value = T(values[i]);
84
if (is_aligned(value, alignment) != is) {
85
return Result(values[i], alignment, 5);
86
}
87
}
88
}
89
}
90
return Result(T(), A(), 0);
91
}
92
93
template<typename T, typename A>
94
static void static_test_alignments() {
95
constexpr StaticTestAlignmentsResult result
96
= static_test_alignments_aux<T>(max_alignment<A>());
97
98
EXPECT_EQ(0, result._status)
99
<< "value = " << result._value
100
<< ", alignment = " << result._alignment
101
<< ", status = " << result._status;
102
}
103
104
template <typename T, typename A>
105
static void test_alignments() {
106
log("### Test: %c" SIZE_FORMAT " " UINT64_FORMAT " : %c" SIZE_FORMAT " " UINT64_FORMAT " ###\n",
107
std::numeric_limits<T>::is_signed ? 's' : 'u', sizeof(T), (uint64_t)std::numeric_limits<T>::max(),
108
std::numeric_limits<A>::is_signed ? 's' : 'u', sizeof(A), (uint64_t)std::numeric_limits<A>::max());
109
110
// Test all possible alignment values that fit in type A.
111
for (A alignment = max_alignment<A>(); alignment > 0; alignment >>= 1) {
112
log("=== Alignment: " UINT64_FORMAT " ===\n", (uint64_t)alignment);
113
114
for (size_t i = 0; i < ARRAY_SIZE(values); i++) {
115
log("--- Value: " UINT64_FORMAT "\n", values[i]);
116
117
// Test align up
118
const uint64_t up = align_up(values[i], alignment);
119
if (0 < up && up <= (uint64_t)std::numeric_limits<T>::max()) {
120
log("Testing align_up: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], up);
121
122
T value = T(values[i]);
123
124
// Check against uint64_t version
125
ASSERT_EQ(align_up((uint64_t)value, alignment), up);
126
// Sanity check
127
ASSERT_GE(align_up(value, alignment), value);
128
}
129
130
// Test align down
131
const uint64_t down = align_down(values[i], alignment);
132
if (down <= (uint64_t)std::numeric_limits<T>::max()) {
133
log("Testing align_down: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: 0x" UINT64_FORMAT_X "\n", (uint64_t)alignment, values[i], down);
134
135
T value = T(values[i]);
136
137
// Check against uint64_t version
138
ASSERT_EQ((uint64_t)align_down(value, alignment), down);
139
// Sanity check
140
ASSERT_LE(align_down(value, alignment), value);
141
}
142
143
// Test is aligned
144
const bool is = is_aligned(values[i], alignment);
145
if (values[i] <= (uint64_t)std::numeric_limits<T>::max()) {
146
log("Testing is_aligned: alignment: 0x" UINT64_FORMAT_X " value: 0x" UINT64_FORMAT_X " expected: %s\n", (uint64_t)alignment, values[i], is ? "true" : "false");
147
148
T value = T(values[i]);
149
150
// Check against uint64_t version
151
ASSERT_EQ(is_aligned(value, alignment), is);
152
}
153
}
154
}
155
156
static_test_alignments<T, A>();
157
}
158
159
TEST(Align, alignments) {
160
// Test the alignment functions with different type combinations.
161
162
test_alignments<int64_t, uint8_t>();
163
test_alignments<int64_t, uint16_t>();
164
test_alignments<int64_t, uint32_t>();
165
test_alignments<int64_t, int8_t>();
166
test_alignments<int64_t, int16_t>();
167
test_alignments<int64_t, int32_t>();
168
test_alignments<int64_t, int64_t>();
169
170
test_alignments<uint32_t, uint8_t>();
171
test_alignments<uint32_t, uint16_t>();
172
test_alignments<uint32_t, uint32_t>();
173
test_alignments<uint32_t, int8_t>();
174
test_alignments<uint32_t, int16_t>();
175
test_alignments<uint32_t, int32_t>();
176
177
test_alignments<int32_t, uint8_t>();
178
test_alignments<int32_t, uint16_t>();
179
test_alignments<int32_t, int8_t>();
180
test_alignments<int32_t, int16_t>();
181
test_alignments<int32_t, int32_t>();
182
183
test_alignments<uint16_t, uint8_t>();
184
test_alignments<uint16_t, uint16_t>();
185
test_alignments<uint16_t, int8_t>();
186
test_alignments<uint16_t, int16_t>();
187
188
test_alignments<int16_t, uint8_t>();
189
test_alignments<int16_t, int8_t>();
190
test_alignments<int16_t, int16_t>();
191
192
test_alignments<uint8_t, int8_t>();
193
test_alignments<uint8_t, uint8_t>();
194
195
test_alignments<int8_t, int8_t>();
196
}
197
198