View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import static org.junit.Assert.fail;
4   
5   import java.util.Arrays;
6   import java.util.Enumeration;
7   import java.util.HashSet;
8   import java.util.Set;
9   import java.util.Vector;
10  
11  import org.junit.Assert;
12  import org.junit.Test;
13  
14  /**
15   * TestImmutableVector.java.
16   * <p>
17   * Copyright (c) 2002-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
19   * distributed under a three-clause BSD-style license, which can be found at
20   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
21   * </p>
22   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
23   */
24  public class TestImmutableVector
25  {
26  
27      /**
28       * Test vectors.
29       */
30      @Test
31      public final void testVector()
32      {
33          Integer[] testData = new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
34          Vector<Integer> intVector = new Vector<>(Arrays.asList(testData));
35          Vector<Integer> vector = new Vector<Integer>(intVector);
36          testIntVector(vector, new ImmutableVector<Integer>(vector, Immutable.WRAP), Immutable.WRAP);
37          vector = new Vector<Integer>(intVector);
38          testIntVector(vector, new ImmutableVector<Integer>(vector, Immutable.COPY), Immutable.COPY);
39          vector = new Vector<Integer>(intVector);
40          testIntVector(vector, new ImmutableVector<Integer>(vector), Immutable.COPY);
41          vector = new Vector<Integer>(intVector);
42          ImmutableVector<Integer> ial = new ImmutableVector<Integer>(vector);
43          testIntVector(vector, new ImmutableVector<Integer>(ial), Immutable.COPY);
44  
45          // Verify that the ImmutableIterator throws an exception when the remove method is called.
46          ImmutableIterator<Integer> ii = ial.iterator();
47          ii.next();
48          try
49          {
50              ii.remove();
51              fail("remove method of ImmutableIterator should have thrown an exception");
52          }
53          catch (UnsupportedOperationException e)
54          {
55              // Ignore expected exception
56          }
57  
58          vector = new Vector<Integer>(intVector);
59          Set<Integer> intSet = new HashSet<>(Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}));
60          testIntVector(vector, new ImmutableVector<Integer>(intSet), Immutable.COPY);
61          ImmutableVector<Integer> iv = new ImmutableVector<Integer>(vector);
62          Assert.assertTrue("toString returns something descriptive", iv.toString().startsWith("ImmutableVector ["));
63          ImmutableVector<Integer> iv2 = new ImmutableVector<>(iv, Immutable.COPY);
64          Assert.assertEquals("ImmutableVector with copy of other ImmutableVector tests equal to it", iv, iv2);
65          Assert.assertEquals("ImmutableVector with copy of other ImmutableVector has same hash code", iv.hashCode(),
66                  iv2.hashCode());
67          iv2 = new ImmutableVector<>(iv, Immutable.WRAP);
68          Assert.assertEquals("ImmutableVector wrapping other ImmutableVector tests equal to it", iv, iv2);
69          Assert.assertEquals("ImmutableVector wrapping other ImmutableVector has same hash code", iv.hashCode(), iv2.hashCode());
70          // start anew as the testIntVector method modifies the underlying data.
71          intVector = new Vector<>(Arrays.asList(testData));
72          iv = new ImmutableVector<Integer>(intVector);
73          ImmutableList<Integer> subList = iv.subList(2, 5);
74          Assert.assertEquals("size of sub list is 3", 3, subList.size());
75          for (int index = 0; index < subList.size(); index++)
76          {
77              Assert.assertEquals("value at index matches", iv.get(index + 2), subList.get(index));
78          }
79          try
80          {
81              iv.subList(-1, 3);
82              Assert.fail("Negative from index should have thrown an IndexOutOfBoundsException");
83          }
84          catch (IndexOutOfBoundsException ioobe)
85          {
86              // Ignore expected exception
87          }
88          try
89          {
90              iv.subList(1, iv.size() + 1);
91              Assert.fail("To index bigger than size should have thrown an IndexOutOfBoundsException");
92          }
93          catch (IndexOutOfBoundsException ioobe)
94          {
95              // Ignore expected exception
96          }
97          try
98          {
99              iv.subList(5, 4);
100             Assert.fail("negative range should have thrown an IllegalArgumentException");
101         }
102         catch (IllegalArgumentException iae)
103         {
104             // Ignore expected exception
105         }
106         subList = iv.subList(4, 4);
107         Assert.assertEquals("sub list should be empty", 0, subList.size());
108         Integer[] justRight = new Integer[iv.size()];
109         iv.copyInto(justRight);
110         for (int index = 0; index < iv.size(); index++)
111         {
112             Assert.assertEquals("contents of array matches", iv.get(index), justRight[index]);
113         }
114         Integer[] bigger = new Integer[iv.size() + 3];
115         bigger[bigger.length - 2] = -1;
116         iv.copyInto(bigger);
117         for (int index = 0; index < iv.size(); index++)
118         {
119             Assert.assertEquals("contents of array matches", iv.get(index), justRight[index]);
120         }
121         Assert.assertEquals("element after required length is still null", null, bigger[iv.size()]);
122         Assert.assertEquals("element at length - 2 is still -1", -1, bigger[bigger.length - 2], 0);
123         Assert.assertEquals("element at length - 1 is null", null, bigger[bigger.length - 1]);
124 
125         Integer[] tooShort = new Integer[iv.size() - 1];
126         try
127         {
128             iv.copyInto(tooShort);
129             Assert.fail("Too short target array should have thrown an IndexOutOfBoundsException");
130         }
131         catch (IndexOutOfBoundsException ioobe)
132         {
133             // Ignore expected exception
134         }
135         Assert.assertTrue("capacity returns capacity of the underlying collection", iv.capacity() >= testData.length);
136 
137         Enumeration<Integer> e = iv.elements();
138         for (int index = 0; index < testData.length; index++)
139         {
140             Assert.assertTrue("There is another element to be had", e.hasMoreElements());
141             Integer got = e.nextElement();
142             Assert.assertEquals("element at index matches", testData[index], got);
143         }
144         Assert.assertFalse("there are no more elements to be had", e.hasMoreElements());
145         for (int index = 0; index < testData.length; index++)
146         {
147             int indexOf = iv.indexOf(testData[index]);
148             Assert.assertEquals("index matches", index, indexOf);
149             Assert.assertEquals("value at index matches", testData[index], iv.get(indexOf));
150             indexOf = iv.lastIndexOf(testData[index]);
151             Assert.assertEquals("index matches", index, indexOf);
152             int noIndex = iv.indexOf(testData[index], indexOf + 1);
153             Assert.assertEquals("there is no later next index for this value", -1, noIndex);
154             noIndex = iv.lastIndexOf(testData[index], indexOf - 1);
155             Assert.assertEquals("there is no earlier next index for this value", -1, noIndex);
156             Assert.assertEquals("get returns same as elementAt", iv.get(index), iv.elementAt(index));
157         }
158         Assert.assertEquals("firstElement returns first element", testData[0], iv.firstElement());
159         Assert.assertEquals("lastElement returns last element", testData[testData.length - 1], iv.lastElement());
160     }
161 
162     /**
163      * ...
164      * @param vector Vector&lt;Integer&gt;; a vector of Integer
165      * @param imVector ImmutableVector&lt;Integer&gt;; an immutable vector of Integer
166      * @param copyOrWrap Immutable
167      */
168     private void testIntVector(final Vector<Integer> vector, final ImmutableVector<Integer> imVector,
169             final Immutable copyOrWrap)
170     {
171         Assert.assertTrue(vector.size() == 10);
172         Assert.assertTrue(imVector.size() == 10);
173         for (int i = 0; i < 10; i++)
174         {
175             Assert.assertTrue(imVector.get(i) == vector.get(i));
176         }
177         Assert.assertFalse(imVector.isEmpty());
178         Assert.assertTrue(imVector.contains(5));
179         Assert.assertFalse(imVector.contains(15));
180         if (copyOrWrap == Immutable.COPY)
181         {
182             Assert.assertTrue(imVector.isCopy());
183             Assert.assertTrue(imVector.toList().equals(vector));
184             Assert.assertFalse(imVector.toList() == vector);
185         }
186         else
187         {
188             Assert.assertTrue(imVector.isWrap());
189             Assert.assertTrue(imVector.toList().equals(vector));
190             Assert.assertFalse(imVector.toList() == vector); // this WRAP method returns a NEW list
191         }
192 
193         Vector<Integer> to = imVector.toVector();
194         Assert.assertTrue(vector.equals(to));
195 
196         Integer[] arr = imVector.toArray(new Integer[] {});
197         Integer[] sar = vector.toArray(new Integer[] {});
198         Assert.assertArrayEquals(arr, sar);
199 
200         // modify the underlying data structure
201         vector.add(11);
202         if (copyOrWrap == Immutable.COPY)
203         {
204             Assert.assertTrue(imVector.size() == 10);
205         }
206         else
207         {
208             Assert.assertTrue(imVector.size() == 11);
209         }
210     }
211 }