10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
- #include " swift/AST/AutoDiff.h"
14
13
#include " swift/AST/IndexSubset.h"
15
14
#include " TestContext.h"
16
15
#include " gtest/gtest.h"
@@ -49,58 +48,45 @@ TEST(IndexSubset, BitWordIndexAndOffset) {
49
48
50
49
TEST (IndexSubset, Equality) {
51
50
TestContext ctx;
52
- EXPECT_EQ (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
53
- /* indices*/ {0 }),
54
- IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
55
- /* indices*/ {0 }));
56
- EXPECT_EQ (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
57
- /* indices*/ {0 , 2 , 4 }),
58
- IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
59
- /* indices*/ {0 , 2 , 4 }));
60
- EXPECT_EQ (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
61
- /* indices*/ {}),
62
- IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
63
- /* indices*/ {}));
64
- EXPECT_NE (IndexSubset::get (ctx.Ctx , /* capacity*/ 1 ,
65
- /* indices*/ {}),
66
- IndexSubset::get (ctx.Ctx , /* capacity*/ 0 ,
67
- /* indices*/ {}));
68
- EXPECT_NE (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
69
- /* indices*/ {0 }),
70
- IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
71
- /* indices*/ {}));
51
+ EXPECT_EQ (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {0 }),
52
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {0 }));
53
+ EXPECT_EQ (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {0 , 2 , 4 }),
54
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {0 , 2 , 4 }));
55
+ EXPECT_EQ (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {}),
56
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {}));
57
+ EXPECT_NE (IndexSubset::get (ctx.Ctx , /* capacity*/ 1 , /* indices*/ {}),
58
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 0 , /* indices*/ {}));
59
+ EXPECT_NE (IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {0 }),
60
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {}));
72
61
}
73
62
74
63
TEST (IndexSubset, Initializers) {
75
64
TestContext ctx;
76
65
// Default init.
77
66
EXPECT_EQ (IndexSubset::getDefault (ctx.Ctx , /* capacity*/ 5 ,
78
- /* includeAll*/ true ),
67
+ /* includeAll*/ true ),
79
68
IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
80
- /* indices*/ {0 , 1 , 2 , 3 , 4 }));
69
+ /* indices*/ {0 , 1 , 2 , 3 , 4 }));
81
70
EXPECT_EQ (IndexSubset::getDefault (ctx.Ctx , /* capacity*/ 5 ,
82
- /* includeAll*/ false ),
71
+ /* includeAll*/ false ),
83
72
IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {}));
84
73
EXPECT_EQ (IndexSubset::getDefault (ctx.Ctx , /* capacity*/ 0 ,
85
- /* includeAll*/ true ),
86
- IndexSubset::get (ctx.Ctx , /* capacity*/ 0 ,
87
- /* indices*/ {}));
74
+ /* includeAll*/ true ),
75
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 0 , /* indices*/ {}));
88
76
EXPECT_EQ (IndexSubset::getDefault (ctx.Ctx , /* capacity*/ 0 ,
89
- /* includeAll*/ false ),
77
+ /* includeAll*/ false ),
90
78
IndexSubset::get (ctx.Ctx , /* capacity*/ 0 , /* indices*/ {}));
91
79
// Bit vector init.
92
80
{
93
81
llvm::SmallBitVector bitVec (6 );
94
82
bitVec.set (1 , 4 );
95
83
EXPECT_EQ (IndexSubset::get (ctx.Ctx , bitVec),
96
- IndexSubset::get (ctx.Ctx , /* capacity*/ 6 ,
97
- /* indices*/ {1 , 2 , 3 }));
84
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 6 , /* indices*/ {1 , 2 , 3 }));
98
85
}
99
86
{
100
87
llvm::SmallBitVector bitVec (0 );
101
88
EXPECT_EQ (IndexSubset::get (ctx.Ctx , bitVec),
102
- IndexSubset::get (ctx.Ctx , /* capacity*/ 0 ,
103
- /* indices*/ {}));
89
+ IndexSubset::get (ctx.Ctx , /* capacity*/ 0 , /* indices*/ {}));
104
90
}
105
91
// String init.
106
92
EXPECT_EQ (IndexSubset::getFromString (ctx.Ctx , " SSSSS" ),
@@ -118,7 +104,7 @@ TEST(IndexSubset, Initializers) {
118
104
TEST (IndexSubset, Bits) {
119
105
TestContext ctx;
120
106
auto *indices1 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
121
- /* indices*/ {0 , 2 , 4 });
107
+ /* indices*/ {0 , 2 , 4 });
122
108
EXPECT_EQ (indices1->getNumBitWords (), 1u );
123
109
EXPECT_EQ (indices1->getCapacity (), 5u );
124
110
EXPECT_TRUE (indices1->contains (0 ));
@@ -128,7 +114,7 @@ TEST(IndexSubset, Bits) {
128
114
EXPECT_TRUE (indices1->contains (4 ));
129
115
130
116
auto *indices2 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
131
- /* indices*/ {1 , 3 });
117
+ /* indices*/ {1 , 3 });
132
118
EXPECT_EQ (indices2->getNumBitWords (), 1u );
133
119
EXPECT_EQ (indices2->getCapacity (), 5u );
134
120
EXPECT_FALSE (indices2->contains (0 ));
@@ -143,7 +129,7 @@ TEST(IndexSubset, Iteration) {
143
129
// Test 1
144
130
{
145
131
auto *indices1 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
146
- /* indices*/ {0 , 2 , 4 });
132
+ /* indices*/ {0 , 2 , 4 });
147
133
// Check forward iteration.
148
134
EXPECT_EQ (indices1->findFirst (), 0 );
149
135
EXPECT_EQ (indices1->findNext (0 ), 2 );
@@ -162,7 +148,7 @@ TEST(IndexSubset, Iteration) {
162
148
// Test 2
163
149
{
164
150
auto *indices2 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
165
- /* indices*/ {1 , 3 });
151
+ /* indices*/ {1 , 3 });
166
152
// Check forward iteration.
167
153
EXPECT_EQ (indices2->findFirst (), 1 );
168
154
EXPECT_EQ (indices2->findNext (1 ), 3 );
@@ -181,11 +167,10 @@ TEST(IndexSubset, Iteration) {
181
167
TEST (IndexSubset, SupersetAndSubset) {
182
168
TestContext ctx;
183
169
auto *indices1 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
184
- /* indices*/ {0 , 2 , 4 });
170
+ /* indices*/ {0 , 2 , 4 });
185
171
EXPECT_TRUE (indices1->isSupersetOf (indices1));
186
172
EXPECT_TRUE (indices1->isSubsetOf (indices1));
187
- auto *indices2 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 ,
188
- /* indices*/ {2 });
173
+ auto *indices2 = IndexSubset::get (ctx.Ctx , /* capacity*/ 5 , /* indices*/ {2 });
189
174
EXPECT_TRUE (indices2->isSupersetOf (indices2));
190
175
EXPECT_TRUE (indices2->isSubsetOf (indices2));
191
176
@@ -202,117 +187,3 @@ TEST(IndexSubset, Insertion) {
202
187
EXPECT_EQ (indices1->adding (3 , ctx.Ctx ),
203
188
IndexSubset::get (ctx.Ctx , 5 , {0 , 2 , 3 , 4 }));
204
189
}
205
-
206
- TEST (IndexSubset, Lowering) {
207
- TestContext testCtx;
208
- auto &C = testCtx.Ctx ;
209
- // ((T, T)) -> ()
210
- EXPECT_EQ (
211
- autodiff::getLoweredParameterIndices (
212
- IndexSubset::get (C, 1 , {0 }),
213
- FunctionType::get ({
214
- FunctionType::Param (
215
- TupleType::get ({C.TheAnyType , C.TheAnyType }, C))},
216
- C.TheEmptyTupleType )),
217
- IndexSubset::get (C, 2 , {0 , 1 }));
218
- // ((), (T, T)) -> ()
219
- EXPECT_EQ (
220
- autodiff::getLoweredParameterIndices (
221
- IndexSubset::get (C, 2 , {1 }),
222
- FunctionType::get ({
223
- FunctionType::Param (C.TheEmptyTupleType ),
224
- FunctionType::Param (
225
- TupleType::get ({C.TheAnyType , C.TheAnyType }, C))},
226
- C.TheEmptyTupleType )),
227
- IndexSubset::get (C, 2 , {0 , 1 }));
228
- // (T, (T, T)) -> ()
229
- EXPECT_EQ (
230
- autodiff::getLoweredParameterIndices (
231
- IndexSubset::get (C, 2 , {1 }),
232
- FunctionType::get ({
233
- FunctionType::Param (C.TheAnyType ),
234
- FunctionType::Param (
235
- TupleType::get ({C.TheAnyType , C.TheAnyType }, C))},
236
- C.TheEmptyTupleType )),
237
- IndexSubset::get (C, 3 , {1 , 2 }));
238
- // (T, (T, T)) -> ()
239
- EXPECT_EQ (
240
- autodiff::getLoweredParameterIndices (
241
- IndexSubset::get (C, 2 , {0 , 1 }),
242
- FunctionType::get ({
243
- FunctionType::Param (C.TheAnyType ),
244
- FunctionType::Param (
245
- TupleType::get ({C.TheAnyType , C.TheAnyType }, C))},
246
- C.TheEmptyTupleType )),
247
- IndexSubset::get (C, 3 , {0 , 1 , 2 }));
248
- // (T, ((T, T)), (T, T), T) -> ()
249
- EXPECT_EQ (
250
- autodiff::getLoweredParameterIndices (
251
- IndexSubset::get (C, 4 , {0 , 1 , 3 }),
252
- FunctionType::get ({
253
- FunctionType::Param (C.TheAnyType ),
254
- FunctionType::Param (
255
- TupleType::get ({
256
- TupleType::get ({C.TheAnyType , C.TheAnyType }, C)}, C)),
257
- FunctionType::Param (
258
- TupleType::get ({C.TheAnyType , C.TheAnyType }, C)),
259
- FunctionType::Param (C.TheAnyType )},
260
- C.TheEmptyTupleType )),
261
- IndexSubset::get (C, 6 , {0 , 1 , 2 , 5 }));
262
- // Method (T) -> ((T, T), (T, T), T) -> ()
263
- // TODO(TF-874): Fix this unit test.
264
- // The current actual result is:
265
- // `(autodiff_index_subset capacity=6 indices=(0, 1, 4))`.
266
- #if 0
267
- EXPECT_EQ(
268
- autodiff::getLoweredParameterIndices(
269
- IndexSubset::get(C, 4, {0, 1, 3}),
270
- FunctionType::get(
271
- {FunctionType::Param(C.TheAnyType)},
272
- FunctionType::get({
273
- FunctionType::Param(
274
- TupleType::get({C.TheAnyType, C.TheAnyType}, C)),
275
- FunctionType::Param(
276
- TupleType::get({C.TheAnyType, C.TheAnyType}, C)),
277
- FunctionType::Param(C.TheAnyType)},
278
- C.TheEmptyTupleType)->withExtInfo(
279
- FunctionType::ExtInfo().withSILRepresentation(
280
- SILFunctionTypeRepresentation::Method)))),
281
- IndexSubset::get(C, 6, {0, 1, 4, 5}));
282
- #endif
283
- }
284
-
285
- TEST (IndexSubset, GetSubsetParameterTypes) {
286
- TestContext testCtx;
287
- auto &C = testCtx.Ctx ;
288
- // (T, T) -> ()
289
- {
290
- SmallVector<Type, 8 > subset;
291
- autodiff::getSubsetParameterTypes (
292
- IndexSubset::get (C, 1 , {0 }),
293
- FunctionType::get (
294
- {FunctionType::Param (C.TheAnyType ),
295
- FunctionType::Param (C.TheAnyType )},
296
- C.TheEmptyTupleType ),
297
- subset);
298
- Type expected[] = {C.TheAnyType };
299
- EXPECT_TRUE (std::equal (subset.begin (), subset.end (), expected,
300
- [](Type ty1, Type ty2) { return ty1->isEqual (ty2); }));
301
- }
302
- // (T) -> (T, T) -> ()
303
- {
304
- SmallVector<Type, 8 > subset;
305
- autodiff::getSubsetParameterTypes (
306
- IndexSubset::get (C, 3 , {0 , 1 , 2 }),
307
- FunctionType::get (
308
- {FunctionType::Param (C.TheIEEE16Type )},
309
- FunctionType::get (
310
- {FunctionType::Param (C.TheAnyType ),
311
- FunctionType::Param (C.TheAnyType )},
312
- C.TheEmptyTupleType )),
313
- subset);
314
- Type expected[] = {C.TheIEEE16Type , C.TheAnyType , C.TheAnyType };
315
- EXPECT_TRUE (std::equal (subset.begin (), subset.end (), expected,
316
- [](Type ty1, Type ty2) { return ty1->isEqual (ty2); }));
317
- }
318
- }
0 commit comments