View Javadoc
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   * TestImmutableArrayList.java.
14   * <p>
15   * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
17   * distributed under a three-clause BSD-style license, which can be found at
18   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
19   * </p>
20   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
21   */
22  public class TestImmutableArrayList
23  {
24  
25      /** Test the ImmutableArrayList. */
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       * Test for the int list.
47       * @param list the expected list
48       * @param imList the immutable ist
49       * @param copyOrWrap whether to expect COPY or WRAP
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); // this WRAP method returns a NEW 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          // modify the underlying data structure
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  }