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"
15 // Disable warnings about potential divide by 0.
17 #pragma warning(disable : 4723)
24 std::vector
<uint64_t> getValidAlignments() {
25 std::vector
<uint64_t> Out
;
26 for (size_t Shift
= 0; Shift
< 64; ++Shift
)
27 Out
.push_back(1ULL << Shift
);
31 TEST(AlignmentTest
, AlignDefaultCTor
) {
32 EXPECT_EQ(Align().value(), 1ULL);
33 EXPECT_EQ(Align::None().value(), 1ULL);
36 TEST(AlignmentTest
, MaybeAlignDefaultCTor
) {
37 EXPECT_FALSE(MaybeAlign().hasValue());
40 TEST(AlignmentTest
, ValidCTors
) {
41 for (uint64_t Value
: getValidAlignments()) {
42 EXPECT_EQ(Align(Value
).value(), Value
);
43 EXPECT_EQ((*MaybeAlign(Value
)).value(), Value
);
47 TEST(AlignmentTest
, CompileTimeConstant
) {
48 EXPECT_EQ(Align::Constant
<1>(), Align(1));
49 EXPECT_EQ(Align::Constant
<2>(), Align(2));
50 EXPECT_EQ(Align::Constant
<4>(), Align(4));
51 EXPECT_EQ(Align::Constant
<8>(), Align(8));
52 EXPECT_EQ(Align::Constant
<16>(), Align(16));
53 EXPECT_EQ(Align::Constant
<32>(), Align(32));
54 EXPECT_EQ(Align::Constant
<64>(), Align(64));
57 TEST(AlignmentTest
, CheckMaybeAlignHasValue
) {
58 EXPECT_TRUE(MaybeAlign(1));
59 EXPECT_TRUE(MaybeAlign(1).hasValue());
60 EXPECT_FALSE(MaybeAlign(0));
61 EXPECT_FALSE(MaybeAlign(0).hasValue());
62 EXPECT_FALSE(MaybeAlign());
63 EXPECT_FALSE(MaybeAlign().hasValue());
66 TEST(AlignmentTest
, Division
) {
67 for (uint64_t Value
: getValidAlignments()) {
69 EXPECT_EQ(Align(Value
) / 2, Value
/ 2);
70 EXPECT_EQ(MaybeAlign(Value
) / 2, Value
/ 2);
73 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
76 TEST(AlignmentTest
, AlignTo
) {
100 for (const auto &T
: kTests
) {
101 MaybeAlign
A(T
.alignment
);
103 EXPECT_EQ(alignTo(T
.offset
, A
), T
.rounded
);
106 EXPECT_EQ(alignTo(T
.offset
, A
.getValue()), T
.rounded
);
111 TEST(AlignmentTest
, Log2
) {
112 for (uint64_t Value
: getValidAlignments()) {
113 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
114 EXPECT_EQ(Log2(MaybeAlign(Value
)), Log2_64(Value
));
118 TEST(AlignmentTest
, MinAlign
) {
128 // MaybeAlign / Align
132 for (const auto &T
: kTests
) {
133 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
134 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
136 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
139 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
142 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
147 TEST(AlignmentTest
, Encode_Decode
) {
148 for (uint64_t Value
: getValidAlignments()) {
151 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
152 EXPECT_EQ(Expected
, Actual
);
155 MaybeAlign
Actual(Value
);
156 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
157 EXPECT_EQ(Expected
, Actual
);
160 MaybeAlign
Actual(0);
161 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
162 EXPECT_EQ(Expected
, Actual
);
165 TEST(AlignmentTest
, isAligned
) {
171 // MaybeAlign / Align
172 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
173 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
174 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
176 for (const auto &T
: kTests
) {
177 MaybeAlign
A(T
.alignment
);
179 EXPECT_EQ(isAligned(A
, T
.offset
), T
.isAligned
);
182 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
187 TEST(AlignmentTest
, AlignComparisons
) {
188 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
189 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
190 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
192 const Align
A(ValidAlignments
[I
- 1]);
193 const Align
B(ValidAlignments
[I
]);
203 EXPECT_EQ(A
, A
.value());
204 EXPECT_NE(A
, B
.value());
205 EXPECT_LT(A
, B
.value());
206 EXPECT_GT(B
, A
.value());
207 EXPECT_LE(A
, B
.value());
208 EXPECT_GE(B
, A
.value());
209 EXPECT_LE(A
, A
.value());
210 EXPECT_GE(A
, A
.value());
212 EXPECT_EQ(std::max(A
, B
), B
);
213 EXPECT_EQ(std::min(A
, B
), A
);
215 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
216 const MaybeAlign
MB(ValidAlignments
[I
]);
226 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
227 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
228 EXPECT_LT(MA
, MB
? (*MB
).value() : 0);
229 EXPECT_GT(MB
, MA
? (*MA
).value() : 0);
230 EXPECT_LE(MA
, MB
? (*MB
).value() : 0);
231 EXPECT_GE(MB
, MA
? (*MA
).value() : 0);
232 EXPECT_LE(MA
, MA
? (*MA
).value() : 0);
233 EXPECT_GE(MA
, MA
? (*MA
).value() : 0);
235 EXPECT_EQ(std::max(A
, B
), B
);
236 EXPECT_EQ(std::min(A
, B
), A
);
240 TEST(AlignmentTest
, Max
) {
241 // We introduce std::max here to test ADL.
245 EXPECT_EQ(max(MaybeAlign(), Align(2)), Align(2));
246 EXPECT_EQ(max(Align(2), MaybeAlign()), Align(2));
248 EXPECT_EQ(max(MaybeAlign(1), Align(2)), Align(2));
249 EXPECT_EQ(max(Align(2), MaybeAlign(1)), Align(2));
251 EXPECT_EQ(max(MaybeAlign(2), Align(2)), Align(2));
252 EXPECT_EQ(max(Align(2), MaybeAlign(2)), Align(2));
254 EXPECT_EQ(max(MaybeAlign(4), Align(2)), Align(4));
255 EXPECT_EQ(max(Align(2), MaybeAlign(4)), Align(4));
258 EXPECT_EQ(max(Align(2), Align(4)), Align(4));
259 EXPECT_EQ(max(MaybeAlign(2), MaybeAlign(4)), MaybeAlign(4));
260 EXPECT_EQ(max(MaybeAlign(), MaybeAlign()), MaybeAlign());
263 TEST(AlignmentTest
, AssumeAligned
) {
264 EXPECT_EQ(assumeAligned(0), Align(1));
265 EXPECT_EQ(assumeAligned(0), Align());
266 EXPECT_EQ(assumeAligned(1), Align(1));
267 EXPECT_EQ(assumeAligned(1), Align());
270 // Death tests reply on assert which is disabled in release mode.
273 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
275 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
276 return {1, 1ULL << 31, 1ULL << 63};
279 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
281 TEST(AlignmentDeathTest
, Log2
) {
282 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
285 TEST(AlignmentDeathTest
, CantConvertUnsetMaybe
) {
286 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
289 TEST(AlignmentDeathTest
, Division
) {
290 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
291 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
293 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
294 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
297 TEST(AlignmentDeathTest
, InvalidCTors
) {
298 EXPECT_DEATH((Align(0)), "Value must not be 0");
299 for (uint64_t Value
: getNonPowerOfTwo()) {
300 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
301 EXPECT_DEATH((MaybeAlign(Value
)),
302 "Alignment is neither 0 nor a power of 2");
306 TEST(AlignmentDeathTest
, ComparisonsWithZero
) {
307 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
308 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
309 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
310 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
311 EXPECT_DEATH((void)(Align(Value
) <= 0), ".* should be defined");
312 EXPECT_DEATH((void)(Align(Value
) > 0), ".* should be defined");
313 EXPECT_DEATH((void)(Align(Value
) < 0), ".* should be defined");
317 TEST(AlignmentDeathTest
, CompareMaybeAlignToZero
) {
318 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
319 // MaybeAlign is allowed to be == or != 0
320 (void)(MaybeAlign(Value
) == 0);
321 (void)(MaybeAlign(Value
) != 0);
322 EXPECT_DEATH((void)(MaybeAlign(Value
) >= 0), ".* should be defined");
323 EXPECT_DEATH((void)(MaybeAlign(Value
) <= 0), ".* should be defined");
324 EXPECT_DEATH((void)(MaybeAlign(Value
) > 0), ".* should be defined");
325 EXPECT_DEATH((void)(MaybeAlign(Value
) < 0), ".* should be defined");
329 TEST(AlignmentDeathTest
, CompareAlignToUndefMaybeAlign
) {
330 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
331 EXPECT_DEATH((void)(Align(Value
) == MaybeAlign(0)), ".* should be defined");
332 EXPECT_DEATH((void)(Align(Value
) != MaybeAlign(0)), ".* should be defined");
333 EXPECT_DEATH((void)(Align(Value
) >= MaybeAlign(0)), ".* should be defined");
334 EXPECT_DEATH((void)(Align(Value
) <= MaybeAlign(0)), ".* should be defined");
335 EXPECT_DEATH((void)(Align(Value
) > MaybeAlign(0)), ".* should be defined");
336 EXPECT_DEATH((void)(Align(Value
) < MaybeAlign(0)), ".* should be defined");
342 } // end anonymous namespace