1 //=== - llvm/unittest/Support/Alignment.cpp - Alignment utility tests -----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Alignment.h"
10 #include "gtest/gtest.h"
18 std::vector
<uint64_t> getValidAlignments() {
19 std::vector
<uint64_t> Out
;
20 for (size_t Shift
= 0; Shift
< 64; ++Shift
)
21 Out
.push_back(1ULL << Shift
);
25 TEST(AlignmentTest
, AlignDefaultCTor
) { EXPECT_EQ(Align().value(), 1ULL); }
27 TEST(AlignmentTest
, MaybeAlignDefaultCTor
) {
28 EXPECT_FALSE(MaybeAlign().hasValue());
31 TEST(AlignmentTest
, ValidCTors
) {
32 for (size_t Value
: getValidAlignments()) {
33 EXPECT_EQ(Align(Value
).value(), Value
);
34 EXPECT_EQ((*MaybeAlign(Value
)).value(), Value
);
38 TEST(AlignmentTest
, CheckMaybeAlignHasValue
) {
39 EXPECT_TRUE(MaybeAlign(1));
40 EXPECT_TRUE(MaybeAlign(1).hasValue());
41 EXPECT_FALSE(MaybeAlign(0));
42 EXPECT_FALSE(MaybeAlign(0).hasValue());
43 EXPECT_FALSE(MaybeAlign());
44 EXPECT_FALSE(MaybeAlign().hasValue());
47 TEST(AlignmentTest
, Division
) {
48 for (size_t Value
: getValidAlignments()) {
50 EXPECT_EQ(Align(Value
) / 2, Value
/ 2);
51 EXPECT_EQ(MaybeAlign(Value
) / 2, Value
/ 2);
54 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
57 TEST(AlignmentTest
, AlignTo
) {
81 for (const auto &T
: kTests
) {
82 MaybeAlign
A(T
.alignment
);
84 EXPECT_EQ(alignTo(T
.offset
, A
), T
.rounded
);
87 EXPECT_EQ(alignTo(T
.offset
, A
.getValue()), T
.rounded
);
91 TEST(AlignmentTest
, Log2
) {
92 for (size_t Value
: getValidAlignments()) {
93 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
94 EXPECT_EQ(Log2(MaybeAlign(Value
)), Log2_64(Value
));
98 TEST(AlignmentTest
, MinAlign
) {
108 // MaybeAlign / Align
112 for (const auto &T
: kTests
) {
113 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
114 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
116 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
118 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
120 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
124 TEST(AlignmentTest
, Encode_Decode
) {
125 for (size_t Value
: getValidAlignments()) {
128 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
129 EXPECT_EQ(Expected
, Actual
);
132 MaybeAlign
Actual(Value
);
133 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
134 EXPECT_EQ(Expected
, Actual
);
137 MaybeAlign
Actual(0);
138 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
139 EXPECT_EQ(Expected
, Actual
);
142 TEST(AlignmentTest
, isAligned
) {
148 // MaybeAlign / Align
149 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
150 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
151 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
153 for (const auto &T
: kTests
) {
154 MaybeAlign
A(T
.alignment
);
156 EXPECT_EQ(isAligned(A
, T
.offset
), T
.isAligned
);
159 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
163 TEST(AlignmentTest
, AlignComparisons
) {
164 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
165 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
166 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
168 const Align
A(ValidAlignments
[I
- 1]);
169 const Align
B(ValidAlignments
[I
]);
179 EXPECT_EQ(A
, A
.value());
180 EXPECT_NE(A
, B
.value());
181 EXPECT_LT(A
, B
.value());
182 EXPECT_GT(B
, A
.value());
183 EXPECT_LE(A
, B
.value());
184 EXPECT_GE(B
, A
.value());
185 EXPECT_LE(A
, A
.value());
186 EXPECT_GE(A
, A
.value());
188 EXPECT_EQ(std::max(A
, B
), B
);
189 EXPECT_EQ(std::min(A
, B
), A
);
191 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
192 const MaybeAlign
MB(ValidAlignments
[I
]);
202 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
203 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
204 EXPECT_LT(MA
, MB
? (*MB
).value() : 0);
205 EXPECT_GT(MB
, MA
? (*MA
).value() : 0);
206 EXPECT_LE(MA
, MB
? (*MB
).value() : 0);
207 EXPECT_GE(MB
, MA
? (*MA
).value() : 0);
208 EXPECT_LE(MA
, MA
? (*MA
).value() : 0);
209 EXPECT_GE(MA
, MA
? (*MA
).value() : 0);
211 EXPECT_EQ(std::max(A
, B
), B
);
212 EXPECT_EQ(std::min(A
, B
), A
);
216 TEST(AlignmentTest
, AssumeAligned
) {
217 EXPECT_EQ(assumeAligned(0), Align(1));
218 EXPECT_EQ(assumeAligned(0), Align());
219 EXPECT_EQ(assumeAligned(1), Align(1));
220 EXPECT_EQ(assumeAligned(1), Align());
223 // Death tests reply on assert which is disabled in release mode.
226 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
228 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
229 return {1, 1ULL << 31, 1ULL << 63};
232 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
234 TEST(AlignmentDeathTest
, Log2
) {
235 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
238 TEST(AlignmentDeathTest
, CantConvertUnsetMaybe
) {
239 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
242 TEST(AlignmentDeathTest
, Division
) {
243 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
244 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
246 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
247 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
250 TEST(AlignmentDeathTest
, InvalidCTors
) {
251 EXPECT_DEATH((Align(0)), "Value must not be 0");
252 for (size_t Value
: getNonPowerOfTwo()) {
253 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
254 EXPECT_DEATH((MaybeAlign(Value
)), "Alignment is not 0 or a power of 2");
258 TEST(AlignmentDeathTest
, ComparisonsWithZero
) {
259 for (size_t Value
: getValidAlignmentsForDeathTest()) {
260 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
261 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
262 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
263 EXPECT_DEATH((void)(Align(Value
) <= 0), ".* should be defined");
264 EXPECT_DEATH((void)(Align(Value
) > 0), ".* should be defined");
265 EXPECT_DEATH((void)(Align(Value
) < 0), ".* should be defined");
269 TEST(AlignmentDeathTest
, CompareMaybeAlignToZero
) {
270 for (size_t Value
: getValidAlignmentsForDeathTest()) {
271 // MaybeAlign is allowed to be == or != 0
272 (void)(MaybeAlign(Value
) == 0);
273 (void)(MaybeAlign(Value
) != 0);
274 EXPECT_DEATH((void)(MaybeAlign(Value
) >= 0), ".* should be defined");
275 EXPECT_DEATH((void)(MaybeAlign(Value
) <= 0), ".* should be defined");
276 EXPECT_DEATH((void)(MaybeAlign(Value
) > 0), ".* should be defined");
277 EXPECT_DEATH((void)(MaybeAlign(Value
) < 0), ".* should be defined");
281 TEST(AlignmentDeathTest
, CompareAlignToUndefMaybeAlign
) {
282 for (size_t Value
: getValidAlignmentsForDeathTest()) {
283 EXPECT_DEATH((void)(Align(Value
) == MaybeAlign(0)), ".* should be defined");
284 EXPECT_DEATH((void)(Align(Value
) != MaybeAlign(0)), ".* should be defined");
285 EXPECT_DEATH((void)(Align(Value
) >= MaybeAlign(0)), ".* should be defined");
286 EXPECT_DEATH((void)(Align(Value
) <= MaybeAlign(0)), ".* should be defined");
287 EXPECT_DEATH((void)(Align(Value
) > MaybeAlign(0)), ".* should be defined");
288 EXPECT_DEATH((void)(Align(Value
) < MaybeAlign(0)), ".* should be defined");
294 } // end anonymous namespace