~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/ArrayIterateTest.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;
 
18
 
 
19
import java.io.IOException;
 
20
import java.util.ArrayList;
 
21
import java.util.Collections;
 
22
import java.util.List;
 
23
 
 
24
import com.gs.collections.api.RichIterable;
 
25
import com.gs.collections.api.block.function.Function;
 
26
import com.gs.collections.api.list.MutableList;
 
27
import com.gs.collections.api.list.primitive.MutableBooleanList;
 
28
import com.gs.collections.api.map.MutableMap;
 
29
import com.gs.collections.api.multimap.Multimap;
 
30
import com.gs.collections.api.multimap.MutableMultimap;
 
31
import com.gs.collections.api.partition.PartitionIterable;
 
32
import com.gs.collections.api.tuple.Pair;
 
33
import com.gs.collections.api.tuple.Twin;
 
34
import com.gs.collections.impl.block.factory.Functions;
 
35
import com.gs.collections.impl.block.factory.Functions2;
 
36
import com.gs.collections.impl.block.factory.IntegerPredicates;
 
37
import com.gs.collections.impl.block.factory.Predicates;
 
38
import com.gs.collections.impl.block.factory.Predicates2;
 
39
import com.gs.collections.impl.block.factory.PrimitiveFunctions;
 
40
import com.gs.collections.impl.block.function.AddFunction;
 
41
import com.gs.collections.impl.block.function.MaxSizeFunction;
 
42
import com.gs.collections.impl.block.function.MinSizeFunction;
 
43
import com.gs.collections.impl.block.function.NegativeIntervalFunction;
 
44
import com.gs.collections.impl.block.procedure.MapPutProcedure;
 
45
import com.gs.collections.impl.factory.Lists;
 
46
import com.gs.collections.impl.list.Interval;
 
47
import com.gs.collections.impl.list.mutable.FastList;
 
48
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
 
49
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
 
50
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
 
51
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
 
52
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
 
53
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
 
54
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
 
55
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
 
56
import com.gs.collections.impl.map.mutable.UnifiedMap;
 
57
import com.gs.collections.impl.multimap.list.FastListMultimap;
 
58
import com.gs.collections.impl.test.Verify;
 
59
import org.junit.Assert;
 
60
import org.junit.Test;
 
61
 
 
62
import static com.gs.collections.impl.factory.Iterables.*;
 
63
 
 
64
public class ArrayIterateTest
 
65
{
 
66
    private static final Integer[] INTEGER_ARRAY = {5, 4, 3, 2, 1};
 
67
 
 
68
    @Test
 
69
    public void injectInto()
 
70
    {
 
71
        Integer[] objectArray = this.threeIntegerArray2();
 
72
        Assert.assertEquals(
 
73
                Integer.valueOf(7),
 
74
                ArrayIterate.injectInto(1, objectArray, AddFunction.INTEGER));
 
75
    }
 
76
 
 
77
    private Integer[] threeIntegerArray2()
 
78
    {
 
79
        return new Integer[]{1, 2, 3};
 
80
    }
 
81
 
 
82
    @Test
 
83
    public void injectIntoDouble()
 
84
    {
 
85
        Double[] objectArray = {(double) 1, (double) 2, (double) 3};
 
86
        Assert.assertEquals(
 
87
                new Double(1 + 1 + 2 + 3),
 
88
                ArrayIterate.injectInto((double) 1, objectArray, AddFunction.DOUBLE));
 
89
    }
 
90
 
 
91
    @Test
 
92
    public void injectIntoPrimitives()
 
93
    {
 
94
        double doubleActual = ArrayIterate.injectInto(1.0d, new Double[]{1.0d, 2.0d, 3.0d}, (doubleParameter, objectParameter) -> doubleParameter + objectParameter);
 
95
        Assert.assertEquals(7.0, doubleActual, 0.000001);
 
96
        long longActual = ArrayIterate.injectInto(1L, new Long[]{1L, 2L, 3L}, (long longParameter, Long objectParameter) -> longParameter + objectParameter);
 
97
        Assert.assertEquals(7L, longActual);
 
98
        int intActual = ArrayIterate.injectInto(1, new Integer[]{1, 2, 3}, (int intParameter, Integer objectParameter) -> intParameter + objectParameter);
 
99
        Assert.assertEquals(7, intActual);
 
100
    }
 
101
 
 
102
    @Test
 
103
    public void injectIntoThrowsOnNullArgument()
 
104
    {
 
105
        Verify.assertThrows(IllegalArgumentException.class, () -> {
 
106
            ArrayIterate.injectInto(0, null, (int intParameter, Object objectParameter) -> 0);
 
107
        });
 
108
        Verify.assertThrows(IllegalArgumentException.class, () -> {
 
109
            ArrayIterate.injectInto(0L, null, (long longParameter, Object objectParameter) -> 0);
 
110
        });
 
111
        Verify.assertThrows(IllegalArgumentException.class, () -> {
 
112
            ArrayIterate.injectInto((double) 0, null, (doubleParameter, objectParameter) -> 0.0);
 
113
        });
 
114
        Verify.assertThrows(IllegalArgumentException.class, () -> {
 
115
            ArrayIterate.injectInto(null, null, null);
 
116
        });
 
117
    }
 
118
 
 
119
    @Test(expected = IllegalArgumentException.class)
 
120
    public void allSatisfyThrowsOnNullArgument()
 
121
    {
 
122
        ArrayIterate.allSatisfy(null, null);
 
123
    }
 
124
 
 
125
    @Test(expected = IllegalArgumentException.class)
 
126
    public void allSatisfyWithThrowsOnNullArgument()
 
127
    {
 
128
        ArrayIterate.allSatisfyWith(null, null, null);
 
129
    }
 
130
 
 
131
    @Test(expected = IllegalArgumentException.class)
 
132
    public void anySatisfyThrowsOnNullArgument()
 
133
    {
 
134
        ArrayIterate.anySatisfy(null, null);
 
135
    }
 
136
 
 
137
    @Test(expected = IllegalArgumentException.class)
 
138
    public void anySatisfyWithThrowsOnNullArgument()
 
139
    {
 
140
        ArrayIterate.anySatisfyWith(null, null, null);
 
141
    }
 
142
 
 
143
    @Test(expected = IllegalArgumentException.class)
 
144
    public void noneSatisfyThrowsOnNullArgument()
 
145
    {
 
146
        ArrayIterate.noneSatisfy(null, null);
 
147
    }
 
148
 
 
149
    @Test(expected = IllegalArgumentException.class)
 
150
    public void noneSatisfyWithThrowsOnNullArgument()
 
151
    {
 
152
        ArrayIterate.noneSatisfyWith(null, null, null);
 
153
    }
 
154
 
 
155
    @Test(expected = IllegalArgumentException.class)
 
156
    public void selectThrowsOnNullArgument()
 
157
    {
 
158
        ArrayIterate.select(null, null);
 
159
    }
 
160
 
 
161
    @Test(expected = IllegalArgumentException.class)
 
162
    public void selectWithTargetThrowsOnNullArgument()
 
163
    {
 
164
        ArrayIterate.select(null, null, null);
 
165
    }
 
166
 
 
167
    @Test(expected = IllegalArgumentException.class)
 
168
    public void selectWithThrowsOnNullArgument()
 
169
    {
 
170
        ArrayIterate.selectWith(null, null, null);
 
171
    }
 
172
 
 
173
    @Test(expected = IllegalArgumentException.class)
 
174
    public void selectWithWithTargetThrowsOnNullArgument()
 
175
    {
 
176
        ArrayIterate.selectWith(null, null, null, null);
 
177
    }
 
178
 
 
179
    @Test(expected = IllegalArgumentException.class)
 
180
    public void rejectThrowsOnNullArgument()
 
181
    {
 
182
        ArrayIterate.reject(null, null);
 
183
    }
 
184
 
 
185
    @Test(expected = IllegalArgumentException.class)
 
186
    public void rejectWithTargetThrowsOnNullArgument()
 
187
    {
 
188
        ArrayIterate.reject(null, null, null);
 
189
    }
 
190
 
 
191
    @Test(expected = IllegalArgumentException.class)
 
192
    public void rejectWithThrowsOnNullArgument()
 
193
    {
 
194
        ArrayIterate.rejectWith(null, null, null);
 
195
    }
 
196
 
 
197
    @Test(expected = IllegalArgumentException.class)
 
198
    public void rejectWithWithTargetThrowsOnNullArgument()
 
199
    {
 
200
        ArrayIterate.rejectWith(null, null, null, null);
 
201
    }
 
202
 
 
203
    @Test(expected = IllegalArgumentException.class)
 
204
    public void collectThrowsOnNullArgument()
 
205
    {
 
206
        ArrayIterate.collect(null, null);
 
207
    }
 
208
 
 
209
    @Test(expected = IllegalArgumentException.class)
 
210
    public void collectIfThrowsOnNullArgument()
 
211
    {
 
212
        ArrayIterate.collectIf(null, null, null);
 
213
    }
 
214
 
 
215
    @Test(expected = IllegalArgumentException.class)
 
216
    public void collectWithTargetThrowsOnNullArgument()
 
217
    {
 
218
        ArrayIterate.collect(null, null, null);
 
219
    }
 
220
 
 
221
    @Test(expected = IllegalArgumentException.class)
 
222
    public void collectIfWithTargetThrowsOnNullArgument()
 
223
    {
 
224
        ArrayIterate.collectIf(null, null, null, null);
 
225
    }
 
226
 
 
227
    @Test(expected = IllegalArgumentException.class)
 
228
    public void collectWithThrowsOnNullArgument()
 
229
    {
 
230
        ArrayIterate.collectWith(null, null, null);
 
231
    }
 
232
 
 
233
    @Test(expected = IllegalArgumentException.class)
 
234
    public void collectWithWithTargetThrowsOnNullArgument()
 
235
    {
 
236
        ArrayIterate.collectWith(null, null, null, null);
 
237
    }
 
238
 
 
239
    @Test(expected = IllegalArgumentException.class)
 
240
    public void flatCollectThrowsOnNullArgument()
 
241
    {
 
242
        ArrayIterate.flatCollect(null, null);
 
243
    }
 
244
 
 
245
    @Test(expected = IllegalArgumentException.class)
 
246
    public void flatCollectWithTargetThrowsOnNullArgument()
 
247
    {
 
248
        ArrayIterate.flatCollect(null, null, null);
 
249
    }
 
250
 
 
251
    @Test(expected = IllegalArgumentException.class)
 
252
    public void forEachThrowsOnNullArgument()
 
253
    {
 
254
        ArrayIterate.forEach(null, null);
 
255
    }
 
256
 
 
257
    @Test(expected = IllegalArgumentException.class)
 
258
    public void forEachWithFromToThrowsOnNullArgument()
 
259
    {
 
260
        ArrayIterate.forEach(null, 0, 0, null);
 
261
    }
 
262
 
 
263
    @Test(expected = IllegalArgumentException.class)
 
264
    public void forEachWithIndexThrowsOnNullArgument()
 
265
    {
 
266
        ArrayIterate.forEachWithIndex(null, null);
 
267
    }
 
268
 
 
269
    @Test(expected = IllegalArgumentException.class)
 
270
    public void forEachWithIndexWithFromToThrowsOnNullArgument()
 
271
    {
 
272
        ArrayIterate.forEachWithIndex(null, 0, 0, null);
 
273
    }
 
274
 
 
275
    @Test
 
276
    public void partition()
 
277
    {
 
278
        PartitionIterable<Integer> result =
 
279
                ArrayIterate.partition(new Integer[]{1, 2, 3, 4, 5}, Predicates.greaterThan(3));
 
280
        Assert.assertEquals(iBag(4, 5), result.getSelected().toBag());
 
281
        Assert.assertEquals(iBag(1, 2, 3), result.getRejected().toBag());
 
282
    }
 
283
 
 
284
    @Test
 
285
    public void injectIntoString()
 
286
    {
 
287
        String[] objectArray = {"1", "2", "3"};
 
288
        Assert.assertEquals("0123", ArrayIterate.injectInto("0", objectArray, AddFunction.STRING));
 
289
    }
 
290
 
 
291
    //todo:review
 
292
    @Test
 
293
    public void injectIntoMaxString()
 
294
    {
 
295
        String[] objectArray = this.threeStringArray();
 
296
        Assert.assertEquals(Integer.valueOf(3),
 
297
                ArrayIterate.injectInto(Integer.MIN_VALUE, objectArray, MaxSizeFunction.STRING));
 
298
    }
 
299
 
 
300
    private String[] threeStringArray()
 
301
    {
 
302
        return new String[]{"1", "12", "123"};
 
303
    }
 
304
 
 
305
    //todo:review
 
306
    @Test
 
307
    public void injectIntoMinString()
 
308
    {
 
309
        String[] objectArray = this.threeStringArray();
 
310
        Assert.assertEquals(
 
311
                Integer.valueOf(1),
 
312
                ArrayIterate.injectInto(Integer.MAX_VALUE, objectArray, MinSizeFunction.STRING));
 
313
    }
 
314
 
 
315
    @Test
 
316
    public void collect()
 
317
    {
 
318
        Boolean[] objectArray = {true, false, null};
 
319
        Assert.assertEquals(
 
320
                iList("true", "false", "null"),
 
321
                ArrayIterate.collect(objectArray, String::valueOf));
 
322
    }
 
323
 
 
324
    @Test
 
325
    public void collectBoolean()
 
326
    {
 
327
        Integer[] objectArray = {-1, 0, 42};
 
328
        Assert.assertEquals(this.getExpectedBooleanResults(),
 
329
                ArrayIterate.collectBoolean(objectArray, PrimitiveFunctions.integerIsPositive()));
 
330
    }
 
331
 
 
332
    @Test
 
333
    public void collectBooleanWithTarget()
 
334
    {
 
335
        Integer[] objectArray = {-1, 0, 42};
 
336
        BooleanArrayList target = new BooleanArrayList();
 
337
        MutableBooleanList result = ArrayIterate.collectBoolean(objectArray, PrimitiveFunctions.integerIsPositive(), target);
 
338
        Assert.assertEquals(this.getExpectedBooleanResults(), result);
 
339
        Assert.assertSame("Target List not returned as result", target, result);
 
340
    }
 
341
 
 
342
    private BooleanArrayList getExpectedBooleanResults()
 
343
    {
 
344
        return BooleanArrayList.newListWith(false, false, true);
 
345
    }
 
346
 
 
347
    @Test
 
348
    public void collectByte()
 
349
    {
 
350
        Integer[] objectArray = {-1, 0, 42};
 
351
        Assert.assertEquals(this.getExpectedByteResults(),
 
352
                ArrayIterate.collectByte(objectArray, PrimitiveFunctions.unboxIntegerToByte()));
 
353
    }
 
354
 
 
355
    @Test
 
356
    public void collectByteWithTarget()
 
357
    {
 
358
        Integer[] objectArray = {-1, 0, 42};
 
359
        ByteArrayList target = new ByteArrayList();
 
360
        ByteArrayList result = ArrayIterate.collectByte(objectArray, PrimitiveFunctions.unboxIntegerToByte(), target);
 
361
        Assert.assertEquals(this.getExpectedByteResults(), result);
 
362
        Assert.assertSame("Target List not returned as result", target, result);
 
363
    }
 
364
 
 
365
    private ByteArrayList getExpectedByteResults()
 
366
    {
 
367
        return ByteArrayList.newListWith((byte) -1, (byte) 0, (byte) 42);
 
368
    }
 
369
 
 
370
    @Test
 
371
    public void collectChar()
 
372
    {
 
373
        Integer[] objectArray = {-1, 0, 42};
 
374
        Assert.assertEquals(this.getExpectedCharResults(),
 
375
                ArrayIterate.collectChar(objectArray, PrimitiveFunctions.unboxIntegerToChar()));
 
376
    }
 
377
 
 
378
    @Test
 
379
    public void collectCharWithTarget()
 
380
    {
 
381
        Integer[] objectArray = {-1, 0, 42};
 
382
        CharArrayList target = new CharArrayList();
 
383
        CharArrayList result = ArrayIterate.collectChar(objectArray, PrimitiveFunctions.unboxIntegerToChar(), target);
 
384
        Assert.assertEquals(this.getExpectedCharResults(), result);
 
385
        Assert.assertSame("Target List not returned as result", target, result);
 
386
    }
 
387
 
 
388
    private CharArrayList getExpectedCharResults()
 
389
    {
 
390
        return CharArrayList.newListWith((char) -1, (char) 0, (char) 42);
 
391
    }
 
392
 
 
393
    @Test
 
394
    public void collectDouble()
 
395
    {
 
396
        Integer[] objectArray = {-1, 0, 42};
 
397
        Assert.assertEquals(this.getExpectedDoubleResults(),
 
398
                ArrayIterate.collectDouble(objectArray, PrimitiveFunctions.unboxIntegerToDouble()));
 
399
    }
 
400
 
 
401
    @Test
 
402
    public void collectDoubleWithTarget()
 
403
    {
 
404
        Integer[] objectArray = {-1, 0, 42};
 
405
        DoubleArrayList target = new DoubleArrayList();
 
406
        DoubleArrayList result = ArrayIterate.collectDouble(objectArray, PrimitiveFunctions.unboxIntegerToDouble(), target);
 
407
        Assert.assertEquals(this.getExpectedDoubleResults(), result);
 
408
        Assert.assertSame("Target List not returned as result", target, result);
 
409
    }
 
410
 
 
411
    private DoubleArrayList getExpectedDoubleResults()
 
412
    {
 
413
        return DoubleArrayList.newListWith(-1.0d, 0.0d, 42.0d);
 
414
    }
 
415
 
 
416
    @Test
 
417
    public void collectFloat()
 
418
    {
 
419
        Integer[] objectArray = {-1, 0, 42};
 
420
        Assert.assertEquals(this.getExpectedFloatResults(),
 
421
                ArrayIterate.collectFloat(objectArray, PrimitiveFunctions.unboxIntegerToFloat()));
 
422
    }
 
423
 
 
424
    @Test
 
425
    public void collectFloatWithTarget()
 
426
    {
 
427
        Integer[] objectArray = {-1, 0, 42};
 
428
        FloatArrayList target = new FloatArrayList();
 
429
        FloatArrayList result = ArrayIterate.collectFloat(objectArray, PrimitiveFunctions.unboxIntegerToFloat(), target);
 
430
        Assert.assertEquals(this.getExpectedFloatResults(), result);
 
431
        Assert.assertSame("Target List not returned as result", target, result);
 
432
    }
 
433
 
 
434
    private FloatArrayList getExpectedFloatResults()
 
435
    {
 
436
        return FloatArrayList.newListWith(-1.0f, 0.0f, 42.0f);
 
437
    }
 
438
 
 
439
    @Test
 
440
    public void collectInt()
 
441
    {
 
442
        Integer[] objectArray = {-1, 0, 42};
 
443
        Assert.assertEquals(this.getExpectedIntResults(),
 
444
                ArrayIterate.collectInt(objectArray, PrimitiveFunctions.unboxIntegerToInt()));
 
445
    }
 
446
 
 
447
    @Test
 
448
    public void collectIntWithTarget()
 
449
    {
 
450
        Integer[] objectArray = {-1, 0, 42};
 
451
        IntArrayList target = new IntArrayList();
 
452
        IntArrayList result = ArrayIterate.collectInt(objectArray, PrimitiveFunctions.unboxIntegerToInt(), target);
 
453
        Assert.assertEquals(this.getExpectedIntResults(), result);
 
454
        Assert.assertSame("Target List not returned as result", target, result);
 
455
    }
 
456
 
 
457
    private IntArrayList getExpectedIntResults()
 
458
    {
 
459
        return IntArrayList.newListWith(-1, 0, 42);
 
460
    }
 
461
 
 
462
    @Test
 
463
    public void collectLong()
 
464
    {
 
465
        Integer[] objectArray = {-1, 0, 42};
 
466
        Assert.assertEquals(this.getExpectedLongResults(),
 
467
                ArrayIterate.collectLong(objectArray, PrimitiveFunctions.unboxIntegerToLong()));
 
468
    }
 
469
 
 
470
    @Test
 
471
    public void collectLongWithTarget()
 
472
    {
 
473
        Integer[] objectArray = {-1, 0, 42};
 
474
        LongArrayList target = new LongArrayList();
 
475
        LongArrayList result = ArrayIterate.collectLong(objectArray, PrimitiveFunctions.unboxIntegerToLong(), target);
 
476
        Assert.assertEquals(this.getExpectedLongResults(), result);
 
477
        Assert.assertSame("Target List not returned as result", target, result);
 
478
    }
 
479
 
 
480
    private LongArrayList getExpectedLongResults()
 
481
    {
 
482
        return LongArrayList.newListWith(-1L, 0L, 42L);
 
483
    }
 
484
 
 
485
    @Test
 
486
    public void collectShort()
 
487
    {
 
488
        Integer[] objectArray = {-1, 0, 42};
 
489
        Assert.assertEquals(this.getExpectedShortResults(),
 
490
                ArrayIterate.collectShort(objectArray, PrimitiveFunctions.unboxIntegerToShort()));
 
491
    }
 
492
 
 
493
    @Test
 
494
    public void collectShortWithTarget()
 
495
    {
 
496
        Integer[] objectArray = {-1, 0, 42};
 
497
        ShortArrayList target = new ShortArrayList();
 
498
        ShortArrayList result = ArrayIterate.collectShort(objectArray, PrimitiveFunctions.unboxIntegerToShort(), target);
 
499
        Assert.assertEquals(this.getExpectedShortResults(), result);
 
500
        Assert.assertSame("Target List not returned as result", target, result);
 
501
    }
 
502
 
 
503
    private ShortArrayList getExpectedShortResults()
 
504
    {
 
505
        return ShortArrayList.newListWith((short) -1, (short) 0, (short) 42);
 
506
    }
 
507
 
 
508
    @Test
 
509
    public void collectWith()
 
510
    {
 
511
        Boolean[] objectArray = {true, false, null};
 
512
        Assert.assertEquals(
 
513
                iList("true", "false", "null"),
 
514
                ArrayIterate.collectWith(objectArray, Functions2.fromFunction(String::valueOf), null));
 
515
    }
 
516
 
 
517
    @Test
 
518
    public void addAllTo()
 
519
    {
 
520
        MutableList<Integer> result = Lists.mutable.of();
 
521
        ArrayIterate.addAllTo(new Integer[]{1, 2, 3}, result);
 
522
        Assert.assertEquals(FastList.newListWith(1, 2, 3), result);
 
523
    }
 
524
 
 
525
    @Test
 
526
    public void getFirstAndLast()
 
527
    {
 
528
        List<Boolean> list = new ArrayList<Boolean>();
 
529
        list.add(Boolean.TRUE);
 
530
        list.add(null);
 
531
        list.add(Boolean.FALSE);
 
532
        Object[] objectArray = list.toArray();
 
533
        Assert.assertEquals(Boolean.TRUE, ArrayIterate.getFirst(objectArray));
 
534
        Assert.assertEquals(Boolean.FALSE, ArrayIterate.getLast(objectArray));
 
535
    }
 
536
 
 
537
    @Test
 
538
    public void getFirstAndLastOnEmpty()
 
539
    {
 
540
        Object[] objectArray = {};
 
541
        Assert.assertNull(ArrayIterate.getFirst(objectArray));
 
542
        Assert.assertNull(ArrayIterate.getLast(objectArray));
 
543
    }
 
544
 
 
545
    @Test
 
546
    public void select()
 
547
    {
 
548
        Assert.assertEquals(
 
549
                FastList.newListWith(5, 4, 3, 2, 1),
 
550
                ArrayIterate.select(INTEGER_ARRAY, Integer.class::isInstance));
 
551
    }
 
552
 
 
553
    @Test
 
554
    public void reject()
 
555
    {
 
556
        Assert.assertEquals(
 
557
                FastList.newListWith(5, 4, 3, 2, 1),
 
558
                ArrayIterate.reject(INTEGER_ARRAY, String.class::isInstance));
 
559
    }
 
560
 
 
561
    @Test
 
562
    public void distinct()
 
563
    {
 
564
        List<Integer> result = FastList.newList();
 
565
        ArrayIterate.distinct(new Integer[]{5, 3, 1, 5, 7, 1}, result);
 
566
        Assert.assertEquals(FastList.newListWith(5, 3, 1, 7), result);
 
567
        result.clear();
 
568
        ArrayIterate.distinct(INTEGER_ARRAY, result);
 
569
        Assert.assertEquals(FastList.newListWith(INTEGER_ARRAY), result);
 
570
    }
 
571
 
 
572
    @Test
 
573
    public void selectWith()
 
574
    {
 
575
        Assert.assertEquals(
 
576
                FastList.newListWith(5, 4, 3, 2, 1),
 
577
                ArrayIterate.selectWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
 
578
    }
 
579
 
 
580
    @Test
 
581
    public void selectInstancesOf()
 
582
    {
 
583
        Assert.assertEquals(
 
584
                iList(1, 3, 5),
 
585
                ArrayIterate.selectInstancesOf(new Number[]{1, 2.0, 3, 4.0, 5}, Integer.class));
 
586
    }
 
587
 
 
588
    @Test
 
589
    public void countOnNullOrEmptyArray()
 
590
    {
 
591
        Assert.assertEquals(0, ArrayIterate.count(null, ignored1 -> true));
 
592
        Assert.assertEquals(0, ArrayIterate.count(new Object[]{}, ignored -> true));
 
593
    }
 
594
 
 
595
    @Test
 
596
    public void count()
 
597
    {
 
598
        Assert.assertEquals(3, ArrayIterate.count(INTEGER_ARRAY, Predicates.lessThan(4)));
 
599
    }
 
600
 
 
601
    @Test
 
602
    public void countWith()
 
603
    {
 
604
        Assert.assertEquals(5, ArrayIterate.countWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
 
605
        Assert.assertEquals(0, ArrayIterate.countWith(new Integer[]{}, Predicates2.instanceOf(), Integer.class));
 
606
        Assert.assertEquals(1, ArrayIterate.countWith(new Object[]{"test", null, Integer.valueOf(2)}, Predicates2.instanceOf(), Integer.class));
 
607
    }
 
608
 
 
609
    @Test
 
610
    public void selectAndRejectWith()
 
611
    {
 
612
        Twin<MutableList<Integer>> result =
 
613
                ArrayIterate.selectAndRejectWith(INTEGER_ARRAY, Predicates2.<Integer>lessThan(), 3);
 
614
        MutableList<Integer> positive = result.getOne();
 
615
        MutableList<Integer> negative = result.getTwo();
 
616
        Verify.assertSize(2, positive);
 
617
        Verify.assertContains(2, positive);
 
618
        Verify.assertNotContains(3, positive);
 
619
        Verify.assertSize(3, negative);
 
620
        Verify.assertNotContains(2, negative);
 
621
        Verify.assertContains(3, negative);
 
622
    }
 
623
 
 
624
    @Test
 
625
    public void selectWithDifferentTargetCollection()
 
626
    {
 
627
        Assert.assertEquals(
 
628
                FastList.newListWith(5, 4, 3, 2, 1),
 
629
                ArrayIterate.select(INTEGER_ARRAY, Integer.class::isInstance, new ArrayList<Integer>()));
 
630
    }
 
631
 
 
632
    @Test
 
633
    public void rejectDifferentTargetCollection()
 
634
    {
 
635
        Verify.assertEmpty(ArrayIterate.reject(INTEGER_ARRAY, Integer.class::isInstance, new ArrayList<Integer>()));
 
636
    }
 
637
 
 
638
    @Test
 
639
    public void rejectWith()
 
640
    {
 
641
        Verify.assertEmpty(ArrayIterate.rejectWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
 
642
        Verify.assertEmpty(ArrayIterate.rejectWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class, new ArrayList<Integer>()));
 
643
    }
 
644
 
 
645
    @Test
 
646
    public void collectIf()
 
647
    {
 
648
        Object[] integers = Lists.fixedSize.of(1, 2, 3).toArray();
 
649
        Verify.assertContainsAll(ArrayIterate.collectIf(integers, Integer.class::isInstance, String::valueOf), "1", "2", "3");
 
650
        Verify.assertContainsAll(ArrayIterate.collectIf(integers, Integer.class::isInstance, String::valueOf, FastList.<String>newList()), "1", "2", "3");
 
651
    }
 
652
 
 
653
    @Test
 
654
    public void toMap()
 
655
    {
 
656
        MutableMap<String, Integer> map = ArrayIterate.toMap(INTEGER_ARRAY, String::valueOf);
 
657
        Verify.assertSize(5, map);
 
658
        Verify.assertContainsKeyValue("1", 1, map);
 
659
        Verify.assertContainsKeyValue("2", 2, map);
 
660
        Verify.assertContainsKeyValue("3", 3, map);
 
661
        Verify.assertContainsKeyValue("4", 4, map);
 
662
        Verify.assertContainsKeyValue("5", 5, map);
 
663
    }
 
664
 
 
665
    @Test
 
666
    public void toMap2()
 
667
    {
 
668
        MutableMap<String, Integer> map = ArrayIterate.toMap(INTEGER_ARRAY, String::valueOf, Functions.squaredInteger());
 
669
        Verify.assertSize(5, map);
 
670
        Verify.assertContainsKeyValue("1", 1, map);
 
671
        Verify.assertContainsKeyValue("2", 4, map);
 
672
        Verify.assertContainsKeyValue("3", 9, map);
 
673
        Verify.assertContainsKeyValue("4", 16, map);
 
674
        Verify.assertContainsKeyValue("5", 25, map);
 
675
    }
 
676
 
 
677
    @Test
 
678
    public void contains()
 
679
    {
 
680
        Assert.assertTrue(ArrayIterate.contains(INTEGER_ARRAY, 5));
 
681
        Assert.assertFalse(ArrayIterate.contains(INTEGER_ARRAY, 6));
 
682
        Assert.assertFalse(ArrayIterate.contains(INTEGER_ARRAY, null));
 
683
        Assert.assertTrue(ArrayIterate.contains(new Object[]{null}, null));
 
684
    }
 
685
 
 
686
    @Test
 
687
    public void containsInt()
 
688
    {
 
689
        int[] array = {1, 2, 3, 4, 5};
 
690
        Assert.assertTrue(ArrayIterate.contains(array, 5));
 
691
        Assert.assertFalse(ArrayIterate.contains(array, 6));
 
692
    }
 
693
 
 
694
    @Test
 
695
    public void containsLong()
 
696
    {
 
697
        long[] array = {1, 2, 3, 4, 5};
 
698
        Assert.assertTrue(ArrayIterate.contains(array, 5));
 
699
        Assert.assertFalse(ArrayIterate.contains(array, 6));
 
700
    }
 
701
 
 
702
    @Test
 
703
    public void containsDouble()
 
704
    {
 
705
        double[] array = {1, 2, 3, 4, 5};
 
706
        Assert.assertTrue(ArrayIterate.contains(array, 5));
 
707
        Assert.assertFalse(ArrayIterate.contains(array, 6));
 
708
    }
 
709
 
 
710
    @Test
 
711
    public void anySatisfy()
 
712
    {
 
713
        Assert.assertTrue(ArrayIterate.anySatisfy(INTEGER_ARRAY, Integer.class::isInstance));
 
714
        Assert.assertFalse(ArrayIterate.anySatisfy(INTEGER_ARRAY, Predicates.isNull()));
 
715
    }
 
716
 
 
717
    @Test
 
718
    public void anySatisfyWith()
 
719
    {
 
720
        Assert.assertTrue(ArrayIterate.anySatisfyWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
 
721
    }
 
722
 
 
723
    @Test
 
724
    public void allSatisfy()
 
725
    {
 
726
        Assert.assertTrue(ArrayIterate.allSatisfy(INTEGER_ARRAY, Integer.class::isInstance));
 
727
        Assert.assertFalse(ArrayIterate.allSatisfy(INTEGER_ARRAY, Predicates.isNull()));
 
728
    }
 
729
 
 
730
    @Test
 
731
    public void allSatisfyWith()
 
732
    {
 
733
        Assert.assertTrue(ArrayIterate.allSatisfyWith(INTEGER_ARRAY, Predicates2.instanceOf(), Integer.class));
 
734
    }
 
735
 
 
736
    @Test
 
737
    public void noneSatisfy()
 
738
    {
 
739
        Assert.assertTrue(ArrayIterate.noneSatisfy(INTEGER_ARRAY, String.class::isInstance));
 
740
        Assert.assertFalse(ArrayIterate.noneSatisfy(INTEGER_ARRAY, Predicates.notNull()));
 
741
    }
 
742
 
 
743
    @Test
 
744
    public void noneSatisfyWith()
 
745
    {
 
746
        Assert.assertTrue(ArrayIterate.noneSatisfyWith(INTEGER_ARRAY, Predicates2.instanceOf(), String.class));
 
747
    }
 
748
 
 
749
    @Test
 
750
    public void isEmpty()
 
751
    {
 
752
        Assert.assertFalse(ArrayIterate.isEmpty(INTEGER_ARRAY));
 
753
        Assert.assertTrue(ArrayIterate.isEmpty(new Object[]{}));
 
754
        Assert.assertTrue(ArrayIterate.isEmpty(null));
 
755
    }
 
756
 
 
757
    @Test
 
758
    public void notEmpty()
 
759
    {
 
760
        Assert.assertTrue(ArrayIterate.notEmpty(new Integer[]{5, 4, 3, 2, 1}));
 
761
        Assert.assertFalse(ArrayIterate.notEmpty(new Object[]{}));
 
762
        Assert.assertFalse(ArrayIterate.notEmpty(null));
 
763
    }
 
764
 
 
765
    @Test
 
766
    public void size()
 
767
    {
 
768
        Assert.assertEquals(5, ArrayIterate.size(new Integer[]{5, 4, 3, 2, 1}));
 
769
        Assert.assertEquals(0, ArrayIterate.size(null));
 
770
    }
 
771
 
 
772
    @Test
 
773
    public void get()
 
774
    {
 
775
        Assert.assertEquals(Integer.valueOf(1), new Integer[]{5, 4, 3, 2, 1}[4]);
 
776
    }
 
777
 
 
778
    @Test
 
779
    public void forEachInBoth()
 
780
    {
 
781
        MutableMap<String, String> map = UnifiedMap.newMap();
 
782
        ArrayIterate.forEachInBoth(
 
783
                new String[]{"1", "2", "3"},
 
784
                new String[]{"a", "b", "c"},
 
785
                new MapPutProcedure<String, String>(map));
 
786
        Assert.assertEquals(
 
787
                UnifiedMap.newWithKeysValues(
 
788
                        "1", "a",
 
789
                        "2", "b",
 
790
                        "3", "c"),
 
791
                map);
 
792
        ArrayIterate.forEachInBoth(null, null, (argument1, argument2) -> Assert.fail());
 
793
    }
 
794
 
 
795
    @Test(expected = RuntimeException.class)
 
796
    public void forEachInBothThrowsOnDifferentLengthArrays()
 
797
    {
 
798
        ArrayIterate.forEachInBoth(new Integer[]{1, 2, 3}, new Integer[]{1, 2}, (argument1, argument2) -> Assert.fail());
 
799
    }
 
800
 
 
801
    @Test
 
802
    public void forEachWithIndex()
 
803
    {
 
804
        Integer[] objectArray = {1, 2, 3, 4};
 
805
        ArrayIterate.forEachWithIndex(objectArray, (i, index) -> Assert.assertEquals(index, i - 1));
 
806
    }
 
807
 
 
808
    private Integer[] createIntegerArray(int size)
 
809
    {
 
810
        Integer[] array = new Integer[size];
 
811
        for (int i = 0; i < size; i++)
 
812
        {
 
813
            array[i] = 1;
 
814
        }
 
815
        return array;
 
816
    }
 
817
 
 
818
    @Test
 
819
    public void detect()
 
820
    {
 
821
        Integer[] array = this.createIntegerArray(1);
 
822
        Assert.assertEquals(Integer.valueOf(1), ArrayIterate.detect(array, integer -> integer == 1));
 
823
    }
 
824
 
 
825
    @Test
 
826
    public void detectWith()
 
827
    {
 
828
        Integer[] array = this.createIntegerArray(1);
 
829
        Assert.assertEquals(
 
830
                Integer.valueOf(1),
 
831
                ArrayIterate.detectWith(array, Predicates2.<Integer>lessThan(), 2));
 
832
        Assert.assertNull(
 
833
                ArrayIterate.detectWith(new Integer[0], Predicates2.<Integer>lessThan(), 2));
 
834
    }
 
835
 
 
836
    @Test
 
837
    public void detectIfNone()
 
838
    {
 
839
        Integer[] array = this.createIntegerArray(1);
 
840
        Assert.assertEquals(Integer.valueOf(7), ArrayIterate.detectIfNone(array, Integer.valueOf(2)::equals, 7));
 
841
        Assert.assertEquals(Integer.valueOf(1), ArrayIterate.detectIfNone(array, Integer.valueOf(1)::equals, 7));
 
842
    }
 
843
 
 
844
    @Test
 
845
    public void detectWithIfNone()
 
846
    {
 
847
        Integer[] array = this.createIntegerArray(1);
 
848
        Assert.assertEquals(Integer.valueOf(7),
 
849
                ArrayIterate.detectWithIfNone(array, Object::equals, 2, 7));
 
850
    }
 
851
 
 
852
    @Test
 
853
    public void indexOf()
 
854
    {
 
855
        String[] array = {"1", "2", "3", null};
 
856
        Assert.assertEquals(0, ArrayIterate.indexOf(array, "1"));
 
857
        Assert.assertEquals(1, ArrayIterate.indexOf(array, "2"));
 
858
        Assert.assertEquals(2, ArrayIterate.indexOf(array, "3"));
 
859
        Assert.assertEquals(3, ArrayIterate.indexOf(array, null));
 
860
        Assert.assertEquals(-1, ArrayIterate.indexOf(array, "4"));
 
861
    }
 
862
 
 
863
    @Test
 
864
    public void indexOfPredicates()
 
865
    {
 
866
        String[] array = {"1", "2", "3", null};
 
867
        Assert.assertEquals(0, ArrayIterate.detectIndex(array, String.class::isInstance));
 
868
        Assert.assertEquals(3, ArrayIterate.detectIndex(array, Predicates.isNull()));
 
869
        Assert.assertEquals(0, ArrayIterate.detectIndexWith(array, Predicates2.instanceOf(), String.class));
 
870
    }
 
871
 
 
872
    @Test
 
873
    public void take()
 
874
    {
 
875
        Assert.assertEquals(ListIterate.take(Interval.zeroTo(0), 5), ArrayIterate.take(Interval.zeroTo(0).toArray(), 5));
 
876
        Assert.assertEquals(ListIterate.take(Interval.zeroTo(5), 5), ArrayIterate.take(Interval.zeroTo(5).toArray(), 5));
 
877
        Assert.assertEquals(ListIterate.take(Interval.zeroTo(10), 5), ArrayIterate.take(Interval.zeroTo(10).toArray(), 5));
 
878
    }
 
879
 
 
880
    @Test(expected = IllegalArgumentException.class)
 
881
    public void take_negative_throws()
 
882
    {
 
883
        ArrayIterate.take(Interval.zeroTo(0).toArray(), -1);
 
884
    }
 
885
 
 
886
    @Test
 
887
    public void drop()
 
888
    {
 
889
        Assert.assertEquals(ListIterate.drop(Interval.zeroTo(0).toList(), 5), ArrayIterate.drop(Interval.zeroTo(0).toList().toArray(), 5));
 
890
        Assert.assertEquals(ListIterate.drop(Interval.zeroTo(5), 5), ArrayIterate.drop(Interval.zeroTo(5).toArray(), 5));
 
891
        Assert.assertEquals(ListIterate.drop(Interval.zeroTo(10), 5), ArrayIterate.drop(Interval.zeroTo(10).toArray(), 5));
 
892
    }
 
893
 
 
894
    @Test(expected = IllegalArgumentException.class)
 
895
    public void drop_negative_throws()
 
896
    {
 
897
        ArrayIterate.drop(Interval.zeroTo(0).toArray(), -1);
 
898
    }
 
899
 
 
900
    @Test
 
901
    public void groupBy()
 
902
    {
 
903
        Integer[] array = {1, 2, 3, 4, 5, 6, 7};
 
904
        Function<Integer, Boolean> isOddFunction = object -> IntegerPredicates.isOdd().accept(object);
 
905
 
 
906
        MutableMap<Boolean, RichIterable<Integer>> expected =
 
907
                UnifiedMap.<Boolean, RichIterable<Integer>>newWithKeysValues(
 
908
                        Boolean.TRUE, FastList.newListWith(1, 3, 5, 7),
 
909
                        Boolean.FALSE, FastList.newListWith(2, 4, 6));
 
910
 
 
911
        Multimap<Boolean, Integer> multimap = ArrayIterate.groupBy(array, isOddFunction);
 
912
        Assert.assertEquals(expected, multimap.toMap());
 
913
    }
 
914
 
 
915
    @Test
 
916
    public void groupByEach()
 
917
    {
 
918
        MutableMultimap<Integer, Integer> expected = FastListMultimap.newMultimap();
 
919
        for (int i = 1; i < 8; i++)
 
920
        {
 
921
            expected.putAll(-i, Interval.fromTo(i, 7));
 
922
        }
 
923
 
 
924
        Multimap<Integer, Integer> actual = ArrayIterate.groupByEach(new Integer[]{1, 2, 3, 4, 5, 6, 7}, new NegativeIntervalFunction());
 
925
        Assert.assertEquals(expected, actual);
 
926
    }
 
927
 
 
928
    @Test
 
929
    public void zip()
 
930
    {
 
931
        String[] array = {"1", "2", "3", "4", "5", "6", "7"};
 
932
        Object[] nulls = Collections.nCopies(array.length, null).toArray();
 
933
        Object[] nullsPlusOne = Collections.nCopies(array.length + 1, null).toArray();
 
934
        Object[] nullsMinusOne = Collections.nCopies(array.length - 1, null).toArray();
 
935
 
 
936
        MutableList<Pair<String, Object>> pairs = ArrayIterate.zip(array, nulls);
 
937
        Assert.assertEquals(
 
938
                FastList.newListWith(array),
 
939
                pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne));
 
940
        Assert.assertEquals(
 
941
                FastList.newListWith(nulls),
 
942
                pairs.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));
 
943
 
 
944
        MutableList<Pair<String, Object>> pairsPlusOne = ArrayIterate.zip(array, nullsPlusOne);
 
945
        Assert.assertEquals(
 
946
                FastList.newListWith(array),
 
947
                pairsPlusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne));
 
948
        Assert.assertEquals(FastList.newListWith(nulls), pairsPlusOne.collect((Function<Pair<?, Object>, Object>) Pair::getTwo, Lists.mutable.of()));
 
949
 
 
950
        MutableList<Pair<String, Object>> pairsMinusOne = ArrayIterate.zip(array, nullsMinusOne);
 
951
        Assert.assertEquals(array.length - 1, pairsMinusOne.size());
 
952
        Assert.assertTrue(FastList.newListWith(array).containsAll(pairsMinusOne.collect((Function<Pair<String, ?>, String>) Pair::getOne)));
 
953
 
 
954
        Assert.assertEquals(
 
955
                ArrayIterate.zip(array, nulls),
 
956
                ArrayIterate.zip(array, nulls, FastList.<Pair<String, Object>>newList()));
 
957
    }
 
958
 
 
959
    @Test
 
960
    public void zipWithIndex()
 
961
    {
 
962
        String[] array = {"1", "2", "3", "4", "5", "6", "7"};
 
963
        MutableList<Pair<String, Integer>> pairs = ArrayIterate.zipWithIndex(array);
 
964
 
 
965
        Assert.assertEquals(
 
966
                FastList.newListWith(array),
 
967
                pairs.collect((Function<Pair<String, ?>, String>) Pair::getOne));
 
968
        Assert.assertEquals(
 
969
                Interval.zeroTo(array.length - 1).toList(),
 
970
                pairs.collect((Function<Pair<?, Integer>, Integer>) Pair::getTwo, FastList.<Integer>newList()));
 
971
 
 
972
        Assert.assertEquals(
 
973
                ArrayIterate.zipWithIndex(array),
 
974
                ArrayIterate.zipWithIndex(array, FastList.<Pair<String, Integer>>newList()));
 
975
    }
 
976
 
 
977
    @Test
 
978
    public void chunk()
 
979
    {
 
980
        String[] array = {"1", "2", "3", "4", "5", "6", "7"};
 
981
        RichIterable<RichIterable<String>> groups = ArrayIterate.chunk(array, 2);
 
982
        RichIterable<Integer> sizes = groups.collect(RichIterable::size);
 
983
        Assert.assertEquals(FastList.newListWith(2, 2, 2, 1), sizes);
 
984
    }
 
985
 
 
986
    @Test(expected = IllegalArgumentException.class)
 
987
    public void chunk_zero_throws()
 
988
    {
 
989
        String[] array = {"1", "2", "3", "4", "5", "6", "7"};
 
990
        ArrayIterate.chunk(array, 0);
 
991
    }
 
992
 
 
993
    @Test
 
994
    public void chunk_large_size()
 
995
    {
 
996
        String[] array = {"1", "2", "3", "4", "5", "6", "7"};
 
997
        Assert.assertEquals(FastList.newListWith(array), ArrayIterate.chunk(array, 10).getFirst());
 
998
    }
 
999
 
 
1000
    @Test
 
1001
    public void makeString()
 
1002
    {
 
1003
        String[] array = {"1", "2", "3", "4", "5"};
 
1004
        Assert.assertEquals("1, 2, 3, 4, 5", ArrayIterate.makeString(array));
 
1005
    }
 
1006
 
 
1007
    @Test
 
1008
    public void appendString()
 
1009
    {
 
1010
        String[] array = {"1", "2", "3", "4", "5"};
 
1011
        StringBuilder stringBuilder = new StringBuilder();
 
1012
        ArrayIterate.appendString(array, stringBuilder);
 
1013
        Assert.assertEquals("1, 2, 3, 4, 5", stringBuilder.toString());
 
1014
    }
 
1015
 
 
1016
    @Test(expected = RuntimeException.class)
 
1017
    public void appendStringThrowsIOException()
 
1018
    {
 
1019
        ArrayIterate.appendString(new String[]{"1", "2", "3"}, new Appendable()
 
1020
        {
 
1021
            public Appendable append(CharSequence csq) throws IOException
 
1022
            {
 
1023
                throw new IOException();
 
1024
            }
 
1025
 
 
1026
            public Appendable append(CharSequence csq, int start, int end) throws IOException
 
1027
            {
 
1028
                throw new IOException();
 
1029
            }
 
1030
 
 
1031
            public Appendable append(char c) throws IOException
 
1032
            {
 
1033
                throw new IOException();
 
1034
            }
 
1035
        });
 
1036
    }
 
1037
 
 
1038
    @Test
 
1039
    public void classIsNonInstantiable()
 
1040
    {
 
1041
        Verify.assertClassNonInstantiable(ArrayIterate.class);
 
1042
    }
 
1043
}