1 package org.djutils.immutablecollections;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.junit.Assert;
10 import org.junit.Test;
11
12
13
14
15
16
17
18
19
20
21
22 public class TestImmutableArrayList
23 {
24
25
26 @Test
27 public final void testArrayList()
28 {
29 List<Integer> intList = Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
30 List<Integer> list = new ArrayList<Integer>(intList);
31 testIntList(list, new ImmutableArrayList<Integer>(list, Immutable.WRAP), Immutable.WRAP);
32 list = new ArrayList<Integer>(intList);
33 testIntList(list, new ImmutableArrayList<Integer>(list, Immutable.COPY), Immutable.COPY);
34 list = new ArrayList<Integer>(intList);
35 testIntList(list, new ImmutableArrayList<Integer>(list), Immutable.COPY);
36 list = new ArrayList<Integer>(intList);
37 ImmutableArrayList<Integer> ial = new ImmutableArrayList<Integer>(list);
38 testIntList(list, new ImmutableArrayList<Integer>(ial), Immutable.COPY);
39
40 list = new ArrayList<Integer>(intList);
41 Set<Integer> intSet = new HashSet<>(Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
42 testIntList(list, new ImmutableArrayList<Integer>(intSet), Immutable.COPY);
43 }
44
45
46
47
48
49
50
51 private void testIntList(final List<Integer> list, final ImmutableList<Integer> imList, final Immutable copyOrWrap)
52 {
53 Assert.assertTrue(list.size() == 10);
54 Assert.assertTrue(imList.size() == 10);
55 for (int i = 0; i < 10; i++)
56 {
57 Assert.assertTrue(imList.get(i) == list.get(i));
58 }
59 Assert.assertFalse(imList.isEmpty());
60 Assert.assertTrue(imList.contains(5));
61 Assert.assertFalse(imList.contains(15));
62 if (copyOrWrap == Immutable.COPY)
63 {
64 Assert.assertTrue(imList.isCopy());
65 Assert.assertTrue(imList.toList().equals(list));
66 Assert.assertFalse(imList.toList() == list);
67 }
68 else
69 {
70 Assert.assertTrue(imList.isWrap());
71 Assert.assertTrue(imList.toList().equals(list));
72 Assert.assertFalse(imList.toList() == list);
73 }
74
75 List<Integer> to = imList.toList();
76 Assert.assertTrue(list.equals(to));
77
78 Integer[] arr = imList.toArray(new Integer[] {});
79 Integer[] sar = list.toArray(new Integer[] {});
80 Assert.assertArrayEquals(arr, sar);
81
82
83 list.add(11);
84 if (copyOrWrap == Immutable.COPY)
85 {
86 Assert.assertTrue(imList.size() == 10);
87 }
88 else
89 {
90 Assert.assertTrue(imList.size() == 11);
91 }
92 }
93
94 }