[LLVM][Alignment] Introduce Alignment In GlobalObject
[llvm-complete.git] / unittests / Support / AlignmentTest.cpp
blob83d6a727721b122d0b8e6d42501496ef61a42984
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 using namespace llvm;
16 namespace {
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);
22 return Out;
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()) {
49 if (Value > 1) {
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) {
58 struct {
59 uint64_t alignment;
60 uint64_t offset;
61 uint64_t rounded;
62 } kTests[] = {
63 // MaybeAlign
64 {0, 0, 0},
65 {0, 1, 1},
66 {0, 5, 5},
67 // MaybeAlign / Align
68 {1, 0, 0},
69 {1, 1, 1},
70 {1, 5, 5},
71 {2, 0, 0},
72 {2, 1, 2},
73 {2, 2, 2},
74 {2, 7, 8},
75 {2, 16, 16},
76 {4, 0, 0},
77 {4, 1, 4},
78 {4, 4, 4},
79 {4, 6, 8},
81 for (const auto &T : kTests) {
82 MaybeAlign A(T.alignment);
83 // Test MaybeAlign
84 EXPECT_EQ(alignTo(T.offset, A), T.rounded);
85 // Test Align
86 if (A) {
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) {
100 struct {
101 uint64_t A;
102 uint64_t B;
103 uint64_t MinAlign;
104 } kTests[] = {
105 // MaybeAlign
106 {0, 0, 0},
107 {0, 8, 8},
108 {2, 0, 2},
109 // MaybeAlign / Align
110 {1, 2, 1},
111 {8, 4, 4},
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);
116 if (T.A) {
117 EXPECT_EQ(commonAlignment(Align(T.A), MaybeAlign(T.B)), T.MinAlign);
119 if (T.B) {
120 EXPECT_EQ(commonAlignment(MaybeAlign(T.A), Align(T.B)), T.MinAlign);
122 if (T.A && T.B) {
123 EXPECT_EQ(commonAlignment(Align(T.A), Align(T.B)), T.MinAlign);
128 TEST(AlignmentTest, Encode_Decode) {
129 for (uint64_t Value : getValidAlignments()) {
131 Align Actual(Value);
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) {
147 struct {
148 uint64_t alignment;
149 uint64_t offset;
150 bool isAligned;
151 } kTests[] = {
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);
159 // Test MaybeAlign
160 EXPECT_EQ(isAligned(A, T.offset), T.isAligned);
161 // Test Align
162 if (A) {
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) {
172 assert(I >= 1);
173 const Align A(ValidAlignments[I - 1]);
174 const Align B(ValidAlignments[I]);
175 EXPECT_EQ(A, A);
176 EXPECT_NE(A, B);
177 EXPECT_LT(A, B);
178 EXPECT_GT(B, A);
179 EXPECT_LE(A, B);
180 EXPECT_GE(B, A);
181 EXPECT_LE(A, A);
182 EXPECT_GE(A, A);
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]);
198 EXPECT_EQ(MA, MA);
199 EXPECT_NE(MA, MB);
200 EXPECT_LT(MA, MB);
201 EXPECT_GT(MB, MA);
202 EXPECT_LE(MA, MB);
203 EXPECT_GE(MB, MA);
204 EXPECT_LE(MA, MA);
205 EXPECT_GE(MA, MA);
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.
229 #ifndef NDEBUG
231 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
232 // slow.
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");
298 #endif // NDEBUG
300 } // end anonymous namespace