This repository has been archived by the owner on Jun 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FnsTest.java
495 lines (397 loc) · 16.6 KB
/
FnsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.cognitect.transducers;
import junit.framework.TestCase;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.cognitect.transducers.Fns.*;
//import static com.cognitect.transducers.Base.*;
public class FnsTest extends TestCase {
private List<Integer> ints(final int n) {
return new ArrayList<Integer>(n) {{
for(int i = 0; i < n; i++) {
add(i);
}
}};
}
private List<Long> longs(final long n) {
return new ArrayList<Long>((int)n) {{
for(long i = 0l; i < n; i++) {
add(i);
}
}};
}
private static ITransducer<String, Long> stringify = map(new Function<Long, String>() {
@Override
public String apply(Long i) {
return i.toString();
}
});
private static IStepFunction<List<String>, String> addString = new IStepFunction<List<String>, String>() {
@Override
public List<String> apply(List<String> result, String input, AtomicBoolean reduced) {
result.add(input);
return result;
}
};
public void testMap() throws Exception {
ITransducer<String, Integer> xf = map(new Function<Integer, String>() {
@Override
public String apply(Integer i) {
return i.toString();
}
});
String s = transduce(xf, new IStepFunction<String, String>() {
@Override
public String apply(String result, String input, AtomicBoolean reduced) {
return result + input + " ";
}
}, "", ints(10));
assertEquals(s, "0 1 2 3 4 5 6 7 8 9 ");
ITransducer<Integer, Integer> xn = map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i;
}
});
List<Integer> nums = transduce(xn, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input + 1);
return result;
}
}, new ArrayList<Integer>(), ints(10));
Integer[] expected = {1,2,3,4,5,6,7,8,9,10};
assertTrue(nums.equals(Arrays.asList(expected)));
// README usage test
List<String> sl = transduce(stringify, addString, new ArrayList<String>(), longs(10));
String[] lexpected = {"0","1","2","3","4","5","6","7","8","9"};
assertTrue(sl.equals(Arrays.asList(lexpected)));
}
public void testFilter() throws Exception {
ITransducer<Integer, Integer> xf = filter(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
return integer.intValue() % 2 != 0;
}
});
List<Integer> odds = transduce(xf, new IStepFunction<ArrayList<Integer>, Integer>() {
@Override
public ArrayList<Integer> apply(ArrayList<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(10));
Integer[] expected = {1,3,5,7,9};
assertTrue(odds.equals(Arrays.asList(expected)));
}
public void testCat() throws Exception {
ITransducer<Integer, Iterable<Integer>> xf = cat();
List<Iterable<Integer>> data = new ArrayList<Iterable<Integer>>() {{
add(ints(10));
add(ints(20));
}};
List<Integer> vals = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), data);
int i=0;
List<Integer> nums = ints(10);
for(int j=0; j<nums.size(); j++) {
assertEquals((int)nums.get(j), (int)vals.get(i));
i += 1;
}
nums = ints(20);
for(int j=0; j<nums.size(); j++) {
assertEquals((int)nums.get(j), (int)vals.get(i));
i += 1;
}
}
public void testMapcat() throws Exception {
ITransducer<Character, Integer> xf = mapcat(new Function<Integer, Iterable<Character>>() {
@Override
public Iterable<Character> apply(Integer integer) {
final String s = integer.toString();
return new ArrayList<Character>(s.length()) {{
for (char c : s.toCharArray())
add(c);
}};
}
});
List<Character> vals = transduce(xf, new IStepFunction<List<Character>, Character>() {
@Override
public List<Character> apply(List<Character> result, Character input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Character>(), ints(10));
Character[] expected = {'0','1','2','3','4','5','6','7','8','9'};
assertTrue(vals.equals(Arrays.asList(expected)));
}
public void testComp() throws Exception {
ITransducer<Integer, Integer> f = filter(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
return integer.intValue() % 2 != 0;
}
});
ITransducer<String, Integer> m = map(new Function<Integer, String>() {
@Override
public String apply(Integer i) {
return i.toString();
}
});
ITransducer<String, Integer> xf = f.comp(m);
List<String> odds = transduce(xf, new IStepFunction<List<String>, String>() {
@Override
public List<String> apply(List<String> result, String input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<String>(), ints(10));
String[] expected = {"1","3","5","7","9"};
assertTrue(odds.equals(Arrays.asList(expected)));
// README Usage tests
ITransducer<Long, Long> filterOdds = filter(new Predicate<Long>() {
@Override
public boolean test(Long num) {
return num.longValue() % 2 != 0;
}
});
ITransducer<String, Long> stringifyOdds = filterOdds.comp(stringify);
List<String> sl = transduce(stringifyOdds, addString, new ArrayList<String>(), longs(10));
String[] lexpected = {"1","3","5","7","9"};
assertTrue(sl.equals(Arrays.asList(lexpected)));
}
public void testTake() throws Exception {
ITransducer<Integer, Integer> xf = take(5);
List<Integer> five = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(20));
Integer[] expected = {0,1,2,3,4};
assertTrue(five.equals(Arrays.asList(expected)));
}
public void testTakeWhile() throws Exception {
ITransducer<Integer, Integer> xf = takeWhile(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
return integer < 10;
}
});
List<Integer> ten = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(20));
Integer[] expected = {0,1,2,3,4,5,6,7,8,9};
assertTrue(ten.equals(Arrays.asList(expected)));
}
public void testDrop() throws Exception {
ITransducer<Integer, Integer> xf = drop(5);
List<Integer> five = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(10));
Integer[] expected = {5,6,7,8,9};
assertTrue(five.equals(Arrays.asList(expected)));
}
public void testDropWhile() throws Exception {
ITransducer<Integer, Integer> xf = dropWhile(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
return integer < 10;
}
});
List<Integer> ten = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(20));
Integer[] expected = {10,11,12,13,14,15,16,17,18,19};
assertTrue(ten.equals(Arrays.asList(expected)));
}
public void testTakeNth() throws Exception {
ITransducer<Integer, Integer> xf = takeNth(2);
List<Integer> evens = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(10));
Integer[] expected = {0,2,4,6,8};
assertTrue(evens.equals(Arrays.asList(expected)));
}
public void testReplace() throws Exception {
ITransducer<Integer, Integer> xf = replace(new HashMap<Integer, Integer>() {{ put(3, 42); }});
List<Integer> evens = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(5));
Integer[] expected = {0,1,2,42,4};
assertTrue(evens.equals(Arrays.asList(expected)));
}
public void testKeep() throws Exception {
ITransducer<Integer, Integer> xf = keep(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return (integer % 2 == 0) ? null : integer;
}
});
List<Integer> odds = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(10));
Integer[] expected = {1,3,5,7,9};
assertTrue(odds.equals(Arrays.asList(expected)));
}
public void testKeepIndexed() throws Exception {
ITransducer<Integer, Integer> xf = keepIndexed(new BiFunction<Long, Integer, Integer>() {
@Override
public Integer apply(Long idx, Integer integer) {
return (idx == 1l || idx == 4l) ? integer : null;
}
});
List<Integer> nums = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), ints(10));
Integer[] expected = {0,3};
assertTrue(nums.equals(Arrays.asList(expected)));
}
public void testDedupe() throws Exception {
Integer[] seed = {1,2,2,3,4,5,5,5,5,5,5,5,0};
ITransducer<Integer, Integer> xf = dedupe();
List<Integer> nums = transduce(xf, new IStepFunction<List<Integer>, Integer>() {
@Override
public List<Integer> apply(List<Integer> result, Integer input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Integer>(), Arrays.asList(seed));
Integer[] expected = {1,2,3,4,5,0};
assertTrue(nums.equals(Arrays.asList(expected)));
}
public void testPartitionBy() throws Exception {
Integer[] seed = {1,1,1,2,2,3,4,5,5};
ITransducer<Iterable<Integer>, Integer> xf = partitionBy(new Function<Integer, Integer>() {
@Override
public Integer apply(final Integer integer) {
return integer;
}
});
List<List<Integer>> vals = transduce(xf, new IStepFunction<List<List<Integer>>, Iterable<Integer>>() {
@Override
public List<List<Integer>> apply(List<List<Integer>> result, Iterable<Integer> input, AtomicBoolean reduced) {
List<Integer> ret = new ArrayList<Integer>();
for (Integer i : input) {
ret.add(i);
}
result.add(ret);
return result;
}
}, new ArrayList<List<Integer>>(), Arrays.asList(seed));
final Integer[] a = {1,1,1};
final Integer[] b = {2,2};
final Integer[] c = {3};
final Integer[] d = {4};
final Integer[] e = {5,5};
List<List<Integer>> expected = new ArrayList<List<Integer>>() {{
add(Arrays.asList(a));
add(Arrays.asList(b));
add(Arrays.asList(c));
add(Arrays.asList(d));
add(Arrays.asList(e));
}};
assertTrue(vals.size() == 5);
for(int i=0; i<expected.size(); i++) {
assertEquals(vals.get(i).size(),expected.get(i).size());
assertTrue(vals.get(i).equals(expected.get(i)));
}
}
public void testPartitionAll() throws Exception {
ITransducer<Iterable<Integer>, Integer> xf = partitionAll(3);
List<List<Integer>> vals = transduce(xf, new IStepFunction<List<List<Integer>>, Iterable<Integer>>() {
@Override
public List<List<Integer>> apply(List<List<Integer>> result, Iterable<Integer> input, AtomicBoolean reduced) {
List<Integer> ret = new ArrayList<Integer>();
for (Integer i : input) {
ret.add(i);
}
result.add(ret);
return result;
}
}, new ArrayList<List<Integer>>(), ints(10));
final Integer[] a = {0,1,2};
final Integer[] b = {3,4,5};
final Integer[] c = {6,7,8};
final Integer[] d = {9};
List<List<Integer>> expected = new ArrayList<List<Integer>>() {{
add(Arrays.asList(a));
add(Arrays.asList(b));
add(Arrays.asList(c));
add(Arrays.asList(d));
}};
assertTrue(vals.size() == 4);
for(int i=0; i<expected.size(); i++) {
assertEquals(vals.get(i).size(),expected.get(i).size());
assertTrue(vals.get(i).equals(expected.get(i)));
}
}
public void testSimpleCovariance() throws Exception {
ITransducer<Integer, Integer> m = map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i * 2;
}
});
List<Integer> input = new ArrayList<Integer>() {{
for(int i : ints(20)) {
add(i);
}
}};
Collection<Number> res = transduce(m, new IStepFunction<Collection<Number>, Number>() {
@Override
public Collection<Number> apply(Collection<Number> result, Number input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Number>(input.size()), input);
assertEquals(20, res.size());
ITransducer<Number, Number> f = filter(new Predicate<Number>() {
@Override
public boolean test(Number number) {
return number.doubleValue() > 10.0;
}
});
res = transduce(m.comp(f), new IStepFunction<Collection<Number>, Number>() {
@Override
public Collection<Number> apply(Collection<Number> result, Number input, AtomicBoolean reduced) {
result.add(input);
return result;
}
}, new ArrayList<Number>(input.size()), input);
assertEquals(14, res.size());
}
}