MemoryObject - Abstract base class for contiguous addressable memory.
[llvm/avr.git] / unittests / Support / ConstantRangeTest.cpp
blobaf01276bf7f69d8df33f512bfb37e2962d3ee5c3
1 //===- llvm/unittest/Support/ConstantRangeTest.cpp - ConstantRange tests --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/ConstantRange.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
16 namespace {
18 class ConstantRangeTest : public ::testing::Test {
19 protected:
20 static ConstantRange Full;
21 static ConstantRange Empty;
22 static ConstantRange One;
23 static ConstantRange Some;
24 static ConstantRange Wrap;
27 ConstantRange ConstantRangeTest::Full(16);
28 ConstantRange ConstantRangeTest::Empty(16, false);
29 ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
30 ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
31 ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
33 TEST_F(ConstantRangeTest, Basics) {
34 EXPECT_TRUE(Full.isFullSet());
35 EXPECT_FALSE(Full.isEmptySet());
36 EXPECT_FALSE(Full.isWrappedSet());
37 EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
38 EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
39 EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
40 EXPECT_TRUE(Full.contains(APInt(16, 0xaa9)));
41 EXPECT_TRUE(Full.contains(APInt(16, 0xaaa)));
43 EXPECT_FALSE(Empty.isFullSet());
44 EXPECT_TRUE(Empty.isEmptySet());
45 EXPECT_FALSE(Empty.isWrappedSet());
46 EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
47 EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
48 EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
49 EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9)));
50 EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa)));
52 EXPECT_FALSE(One.isFullSet());
53 EXPECT_FALSE(One.isEmptySet());
54 EXPECT_FALSE(One.isWrappedSet());
55 EXPECT_FALSE(One.contains(APInt(16, 0x0)));
56 EXPECT_FALSE(One.contains(APInt(16, 0x9)));
57 EXPECT_TRUE(One.contains(APInt(16, 0xa)));
58 EXPECT_FALSE(One.contains(APInt(16, 0xaa9)));
59 EXPECT_FALSE(One.contains(APInt(16, 0xaaa)));
61 EXPECT_FALSE(Some.isFullSet());
62 EXPECT_FALSE(Some.isEmptySet());
63 EXPECT_FALSE(Some.isWrappedSet());
64 EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
65 EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
66 EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
67 EXPECT_TRUE(Some.contains(APInt(16, 0xaa9)));
68 EXPECT_FALSE(Some.contains(APInt(16, 0xaaa)));
70 EXPECT_FALSE(Wrap.isFullSet());
71 EXPECT_FALSE(Wrap.isEmptySet());
72 EXPECT_TRUE(Wrap.isWrappedSet());
73 EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
74 EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
75 EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
76 EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9)));
77 EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa)));
80 TEST_F(ConstantRangeTest, Equality) {
81 EXPECT_EQ(Full, Full);
82 EXPECT_EQ(Empty, Empty);
83 EXPECT_EQ(One, One);
84 EXPECT_EQ(Some, Some);
85 EXPECT_EQ(Wrap, Wrap);
86 EXPECT_NE(Full, Empty);
87 EXPECT_NE(Full, One);
88 EXPECT_NE(Full, Some);
89 EXPECT_NE(Full, Wrap);
90 EXPECT_NE(Empty, One);
91 EXPECT_NE(Empty, Some);
92 EXPECT_NE(Empty, Wrap);
93 EXPECT_NE(One, Some);
94 EXPECT_NE(One, Wrap);
95 EXPECT_NE(Some, Wrap);
98 TEST_F(ConstantRangeTest, SingleElement) {
99 EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL));
100 EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL));
101 EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
102 EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL));
103 EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL));
105 EXPECT_FALSE(Full.isSingleElement());
106 EXPECT_FALSE(Empty.isSingleElement());
107 EXPECT_TRUE(One.isSingleElement());
108 EXPECT_FALSE(Some.isSingleElement());
109 EXPECT_FALSE(Wrap.isSingleElement());
112 TEST_F(ConstantRangeTest, GetSetSize) {
113 EXPECT_EQ(Full.getSetSize(), APInt(16, 0));
114 EXPECT_EQ(Empty.getSetSize(), APInt(16, 0));
115 EXPECT_EQ(One.getSetSize(), APInt(16, 1));
116 EXPECT_EQ(Some.getSetSize(), APInt(16, 0xaa0));
117 EXPECT_EQ(Wrap.getSetSize(), APInt(16, 0x10000 - 0xaa0));
120 TEST_F(ConstantRangeTest, GetMinsAndMaxes) {
121 EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
122 EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
123 EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
124 EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
126 EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
127 EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
128 EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
129 EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
131 EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
132 EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
133 EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
134 EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
136 EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
137 EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
138 EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
139 EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
141 // Found by Klee
142 EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
143 APInt(4, 7));
146 TEST_F(ConstantRangeTest, Trunc) {
147 ConstantRange TFull = Full.truncate(10);
148 ConstantRange TEmpty = Empty.truncate(10);
149 ConstantRange TOne = One.truncate(10);
150 ConstantRange TSome = Some.truncate(10);
151 ConstantRange TWrap = Wrap.truncate(10);
152 EXPECT_TRUE(TFull.isFullSet());
153 EXPECT_TRUE(TEmpty.isEmptySet());
154 EXPECT_EQ(TOne, ConstantRange(APInt(One.getLower()).trunc(10),
155 APInt(One.getUpper()).trunc(10)));
156 EXPECT_TRUE(TSome.isFullSet());
159 TEST_F(ConstantRangeTest, ZExt) {
160 ConstantRange ZFull = Full.zeroExtend(20);
161 ConstantRange ZEmpty = Empty.zeroExtend(20);
162 ConstantRange ZOne = One.zeroExtend(20);
163 ConstantRange ZSome = Some.zeroExtend(20);
164 ConstantRange ZWrap = Wrap.zeroExtend(20);
165 EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
166 EXPECT_TRUE(ZEmpty.isEmptySet());
167 EXPECT_EQ(ZOne, ConstantRange(APInt(One.getLower()).zext(20),
168 APInt(One.getUpper()).zext(20)));
169 EXPECT_EQ(ZSome, ConstantRange(APInt(Some.getLower()).zext(20),
170 APInt(Some.getUpper()).zext(20)));
171 EXPECT_EQ(ZWrap, ConstantRange(APInt(Wrap.getLower()).zext(20),
172 APInt(Wrap.getUpper()).zext(20)));
175 TEST_F(ConstantRangeTest, SExt) {
176 ConstantRange SFull = Full.signExtend(20);
177 ConstantRange SEmpty = Empty.signExtend(20);
178 ConstantRange SOne = One.signExtend(20);
179 ConstantRange SSome = Some.signExtend(20);
180 ConstantRange SWrap = Wrap.signExtend(20);
181 EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
182 APInt(20, INT16_MAX + 1, true)));
183 EXPECT_TRUE(SEmpty.isEmptySet());
184 EXPECT_EQ(SOne, ConstantRange(APInt(One.getLower()).sext(20),
185 APInt(One.getUpper()).sext(20)));
186 EXPECT_EQ(SSome, ConstantRange(APInt(Some.getLower()).sext(20),
187 APInt(Some.getUpper()).sext(20)));
188 EXPECT_EQ(SWrap, ConstantRange(APInt(Wrap.getLower()).sext(20),
189 APInt(Wrap.getUpper()).sext(20)));
192 TEST_F(ConstantRangeTest, IntersectWith) {
193 EXPECT_TRUE(Empty.intersectWith(Full).isEmptySet());
194 EXPECT_TRUE(Empty.intersectWith(Empty).isEmptySet());
195 EXPECT_TRUE(Empty.intersectWith(One).isEmptySet());
196 EXPECT_TRUE(Empty.intersectWith(Some).isEmptySet());
197 EXPECT_TRUE(Empty.intersectWith(Wrap).isEmptySet());
198 EXPECT_TRUE(Full.intersectWith(Full).isFullSet());
199 EXPECT_TRUE(Some.intersectWith(Some) == Some);
200 EXPECT_TRUE(Some.intersectWith(One) == One);
201 EXPECT_TRUE(Full.intersectWith(One) == One);
202 EXPECT_TRUE(Full.intersectWith(Some) == Some);
203 EXPECT_TRUE(Some.intersectWith(Wrap).isEmptySet());
204 EXPECT_TRUE(One.intersectWith(Wrap).isEmptySet());
205 EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
207 // Klee generated testcase from PR4545.
208 // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
209 // 01..4.6789ABCDEF where the dots represent values not in the intersection.
210 ConstantRange LHS(APInt(16, 4), APInt(16, 2));
211 ConstantRange RHS(APInt(16, 6), APInt(16, 5));
212 EXPECT_EQ(LHS.intersectWith(RHS), LHS);
215 TEST_F(ConstantRangeTest, UnionWith) {
216 EXPECT_EQ(Wrap.unionWith(One),
217 ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
218 EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
219 EXPECT_TRUE(Empty.unionWith(Empty).isEmptySet());
220 EXPECT_TRUE(Full.unionWith(Full).isFullSet());
221 EXPECT_TRUE(Some.unionWith(Wrap).isFullSet());
223 // PR4545
224 EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
225 ConstantRange(APInt(16, 0), APInt(16, 8))),
226 ConstantRange(APInt(16, 14), APInt(16, 8)));
227 EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
228 ConstantRange(APInt(16, 4), APInt(16, 0))),
229 ConstantRange(16));
230 EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
231 ConstantRange(APInt(16, 2), APInt(16, 1))),
232 ConstantRange(16));
235 TEST_F(ConstantRangeTest, SubtractAPInt) {
236 EXPECT_TRUE(Full.subtract(APInt(16, 4)).isFullSet());
237 EXPECT_TRUE(Empty.subtract(APInt(16, 4)).isEmptySet());
238 EXPECT_EQ(Some.subtract(APInt(16, 4)),
239 ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
240 EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
241 ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
242 EXPECT_EQ(One.subtract(APInt(16, 4)),
243 ConstantRange(APInt(16, 0x6)));
246 TEST_F(ConstantRangeTest, Add) {
247 EXPECT_TRUE(Full.add(APInt(16, 4)).isFullSet());
248 EXPECT_EQ(Full.add(Full), Full);
249 EXPECT_EQ(Full.add(Empty), Empty);
250 EXPECT_EQ(Full.add(One), Full);
251 EXPECT_EQ(Full.add(Some), Full);
252 EXPECT_EQ(Full.add(Wrap), Full);
253 EXPECT_EQ(Empty.add(Empty), Empty);
254 EXPECT_EQ(Empty.add(One), Empty);
255 EXPECT_EQ(Empty.add(Some), Empty);
256 EXPECT_EQ(Empty.add(Wrap), Empty);
257 EXPECT_TRUE(Empty.add(APInt(16, 4)).isEmptySet());
258 EXPECT_EQ(Some.add(APInt(16, 4)),
259 ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
260 EXPECT_EQ(Wrap.add(APInt(16, 4)),
261 ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
262 EXPECT_EQ(One.add(APInt(16, 4)),
263 ConstantRange(APInt(16, 0xe)));
266 TEST_F(ConstantRangeTest, Multiply) {
267 EXPECT_EQ(Full.multiply(Full), Full);
268 EXPECT_EQ(Full.multiply(Empty), Empty);
269 EXPECT_EQ(Full.multiply(One), Full);
270 EXPECT_EQ(Full.multiply(Some), Full);
271 EXPECT_EQ(Full.multiply(Wrap), Full);
272 EXPECT_EQ(Empty.multiply(Empty), Empty);
273 EXPECT_EQ(Empty.multiply(One), Empty);
274 EXPECT_EQ(Empty.multiply(Some), Empty);
275 EXPECT_EQ(Empty.multiply(Wrap), Empty);
276 EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
277 APInt(16, 0xa*0xa + 1)));
278 EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
279 APInt(16, 0xa*0xaa9 + 1)));
280 EXPECT_TRUE(One.multiply(Wrap).isFullSet());
281 EXPECT_TRUE(Some.multiply(Some).isFullSet());
282 EXPECT_EQ(Some.multiply(Wrap), Full);
283 EXPECT_EQ(Wrap.multiply(Wrap), Full);
285 // http://llvm.org/PR4545
286 EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
287 ConstantRange(APInt(4, 6), APInt(4, 2))),
288 ConstantRange(4, /*isFullSet=*/true));
291 TEST_F(ConstantRangeTest, UMax) {
292 EXPECT_TRUE(Full.umax(Full).isFullSet());
293 EXPECT_TRUE(Full.umax(Empty).isEmptySet());
294 EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
295 EXPECT_TRUE(Full.umax(Wrap).isFullSet());
296 EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
297 EXPECT_EQ(Empty.umax(Empty), Empty);
298 EXPECT_EQ(Empty.umax(Some), Empty);
299 EXPECT_EQ(Empty.umax(Wrap), Empty);
300 EXPECT_EQ(Empty.umax(One), Empty);
301 EXPECT_EQ(Some.umax(Some), Some);
302 EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
303 EXPECT_EQ(Some.umax(One), Some);
304 // TODO: ConstantRange is currently over-conservative here.
305 EXPECT_EQ(Wrap.umax(Wrap), Full);
306 EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
307 EXPECT_EQ(One.umax(One), One);
310 TEST_F(ConstantRangeTest, SMax) {
311 EXPECT_TRUE(Full.smax(Full).isFullSet());
312 EXPECT_TRUE(Full.smax(Empty).isEmptySet());
313 EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
314 APInt::getSignedMinValue(16)));
315 EXPECT_TRUE(Full.smax(Wrap).isFullSet());
316 EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
317 APInt::getSignedMinValue(16)));
318 EXPECT_EQ(Empty.smax(Empty), Empty);
319 EXPECT_EQ(Empty.smax(Some), Empty);
320 EXPECT_EQ(Empty.smax(Wrap), Empty);
321 EXPECT_EQ(Empty.smax(One), Empty);
322 EXPECT_EQ(Some.smax(Some), Some);
323 EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
324 APInt(16, (uint64_t)INT16_MIN)));
325 EXPECT_EQ(Some.smax(One), Some);
326 EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
327 APInt(16, (uint64_t)INT16_MIN)));
328 EXPECT_EQ(One.smax(One), One);
331 TEST_F(ConstantRangeTest, UDiv) {
332 EXPECT_EQ(Full.udiv(Full), Full);
333 EXPECT_EQ(Full.udiv(Empty), Empty);
334 EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
335 APInt(16, 0xffff / 0xa + 1)));
336 EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
337 APInt(16, 0xffff / 0xa + 1)));
338 EXPECT_EQ(Full.udiv(Wrap), Full);
339 EXPECT_EQ(Empty.udiv(Empty), Empty);
340 EXPECT_EQ(Empty.udiv(One), Empty);
341 EXPECT_EQ(Empty.udiv(Some), Empty);
342 EXPECT_EQ(Empty.udiv(Wrap), Empty);
343 EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
344 EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
345 EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
346 EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
347 EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
348 EXPECT_EQ(Wrap.udiv(Wrap), Full);
351 } // anonymous namespace