-
Notifications
You must be signed in to change notification settings - Fork 587
/
LazyIterateTest.java
270 lines (250 loc) · 10.3 KB
/
LazyIterateTest.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
/*
* Copyright (c) 2015 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.utility;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.Procedures;
import org.eclipse.collections.impl.block.function.AddFunction;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.math.IntegerSum;
import org.eclipse.collections.impl.math.Sum;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.Tuples;
import org.junit.Assert;
import org.junit.Test;
public class LazyIterateTest
{
@Test
public void selectForEach()
{
LazyIterable<Integer> select = LazyIterate.select(Interval.oneTo(5), Predicates.lessThan(5));
int sum = select.injectInto(0, AddFunction.INTEGER_TO_INT);
Assert.assertEquals(10, sum);
}
@Test
public void selectForEachWithIndex()
{
LazyIterable<Integer> select = LazyIterate.select(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
select.forEachWithIndex((object, index) -> {
sum.add(object);
sum.add(index);
});
Assert.assertEquals(16, sum.getValue().intValue());
}
@Test
public void selectIterator()
{
LazyIterable<Integer> select = LazyIterate.select(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
for (Integer each : select)
{
sum.add(each);
}
Assert.assertEquals(10, sum.getValue().intValue());
}
@Test
public void selectForEachWith()
{
LazyIterable<Integer> select = LazyIterate.select(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
select.forEachWith((each, aSum) -> aSum.add(each), sum);
Assert.assertEquals(10, sum.getValue().intValue());
}
@Test
public void rejectForEach()
{
LazyIterable<Integer> select = LazyIterate.reject(Interval.oneTo(5), Predicates.lessThan(5));
int sum = select.injectInto(0, AddFunction.INTEGER_TO_INT);
Assert.assertEquals(5, sum);
}
@Test
public void rejectForEachWithIndex()
{
LazyIterable<Integer> select = LazyIterate.reject(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
select.forEachWithIndex((object, index) -> {
sum.add(object);
sum.add(index);
});
Assert.assertEquals(5, sum.getValue().intValue());
}
@Test
public void rejectIterator()
{
LazyIterable<Integer> select = LazyIterate.reject(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
for (Integer each : select)
{
sum.add(each);
}
Assert.assertEquals(5, sum.getValue().intValue());
}
@Test
public void rejectForEachWith()
{
LazyIterable<Integer> select = LazyIterate.reject(Interval.oneTo(5), Predicates.lessThan(5));
Sum sum = new IntegerSum(0);
select.forEachWith((each, aSum) -> aSum.add(each), sum);
Assert.assertEquals(5, sum.getValue().intValue());
}
@Test
public void collectForEach()
{
LazyIterable<String> select = LazyIterate.collect(Interval.oneTo(5), String::valueOf);
Appendable builder = new StringBuilder();
Procedure<String> appendProcedure = Procedures.append(builder);
select.forEach(appendProcedure);
Assert.assertEquals("12345", builder.toString());
}
@Test
public void collectForEachWithIndex()
{
LazyIterable<String> select = LazyIterate.collect(Interval.oneTo(5), String::valueOf);
StringBuilder builder = new StringBuilder("");
select.forEachWithIndex((object, index) -> {
builder.append(object);
builder.append(index);
});
Assert.assertEquals("1021324354", builder.toString());
}
@Test
public void collectIterator()
{
LazyIterable<String> select = LazyIterate.collect(Interval.oneTo(5), String::valueOf);
StringBuilder builder = new StringBuilder("");
for (String each : select)
{
builder.append(each);
}
Assert.assertEquals("12345", builder.toString());
}
@Test
public void collectForEachWith()
{
LazyIterable<String> select = LazyIterate.collect(Interval.oneTo(5), String::valueOf);
StringBuilder builder = new StringBuilder("");
select.forEachWith((each, aBuilder) -> aBuilder.append(each), builder);
Assert.assertEquals("12345", builder.toString());
}
@Test
public void asDeferred()
{
MutableList<Integer> expected = FastList.newList(Interval.oneTo(5));
MutableList<Integer> actual0 = LazyIterate.adapt(Interval.oneTo(5)).toList();
MutableList<Integer> actual1 = Interval.oneTo(5).asLazy().toList();
MutableList<Integer> actual2 = FastList.newList(Interval.oneTo(5)).asLazy().toList();
MutableList<Integer> actual3 = actual2.asUnmodifiable().asLazy().toList();
MutableList<Integer> actual4 = actual2.asSynchronized().asLazy().toList();
MutableList<Integer> actual5 = actual2.asLazy().select(ignored -> true).toList();
MutableList<Integer> actual6 = actual2.toImmutable().asLazy().toList();
ImmutableList<Integer> actual7 = actual2.asLazy().toList().toImmutable();
Assert.assertEquals(expected, actual0);
Assert.assertEquals(expected, actual1);
Assert.assertEquals(expected, actual2);
Assert.assertEquals(expected, actual3);
Assert.assertEquals(expected, actual4);
Assert.assertEquals(expected, actual5);
Assert.assertEquals(expected, actual6);
Assert.assertEquals(expected, actual7);
}
@Test
public void classIsNonInstantiable()
{
Verify.assertClassNonInstantiable(LazyIterate.class);
}
@Test
public void cartesianProduct()
{
MutableList<Integer> iterable1 = Lists.mutable.with(1, 2);
MutableList<Integer> iterable2 = Lists.mutable.with(2, 3, 4);
MutableBag<Pair<Integer, Integer>> expectedCartesianProduct1 = Bags.mutable.with(
Tuples.pair(1, 2),
Tuples.pair(2, 2),
Tuples.pair(1, 3),
Tuples.pair(2, 3),
Tuples.pair(1, 4),
Tuples.pair(2, 4));
Assert.assertEquals(expectedCartesianProduct1, LazyIterate.cartesianProduct(iterable1, iterable2).toBag());
MutableBag<Pair<Integer, Integer>> expectedCartesianProduct2 = Bags.mutable.with(
Tuples.pair(2, 1),
Tuples.pair(3, 1),
Tuples.pair(4, 1),
Tuples.pair(2, 2),
Tuples.pair(3, 2),
Tuples.pair(4, 2));
Assert.assertEquals(expectedCartesianProduct2, LazyIterate.cartesianProduct(iterable2, iterable1).toBag());
}
@Test
public void cartesianProductDuplicatesToConcreteCollections()
{
MutableList<Integer> iterable1 = Lists.mutable.with(1, 1);
MutableList<Integer> iterable2 = Lists.mutable.with(2, 2);
MutableBag<Pair<Integer, Integer>> expectedBag = Bags.mutable.with(
Tuples.pair(1, 2),
Tuples.pair(1, 2),
Tuples.pair(1, 2),
Tuples.pair(1, 2));
Assert.assertEquals(expectedBag, LazyIterate.cartesianProduct(iterable1, iterable2).toBag());
MutableSet<Pair<Integer, Integer>> expectedSet = Sets.mutable.with(Tuples.pair(1, 2));
Assert.assertEquals(expectedSet, LazyIterate.cartesianProduct(iterable1, iterable2).toSet());
MutableList<Pair<Integer, Integer>> expectedList = Lists.mutable.with(
Tuples.pair(1, 2),
Tuples.pair(1, 2),
Tuples.pair(1, 2),
Tuples.pair(1, 2));
Assert.assertEquals(expectedList, LazyIterate.cartesianProduct(iterable1, iterable2).toList());
}
@Test
public void cartesianProductWithFunction()
{
MutableList<Integer> iterable1 = Lists.mutable.with(1, 2);
MutableList<Integer> iterable2 = Lists.mutable.with(2, 3, 4);
MutableBag<Pair<Integer, Integer>> expectedCartesianProduct = Bags.mutable.with(
Tuples.pair(1, 2),
Tuples.pair(2, 2),
Tuples.pair(1, 3),
Tuples.pair(2, 3),
Tuples.pair(1, 4),
Tuples.pair(2, 4));
Assert.assertEquals(
expectedCartesianProduct,
LazyIterate.cartesianProduct(iterable1, iterable2, Tuples::pair).toBag());
MutableBag<MutableList<Integer>> expectedCartesianProduct2 = Bags.mutable.with(
Lists.mutable.with(2, 1),
Lists.mutable.with(3, 1),
Lists.mutable.with(4, 1),
Lists.mutable.with(2, 2),
Lists.mutable.with(3, 2),
Lists.mutable.with(4, 2));
Assert.assertEquals(
expectedCartesianProduct2,
LazyIterate.cartesianProduct(iterable2, iterable1, Lists.mutable::with).toBag());
}
@Test
public void cartesianProduct_empty()
{
Assert.assertEquals(
Bags.mutable.empty(),
LazyIterate.cartesianProduct(
Lists.mutable.with(1, 2),
Lists.mutable.empty()).toBag());
}
}