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
) { EXPECT_EQ(Align().value(), 1ULL); }
33 TEST(AlignmentTest
, MaybeAlignDefaultCTor
) {
34 EXPECT_FALSE(MaybeAlign().hasValue());
37 TEST(AlignmentTest
, ValidCTors
) {
38 for (uint64_t Value
: getValidAlignments()) {
39 EXPECT_EQ(Align(Value
).value(), Value
);
40 EXPECT_EQ((*MaybeAlign(Value
)).value(), Value
);
44 TEST(AlignmentTest
, CheckMaybeAlignHasValue
) {
45 EXPECT_TRUE(MaybeAlign(1));
46 EXPECT_TRUE(MaybeAlign(1).hasValue());
47 EXPECT_FALSE(MaybeAlign(0));
48 EXPECT_FALSE(MaybeAlign(0).hasValue());
49 EXPECT_FALSE(MaybeAlign());
50 EXPECT_FALSE(MaybeAlign().hasValue());
53 TEST(AlignmentTest
, Division
) {
54 for (uint64_t Value
: getValidAlignments()) {
56 EXPECT_EQ(Align(Value
) / 2, Value
/ 2);
57 EXPECT_EQ(MaybeAlign(Value
) / 2, Value
/ 2);
60 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
63 TEST(AlignmentTest
, AlignTo
) {
87 for (const auto &T
: kTests
) {
88 MaybeAlign
A(T
.alignment
);
90 EXPECT_EQ(alignTo(T
.offset
, A
), T
.rounded
);
93 EXPECT_EQ(alignTo(T
.offset
, A
.getValue()), T
.rounded
);
98 TEST(AlignmentTest
, Log2
) {
99 for (uint64_t Value
: getValidAlignments()) {
100 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
101 EXPECT_EQ(Log2(MaybeAlign(Value
)), Log2_64(Value
));
105 TEST(AlignmentTest
, MinAlign
) {
115 // MaybeAlign / Align
119 for (const auto &T
: kTests
) {
120 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
121 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
123 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
126 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
129 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
134 TEST(AlignmentTest
, Encode_Decode
) {
135 for (uint64_t Value
: getValidAlignments()) {
138 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
139 EXPECT_EQ(Expected
, Actual
);
142 MaybeAlign
Actual(Value
);
143 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
144 EXPECT_EQ(Expected
, Actual
);
147 MaybeAlign
Actual(0);
148 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
149 EXPECT_EQ(Expected
, Actual
);
152 TEST(AlignmentTest
, isAligned
) {
158 // MaybeAlign / Align
159 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
160 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
161 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
163 for (const auto &T
: kTests
) {
164 MaybeAlign
A(T
.alignment
);
166 EXPECT_EQ(isAligned(A
, T
.offset
), T
.isAligned
);
169 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
174 TEST(AlignmentTest
, AlignComparisons
) {
175 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
176 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
177 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
179 const Align
A(ValidAlignments
[I
- 1]);
180 const Align
B(ValidAlignments
[I
]);
190 EXPECT_EQ(A
, A
.value());
191 EXPECT_NE(A
, B
.value());
192 EXPECT_LT(A
, B
.value());
193 EXPECT_GT(B
, A
.value());
194 EXPECT_LE(A
, B
.value());
195 EXPECT_GE(B
, A
.value());
196 EXPECT_LE(A
, A
.value());
197 EXPECT_GE(A
, A
.value());
199 EXPECT_EQ(std::max(A
, B
), B
);
200 EXPECT_EQ(std::min(A
, B
), A
);
202 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
203 const MaybeAlign
MB(ValidAlignments
[I
]);
213 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
214 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
215 EXPECT_LT(MA
, MB
? (*MB
).value() : 0);
216 EXPECT_GT(MB
, MA
? (*MA
).value() : 0);
217 EXPECT_LE(MA
, MB
? (*MB
).value() : 0);
218 EXPECT_GE(MB
, MA
? (*MA
).value() : 0);
219 EXPECT_LE(MA
, MA
? (*MA
).value() : 0);
220 EXPECT_GE(MA
, MA
? (*MA
).value() : 0);
222 EXPECT_EQ(std::max(A
, B
), B
);
223 EXPECT_EQ(std::min(A
, B
), A
);
227 TEST(AlignmentTest
, AssumeAligned
) {
228 EXPECT_EQ(assumeAligned(0), Align(1));
229 EXPECT_EQ(assumeAligned(0), Align());
230 EXPECT_EQ(assumeAligned(1), Align(1));
231 EXPECT_EQ(assumeAligned(1), Align());
234 // Death tests reply on assert which is disabled in release mode.
237 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
239 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
240 return {1, 1ULL << 31, 1ULL << 63};
243 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
245 TEST(AlignmentDeathTest
, Log2
) {
246 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
249 TEST(AlignmentDeathTest
, CantConvertUnsetMaybe
) {
250 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
253 TEST(AlignmentDeathTest
, Division
) {
254 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
255 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
257 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
258 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
261 TEST(AlignmentDeathTest
, InvalidCTors
) {
262 EXPECT_DEATH((Align(0)), "Value must not be 0");
263 for (uint64_t Value
: getNonPowerOfTwo()) {
264 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
265 EXPECT_DEATH((MaybeAlign(Value
)),
266 "Alignment is neither 0 nor a power of 2");
270 TEST(AlignmentDeathTest
, ComparisonsWithZero
) {
271 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
272 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
273 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
274 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
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");
281 TEST(AlignmentDeathTest
, CompareMaybeAlignToZero
) {
282 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
283 // MaybeAlign is allowed to be == or != 0
284 (void)(MaybeAlign(Value
) == 0);
285 (void)(MaybeAlign(Value
) != 0);
286 EXPECT_DEATH((void)(MaybeAlign(Value
) >= 0), ".* should be defined");
287 EXPECT_DEATH((void)(MaybeAlign(Value
) <= 0), ".* should be defined");
288 EXPECT_DEATH((void)(MaybeAlign(Value
) > 0), ".* should be defined");
289 EXPECT_DEATH((void)(MaybeAlign(Value
) < 0), ".* should be defined");
293 TEST(AlignmentDeathTest
, CompareAlignToUndefMaybeAlign
) {
294 for (uint64_t Value
: getValidAlignmentsForDeathTest()) {
295 EXPECT_DEATH((void)(Align(Value
) == MaybeAlign(0)), ".* should be defined");
296 EXPECT_DEATH((void)(Align(Value
) != MaybeAlign(0)), ".* should be defined");
297 EXPECT_DEATH((void)(Align(Value
) >= MaybeAlign(0)), ".* should be defined");
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");
306 } // end anonymous namespace