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 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
27 std::vector
<uint64_t> getValidAlignmentsForDeathTest() {
28 return {1, 1ULL << 31, 1ULL << 63};
31 std::vector
<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
33 TEST(Alignment
, AlignDefaultCTor
) { EXPECT_EQ(Align().value(), 1ULL); }
35 TEST(Alignment
, MaybeAlignDefaultCTor
) {
36 EXPECT_FALSE(MaybeAlign().hasValue());
39 TEST(Alignment
, ValidCTors
) {
40 for (size_t Value
: getValidAlignments()) {
41 EXPECT_EQ(Align(Value
).value(), Value
);
42 EXPECT_EQ((*MaybeAlign(Value
)).value(), Value
);
46 TEST(Alignment
, InvalidCTors
) {
47 EXPECT_DEATH((Align(0)), "Value must not be 0");
48 for (size_t Value
: getNonPowerOfTwo()) {
49 EXPECT_DEATH((Align(Value
)), "Alignment is not a power of 2");
50 EXPECT_DEATH((MaybeAlign(Value
)), "Alignment is not 0 or a power of 2");
54 TEST(Alignment
, CheckMaybeAlignHasValue
) {
55 EXPECT_TRUE(MaybeAlign(1));
56 EXPECT_TRUE(MaybeAlign(1).hasValue());
57 EXPECT_FALSE(MaybeAlign(0));
58 EXPECT_FALSE(MaybeAlign(0).hasValue());
59 EXPECT_FALSE(MaybeAlign());
60 EXPECT_FALSE(MaybeAlign().hasValue());
63 TEST(Alignment
, CantConvertUnsetMaybe
) {
64 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
67 TEST(Alignment
, Division
) {
68 for (size_t Value
: getValidAlignments()) {
70 EXPECT_DEATH(Align(Value
) / 2, "Can't halve byte alignment");
71 EXPECT_DEATH(MaybeAlign(Value
) / 2, "Can't halve byte alignment");
73 EXPECT_EQ(Align(Value
) / 2, Value
/ 2);
74 EXPECT_EQ(MaybeAlign(Value
) / 2, Value
/ 2);
77 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
79 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
80 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
83 TEST(Alignment
, AlignTo
) {
107 for (const auto &T
: kTests
) {
108 MaybeAlign
A(T
.alignment
);
110 EXPECT_EQ(alignTo(T
.offset
, A
), T
.rounded
);
113 EXPECT_EQ(alignTo(T
.offset
, A
.getValue()), T
.rounded
);
117 TEST(Alignment
, Log2
) {
118 for (size_t Value
: getValidAlignments()) {
119 EXPECT_EQ(Log2(Align(Value
)), Log2_64(Value
));
120 EXPECT_EQ(Log2(MaybeAlign(Value
)), Log2_64(Value
));
122 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
125 TEST(Alignment
, MinAlign
) {
135 // MaybeAlign / Align
139 for (const auto &T
: kTests
) {
140 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
141 EXPECT_EQ(MinAlign(T
.A
, T
.B
), T
.MinAlign
);
143 EXPECT_EQ(commonAlignment(Align(T
.A
), MaybeAlign(T
.B
)), T
.MinAlign
);
145 EXPECT_EQ(commonAlignment(MaybeAlign(T
.A
), Align(T
.B
)), T
.MinAlign
);
147 EXPECT_EQ(commonAlignment(Align(T
.A
), Align(T
.B
)), T
.MinAlign
);
151 TEST(Alignment
, Encode_Decode
) {
152 for (size_t Value
: getValidAlignments()) {
155 Align Expected
= decodeMaybeAlign(encode(Actual
)).getValue();
156 EXPECT_EQ(Expected
, Actual
);
159 MaybeAlign
Actual(Value
);
160 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
161 EXPECT_EQ(Expected
, Actual
);
164 MaybeAlign
Actual(0);
165 MaybeAlign Expected
= decodeMaybeAlign(encode(Actual
));
166 EXPECT_EQ(Expected
, Actual
);
169 TEST(Alignment
, isAligned
) {
175 // MaybeAlign / Align
176 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
177 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
178 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
180 for (const auto &T
: kTests
) {
181 MaybeAlign
A(T
.alignment
);
183 EXPECT_EQ(isAligned(A
, T
.offset
), T
.isAligned
);
186 EXPECT_EQ(isAligned(A
.getValue(), T
.offset
), T
.isAligned
);
190 TEST(Alignment
, AlignComparisons
) {
191 std::vector
<uint64_t> ValidAlignments
= getValidAlignments();
192 std::sort(ValidAlignments
.begin(), ValidAlignments
.end());
193 for (size_t I
= 1; I
< ValidAlignments
.size(); ++I
) {
195 const Align
A(ValidAlignments
[I
- 1]);
196 const Align
B(ValidAlignments
[I
]);
206 EXPECT_EQ(A
, A
.value());
207 EXPECT_NE(A
, B
.value());
208 EXPECT_LT(A
, B
.value());
209 EXPECT_GT(B
, A
.value());
210 EXPECT_LE(A
, B
.value());
211 EXPECT_GE(B
, A
.value());
212 EXPECT_LE(A
, A
.value());
213 EXPECT_GE(A
, A
.value());
215 EXPECT_EQ(std::max(A
, B
), B
);
216 EXPECT_EQ(std::min(A
, B
), A
);
218 const MaybeAlign
MA(ValidAlignments
[I
- 1]);
219 const MaybeAlign
MB(ValidAlignments
[I
]);
229 EXPECT_EQ(MA
, MA
? (*MA
).value() : 0);
230 EXPECT_NE(MA
, MB
? (*MB
).value() : 0);
231 EXPECT_LT(MA
, MB
? (*MB
).value() : 0);
232 EXPECT_GT(MB
, MA
? (*MA
).value() : 0);
233 EXPECT_LE(MA
, MB
? (*MB
).value() : 0);
234 EXPECT_GE(MB
, MA
? (*MA
).value() : 0);
235 EXPECT_LE(MA
, MA
? (*MA
).value() : 0);
236 EXPECT_GE(MA
, MA
? (*MA
).value() : 0);
238 EXPECT_EQ(std::max(A
, B
), B
);
239 EXPECT_EQ(std::min(A
, B
), A
);
243 TEST(Alignment
, AssumeAligned
) {
244 EXPECT_EQ(assumeAligned(0), Align(1));
245 EXPECT_EQ(assumeAligned(0), Align());
246 EXPECT_EQ(assumeAligned(1), Align(1));
247 EXPECT_EQ(assumeAligned(1), Align());
250 TEST(Alignment
, ComparisonsWithZero
) {
251 for (size_t Value
: getValidAlignmentsForDeathTest()) {
252 EXPECT_DEATH((void)(Align(Value
) == 0), ".* should be defined");
253 EXPECT_DEATH((void)(Align(Value
) != 0), ".* should be defined");
254 EXPECT_DEATH((void)(Align(Value
) >= 0), ".* should be defined");
255 EXPECT_DEATH((void)(Align(Value
) <= 0), ".* should be defined");
256 EXPECT_DEATH((void)(Align(Value
) > 0), ".* should be defined");
257 EXPECT_DEATH((void)(Align(Value
) < 0), ".* should be defined");
261 TEST(Alignment
, CompareMaybeAlignToZero
) {
262 for (size_t Value
: getValidAlignmentsForDeathTest()) {
263 // MaybeAlign is allowed to be == or != 0
264 (void)(MaybeAlign(Value
) == 0);
265 (void)(MaybeAlign(Value
) != 0);
266 EXPECT_DEATH((void)(MaybeAlign(Value
) >= 0), ".* should be defined");
267 EXPECT_DEATH((void)(MaybeAlign(Value
) <= 0), ".* should be defined");
268 EXPECT_DEATH((void)(MaybeAlign(Value
) > 0), ".* should be defined");
269 EXPECT_DEATH((void)(MaybeAlign(Value
) < 0), ".* should be defined");
273 TEST(Alignment
, CompareAlignToUndefMaybeAlign
) {
274 for (size_t Value
: getValidAlignmentsForDeathTest()) {
275 EXPECT_DEATH((void)(Align(Value
) == MaybeAlign(0)), ".* should be defined");
276 EXPECT_DEATH((void)(Align(Value
) != MaybeAlign(0)), ".* should be defined");
277 EXPECT_DEATH((void)(Align(Value
) >= MaybeAlign(0)), ".* should be defined");
278 EXPECT_DEATH((void)(Align(Value
) <= MaybeAlign(0)), ".* should be defined");
279 EXPECT_DEATH((void)(Align(Value
) > MaybeAlign(0)), ".* should be defined");
280 EXPECT_DEATH((void)(Align(Value
) < MaybeAlign(0)), ".* should be defined");
284 } // end anonymous namespace