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 TEST(AlignmentTest
, AlignOfConstant
) {
25 EXPECT_EQ(Align::Of
<uint8_t>(), Align(alignof(uint8_t)));
26 EXPECT_EQ(Align::Of
<uint16_t>(), Align(alignof(uint16_t)));
27 EXPECT_EQ(Align::Of
<uint32_t>(), Align(alignof(uint32_t)));
28 EXPECT_EQ(Align::Of
<uint64_t>(), Align(alignof(uint64_t)));
31 TEST(AlignmentTest
, AlignConstant
) {
32 EXPECT_EQ(Align::Constant
<1>(), Align(1));
33 EXPECT_EQ(Align::Constant
<2>(), Align(2));
34 EXPECT_EQ(Align::Constant
<4>(), Align(4));
35 EXPECT_EQ(Align::Constant
<8>(), Align(8));
36 EXPECT_EQ(Align::Constant
<16>(), Align(16));
37 EXPECT_EQ(Align::Constant
<32>(), Align(32));
38 EXPECT_EQ(Align::Constant
<64>(), Align(64));
41 TEST(AlignmentTest
, AlignConstexprConstant
) {
42 constexpr Align kConstantAlign
= Align::Of
<uint64_t>();
43 EXPECT_EQ(Align(alignof(uint64_t)), kConstantAlign
);
46 std::vector
<uint64_t> getValidAlignments() {
47 std::vector
<uint64_t> Out
;
48 for (size_t Shift
= 0; Shift
< 64; ++Shift
)
49 Out
.push_back(1ULL << Shift
);
53 TEST(AlignmentTest
, AlignDefaultCTor
) {
54 EXPECT_EQ(Align().value(), 1ULL);
57 TEST(AlignmentTest
, MaybeAlignDefaultCTor
) {
58 EXPECT_FALSE(MaybeAlign().hasValue());
61 TEST(AlignmentTest
, ValidCTors
) {
62 for (uint64_t Value
: getValidAlignments()) {
63 EXPECT_EQ(Align(Value
).value(), Value
);
64 EXPECT_EQ((*MaybeAlign(Value
)).value(), Value
);
68 TEST(AlignmentTest
, CheckMaybeAlignHasValue
) {
69 EXPECT_TRUE(MaybeAlign(1));
70 EXPECT_TRUE(MaybeAlign(1).hasValue());
71 EXPECT_FALSE(MaybeAlign(0));
72 EXPECT_FALSE(MaybeAlign(0).hasValue());
73 EXPECT_FALSE(MaybeAlign());
74 EXPECT_FALSE(MaybeAlign().hasValue());
77 TEST(AlignmentTest
, Division
) {
78 for (uint64_t Value
: getValidAlignments()) {
80 EXPECT_EQ(Align(Value
) / 2, Value
/ 2);
81 EXPECT_EQ(MaybeAlign(Value
) / 2, Value
/ 2);
84 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
87 TEST(AlignmentTest
, AlignTo
) {
92 const void *forgedAddr() const {
93 // A value of any integral or enumeration type can be converted to a
95 return reinterpret_cast<const void *>(offset
);
102 // MaybeAlign / Align
116 for (const auto &T
: kTests
) {
117 MaybeAlign
A(T
.alignment
);
119 EXPECT_EQ(alignTo(T
.offset
, A
), T
.rounded
);
122 EXPECT_EQ(alignTo(T
.offset
, A
.getValue()), T
.rounded
);
123 EXPECT_EQ(alignAddr(T
.forgedAddr(), A
.getValue()), T
.rounded
);
128 TEST(AlignmentTest
, AlignToWithSkew
) {
129 EXPECT_EQ(alignTo(5, Align(8), 0), alignTo(5, Align(8)));
130 EXPECT_EQ(alignTo(5, Align(8), 7), 7U);
131 EXPECT_EQ(alignTo(17, Align(8), 1), 17U);
132 EXPECT_EQ(alignTo(~0LL, Align(8), 3), 3U);
135 TEST(AlignmentTest
, Log2
) {
136 for (uint64_t Value
: getValidAlignments()) {
137 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
141 TEST(AlignmentTest
, MinAlign
) {
151 // MaybeAlign / Align
155 for (const auto &T
: kTests
) {
156 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
157 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
159 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
162 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
165 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
170 TEST(AlignmentTest
, Encode_Decode
) {
171 for (uint64_t Value
: getValidAlignments()) {
174 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
175 EXPECT_EQ(Expected
, Actual
);
178 MaybeAlign
Actual(Value
);
179 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
180 EXPECT_EQ(Expected
, Actual
);
183 MaybeAlign
Actual(0);
184 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
185 EXPECT_EQ(Expected
, Actual
);
188 TEST(AlignmentTest
, isAligned_isAddrAligned
) {
193 const void *forgedAddr() const {
194 // A value of any integral or enumeration type can be converted to a
196 return reinterpret_cast<const void *>(offset
);
199 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
200 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
201 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
203 for (const auto &T
: kTests
) {
204 MaybeAlign
A(T
.alignment
);
207 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
208 EXPECT_EQ(isAddrAligned(A
.getValue(), T
.forgedAddr()), T
.isAligned
);
213 TEST(AlignmentTest
, offsetToAlignment
) {
217 uint64_t alignedOffset
;
218 const void *forgedAddr() const {
219 // A value of any integral or enumeration type can be converted to a
221 return reinterpret_cast<const void *>(offset
);
224 {1, 0, 0}, {1, 1, 0}, {1, 5, 0}, {2, 0, 0}, {2, 1, 1}, {2, 2, 0},
225 {2, 7, 1}, {2, 16, 0}, {4, 0, 0}, {4, 1, 3}, {4, 4, 0}, {4, 6, 2},
227 for (const auto &T
: kTests
) {
228 const Align
A(T
.alignment
);
229 EXPECT_EQ(offsetToAlignment(T
.offset
, A
), T
.alignedOffset
);
230 EXPECT_EQ(offsetToAlignedAddr(T
.forgedAddr(), A
), T
.alignedOffset
);
234 TEST(AlignmentTest
, AlignComparisons
) {
235 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
236 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
237 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
239 const Align
A(ValidAlignments
[I
- 1]);
240 const Align
B(ValidAlignments
[I
]);
250 EXPECT_EQ(A
, A
.value());
251 EXPECT_NE(A
, B
.value());
252 EXPECT_LT(A
, B
.value());
253 EXPECT_GT(B
, A
.value());
254 EXPECT_LE(A
, B
.value());
255 EXPECT_GE(B
, A
.value());
256 EXPECT_LE(A
, A
.value());
257 EXPECT_GE(A
, A
.value());
259 EXPECT_EQ(std::max(A
, B
), B
);
260 EXPECT_EQ(std::min(A
, B
), A
);
262 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
263 const MaybeAlign
MB(ValidAlignments
[I
]);
267 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
268 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
270 EXPECT_EQ(std::max(A
, B
), B
);
271 EXPECT_EQ(std::min(A
, B
), A
);
275 TEST(AlignmentTest
, Max
) {
276 // We introduce std::max here to test ADL.
280 EXPECT_EQ(max(MaybeAlign(), Align(2)), Align(2));
281 EXPECT_EQ(max(Align(2), MaybeAlign()), Align(2));
283 EXPECT_EQ(max(MaybeAlign(1), Align(2)), Align(2));
284 EXPECT_EQ(max(Align(2), MaybeAlign(1)), Align(2));
286 EXPECT_EQ(max(MaybeAlign(2), Align(2)), Align(2));
287 EXPECT_EQ(max(Align(2), MaybeAlign(2)), Align(2));
289 EXPECT_EQ(max(MaybeAlign(4), Align(2)), Align(4));
290 EXPECT_EQ(max(Align(2), MaybeAlign(4)), Align(4));
293 EXPECT_EQ(max(Align(2), Align(4)), Align(4));
296 TEST(AlignmentTest
, AssumeAligned
) {
297 EXPECT_EQ(assumeAligned(0), Align(1));
298 EXPECT_EQ(assumeAligned(0), Align());
299 EXPECT_EQ(assumeAligned(1), Align(1));
300 EXPECT_EQ(assumeAligned(1), Align());
303 // Death tests reply on assert which is disabled in release mode.
306 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
308 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
309 return {1, 1ULL << 31, 1ULL << 63};
312 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
314 TEST(AlignmentDeathTest
, CantConvertUnsetMaybe
) {
315 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
318 TEST(AlignmentDeathTest
, Division
) {
319 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
320 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
322 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
323 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
326 TEST(AlignmentDeathTest
, InvalidCTors
) {
327 EXPECT_DEATH((Align(0)), "Value must not be 0");
328 for (uint64_t Value
: getNonPowerOfTwo()) {
329 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
330 EXPECT_DEATH((MaybeAlign(Value
)),
331 "Alignment is neither 0 nor a power of 2");
335 TEST(AlignmentDeathTest
, ComparisonsWithZero
) {
336 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
337 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
338 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
339 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
340 EXPECT_DEATH((void)(Align(Value
) <= 0), ".* should be defined");
341 EXPECT_DEATH((void)(Align(Value
) > 0), ".* should be defined");
342 EXPECT_DEATH((void)(Align(Value
) < 0), ".* should be defined");
346 TEST(AlignmentDeathTest
, CompareMaybeAlignToZero
) {
347 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
348 // MaybeAlign is allowed to be == or != 0
349 (void)(MaybeAlign(Value
) == 0);
350 (void)(MaybeAlign(Value
) != 0);
354 TEST(AlignmentDeathTest
, AlignAddr
) {
355 const void *const unaligned_high_ptr
=
356 reinterpret_cast<const void *>(std::numeric_limits
<uintptr_t>::max() - 1);
357 EXPECT_DEATH(alignAddr(unaligned_high_ptr
, Align(16)), "Overflow");
362 } // end anonymous namespace