[ARM] Remove declaration of unimplemented function. NFC.
[llvm-complete.git] / unittests / Support / AlignmentTest.cpp
blob420772541d4a58c2168f1b0f3082cfd9a1877f9a
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) { 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()) {
55 if (Value > 1) {
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) {
64 struct {
65 uint64_t alignment;
66 uint64_t offset;
67 uint64_t rounded;
68 } kTests[] = {
69 // MaybeAlign
70 {0, 0, 0},
71 {0, 1, 1},
72 {0, 5, 5},
73 // MaybeAlign / Align
74 {1, 0, 0},
75 {1, 1, 1},
76 {1, 5, 5},
77 {2, 0, 0},
78 {2, 1, 2},
79 {2, 2, 2},
80 {2, 7, 8},
81 {2, 16, 16},
82 {4, 0, 0},
83 {4, 1, 4},
84 {4, 4, 4},
85 {4, 6, 8},
87 for (const auto &T : kTests) {
88 MaybeAlign A(T.alignment);
89 // Test MaybeAlign
90 EXPECT_EQ(alignTo(T.offset, A), T.rounded);
91 // Test Align
92 if (A) {
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) {
106 struct {
107 uint64_t A;
108 uint64_t B;
109 uint64_t MinAlign;
110 } kTests[] = {
111 // MaybeAlign
112 {0, 0, 0},
113 {0, 8, 8},
114 {2, 0, 2},
115 // MaybeAlign / Align
116 {1, 2, 1},
117 {8, 4, 4},
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);
122 if (T.A) {
123 EXPECT_EQ(commonAlignment(Align(T.A), MaybeAlign(T.B)), T.MinAlign);
125 if (T.B) {
126 EXPECT_EQ(commonAlignment(MaybeAlign(T.A), Align(T.B)), T.MinAlign);
128 if (T.A && T.B) {
129 EXPECT_EQ(commonAlignment(Align(T.A), Align(T.B)), T.MinAlign);
134 TEST(AlignmentTest, Encode_Decode) {
135 for (uint64_t Value : getValidAlignments()) {
137 Align Actual(Value);
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) {
153 struct {
154 uint64_t alignment;
155 uint64_t offset;
156 bool isAligned;
157 } kTests[] = {
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);
165 // Test MaybeAlign
166 EXPECT_EQ(isAligned(A, T.offset), T.isAligned);
167 // Test Align
168 if (A) {
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) {
178 assert(I >= 1);
179 const Align A(ValidAlignments[I - 1]);
180 const Align B(ValidAlignments[I]);
181 EXPECT_EQ(A, A);
182 EXPECT_NE(A, B);
183 EXPECT_LT(A, B);
184 EXPECT_GT(B, A);
185 EXPECT_LE(A, B);
186 EXPECT_GE(B, A);
187 EXPECT_LE(A, A);
188 EXPECT_GE(A, A);
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]);
204 EXPECT_EQ(MA, MA);
205 EXPECT_NE(MA, MB);
206 EXPECT_LT(MA, MB);
207 EXPECT_GT(MB, MA);
208 EXPECT_LE(MA, MB);
209 EXPECT_GE(MB, MA);
210 EXPECT_LE(MA, MA);
211 EXPECT_GE(MA, MA);
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.
235 #ifndef NDEBUG
237 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
238 // slow.
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");
304 #endif // NDEBUG
306 } // end anonymous namespace
308 #ifdef _MSC_VER
309 #pragma warning(pop)
310 #endif