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 (uint64_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 (uint64_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
);
92 TEST(AlignmentTest
, Log2
) {
93 for (uint64_t Value
: getValidAlignments()) {
94 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
95 EXPECT_EQ(Log2(MaybeAlign(Value
)), Log2_64(Value
));
99 TEST(AlignmentTest
, MinAlign
) {
109 // MaybeAlign / Align
113 for (const auto &T
: kTests
) {
114 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
115 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
117 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
120 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
123 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
128 TEST(AlignmentTest
, Encode_Decode
) {
129 for (uint64_t Value
: getValidAlignments()) {
132 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
133 EXPECT_EQ(Expected
, Actual
);
136 MaybeAlign
Actual(Value
);
137 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
138 EXPECT_EQ(Expected
, Actual
);
141 MaybeAlign
Actual(0);
142 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
143 EXPECT_EQ(Expected
, Actual
);
146 TEST(AlignmentTest
, isAligned
) {
152 // MaybeAlign / Align
153 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
154 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
155 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
157 for (const auto &T
: kTests
) {
158 MaybeAlign
A(T
.alignment
);
160 EXPECT_EQ(isAligned(A
, T
.offset
), T
.isAligned
);
163 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
168 TEST(AlignmentTest
, AlignComparisons
) {
169 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
170 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
171 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
173 const Align
A(ValidAlignments
[I
- 1]);
174 const Align
B(ValidAlignments
[I
]);
184 EXPECT_EQ(A
, A
.value());
185 EXPECT_NE(A
, B
.value());
186 EXPECT_LT(A
, B
.value());
187 EXPECT_GT(B
, A
.value());
188 EXPECT_LE(A
, B
.value());
189 EXPECT_GE(B
, A
.value());
190 EXPECT_LE(A
, A
.value());
191 EXPECT_GE(A
, A
.value());
193 EXPECT_EQ(std::max(A
, B
), B
);
194 EXPECT_EQ(std::min(A
, B
), A
);
196 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
197 const MaybeAlign
MB(ValidAlignments
[I
]);
207 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
208 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
209 EXPECT_LT(MA
, MB
? (*MB
).value() : 0);
210 EXPECT_GT(MB
, MA
? (*MA
).value() : 0);
211 EXPECT_LE(MA
, MB
? (*MB
).value() : 0);
212 EXPECT_GE(MB
, MA
? (*MA
).value() : 0);
213 EXPECT_LE(MA
, MA
? (*MA
).value() : 0);
214 EXPECT_GE(MA
, MA
? (*MA
).value() : 0);
216 EXPECT_EQ(std::max(A
, B
), B
);
217 EXPECT_EQ(std::min(A
, B
), A
);
221 TEST(AlignmentTest
, AssumeAligned
) {
222 EXPECT_EQ(assumeAligned(0), Align(1));
223 EXPECT_EQ(assumeAligned(0), Align());
224 EXPECT_EQ(assumeAligned(1), Align(1));
225 EXPECT_EQ(assumeAligned(1), Align());
228 // Death tests reply on assert which is disabled in release mode.
231 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
233 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
234 return {1, 1ULL << 31, 1ULL << 63};
237 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
239 TEST(AlignmentDeathTest
, Log2
) {
240 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
243 TEST(AlignmentDeathTest
, CantConvertUnsetMaybe
) {
244 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
247 TEST(AlignmentDeathTest
, Division
) {
248 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
249 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
251 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
252 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
255 TEST(AlignmentDeathTest
, InvalidCTors
) {
256 EXPECT_DEATH((Align(0)), "Value must not be 0");
257 for (uint64_t Value
: getNonPowerOfTwo()) {
258 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
259 EXPECT_DEATH((MaybeAlign(Value
)),
260 "Alignment is neither 0 nor a power of 2");
264 TEST(AlignmentDeathTest
, ComparisonsWithZero
) {
265 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
266 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
267 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
268 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
269 EXPECT_DEATH((void)(Align(Value
) <= 0), ".* should be defined");
270 EXPECT_DEATH((void)(Align(Value
) > 0), ".* should be defined");
271 EXPECT_DEATH((void)(Align(Value
) < 0), ".* should be defined");
275 TEST(AlignmentDeathTest
, CompareMaybeAlignToZero
) {
276 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
277 // MaybeAlign is allowed to be == or != 0
278 (void)(MaybeAlign(Value
) == 0);
279 (void)(MaybeAlign(Value
) != 0);
280 EXPECT_DEATH((void)(MaybeAlign(Value
) >= 0), ".* should be defined");
281 EXPECT_DEATH((void)(MaybeAlign(Value
) <= 0), ".* should be defined");
282 EXPECT_DEATH((void)(MaybeAlign(Value
) > 0), ".* should be defined");
283 EXPECT_DEATH((void)(MaybeAlign(Value
) < 0), ".* should be defined");
287 TEST(AlignmentDeathTest
, CompareAlignToUndefMaybeAlign
) {
288 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
289 EXPECT_DEATH((void)(Align(Value
) == MaybeAlign(0)), ".* should be defined");
290 EXPECT_DEATH((void)(Align(Value
) != MaybeAlign(0)), ".* should be defined");
291 EXPECT_DEATH((void)(Align(Value
) >= MaybeAlign(0)), ".* should be defined");
292 EXPECT_DEATH((void)(Align(Value
) <= MaybeAlign(0)), ".* should be defined");
293 EXPECT_DEATH((void)(Align(Value
) > MaybeAlign(0)), ".* should be defined");
294 EXPECT_DEATH((void)(Align(Value
) < MaybeAlign(0)), ".* should be defined");
300 } // end anonymous namespace