[Alignment][NFC] Allow constexpr Align
[llvm-core.git] / unittests / Support / AlignmentTest.cpp
blob3d35a612b8325d0f969e6f6a0e4e65f8203cea30
1 //=== - llvm/unittest/Support/Alignment.cpp - Alignment utility tests -----===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Alignment.h"
10 #include "gtest/gtest.h"
12 #include <vector>
14 #ifdef _MSC_VER
15 // Disable warnings about potential divide by 0.
16 #pragma warning(push)
17 #pragma warning(disable : 4723)
18 #endif
20 using namespace llvm;
22 namespace {
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);
28 return Out;
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, CompileTimeConstant) {
48 EXPECT_EQ(Align::Constant<1>(), Align(1));
49 EXPECT_EQ(Align::Constant<2>(), Align(2));
50 EXPECT_EQ(Align::Constant<4>(), Align(4));
51 EXPECT_EQ(Align::Constant<8>(), Align(8));
52 EXPECT_EQ(Align::Constant<16>(), Align(16));
53 EXPECT_EQ(Align::Constant<32>(), Align(32));
54 EXPECT_EQ(Align::Constant<64>(), Align(64));
57 TEST(AlignmentTest, CheckMaybeAlignHasValue) {
58 EXPECT_TRUE(MaybeAlign(1));
59 EXPECT_TRUE(MaybeAlign(1).hasValue());
60 EXPECT_FALSE(MaybeAlign(0));
61 EXPECT_FALSE(MaybeAlign(0).hasValue());
62 EXPECT_FALSE(MaybeAlign());
63 EXPECT_FALSE(MaybeAlign().hasValue());
66 TEST(AlignmentTest, Division) {
67 for (uint64_t Value : getValidAlignments()) {
68 if (Value > 1) {
69 EXPECT_EQ(Align(Value) / 2, Value / 2);
70 EXPECT_EQ(MaybeAlign(Value) / 2, Value / 2);
73 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
76 TEST(AlignmentTest, AlignTo) {
77 struct {
78 uint64_t alignment;
79 uint64_t offset;
80 uint64_t rounded;
81 } kTests[] = {
82 // MaybeAlign
83 {0, 0, 0},
84 {0, 1, 1},
85 {0, 5, 5},
86 // MaybeAlign / Align
87 {1, 0, 0},
88 {1, 1, 1},
89 {1, 5, 5},
90 {2, 0, 0},
91 {2, 1, 2},
92 {2, 2, 2},
93 {2, 7, 8},
94 {2, 16, 16},
95 {4, 0, 0},
96 {4, 1, 4},
97 {4, 4, 4},
98 {4, 6, 8},
100 for (const auto &T : kTests) {
101 MaybeAlign A(T.alignment);
102 // Test MaybeAlign
103 EXPECT_EQ(alignTo(T.offset, A), T.rounded);
104 // Test Align
105 if (A) {
106 EXPECT_EQ(alignTo(T.offset, A.getValue()), T.rounded);
111 TEST(AlignmentTest, Log2) {
112 for (uint64_t Value : getValidAlignments()) {
113 EXPECT_EQ(Log2(Align(Value)), Log2_64(Value));
114 EXPECT_EQ(Log2(MaybeAlign(Value)), Log2_64(Value));
118 TEST(AlignmentTest, MinAlign) {
119 struct {
120 uint64_t A;
121 uint64_t B;
122 uint64_t MinAlign;
123 } kTests[] = {
124 // MaybeAlign
125 {0, 0, 0},
126 {0, 8, 8},
127 {2, 0, 2},
128 // MaybeAlign / Align
129 {1, 2, 1},
130 {8, 4, 4},
132 for (const auto &T : kTests) {
133 EXPECT_EQ(commonAlignment(MaybeAlign(T.A), MaybeAlign(T.B)), T.MinAlign);
134 EXPECT_EQ(MinAlign(T.A, T.B), T.MinAlign);
135 if (T.A) {
136 EXPECT_EQ(commonAlignment(Align(T.A), MaybeAlign(T.B)), T.MinAlign);
138 if (T.B) {
139 EXPECT_EQ(commonAlignment(MaybeAlign(T.A), Align(T.B)), T.MinAlign);
141 if (T.A && T.B) {
142 EXPECT_EQ(commonAlignment(Align(T.A), Align(T.B)), T.MinAlign);
147 TEST(AlignmentTest, Encode_Decode) {
148 for (uint64_t Value : getValidAlignments()) {
150 Align Actual(Value);
151 Align Expected = decodeMaybeAlign(encode(Actual)).getValue();
152 EXPECT_EQ(Expected, Actual);
155 MaybeAlign Actual(Value);
156 MaybeAlign Expected = decodeMaybeAlign(encode(Actual));
157 EXPECT_EQ(Expected, Actual);
160 MaybeAlign Actual(0);
161 MaybeAlign Expected = decodeMaybeAlign(encode(Actual));
162 EXPECT_EQ(Expected, Actual);
165 TEST(AlignmentTest, isAligned) {
166 struct {
167 uint64_t alignment;
168 uint64_t offset;
169 bool isAligned;
170 } kTests[] = {
171 // MaybeAlign / Align
172 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
173 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
174 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
176 for (const auto &T : kTests) {
177 MaybeAlign A(T.alignment);
178 // Test MaybeAlign
179 EXPECT_EQ(isAligned(A, T.offset), T.isAligned);
180 // Test Align
181 if (A) {
182 EXPECT_EQ(isAligned(A.getValue(), T.offset), T.isAligned);
187 TEST(AlignmentTest, AlignComparisons) {
188 std::vector<uint64_t> ValidAlignments = getValidAlignments();
189 std::sort(ValidAlignments.begin(), ValidAlignments.end());
190 for (size_t I = 1; I < ValidAlignments.size(); ++I) {
191 assert(I >= 1);
192 const Align A(ValidAlignments[I - 1]);
193 const Align B(ValidAlignments[I]);
194 EXPECT_EQ(A, A);
195 EXPECT_NE(A, B);
196 EXPECT_LT(A, B);
197 EXPECT_GT(B, A);
198 EXPECT_LE(A, B);
199 EXPECT_GE(B, A);
200 EXPECT_LE(A, A);
201 EXPECT_GE(A, A);
203 EXPECT_EQ(A, A.value());
204 EXPECT_NE(A, B.value());
205 EXPECT_LT(A, B.value());
206 EXPECT_GT(B, A.value());
207 EXPECT_LE(A, B.value());
208 EXPECT_GE(B, A.value());
209 EXPECT_LE(A, A.value());
210 EXPECT_GE(A, A.value());
212 EXPECT_EQ(std::max(A, B), B);
213 EXPECT_EQ(std::min(A, B), A);
215 const MaybeAlign MA(ValidAlignments[I - 1]);
216 const MaybeAlign MB(ValidAlignments[I]);
217 EXPECT_EQ(MA, MA);
218 EXPECT_NE(MA, MB);
219 EXPECT_LT(MA, MB);
220 EXPECT_GT(MB, MA);
221 EXPECT_LE(MA, MB);
222 EXPECT_GE(MB, MA);
223 EXPECT_LE(MA, MA);
224 EXPECT_GE(MA, MA);
226 EXPECT_EQ(MA, MA ? (*MA).value() : 0);
227 EXPECT_NE(MA, MB ? (*MB).value() : 0);
228 EXPECT_LT(MA, MB ? (*MB).value() : 0);
229 EXPECT_GT(MB, MA ? (*MA).value() : 0);
230 EXPECT_LE(MA, MB ? (*MB).value() : 0);
231 EXPECT_GE(MB, MA ? (*MA).value() : 0);
232 EXPECT_LE(MA, MA ? (*MA).value() : 0);
233 EXPECT_GE(MA, MA ? (*MA).value() : 0);
235 EXPECT_EQ(std::max(A, B), B);
236 EXPECT_EQ(std::min(A, B), A);
240 TEST(AlignmentTest, Max) {
241 // We introduce std::max here to test ADL.
242 using std::max;
244 // Uses llvm::max.
245 EXPECT_EQ(max(MaybeAlign(), Align(2)), Align(2));
246 EXPECT_EQ(max(Align(2), MaybeAlign()), Align(2));
248 EXPECT_EQ(max(MaybeAlign(1), Align(2)), Align(2));
249 EXPECT_EQ(max(Align(2), MaybeAlign(1)), Align(2));
251 EXPECT_EQ(max(MaybeAlign(2), Align(2)), Align(2));
252 EXPECT_EQ(max(Align(2), MaybeAlign(2)), Align(2));
254 EXPECT_EQ(max(MaybeAlign(4), Align(2)), Align(4));
255 EXPECT_EQ(max(Align(2), MaybeAlign(4)), Align(4));
257 // Uses std::max.
258 EXPECT_EQ(max(Align(2), Align(4)), Align(4));
259 EXPECT_EQ(max(MaybeAlign(2), MaybeAlign(4)), MaybeAlign(4));
260 EXPECT_EQ(max(MaybeAlign(), MaybeAlign()), MaybeAlign());
263 TEST(AlignmentTest, AssumeAligned) {
264 EXPECT_EQ(assumeAligned(0), Align(1));
265 EXPECT_EQ(assumeAligned(0), Align());
266 EXPECT_EQ(assumeAligned(1), Align(1));
267 EXPECT_EQ(assumeAligned(1), Align());
270 // Death tests reply on assert which is disabled in release mode.
271 #ifndef NDEBUG
273 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
274 // slow.
275 std::vector<uint64_t> getValidAlignmentsForDeathTest() {
276 return {1, 1ULL << 31, 1ULL << 63};
279 std::vector<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
281 TEST(AlignmentDeathTest, Log2) {
282 EXPECT_DEATH(Log2(MaybeAlign(0)), ".* should be defined");
285 TEST(AlignmentDeathTest, CantConvertUnsetMaybe) {
286 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
289 TEST(AlignmentDeathTest, Division) {
290 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
291 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
293 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
294 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
297 TEST(AlignmentDeathTest, InvalidCTors) {
298 EXPECT_DEATH((Align(0)), "Value must not be 0");
299 for (uint64_t Value : getNonPowerOfTwo()) {
300 EXPECT_DEATH((Align(Value)), "Alignment is not a power of 2");
301 EXPECT_DEATH((MaybeAlign(Value)),
302 "Alignment is neither 0 nor a power of 2");
306 TEST(AlignmentDeathTest, ComparisonsWithZero) {
307 for (uint64_t Value : getValidAlignmentsForDeathTest()) {
308 EXPECT_DEATH((void)(Align(Value) == 0), ".* should be defined");
309 EXPECT_DEATH((void)(Align(Value) != 0), ".* should be defined");
310 EXPECT_DEATH((void)(Align(Value) >= 0), ".* should be defined");
311 EXPECT_DEATH((void)(Align(Value) <= 0), ".* should be defined");
312 EXPECT_DEATH((void)(Align(Value) > 0), ".* should be defined");
313 EXPECT_DEATH((void)(Align(Value) < 0), ".* should be defined");
317 TEST(AlignmentDeathTest, CompareMaybeAlignToZero) {
318 for (uint64_t Value : getValidAlignmentsForDeathTest()) {
319 // MaybeAlign is allowed to be == or != 0
320 (void)(MaybeAlign(Value) == 0);
321 (void)(MaybeAlign(Value) != 0);
322 EXPECT_DEATH((void)(MaybeAlign(Value) >= 0), ".* should be defined");
323 EXPECT_DEATH((void)(MaybeAlign(Value) <= 0), ".* should be defined");
324 EXPECT_DEATH((void)(MaybeAlign(Value) > 0), ".* should be defined");
325 EXPECT_DEATH((void)(MaybeAlign(Value) < 0), ".* should be defined");
329 TEST(AlignmentDeathTest, CompareAlignToUndefMaybeAlign) {
330 for (uint64_t Value : getValidAlignmentsForDeathTest()) {
331 EXPECT_DEATH((void)(Align(Value) == MaybeAlign(0)), ".* should be defined");
332 EXPECT_DEATH((void)(Align(Value) != MaybeAlign(0)), ".* should be defined");
333 EXPECT_DEATH((void)(Align(Value) >= MaybeAlign(0)), ".* should be defined");
334 EXPECT_DEATH((void)(Align(Value) <= MaybeAlign(0)), ".* should be defined");
335 EXPECT_DEATH((void)(Align(Value) > MaybeAlign(0)), ".* should be defined");
336 EXPECT_DEATH((void)(Align(Value) < MaybeAlign(0)), ".* should be defined");
340 #endif // NDEBUG
342 } // end anonymous namespace
344 #ifdef _MSC_VER
345 #pragma warning(pop)
346 #endif