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
, CheckMaybeAlignHasValue
) {
48 EXPECT_TRUE(MaybeAlign(1));
49 EXPECT_TRUE(MaybeAlign(1).hasValue());
50 EXPECT_FALSE(MaybeAlign(0));
51 EXPECT_FALSE(MaybeAlign(0).hasValue());
52 EXPECT_FALSE(MaybeAlign());
53 EXPECT_FALSE(MaybeAlign().hasValue());
56 TEST(AlignmentTest
, Division
) {
57 for (uint64_t Value
: getValidAlignments()) {
59 EXPECT_EQ(Align(Value
) / 2, Value
/ 2);
60 EXPECT_EQ(MaybeAlign(Value
) / 2, Value
/ 2);
63 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
66 TEST(AlignmentTest
, AlignTo
) {
90 for (const auto &T
: kTests
) {
91 MaybeAlign
A(T
.alignment
);
93 EXPECT_EQ(alignTo(T
.offset
, A
), T
.rounded
);
96 EXPECT_EQ(alignTo(T
.offset
, A
.getValue()), T
.rounded
);
101 TEST(AlignmentTest
, Log2
) {
102 for (uint64_t Value
: getValidAlignments()) {
103 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
104 EXPECT_EQ(Log2(MaybeAlign(Value
)), Log2_64(Value
));
108 TEST(AlignmentTest
, MinAlign
) {
118 // MaybeAlign / Align
122 for (const auto &T
: kTests
) {
123 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
124 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
126 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
129 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
132 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
137 TEST(AlignmentTest
, Encode_Decode
) {
138 for (uint64_t Value
: getValidAlignments()) {
141 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
142 EXPECT_EQ(Expected
, Actual
);
145 MaybeAlign
Actual(Value
);
146 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
147 EXPECT_EQ(Expected
, Actual
);
150 MaybeAlign
Actual(0);
151 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
152 EXPECT_EQ(Expected
, Actual
);
155 TEST(AlignmentTest
, isAligned
) {
161 // MaybeAlign / Align
162 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
163 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
164 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
166 for (const auto &T
: kTests
) {
167 MaybeAlign
A(T
.alignment
);
169 EXPECT_EQ(isAligned(A
, T
.offset
), T
.isAligned
);
172 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
177 TEST(AlignmentTest
, AlignComparisons
) {
178 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
179 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
180 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
182 const Align
A(ValidAlignments
[I
- 1]);
183 const Align
B(ValidAlignments
[I
]);
193 EXPECT_EQ(A
, A
.value());
194 EXPECT_NE(A
, B
.value());
195 EXPECT_LT(A
, B
.value());
196 EXPECT_GT(B
, A
.value());
197 EXPECT_LE(A
, B
.value());
198 EXPECT_GE(B
, A
.value());
199 EXPECT_LE(A
, A
.value());
200 EXPECT_GE(A
, A
.value());
202 EXPECT_EQ(std::max(A
, B
), B
);
203 EXPECT_EQ(std::min(A
, B
), A
);
205 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
206 const MaybeAlign
MB(ValidAlignments
[I
]);
216 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
217 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
218 EXPECT_LT(MA
, MB
? (*MB
).value() : 0);
219 EXPECT_GT(MB
, MA
? (*MA
).value() : 0);
220 EXPECT_LE(MA
, MB
? (*MB
).value() : 0);
221 EXPECT_GE(MB
, MA
? (*MA
).value() : 0);
222 EXPECT_LE(MA
, MA
? (*MA
).value() : 0);
223 EXPECT_GE(MA
, MA
? (*MA
).value() : 0);
225 EXPECT_EQ(std::max(A
, B
), B
);
226 EXPECT_EQ(std::min(A
, B
), A
);
230 TEST(AlignmentTest
, AssumeAligned
) {
231 EXPECT_EQ(assumeAligned(0), Align(1));
232 EXPECT_EQ(assumeAligned(0), Align());
233 EXPECT_EQ(assumeAligned(1), Align(1));
234 EXPECT_EQ(assumeAligned(1), Align());
237 // Death tests reply on assert which is disabled in release mode.
240 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
242 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
243 return {1, 1ULL << 31, 1ULL << 63};
246 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
248 TEST(AlignmentDeathTest
, Log2
) {
249 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
252 TEST(AlignmentDeathTest
, CantConvertUnsetMaybe
) {
253 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
256 TEST(AlignmentDeathTest
, Division
) {
257 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
258 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
260 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
261 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
264 TEST(AlignmentDeathTest
, InvalidCTors
) {
265 EXPECT_DEATH((Align(0)), "Value must not be 0");
266 for (uint64_t Value
: getNonPowerOfTwo()) {
267 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
268 EXPECT_DEATH((MaybeAlign(Value
)),
269 "Alignment is neither 0 nor a power of 2");
273 TEST(AlignmentDeathTest
, ComparisonsWithZero
) {
274 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
275 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
276 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
277 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
278 EXPECT_DEATH((void)(Align(Value
) <= 0), ".* should be defined");
279 EXPECT_DEATH((void)(Align(Value
) > 0), ".* should be defined");
280 EXPECT_DEATH((void)(Align(Value
) < 0), ".* should be defined");
284 TEST(AlignmentDeathTest
, CompareMaybeAlignToZero
) {
285 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
286 // MaybeAlign is allowed to be == or != 0
287 (void)(MaybeAlign(Value
) == 0);
288 (void)(MaybeAlign(Value
) != 0);
289 EXPECT_DEATH((void)(MaybeAlign(Value
) >= 0), ".* should be defined");
290 EXPECT_DEATH((void)(MaybeAlign(Value
) <= 0), ".* should be defined");
291 EXPECT_DEATH((void)(MaybeAlign(Value
) > 0), ".* should be defined");
292 EXPECT_DEATH((void)(MaybeAlign(Value
) < 0), ".* should be defined");
296 TEST(AlignmentDeathTest
, CompareAlignToUndefMaybeAlign
) {
297 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
298 EXPECT_DEATH((void)(Align(Value
) == MaybeAlign(0)), ".* should be defined");
299 EXPECT_DEATH((void)(Align(Value
) != MaybeAlign(0)), ".* should be defined");
300 EXPECT_DEATH((void)(Align(Value
) >= MaybeAlign(0)), ".* should be defined");
301 EXPECT_DEATH((void)(Align(Value
) <= MaybeAlign(0)), ".* should be defined");
302 EXPECT_DEATH((void)(Align(Value
) > MaybeAlign(0)), ".* should be defined");
303 EXPECT_DEATH((void)(Align(Value
) < MaybeAlign(0)), ".* should be defined");
309 } // end anonymous namespace