~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to unit-tests/src/test/java/com/gs/collections/impl/utility/internal/RandomAccessListIterateTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package com.gs.collections.impl.utility.internal;
 
18
 
 
19
import java.util.ArrayList;
 
20
import java.util.Collection;
 
21
import java.util.List;
 
22
 
 
23
import com.gs.collections.api.block.function.Function2;
 
24
import com.gs.collections.api.block.predicate.Predicate2;
 
25
import com.gs.collections.api.block.procedure.Procedure2;
 
26
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
 
27
import com.gs.collections.api.list.MutableList;
 
28
import com.gs.collections.api.set.MutableSet;
 
29
import com.gs.collections.api.tuple.Pair;
 
30
import com.gs.collections.api.tuple.Twin;
 
31
import com.gs.collections.impl.block.factory.ObjectIntProcedures;
 
32
import com.gs.collections.impl.block.factory.Predicates;
 
33
import com.gs.collections.impl.block.factory.Predicates2;
 
34
import com.gs.collections.impl.block.factory.Procedures2;
 
35
import com.gs.collections.impl.block.function.AddFunction;
 
36
import com.gs.collections.impl.block.function.MaxSizeFunction;
 
37
import com.gs.collections.impl.block.function.MinSizeFunction;
 
38
import com.gs.collections.impl.block.procedure.CollectionAddProcedure;
 
39
import com.gs.collections.impl.block.procedure.DoNothingProcedure;
 
40
import com.gs.collections.impl.factory.Lists;
 
41
import com.gs.collections.impl.list.Interval;
 
42
import com.gs.collections.impl.list.mutable.FastList;
 
43
import com.gs.collections.impl.set.mutable.UnifiedSet;
 
44
import com.gs.collections.impl.test.Verify;
 
45
import com.gs.collections.impl.tuple.Tuples;
 
46
import com.gs.collections.impl.utility.Iterate;
 
47
import com.gs.collections.impl.utility.ListIterate;
 
48
import org.junit.Assert;
 
49
import org.junit.Test;
 
50
 
 
51
import static com.gs.collections.impl.factory.Iterables.*;
 
52
 
 
53
public class RandomAccessListIterateTest
 
54
{
 
55
    @Test(expected = IllegalArgumentException.class)
 
56
    public void forEachWithNegativeFroms()
 
57
    {
 
58
        RandomAccessListIterate.forEach(FastList.newList(), -1, 1, DoNothingProcedure.DO_NOTHING);
 
59
    }
 
60
 
 
61
    @Test(expected = IllegalArgumentException.class)
 
62
    public void forEachWithNegativeTos()
 
63
    {
 
64
        RandomAccessListIterate.forEach(FastList.newList(), 1, -1, DoNothingProcedure.DO_NOTHING);
 
65
    }
 
66
 
 
67
    @Test
 
68
    public void forEachInBothWithNull()
 
69
    {
 
70
        RandomAccessListIterate.forEachInBoth(null, FastList.newListWith(1, 2, 3), new FailProcedure2());
 
71
        RandomAccessListIterate.forEachInBoth(FastList.newListWith(1, 2, 3), null, new FailProcedure2());
 
72
    }
 
73
 
 
74
    @Test(expected = IllegalArgumentException.class)
 
75
    public void forEachInBothThrowsOnMisMatchedLists()
 
76
    {
 
77
        RandomAccessListIterate.forEachInBoth(FastList.newListWith("1", 2), FastList.newListWith(1, 2, 3),
 
78
                Procedures2.fromProcedure(DoNothingProcedure.DO_NOTHING));
 
79
    }
 
80
 
 
81
    @Test
 
82
    public void removeIf()
 
83
    {
 
84
        List<Integer> result = RandomAccessListIterate.removeIf(FastList.newListWith(1, 2, 3), Predicates.greaterThan(1));
 
85
        Verify.assertSize(1, result);
 
86
    }
 
87
 
 
88
    @Test
 
89
    public void dropWithLargeCount()
 
90
    {
 
91
        Verify.assertEmpty(RandomAccessListIterate.drop(FastList.newListWith(1, 2, 3), 5));
 
92
    }
 
93
 
 
94
    @Test
 
95
    public void injectInto()
 
96
    {
 
97
        MutableList<Integer> list = Lists.fixedSize.of(1, 2, 3);
 
98
        Assert.assertEquals(Integer.valueOf(7), RandomAccessListIterate.injectInto(1, list, AddFunction.INTEGER));
 
99
    }
 
100
 
 
101
    @Test
 
102
    public void injectIntoInt()
 
103
    {
 
104
        MutableList<Integer> list = Lists.fixedSize.of(1, 2, 3);
 
105
        Assert.assertEquals(7, RandomAccessListIterate.injectInto(1, list, AddFunction.INTEGER_TO_INT));
 
106
    }
 
107
 
 
108
    @Test
 
109
    public void injectIntoLong()
 
110
    {
 
111
        MutableList<Integer> list = Lists.fixedSize.of(1, 2, 3);
 
112
        Assert.assertEquals(7, RandomAccessListIterate.injectInto(1, list, AddFunction.INTEGER_TO_LONG));
 
113
    }
 
114
 
 
115
    @Test
 
116
    public void injectIntoDouble()
 
117
    {
 
118
        MutableList<Double> list = Lists.fixedSize.of(1.0, 2.0, 3.0);
 
119
        Assert.assertEquals(7.0d, RandomAccessListIterate.injectInto(1.0, list, AddFunction.DOUBLE), 0.001);
 
120
    }
 
121
 
 
122
    @Test
 
123
    public void injectIntoString()
 
124
    {
 
125
        MutableList<String> list = Lists.fixedSize.of("1", "2", "3");
 
126
        Assert.assertEquals("0123", RandomAccessListIterate.injectInto("0", list, AddFunction.STRING));
 
127
    }
 
128
 
 
129
    @Test
 
130
    public void injectIntoMaxString()
 
131
    {
 
132
        MutableList<String> list = Lists.fixedSize.of("1", "12", "123");
 
133
        Function2<Integer, String, Integer> function = MaxSizeFunction.STRING;
 
134
        Assert.assertEquals(Integer.valueOf(3), RandomAccessListIterate.injectInto(Integer.MIN_VALUE, list, function));
 
135
    }
 
136
 
 
137
    @Test
 
138
    public void injectIntoMinString()
 
139
    {
 
140
        MutableList<String> list = Lists.fixedSize.of("1", "12", "123");
 
141
        Function2<Integer, String, Integer> function = MinSizeFunction.STRING;
 
142
        Assert.assertEquals(Integer.valueOf(1), RandomAccessListIterate.injectInto(Integer.MAX_VALUE, list, function));
 
143
    }
 
144
 
 
145
    @Test
 
146
    public void collect()
 
147
    {
 
148
        Assert.assertEquals(
 
149
                iList("true", "false", "null"),
 
150
                RandomAccessListIterate.collect(mList(true, false, null), String::valueOf));
 
151
    }
 
152
 
 
153
    @Test
 
154
    public void collectReflective()
 
155
    {
 
156
        Assert.assertEquals(
 
157
                iList("true", "false", "null"),
 
158
                RandomAccessListIterate.collect(mList(true, false, null), String::valueOf));
 
159
 
 
160
        Assert.assertEquals(
 
161
                iList("true", "false", "null"),
 
162
                RandomAccessListIterate.collect(mList(true, false, null), String::valueOf, new ArrayList<String>()));
 
163
    }
 
164
 
 
165
    @Test
 
166
    public void flattenReflective()
 
167
    {
 
168
        MutableList<MutableList<Boolean>> list = Lists.fixedSize.<MutableList<Boolean>>of(
 
169
                Lists.fixedSize.of(true, false),
 
170
                Lists.fixedSize.of(true, null));
 
171
        MutableList<Boolean> newList = RandomAccessListIterate.flatCollect(list, mutableList -> mutableList.toList());
 
172
        Verify.assertListsEqual(
 
173
                FastList.newListWith(true, false, true, null),
 
174
                newList);
 
175
 
 
176
        MutableSet<Boolean> newSet = RandomAccessListIterate.flatCollect(list, mutableList -> mutableList.toSet(), UnifiedSet.<Boolean>newSet());
 
177
        Verify.assertSetsEqual(
 
178
                UnifiedSet.newSetWith(true, false, null),
 
179
                newSet);
 
180
    }
 
181
 
 
182
    @Test
 
183
    public void getLast()
 
184
    {
 
185
        MutableList<Boolean> list = Lists.fixedSize.of(true, null, false);
 
186
        Assert.assertEquals(Boolean.FALSE, RandomAccessListIterate.getLast(list));
 
187
    }
 
188
 
 
189
    @Test
 
190
    public void getLastOnEmpty()
 
191
    {
 
192
        List<?> list = new ArrayList<Object>();
 
193
        Assert.assertNull(RandomAccessListIterate.getLast(list));
 
194
    }
 
195
 
 
196
    @Test
 
197
    public void count()
 
198
    {
 
199
        MutableList<Integer> list = this.getIntegerList();
 
200
        int result = RandomAccessListIterate.count(list, Predicates.attributeEqual(Number::intValue, 3));
 
201
        Assert.assertEquals(1, result);
 
202
        int result2 = RandomAccessListIterate.count(list, Predicates.attributeEqual(Number::intValue, 6));
 
203
        Assert.assertEquals(0, result2);
 
204
    }
 
205
 
 
206
    private MutableList<Integer> getIntegerList()
 
207
    {
 
208
        return Interval.toReverseList(1, 5);
 
209
    }
 
210
 
 
211
    @Test
 
212
    public void forEachWithIndex()
 
213
    {
 
214
        MutableList<Integer> list = this.getIntegerList();
 
215
        Iterate.sortThis(list);
 
216
        RandomAccessListIterate.forEachWithIndex(list, (object, index) -> Assert.assertEquals(index, object - 1));
 
217
    }
 
218
 
 
219
    @Test
 
220
    public void forEachUsingFromTo()
 
221
    {
 
222
        MutableList<Integer> integers = Interval.oneTo(5).toList();
 
223
 
 
224
        this.assertForEachUsingFromTo(integers);
 
225
        MutableList<Integer> reverseResults = Lists.mutable.of();
 
226
        CollectionAddProcedure<Integer> procedure = CollectionAddProcedure.on(reverseResults);
 
227
        this.assertReverseForEachUsingFromTo(integers, reverseResults, procedure);
 
228
    }
 
229
 
 
230
    private void assertForEachUsingFromTo(List<Integer> integers)
 
231
    {
 
232
        MutableList<Integer> results = Lists.mutable.of();
 
233
        RandomAccessListIterate.forEach(integers, 0, 4, CollectionAddProcedure.on(results));
 
234
        Assert.assertEquals(integers, results);
 
235
        MutableList<Integer> reverseResults = Lists.mutable.of();
 
236
        CollectionAddProcedure<Integer> procedure = CollectionAddProcedure.on(reverseResults);
 
237
 
 
238
        Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEach(integers, 4, -1, procedure));
 
239
        Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEach(integers, -1, 4, procedure));
 
240
    }
 
241
 
 
242
    private void assertReverseForEachUsingFromTo(List<Integer> integers, MutableList<Integer> reverseResults, CollectionAddProcedure<Integer> procedure)
 
243
    {
 
244
        RandomAccessListIterate.forEach(integers, 4, 0, procedure);
 
245
        Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults);
 
246
    }
 
247
 
 
248
    @Test
 
249
    public void forEachWithIndexUsingFromTo()
 
250
    {
 
251
        MutableList<Integer> integers = Interval.oneTo(5).toList();
 
252
        this.assertForEachWithIndexUsingFromTo(integers);
 
253
        MutableList<Integer> reverseResults = Lists.mutable.of();
 
254
        ObjectIntProcedure<Integer> objectIntProcedure = ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(reverseResults));
 
255
        this.assertReverseForEachIndexUsingFromTo(integers, reverseResults, objectIntProcedure);
 
256
    }
 
257
 
 
258
    private void assertForEachWithIndexUsingFromTo(List<Integer> integers)
 
259
    {
 
260
        MutableList<Integer> results = Lists.mutable.of();
 
261
        RandomAccessListIterate.forEachWithIndex(integers, 0, 4, ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(results)));
 
262
        Assert.assertEquals(integers, results);
 
263
        MutableList<Integer> reverseResults = Lists.mutable.of();
 
264
        ObjectIntProcedure<Integer> objectIntProcedure = ObjectIntProcedures.fromProcedure(CollectionAddProcedure.on(reverseResults));
 
265
        Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEachWithIndex(integers, 4, -1, objectIntProcedure));
 
266
        Verify.assertThrows(IllegalArgumentException.class, () -> RandomAccessListIterate.forEachWithIndex(integers, -1, 4, objectIntProcedure));
 
267
    }
 
268
 
 
269
    private void assertReverseForEachIndexUsingFromTo(MutableList<Integer> integers, MutableList<Integer> reverseResults, ObjectIntProcedure<Integer> objectIntProcedure)
 
270
    {
 
271
        RandomAccessListIterate.forEachWithIndex(integers, 4, 0, objectIntProcedure);
 
272
        Assert.assertEquals(ListIterate.reverseThis(integers), reverseResults);
 
273
    }
 
274
 
 
275
    @Test
 
276
    public void forEachInBoth()
 
277
    {
 
278
        MutableList<String> list1 = Lists.fixedSize.of("1", "2");
 
279
        MutableList<String> list2 = Lists.fixedSize.of("a", "b");
 
280
        List<Pair<String, String>> list = new ArrayList<Pair<String, String>>();
 
281
        RandomAccessListIterate.forEachInBoth(list1, list2, (argument1, argument2) -> {
 
282
            list.add(Tuples.twin(argument1, argument2));
 
283
        });
 
284
        Assert.assertEquals(FastList.newListWith(Tuples.twin("1", "a"), Tuples.twin("2", "b")), list);
 
285
    }
 
286
 
 
287
    @Test
 
288
    public void detectWith()
 
289
    {
 
290
        MutableList<Integer> list = this.getIntegerList();
 
291
        Assert.assertEquals(Integer.valueOf(1), RandomAccessListIterate.detectWith(list, Object::equals, 1));
 
292
        MutableList<Integer> list2 = Lists.fixedSize.of(1, 2, 2);
 
293
        Assert.assertSame(list2.get(1), RandomAccessListIterate.detectWith(list2, Object::equals, 2));
 
294
    }
 
295
 
 
296
    @Test
 
297
    public void selectWith()
 
298
    {
 
299
        MutableList<Integer> list = this.getIntegerList();
 
300
        Verify.assertSize(5, RandomAccessListIterate.selectWith(list, Predicates2.instanceOf(), Integer.class));
 
301
    }
 
302
 
 
303
    @Test
 
304
    public void rejectWith()
 
305
    {
 
306
        MutableList<Integer> list = this.getIntegerList();
 
307
        Verify.assertEmpty(RandomAccessListIterate.rejectWith(list, Predicates2.instanceOf(), Integer.class));
 
308
    }
 
309
 
 
310
    @Test
 
311
    public void distinct()
 
312
    {
 
313
        MutableList<Integer> list = FastList.newListWith(5, 2, 6, 2, 3, 5, 2);
 
314
        MutableList<Integer> actualList = FastList.newList();
 
315
        RandomAccessListIterate.distinct(list, actualList);
 
316
        Verify.assertListsEqual(FastList.newListWith(5, 2, 6, 3), actualList);
 
317
        Verify.assertSize(7, list);
 
318
    }
 
319
 
 
320
    @Test
 
321
    public void selectAndRejectWith()
 
322
    {
 
323
        MutableList<Integer> list = this.getIntegerList();
 
324
        Twin<MutableList<Integer>> result = RandomAccessListIterate.selectAndRejectWith(list, Predicates2.in(), Lists.fixedSize.of(1));
 
325
        Verify.assertSize(1, result.getOne());
 
326
        Verify.assertSize(4, result.getTwo());
 
327
    }
 
328
 
 
329
    @Test
 
330
    public void anySatisfyWith()
 
331
    {
 
332
        MutableList<Integer> list = this.getIntegerList();
 
333
        Assert.assertTrue(RandomAccessListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Integer.class));
 
334
        Assert.assertFalse(RandomAccessListIterate.anySatisfyWith(list, Predicates2.instanceOf(), Double.class));
 
335
    }
 
336
 
 
337
    @Test
 
338
    public void allSatisfyWith()
 
339
    {
 
340
        MutableList<Integer> list = this.getIntegerList();
 
341
        Assert.assertTrue(RandomAccessListIterate.allSatisfyWith(list, Predicates2.instanceOf(), Integer.class));
 
342
        Predicate2<Integer, Integer> greaterThanPredicate = Predicates2.greaterThan();
 
343
        Assert.assertFalse(RandomAccessListIterate.allSatisfyWith(list, greaterThanPredicate, 2));
 
344
    }
 
345
 
 
346
    @Test
 
347
    public void countWith()
 
348
    {
 
349
        Assert.assertEquals(5, RandomAccessListIterate.countWith(this.getIntegerList(), Predicates2.instanceOf(), Integer.class));
 
350
    }
 
351
 
 
352
    @Test
 
353
    public void collectIf()
 
354
    {
 
355
        MutableList<Integer> integers = Lists.fixedSize.of(1, 2, 3);
 
356
        Verify.assertContainsAll(RandomAccessListIterate.collectIf(integers, Integer.class::isInstance, String::valueOf), "1", "2", "3");
 
357
        Verify.assertContainsAll(RandomAccessListIterate.collectIf(integers, Integer.class::isInstance, String::valueOf, new ArrayList<String>()), "1", "2", "3");
 
358
    }
 
359
 
 
360
    @Test
 
361
    public void take()
 
362
    {
 
363
        MutableList<Integer> integers = this.getIntegerList();
 
364
        Collection<Integer> results = RandomAccessListIterate.take(integers, 2);
 
365
        Assert.assertEquals(FastList.newListWith(5, 4), results);
 
366
 
 
367
        Verify.assertSize(0, RandomAccessListIterate.take(integers, 0));
 
368
        Verify.assertSize(5, RandomAccessListIterate.take(integers, 5));
 
369
        Verify.assertSize(5, RandomAccessListIterate.take(integers, 10));
 
370
 
 
371
        MutableList<Integer> integers2 = Lists.fixedSize.of();
 
372
        Verify.assertSize(0, RandomAccessListIterate.take(integers2, 2));
 
373
 
 
374
        Verify.assertSize(0, RandomAccessListIterate.take(Lists.fixedSize.of(), 2));
 
375
    }
 
376
 
 
377
    @Test(expected = IllegalArgumentException.class)
 
378
    public void take_throws()
 
379
    {
 
380
        RandomAccessListIterate.take(this.getIntegerList(), -1);
 
381
    }
 
382
 
 
383
    @Test
 
384
    public void drop()
 
385
    {
 
386
        MutableList<Integer> integers = this.getIntegerList();
 
387
        MutableList<Integer> results = RandomAccessListIterate.drop(integers, 2);
 
388
        Assert.assertEquals(FastList.newListWith(3, 2, 1), results);
 
389
 
 
390
        Verify.assertSize(0, RandomAccessListIterate.drop(integers, 5));
 
391
        Verify.assertSize(0, RandomAccessListIterate.drop(integers, 6));
 
392
        Verify.assertSize(5, RandomAccessListIterate.drop(integers, 0));
 
393
 
 
394
        MutableList<Integer> integers2 = Lists.fixedSize.of();
 
395
        Verify.assertSize(0, RandomAccessListIterate.drop(integers2, 2));
 
396
 
 
397
        Verify.assertSize(0, RandomAccessListIterate.drop(Lists.fixedSize.<Integer>of(), 2));
 
398
    }
 
399
 
 
400
    @Test(expected = IllegalArgumentException.class)
 
401
    public void drop_throws()
 
402
    {
 
403
        RandomAccessListIterate.drop(this.getIntegerList(), -1);
 
404
    }
 
405
 
 
406
    private static class FailProcedure2 implements Procedure2<Object, Integer>
 
407
    {
 
408
        private static final long serialVersionUID = 1L;
 
409
 
 
410
        public void value(Object argument1, Integer argument2)
 
411
        {
 
412
            Assert.fail();
 
413
        }
 
414
    }
 
415
 
 
416
    @Test
 
417
    public void classIsNonInstantiable()
 
418
    {
 
419
        Verify.assertClassNonInstantiable(RandomAccessListIterate.class);
 
420
    }
 
421
}