-
Notifications
You must be signed in to change notification settings - Fork 587
/
ImmutableEmptySortedSetTest.java
821 lines (718 loc) · 29.2 KB
/
ImmutableEmptySortedSetTest.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*
* Copyright (c) 2018 Goldman Sachs and others.
* 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.set.sorted.immutable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.partition.set.sorted.PartitionImmutableSortedSet;
import org.eclipse.collections.api.set.sorted.ImmutableSortedSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.SortedSets;
import org.eclipse.collections.impl.factory.Stacks;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.AddToList;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.BooleanArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.ByteArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.CharArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.DoubleArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.FloatArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.LongArrayList;
import org.eclipse.collections.impl.list.mutable.primitive.ShortArrayList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.sorted.mutable.TreeSortedSet;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.junit.Assert;
import org.junit.Test;
public class ImmutableEmptySortedSetTest extends AbstractImmutableSortedSetTestCase
{
@Override
protected ImmutableSortedSet<Integer> classUnderTest()
{
return SortedSets.immutable.of();
}
@Override
protected ImmutableSortedSet<Integer> classUnderTest(Comparator<? super Integer> comparator)
{
return SortedSets.immutable.of(comparator);
}
@Test
public void testContainsAll()
{
Assert.assertTrue(this.classUnderTest().containsAllIterable(UnifiedSet.<Integer>newSet()));
Assert.assertFalse(this.classUnderTest().containsAllIterable(UnifiedSet.newSetWith(1)));
}
@Test
public void testNewSortedSet()
{
Assert.assertSame(SortedSets.immutable.of(), SortedSets.immutable.ofAll(FastList.newList()));
Assert.assertSame(SortedSets.immutable.of(), SortedSets.immutable.ofSortedSet(TreeSortedSet.newSet()));
Assert.assertNotSame(SortedSets.immutable.of(),
SortedSets.immutable.ofSortedSet(TreeSortedSet.newSet(Comparators.reverseNaturalOrder())));
}
@Override
@Test
public void newWith()
{
Assert.assertEquals(UnifiedSet.newSetWith(1), this.classUnderTest().newWith(1));
Assert.assertSame(SortedSets.immutable.empty(), SortedSets.immutable.of(FastList.newList().toArray()));
Assert.assertEquals(SortedSets.immutable.empty(),
SortedSets.immutable.of(Comparators.naturalOrder(), FastList.newList().toArray()));
Assert.assertEquals(Comparators.<Integer>reverseNaturalOrder(),
this.classUnderTest(Comparators.reverseNaturalOrder()).newWith(1).comparator());
Verify.assertSortedSetsEqual(TreeSortedSet.newSetWith(Comparators.reverseNaturalOrder(), 1, 2),
this.classUnderTest(Comparators.reverseNaturalOrder()).newWith(1).newWith(2).castToSortedSet());
}
@Override
@Test
public void newWithout()
{
Assert.assertEquals(SortedSets.immutable.empty(), SortedSets.immutable.empty().newWithout(1));
Assert.assertSame(SortedSets.immutable.empty(), SortedSets.immutable.empty().newWithout(1));
Assert.assertEquals(SortedSets.immutable.empty(), SortedSets.immutable.empty().newWithoutAll(Interval.oneTo(3)));
Assert.assertSame(SortedSets.immutable.empty(), SortedSets.immutable.empty().newWithoutAll(Interval.oneTo(3)));
Assert.assertEquals(Comparators.<Integer>reverseNaturalOrder(),
this.classUnderTest(Comparators.reverseNaturalOrder()).newWithout(1).comparator());
}
@Override
@Test
public void detect()
{
Assert.assertNull(this.classUnderTest().detect(Integer.valueOf(1)::equals));
}
@Override
@Test
public void detectWith()
{
Assert.assertNull(this.classUnderTest().detectWith(Object::equals, Integer.valueOf(1)));
}
@Override
@Test
public void detectIndex()
{
//Any predicate will result in -1
Assert.assertEquals(Integer.valueOf(-1), Integer.valueOf(this.classUnderTest().detectIndex(Predicates.alwaysTrue())));
}
@Override
@Test
public void corresponds()
{
//Evaluates true for all empty sets and false for all non-empty sets
ImmutableSortedSet<Integer> integers1 = this.classUnderTest();
Assert.assertTrue(integers1.corresponds(Lists.mutable.of(), Predicates2.alwaysFalse()));
ImmutableSortedSet<Integer> integers2 = integers1.newWith(Integer.valueOf(1));
Assert.assertFalse(integers2.corresponds(integers1, Predicates2.alwaysTrue()));
}
@Override
@Test
public void allSatisfy()
{
Assert.assertTrue(this.classUnderTest().allSatisfy(Integer.class::isInstance));
}
@Override
@Test
public void anySatisfy()
{
Assert.assertFalse(this.classUnderTest().anySatisfy(Integer.class::isInstance));
}
@Override
@Test(expected = NoSuchElementException.class)
public void getFirst()
{
this.classUnderTest().getFirst();
}
@Override
@Test(expected = NoSuchElementException.class)
public void getLast()
{
this.classUnderTest().getLast();
}
@Test(expected = IllegalStateException.class)
public void getOnly()
{
this.classUnderTest().getOnly();
}
@Override
@Test
public void isEmpty()
{
Verify.assertIterableEmpty(this.classUnderTest());
Assert.assertFalse(this.classUnderTest().notEmpty());
}
@Override
@Test(expected = NoSuchElementException.class)
public void min()
{
this.classUnderTest().min(Integer::compareTo);
}
@Override
@Test(expected = NoSuchElementException.class)
public void max()
{
this.classUnderTest().max(Integer::compareTo);
}
@Override
@Test(expected = NoSuchElementException.class)
public void min_without_comparator()
{
this.classUnderTest().min();
}
@Override
@Test(expected = NoSuchElementException.class)
public void max_without_comparator()
{
this.classUnderTest().max();
}
@Override
@Test(expected = NoSuchElementException.class)
public void minBy()
{
this.classUnderTest().minBy(String::valueOf);
}
@Override
@Test(expected = NoSuchElementException.class)
public void maxBy()
{
this.classUnderTest().maxBy(String::valueOf);
}
@Override
@Test
public void zip()
{
ImmutableSortedSet<Integer> immutableSet = this.classUnderTest(Comparators.reverseNaturalOrder());
ImmutableList<Pair<Integer, Integer>> pairs = immutableSet.zip(Interval.oneTo(10));
Assert.assertEquals(FastList.<Pair<Integer, Integer>>newList(), pairs);
Assert.assertEquals(
UnifiedSet.<Pair<Integer, Integer>>newSet(),
immutableSet.zip(Interval.oneTo(10), UnifiedSet.newSet()));
ImmutableList<Pair<Integer, Integer>> pairsWithExtras = pairs.newWith(Tuples.pair(1, 5)).newWith(Tuples.pair(5, 1));
Assert.assertEquals(FastList.newListWith(Tuples.pair(1, 5), Tuples.pair(5, 1)), pairsWithExtras.toList());
}
@Override
@Test
public void zipWithIndex()
{
ImmutableSortedSet<Integer> set = this.classUnderTest();
ImmutableSortedSet<Pair<Integer, Integer>> pairs = set.zipWithIndex();
Assert.assertEquals(UnifiedSet.<Pair<Integer, Integer>>newSet(), pairs);
Assert.assertEquals(
UnifiedSet.<Pair<Integer, Integer>>newSet(),
set.zipWithIndex(UnifiedSet.newSet()));
Assert.assertNotNull(pairs.comparator());
ImmutableSortedSet<Pair<Integer, Integer>> pairsWithExtras = pairs.newWith(Tuples.pair(1, 5)).newWith(Tuples.pair(5, 1));
Assert.assertEquals(FastList.newListWith(Tuples.pair(1, 5), Tuples.pair(5, 1)), pairsWithExtras.toList());
}
@Test
public void chunk()
{
Verify.assertIterableEmpty(this.classUnderTest().chunk(2));
}
@Override
@Test(expected = IllegalArgumentException.class)
public void chunk_zero_throws()
{
this.classUnderTest().chunk(0);
}
@Override
@Test
public void chunk_large_size()
{
Verify.assertIterableEmpty(this.classUnderTest().chunk(10));
}
@Override
@Test
public void union()
{
Verify.assertSortedSetsEqual(
TreeSortedSet.newSetWith("a", "b", "c"),
SortedSets.immutable.<String>empty().union(UnifiedSet.newSetWith("a", "b", "c")).castToSortedSet());
Verify.assertListsEqual(FastList.newListWith(3, 2, 1),
this.classUnderTest(Comparators.reverseNaturalOrder()).union(UnifiedSet.newSetWith(1, 2, 3)).toList());
}
@Override
@Test
public void unionInto()
{
Assert.assertEquals(
UnifiedSet.newSetWith("a", "b", "c"),
SortedSets.immutable.<String>empty().unionInto(UnifiedSet.newSetWith("a", "b", "c"), UnifiedSet.newSet()));
}
@Override
@Test
public void intersect()
{
Assert.assertEquals(
UnifiedSet.<String>newSet(),
SortedSets.immutable.<String>empty().intersect(UnifiedSet.newSetWith("1", "2", "3")));
Assert.assertEquals(Comparators.<Integer>reverseNaturalOrder(),
this.classUnderTest(Comparators.reverseNaturalOrder()).intersect(UnifiedSet.newSetWith(1, 2, 3)).comparator());
}
@Override
@Test
public void intersectInto()
{
Assert.assertEquals(
UnifiedSet.<String>newSet(),
SortedSets.immutable.<String>empty().intersectInto(UnifiedSet.newSetWith("1", "2", "3"), UnifiedSet.newSet()));
}
@Override
@Test
public void difference()
{
Assert.assertEquals(
UnifiedSet.<String>newSet(),
SortedSets.immutable.<String>empty().difference(UnifiedSet.newSetWith("not present")));
Assert.assertEquals(Comparators.<Integer>reverseNaturalOrder(),
this.classUnderTest(Comparators.reverseNaturalOrder()).difference(UnifiedSet.newSetWith(1, 2, 3)).comparator());
}
@Override
@Test
public void differenceInto()
{
ImmutableSortedSet<String> set = SortedSets.immutable.empty();
Assert.assertEquals(
UnifiedSet.<String>newSet(),
set.differenceInto(UnifiedSet.newSetWith("not present"), UnifiedSet.newSet()));
Verify.assertListsEqual(FastList.newListWith(3, 2, 1),
this.classUnderTest(Comparators.reverseNaturalOrder()).union(UnifiedSet.newSetWith(1, 2, 3)).toList());
}
@Override
@Test
public void symmetricDifference()
{
Assert.assertEquals(
UnifiedSet.newSetWith("not present"),
SortedSets.immutable.<String>empty().symmetricDifference(UnifiedSet.newSetWith("not present")));
Verify.assertSortedSetsEqual(TreeSortedSet.newSetWith(Comparators.reverseNaturalOrder(), 1, 2, 3, 4),
SortedSets.immutable.of(Comparators.<Integer>reverseNaturalOrder()).symmetricDifference(UnifiedSet.newSetWith(1, 3, 2, 4)).castToSortedSet());
}
@Override
@Test
public void symmetricDifferenceInto()
{
Assert.assertEquals(
UnifiedSet.newSetWith("not present"),
SortedSets.immutable.<String>empty().symmetricDifferenceInto(UnifiedSet.newSetWith("not present"), UnifiedSet.newSet()));
}
@Override
@Test
public void equalsAndHashCode()
{
ImmutableSortedSet<Integer> immutable = this.classUnderTest();
Verify.assertEqualsAndHashCode(UnifiedSet.newSet(), immutable);
Verify.assertPostSerializedIdentity(immutable);
Assert.assertNotEquals(Lists.mutable.empty(), immutable);
ImmutableSortedSet<Integer> setWithComparator = this.classUnderTest(Comparators.reverseNaturalOrder());
Verify.assertEqualsAndHashCode(UnifiedSet.newSet(), setWithComparator);
Verify.assertPostSerializedEqualsAndHashCode(setWithComparator);
}
@Override
@Test
public void contains()
{
ImmutableSortedSet<Integer> set = this.classUnderTest();
Verify.assertNotContains(Integer.valueOf(1), set.castToSortedSet());
Verify.assertEmpty(set.castToSortedSet());
Verify.assertThrows(NullPointerException.class, () -> set.contains(null));
}
@Override
@Test
public void containsAllIterable()
{
ImmutableSortedSet<Integer> set = this.classUnderTest();
Assert.assertFalse(set.containsAllIterable(FastList.newListWith(1, 2, 3)));
Assert.assertTrue(set.containsAllIterable(set));
}
@Override
@Test
public void iterator()
{
Iterator<Integer> iterator = this.classUnderTest().iterator();
Assert.assertFalse(iterator.hasNext());
Verify.assertThrows(NoSuchElementException.class, (Runnable) iterator::next);
}
@Override
@Test
public void select()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
Verify.assertEmpty(set.select(Predicates.lessThan(4)).castToSortedSet());
Assert.assertEquals(Collections.<Integer>reverseOrder(), set.select(Predicates.lessThan(3)).comparator());
}
@Override
@Test
public void selectWith()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(integers.selectWith(Predicates2.lessThan(), 4).castToSortedSet());
Assert.assertEquals(Collections.<Integer>reverseOrder(), integers.selectWith(Predicates2.lessThan(), 3).comparator());
}
@Override
@Test
public void reject()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
Verify.assertEmpty(set.reject(Predicates.lessThan(3)).castToSortedSet());
Assert.assertEquals(Collections.<Integer>reverseOrder(), set.reject(Predicates.lessThan(3)).comparator());
}
@Override
@Test
public void rejectWith()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(integers.rejectWith(Predicates2.greaterThanOrEqualTo(), 4).castToSortedSet());
Assert.assertEquals(Collections.<Integer>reverseOrder(), integers.rejectWith(Predicates2.greaterThanOrEqualTo(), 4).comparator());
}
@Override
@Test
public void partition()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
PartitionImmutableSortedSet<Integer> partition = set.partition(Predicates.lessThan(4));
Verify.assertIterableEmpty(partition.getSelected());
Verify.assertIterableEmpty(partition.getRejected());
Assert.assertEquals(Collections.<Integer>reverseOrder(), partition.getSelected().comparator());
Assert.assertEquals(Collections.<Integer>reverseOrder(), partition.getRejected().comparator());
}
@Override
@Test
public void partitionWith()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
PartitionImmutableSortedSet<Integer> partition = set.partitionWith(Predicates2.lessThan(), 4);
Verify.assertIterableEmpty(partition.getSelected());
Verify.assertIterableEmpty(partition.getRejected());
Assert.assertEquals(Collections.<Integer>reverseOrder(), partition.getSelected().comparator());
Assert.assertEquals(Collections.<Integer>reverseOrder(), partition.getRejected().comparator());
}
@Override
@Test
public void collect()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Verify.assertIterableEmpty(integers.collect(Functions.getIntegerPassThru()));
}
/**
* @since 9.1.
*/
@Override
@Test
public void collectWithIndex()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(
Lists.mutable.empty(),
integers.collectWithIndex(PrimitiveTuples::pair));
}
/**
* @since 9.1.
*/
@Override
@Test
public void collectWithIndexWithTarget()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(
Lists.mutable.empty(),
integers.collectWithIndex(PrimitiveTuples::pair, Lists.mutable.empty()));
}
@Override
@Test
public void collectWith()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Verify.assertIterableEmpty(integers.collectWith((value, parameter) -> value / parameter, 1));
}
@Override
@Test
public void collectToTarget()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest();
ImmutableSortedSet<Integer> collect = integers.collect(Functions.getIntegerPassThru(), TreeSortedSet.newSet(Collections.reverseOrder())).toImmutable();
Verify.assertIterableEmpty(collect);
Assert.assertEquals(Collections.<Integer>reverseOrder(), collect.comparator());
}
@Override
@Test
public void partitionWhile()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
PartitionImmutableSortedSet<Integer> partition = set.partitionWhile(Predicates.lessThan(4));
Verify.assertIterableEmpty(partition.getSelected());
Verify.assertIterableEmpty(partition.getRejected());
Assert.assertEquals(Collections.<Integer>reverseOrder(), partition.getSelected().comparator());
Assert.assertEquals(Collections.<Integer>reverseOrder(), partition.getRejected().comparator());
}
@Override
@Test
public void takeWhile()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
ImmutableSortedSet<Integer> take = set.takeWhile(Predicates.lessThan(4));
Verify.assertIterableEmpty(take);
Assert.assertEquals(Collections.<Integer>reverseOrder(), take.comparator());
}
@Override
@Test
public void dropWhile()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
ImmutableSortedSet<Integer> drop = set.dropWhile(Predicates.lessThan(4));
Verify.assertIterableEmpty(drop);
Assert.assertEquals(Collections.<Integer>reverseOrder(), drop.comparator());
}
@Override
@Test
public void selectInstancesOf()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Verify.assertEmpty(set.castToSortedSet());
Verify.assertEmpty(set.selectInstancesOf(Integer.class).castToSortedSet());
Assert.assertEquals(Collections.<Double>reverseOrder(), set.selectInstancesOf(Double.class).comparator());
}
@Override
@Test
public void toSortedSet()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Collections.reverseOrder());
Assert.assertNull(set.toSortedSet().comparator());
Verify.assertEmpty(set.toSortedSet());
}
@Override
@Test
public void toSortedSetWithComparator()
{
ImmutableSortedSet<Integer> set = this.classUnderTest();
Assert.assertEquals(Collections.<Integer>reverseOrder(), set.toSortedSet(Collections.reverseOrder()).comparator());
Verify.assertEmpty(set.toSortedSet());
}
@Test(expected = NoSuchElementException.class)
public void first()
{
this.classUnderTest().castToSortedSet().first();
}
@Test(expected = NoSuchElementException.class)
public void last()
{
this.classUnderTest().castToSortedSet().last();
}
@Test
public void compareTo()
{
Assert.assertEquals(0, (long) this.classUnderTest().compareTo(this.classUnderTest()));
Assert.assertEquals(-1, this.classUnderTest().compareTo(TreeSortedSet.newSetWith(1)));
Assert.assertEquals(-4, this.classUnderTest().compareTo(TreeSortedSet.newSetWith(1, 2, 3, 4)));
}
@Override
@Test
public void cartesianProduct()
{
LazyIterable<Pair<Integer, Integer>> emptyProduct = this.classUnderTest().cartesianProduct(SortedSets.immutable.of(1, 2, 3));
Verify.assertEmpty(emptyProduct.toList());
LazyIterable<Pair<Integer, Integer>> empty2 = this.classUnderTest().cartesianProduct(TreeSortedSet.newSet());
Verify.assertEmpty(empty2.toList());
}
@Override
@Test
public void indexOf()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Comparators.reverseNaturalOrder());
Assert.assertEquals(-1, integers.indexOf(4));
Assert.assertEquals(-1, integers.indexOf(3));
Assert.assertEquals(-1, integers.indexOf(2));
Assert.assertEquals(-1, integers.indexOf(1));
Assert.assertEquals(-1, integers.indexOf(0));
Assert.assertEquals(-1, integers.indexOf(5));
}
@Override
@Test
public void forEachFromTo()
{
MutableSortedSet<Integer> result = TreeSortedSet.newSet(Comparators.reverseNaturalOrder());
ImmutableSortedSet<Integer> integers = this.classUnderTest(Comparators.reverseNaturalOrder());
Verify.assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(-1, 0, result::add));
Verify.assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(0, -1, result::add));
Verify.assertThrows(IndexOutOfBoundsException.class, () -> integers.forEach(0, 0, result::add));
}
@Override
@Test
public void forEachWithIndexWithFromTo()
{
MutableList<Integer> result = Lists.mutable.empty();
ImmutableSortedSet<Integer> integers = this.classUnderTest(Comparators.reverseNaturalOrder());
Verify.assertThrows(IndexOutOfBoundsException.class, () -> integers.forEachWithIndex(-1, 0, new AddToList(result)));
Verify.assertThrows(IndexOutOfBoundsException.class, () -> integers.forEachWithIndex(0, -1, new AddToList(result)));
Verify.assertThrows(IndexOutOfBoundsException.class, () -> integers.forEachWithIndex(0, 0, new AddToList(result)));
}
@Override
public void toStack()
{
ImmutableSortedSet<Integer> set = this.classUnderTest(Comparators.reverseNaturalOrder());
Assert.assertEquals(Stacks.mutable.with(), set.toStack());
}
@Override
@Test
public void powerSet()
{
Verify.assertSize(1, this.classUnderTest().powerSet().castToSortedSet());
Assert.assertEquals(SortedSets.immutable.of(SortedSets.immutable.<Integer>empty()), this.classUnderTest().powerSet());
}
@Override
@Test
public void toSortedMap()
{
MutableSortedMap<Integer, Integer> map = this.classUnderTest().toSortedMap(Functions.getIntegerPassThru(), Functions.getIntegerPassThru());
Verify.assertEmpty(map);
Verify.assertInstanceOf(TreeSortedMap.class, map);
}
@Override
@Test
public void toSortedMap_with_comparator()
{
MutableSortedMap<Integer, Integer> map = this.classUnderTest().toSortedMap(Comparators.reverseNaturalOrder(),
Functions.getIntegerPassThru(), Functions.getIntegerPassThru());
Verify.assertEmpty(map);
Verify.assertInstanceOf(TreeSortedMap.class, map);
Assert.assertEquals(Comparators.<Integer>reverseNaturalOrder(), map.comparator());
}
@Override
@Test
public void toSortedMapBy()
{
MutableSortedMap<Integer, Integer> map = this.classUnderTest().toSortedMapBy(key -> -key,
Functions.getIntegerPassThru(), Functions.getIntegerPassThru());
Verify.assertEmpty(map);
Verify.assertInstanceOf(TreeSortedMap.class, map);
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void subSet()
{
this.classUnderTest().castToSortedSet().subSet(1, 4);
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void headSet()
{
this.classUnderTest().castToSortedSet().headSet(4);
}
@Override
@Test(expected = UnsupportedOperationException.class)
public void tailSet()
{
this.classUnderTest().castToSortedSet().tailSet(1);
}
@Override
@Test
public void collectBoolean()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(BooleanArrayList.newListWith(), integers.collectBoolean(PrimitiveFunctions.integerIsPositive()));
}
@Override
@Test
public void collectByte()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(ByteArrayList.newListWith(), integers.collectByte(PrimitiveFunctions.unboxIntegerToByte()));
}
@Override
@Test
public void collectChar()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(CharArrayList.newListWith(), integers.collectChar(integer -> (char) (integer.intValue() + 64)));
}
@Override
@Test
public void collectDouble()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(DoubleArrayList.newListWith(), integers.collectDouble(PrimitiveFunctions.unboxIntegerToDouble()));
}
@Override
@Test
public void collectFloat()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(FloatArrayList.newListWith(), integers.collectFloat(PrimitiveFunctions.unboxIntegerToFloat()));
}
@Override
@Test
public void collectInt()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(IntArrayList.newListWith(), integers.collectInt(PrimitiveFunctions.unboxIntegerToInt()));
}
@Override
@Test
public void collectLong()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(LongArrayList.newListWith(), integers.collectLong(PrimitiveFunctions.unboxIntegerToLong()));
}
@Override
@Test
public void collectShort()
{
ImmutableSortedSet<Integer> integers = this.classUnderTest(Collections.reverseOrder());
Assert.assertEquals(ShortArrayList.newListWith(), integers.collectShort(PrimitiveFunctions.unboxIntegerToShort()));
}
@Override
@Test
public void groupByUniqueKey_throws()
{
super.groupByUniqueKey_throws();
Assert.assertEquals(UnifiedMap.newMap().toImmutable(), this.classUnderTest().groupByUniqueKey(id -> id));
}
@Override
@Test
public void groupByUniqueKey_target_throws()
{
super.groupByUniqueKey_target_throws();
Assert.assertEquals(UnifiedMap.newMap(), this.classUnderTest().groupByUniqueKey(id -> id, UnifiedMap.newMap()));
}
@Override
@Test
public void take()
{
Assert.assertEquals(this.classUnderTest(), this.classUnderTest().take(2));
}
@Override
@Test
public void drop()
{
Assert.assertEquals(this.classUnderTest(), this.classUnderTest().drop(2));
}
}