blob: 2d0e3578942588fc7c37099115b21fbb43991729 [file] [log] [blame]
Stepan Dyatkovskiy31219d22012-06-02 13:47:12 +00001//===- llvm/unittest/Support/IntegersSubsetTest.cpp - IntegersSubset 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//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/APInt.h"
11#include "llvm/Support/IntegersSubset.h"
12#include "llvm/Support/IntegersSubsetMapping.h"
13
14#include "gtest/gtest.h"
15
16#include <vector>
17
18using namespace llvm;
19
20namespace {
21
22 class Int : public APInt {
23 public:
Stepan Dyatkovskiyb787d412012-06-26 11:57:43 +000024 Int() {}
Stepan Dyatkovskiy31219d22012-06-02 13:47:12 +000025 Int(uint64_t V) : APInt(64, V) {}
Stepan Dyatkovskiy20cb4912012-06-05 07:43:08 +000026 Int(const APInt& Src) : APInt(Src) {}
Stepan Dyatkovskiy31219d22012-06-02 13:47:12 +000027 bool operator < (const APInt& RHS) const { return ult(RHS); }
28 bool operator > (const APInt& RHS) const { return ugt(RHS); }
29 bool operator <= (const APInt& RHS) const { return ule(RHS); }
30 bool operator >= (const APInt& RHS) const { return uge(RHS); }
31 };
32
33 typedef IntRange<Int> Range;
34 typedef IntegersSubsetGeneric<Int> Subset;
35 typedef IntegersSubsetMapping<unsigned,Subset,Int> Mapping;
36
37 TEST(IntegersSubsetTest, GeneralTest) {
38
39 // Test construction.
40
41 std::vector<Range> Ranges;
42 Ranges.reserve(3);
43
44 // Initialize Subset as union of three pairs:
45 // { {0, 8}, {10, 18}, {20, 28} }
46 for (unsigned i = 0; i < 3; ++i)
47 Ranges.push_back(Range(Int(i*10), Int(i*10 + 8)));
48
49 Subset TheSubset(Ranges);
50
51 for (unsigned i = 0; i < 3; ++i) {
52 EXPECT_EQ(TheSubset.getItem(i).getLow(), Int(i*10));
53 EXPECT_EQ(TheSubset.getItem(i).getHigh(), Int(i*10 + 8));
54 }
55
56 EXPECT_EQ(TheSubset.getNumItems(), 3ULL);
57
58 // Test belonging to range.
59
60 EXPECT_TRUE(TheSubset.isSatisfies(Int(5)));
61 EXPECT_FALSE(TheSubset.isSatisfies(Int(9)));
62
63 // Test when subset contains the only item.
64
65 Ranges.clear();
66 Ranges.push_back(Range(Int(10), Int(10)));
67
68 Subset TheSingleNumber(Ranges);
69
70 EXPECT_TRUE(TheSingleNumber.isSingleNumber());
71
72 Ranges.push_back(Range(Int(12), Int(15)));
73
74 Subset NotASingleNumber(Ranges);
75
76 EXPECT_FALSE(NotASingleNumber.isSingleNumber());
77
78 // Test when subset contains items that are not a ranges but
79 // the single numbers.
80
81 Ranges.clear();
82 Ranges.push_back(Range(Int(10), Int(10)));
83 Ranges.push_back(Range(Int(15), Int(19)));
84
85 Subset WithSingleNumberItems(Ranges);
86
87 EXPECT_TRUE(WithSingleNumberItems.isSingleNumber(0));
88 EXPECT_FALSE(WithSingleNumberItems.isSingleNumber(1));
89
90 // Test size of subset. Note subset itself may be not optimized (improper),
91 // so it may contain duplicates, and the size of subset { {0, 9} {5, 9} }
92 // will 15 instead of 10.
93
94 Ranges.clear();
95 Ranges.push_back(Range(Int(0), Int(9)));
96 Ranges.push_back(Range(Int(5), Int(9)));
97
98 Subset NotOptimizedSubset(Ranges);
99
100 EXPECT_EQ(NotOptimizedSubset.getSize(), 15ULL);
101
102 // Test access to a single value.
103 // getSingleValue(idx) method represents subset as flat numbers collection,
104 // so subset { {0, 3}, {8, 10} } will represented as array
105 // { 0, 1, 2, 3, 8, 9, 10 }.
106
107 Ranges.clear();
108 Ranges.push_back(Range(Int(0), Int(3)));
109 Ranges.push_back(Range(Int(8), Int(10)));
110
111 Subset OneMoreSubset(Ranges);
112
113 EXPECT_EQ(OneMoreSubset.getSingleValue(5), Int(9));
114 }
115
116 TEST(IntegersSubsetTest, MappingTest) {
117
118 Mapping::Cases TheCases;
119
120 unsigned Successors[3] = {0, 1, 2};
121
122 // Test construction.
123
124 Mapping TheMapping;
125 for (unsigned i = 0; i < 3; ++i)
126 TheMapping.add(Int(10*i), Int(10*i + 9), Successors + i);
127 TheMapping.add(Int(111), Int(222), Successors);
128 TheMapping.removeItem(--TheMapping.end());
129
130 TheMapping.getCases(TheCases);
131
132 EXPECT_EQ(TheCases.size(), 3ULL);
133
134 for (unsigned i = 0; i < 3; ++i) {
135 Mapping::Cases::iterator CaseIt = TheCases.begin();
136 std::advance(CaseIt, i);
137 EXPECT_EQ(CaseIt->first, Successors + i);
138 EXPECT_EQ(CaseIt->second.getNumItems(), 1ULL);
139 EXPECT_EQ(CaseIt->second.getItem(0), Range(Int(10*i), Int(10*i + 9)));
140 }
141
142 // Test verification.
143
144 Mapping ImproperMapping;
145 ImproperMapping.add(Int(10), Int(11), Successors + 0);
146 ImproperMapping.add(Int(11), Int(12), Successors + 1);
147
148 Mapping::RangeIterator ErrItem;
149 EXPECT_FALSE(ImproperMapping.verify(ErrItem));
150 EXPECT_EQ(ErrItem, --ImproperMapping.end());
151
152 Mapping ProperMapping;
153 ProperMapping.add(Int(10), Int(11), Successors + 0);
154 ProperMapping.add(Int(12), Int(13), Successors + 1);
155
156 EXPECT_TRUE(ProperMapping.verify(ErrItem));
157
158 // Test optimization.
159
160 Mapping ToBeOptimized;
161
162 for (unsigned i = 0; i < 3; ++i) {
163 ToBeOptimized.add(Int(i * 10), Int(i * 10 + 1), Successors + i);
164 ToBeOptimized.add(Int(i * 10 + 2), Int(i * 10 + 9), Successors + i);
165 }
166
167 ToBeOptimized.optimize();
168
169 TheCases.clear();
170 ToBeOptimized.getCases(TheCases);
171
172 EXPECT_EQ(TheCases.size(), 3ULL);
173
174 for (unsigned i = 0; i < 3; ++i) {
175 Mapping::Cases::iterator CaseIt = TheCases.begin();
176 std::advance(CaseIt, i);
177 EXPECT_EQ(CaseIt->first, Successors + i);
178 EXPECT_EQ(CaseIt->second.getNumItems(), 1ULL);
179 EXPECT_EQ(CaseIt->second.getItem(0), Range(Int(i * 10), Int(i * 10 + 9)));
180 }
181 }
Stepan Dyatkovskiyb787d412012-06-26 11:57:43 +0000182
183 typedef unsigned unsigned_pair[2];
184 typedef unsigned_pair unsigned_ranges[];
185
186 void TestDiff(
187 const unsigned_ranges LHS,
188 unsigned LSize,
189 const unsigned_ranges RHS,
190 unsigned RSize,
191 const unsigned_ranges ExcludeRes,
192 unsigned ExcludeResSize,
193 const unsigned_ranges IntersectRes,
194 unsigned IntersectResSize
195 ) {
196 Mapping::RangesCollection Ranges;
197
198 Mapping LHSMapping;
199 for (unsigned i = 0; i < LSize; ++i)
200 Ranges.push_back(Range(Int(LHS[i][0]), Int(LHS[i][1])));
201 LHSMapping.add(Ranges);
202
203 Ranges.clear();
204
205 Mapping RHSMapping;
206 for (unsigned i = 0; i < RSize; ++i)
207 Ranges.push_back(Range(Int(RHS[i][0]), Int(RHS[i][1])));
208 RHSMapping.add(Ranges);
209
210 Mapping LExclude, Intersection;
211
212 LHSMapping.diff(&LExclude, &Intersection, 0, RHSMapping);
213
214 if (ExcludeResSize) {
215 EXPECT_EQ(LExclude.size(), ExcludeResSize);
216
217 unsigned i = 0;
218 for (Mapping::RangeIterator rei = LExclude.begin(),
219 e = LExclude.end(); rei != e; ++rei, ++i)
220 EXPECT_EQ(rei->first, Range(ExcludeRes[i][0], ExcludeRes[i][1]));
221 } else
222 EXPECT_TRUE(LExclude.empty());
223
224 if (IntersectResSize) {
225 EXPECT_EQ(Intersection.size(), IntersectResSize);
226
227 unsigned i = 0;
228 for (Mapping::RangeIterator ii = Intersection.begin(),
229 e = Intersection.end(); ii != e; ++ii, ++i)
230 EXPECT_EQ(ii->first, Range(IntersectRes[i][0], IntersectRes[i][1]));
231 } else
232 EXPECT_TRUE(Intersection.empty());
233
234 LExclude.clear();
235 Intersection.clear();
236 RHSMapping.diff(0, &Intersection, &LExclude, LHSMapping);
237
238 // Check LExclude again.
239 if (ExcludeResSize) {
240 EXPECT_EQ(LExclude.size(), ExcludeResSize);
241
242 unsigned i = 0;
243 for (Mapping::RangeIterator rei = LExclude.begin(),
244 e = LExclude.end(); rei != e; ++rei, ++i)
245 EXPECT_EQ(rei->first, Range(ExcludeRes[i][0], ExcludeRes[i][1]));
246 } else
247 EXPECT_TRUE(LExclude.empty());
248 }
249
250 TEST(IntegersSubsetTest, DiffTest) {
251 {
252 unsigned_ranges LHS = { { 0, 4 }, { 7, 10 }, { 13, 17 } };
253 unsigned_ranges RHS = { { 3, 14 } };
254 unsigned_ranges ExcludeRes = { { 0, 2 }, { 15, 17 } };
255 unsigned_ranges IntersectRes = { { 3, 4 }, { 7, 10 }, { 13, 14 } };
256
257 TestDiff(LHS, 3, RHS, 1, ExcludeRes, 2, IntersectRes, 3);
258 }
259
260 {
261 unsigned_ranges LHS = { { 0, 4 }, { 7, 10 }, { 13, 17 } };
262 unsigned_ranges RHS = { { 0, 4 }, { 13, 17 } };
263 unsigned_ranges ExcludeRes = { { 7, 10 } };
264 unsigned_ranges IntersectRes = { { 0, 4 }, { 13, 17 } };
265
266 TestDiff(LHS, 3, RHS, 2, ExcludeRes, 1, IntersectRes, 2);
267 }
268
269 {
270 unsigned_ranges LHS = { { 0, 17 } };
271 unsigned_ranges RHS = { { 1, 5 }, { 10, 12 }, { 15, 16 } };
272 unsigned_ranges ExcludeRes =
273 { { 0, 0 }, { 6, 9 }, { 13, 14 }, { 17, 17 } };
274 unsigned_ranges IntersectRes = { { 1, 5 }, { 10, 12 }, { 15, 16 } };
275
276 TestDiff(LHS, 1, RHS, 3, ExcludeRes, 4, IntersectRes, 3);
277 }
278
279 {
280 unsigned_ranges LHS = { { 2, 4 } };
281 unsigned_ranges RHS = { { 0, 5 } };
282 unsigned_ranges ExcludeRes = { {-1UL, -1UL} };
283 unsigned_ranges IntersectRes = { { 2, 4 } };
284
285 TestDiff(LHS, 1, RHS, 1, ExcludeRes, 0, IntersectRes, 1);
286 }
287
288 {
289 unsigned_ranges LHS = { { 2, 4 } };
290 unsigned_ranges RHS = { { 7, 8 } };
291 unsigned_ranges ExcludeRes = { { 2, 4 } };
292 unsigned_ranges IntersectRes = { {-1UL, -1UL} };
293
294 TestDiff(LHS, 1, RHS, 1, ExcludeRes, 1, IntersectRes, 0);
295 }
296
297 {
298 unsigned_ranges LHS = { { 3, 7 } };
299 unsigned_ranges RHS = { { 1, 4 } };
300 unsigned_ranges ExcludeRes = { { 5, 7 } };
301 unsigned_ranges IntersectRes = { { 3, 4 } };
302
303 TestDiff(LHS, 1, RHS, 1, ExcludeRes, 1, IntersectRes, 1);
304 }
305
306 {
307 unsigned_ranges LHS = { { 0, 7 } };
308 unsigned_ranges RHS = { { 0, 5 }, { 6, 9 } };
309 unsigned_ranges ExcludeRes = { {-1UL, -1UL} };
310 unsigned_ranges IntersectRes = { { 0, 5 }, {6, 7} };
311
312 TestDiff(LHS, 1, RHS, 2, ExcludeRes, 0, IntersectRes, 2);
313 }
314
315 {
316 unsigned_ranges LHS = { { 17, 17 } };
317 unsigned_ranges RHS = { { 4, 4 } };
318 unsigned_ranges ExcludeRes = { {17, 17} };
319 unsigned_ranges IntersectRes = { { -1UL, -1UL } };
320
321 TestDiff(LHS, 1, RHS, 1, ExcludeRes, 1, IntersectRes, 0);
322 }
323 }
Stepan Dyatkovskiy31219d22012-06-02 13:47:12 +0000324}