1 package org.djutils.immutablecollections;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Comparator;
10 import java.util.List;
11
12 import org.junit.Test;
13
14
15
16
17
18
19
20
21
22
23 public class TestImmutableCollectionStatics
24 {
25
26
27
28
29 @Test
30 public void testEmptyConstructors()
31 {
32 assertEquals("empty immutable set is empty", 0, ImmutableCollections.emptyImmutableSet().size());
33 assertEquals("empty immutable list is empty", 0, ImmutableCollections.emptyImmutableList().size());
34 assertEquals("empty immutable map is empty", 0, ImmutableCollections.emptyImmutableMap().size());
35 }
36
37
38
39
40 @Test
41 public void testSearchers()
42 {
43 final Integer[] values = new Integer[] {10, 2, -5, 2, 2, 6};
44 ImmutableList<Integer> il = new ImmutableArrayList<>(new ArrayList<Integer>(Arrays.asList(values)), Immutable.WRAP);
45 assertEquals("max", Integer.valueOf(10), ImmutableCollections.max(il));
46 assertEquals("min", Integer.valueOf(-5), ImmutableCollections.min(il));
47 Comparator<Integer> indexComparator = new Comparator<Integer>()
48 {
49
50 @Override
51 public int compare(final Integer o1, final Integer o2)
52 {
53 return Arrays.binarySearch(values, o1) - Arrays.binarySearch(values, o2);
54 }
55 };
56 assertEquals("custom comparator max", Integer.valueOf(6), ImmutableCollections.max(il, indexComparator));
57 assertEquals("custom comparator min", Integer.valueOf(10), ImmutableCollections.min(il, indexComparator));
58 assertEquals("number of 10s", 1, ImmutableCollections.frequency(il, Integer.valueOf(10)));
59 assertEquals("number of 100s", 0, ImmutableCollections.frequency(il, Integer.valueOf(100)));
60 assertEquals("number of 2s", 3, ImmutableCollections.frequency(il, Integer.valueOf(2)));
61 Integer[] subList = new Integer[] {2, 2};
62 ImmutableList<Integer> isl = new ImmutableArrayList<Integer>(new ArrayList<>(Arrays.asList(subList)));
63 assertEquals("position of sub list", 3, ImmutableCollections.indexOfSubList(il, isl));
64 assertEquals("position of non-sub list", -1, ImmutableCollections.indexOfSubList(isl, il));
65 assertEquals("last position of sub list", 3, ImmutableCollections.lastIndexOfSubList(il, isl));
66 assertEquals("last position of non-sub list", -1, ImmutableCollections.lastIndexOfSubList(isl, il));
67 List<Integer> msl = new ArrayList<>(Arrays.asList(subList));
68 assertEquals("position of sub list", 3, ImmutableCollections.indexOfSubList(il, msl));
69 assertEquals("position of non-sub list", -1, ImmutableCollections.indexOfSubList(msl, il));
70 assertEquals("last position of sub list", 3, ImmutableCollections.lastIndexOfSubList(il, msl));
71 assertEquals("last position of non-sub list", -1, ImmutableCollections.lastIndexOfSubList(msl, il));
72
73 Arrays.sort(values);
74 il = new ImmutableArrayList<>(new ArrayList<Integer>(Arrays.asList(values)));
75 assertEquals("position of 6", 4, ImmutableCollections.binarySearch(il, Integer.valueOf(6)));
76 assertEquals("position where 5 would be if it were present", -5,
77 ImmutableCollections.binarySearch(il, Integer.valueOf(5)));
78 final Integer[] uniqueValues = new Integer[] {10, 2, -5, 6};
79 ImmutableList<Integer> il2 = new ImmutableArrayList<>(new ArrayList<Integer>(Arrays.asList(uniqueValues)));
80 indexComparator = new Comparator<Integer>()
81 {
82
83 @Override
84 public int compare(final Integer o1, final Integer o2)
85 {
86 return Arrays.binarySearch(uniqueValues, o1) - Arrays.binarySearch(uniqueValues, o2);
87 }
88 };
89 assertEquals("position of 2 binary search with crazy comparator", 1,
90 ImmutableCollections.binarySearch(il2, Integer.valueOf(2), indexComparator));
91 assertEquals("position of 10 binary search with crazy comparator", 0,
92 ImmutableCollections.binarySearch(il2, Integer.valueOf(10), indexComparator));
93 assertEquals("position of 6 binary search with crazy comparator", 3,
94 ImmutableCollections.binarySearch(il2, Integer.valueOf(6), indexComparator));
95 assertFalse("The collections are not disjoint", ImmutableCollections.disjoint(il, il2));
96 ImmutableList<Integer> il3 = new ImmutableArrayList<>(new ArrayList<Integer>(Arrays.asList(new Integer[] {99, 999})));
97 assertTrue("The collections are disjoint", ImmutableCollections.disjoint(il, il3));
98 List<Integer> mutableList = new ArrayList<>(Arrays.asList(uniqueValues));
99 assertFalse("The collections are not disjoint", ImmutableCollections.disjoint(il, mutableList));
100 assertFalse("The collections are not disjoint", ImmutableCollections.disjoint(mutableList, il));
101 mutableList = new ArrayList<Integer>(Arrays.asList(new Integer[] {99, 999}));
102 assertTrue("The collections are disjoint", ImmutableCollections.disjoint(il, mutableList));
103 assertTrue("The collections are disjoint", ImmutableCollections.disjoint(mutableList, il));
104
105 }
106 }