View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import org.junit.Test;
7   
8   /**
9    * TestImmutableSortedMap tests the static of() methods of the ImmutableSortedMap. <br>
10   * <br>
11   * Copyright (c) 2020-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
13   * distributed under a three-clause BSD-style license, which can be found at
14   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public class TestImmutableSortedMap
19  {
20      /** test the of() methods. */
21      @Test
22      public void testOf()
23      {
24          ImmutableSortedMap<Integer, String> map = ImmutableSortedMap.of();
25          assertTrue(map.isEmpty());
26  
27          map = ImmutableSortedMap.of(1, "a");
28          assertEquals(1, map.size());
29          ImmutableIterator<Integer> itKey = map.keySet().iterator();
30          for (int i = 0; i < map.size(); i++)
31          {
32              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
33              assertTrue(map.containsValue("" + (char) ('a' + i)));
34              assertEquals("" + (char) ('a' + i), map.get(i + 1));
35              assertEquals(Integer.valueOf(i + 1), itKey.next());
36          }
37  
38          map = ImmutableSortedMap.of(1, "a", 2, "b");
39          assertEquals(2, map.size());
40          itKey = map.keySet().iterator();
41          for (int i = 0; i < map.size(); i++)
42          {
43              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
44              assertTrue(map.containsValue("" + (char) ('a' + i)));
45              assertEquals("" + (char) ('a' + i), map.get(i + 1));
46              assertEquals(Integer.valueOf(i + 1), itKey.next());
47          }
48  
49          map = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c");
50          assertEquals(3, map.size());
51          itKey = map.keySet().iterator();
52          for (int i = 0; i < map.size(); i++)
53          {
54              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
55              assertTrue(map.containsValue("" + (char) ('a' + i)));
56              assertEquals("" + (char) ('a' + i), map.get(i + 1));
57              assertEquals(Integer.valueOf(i + 1), itKey.next());
58          }
59  
60          map = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d");
61          assertEquals(4, map.size());
62          itKey = map.keySet().iterator();
63          for (int i = 0; i < map.size(); i++)
64          {
65              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
66              assertTrue(map.containsValue("" + (char) ('a' + i)));
67              assertEquals("" + (char) ('a' + i), map.get(i + 1));
68              assertEquals(Integer.valueOf(i + 1), itKey.next());
69          }
70  
71          map = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e");
72          assertEquals(5, map.size());
73          itKey = map.keySet().iterator();
74          for (int i = 0; i < map.size(); i++)
75          {
76              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
77              assertTrue(map.containsValue("" + (char) ('a' + i)));
78              assertEquals("" + (char) ('a' + i), map.get(i + 1));
79              assertEquals(Integer.valueOf(i + 1), itKey.next());
80          }
81  
82          map = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f");
83          assertEquals(6, map.size());
84          itKey = map.keySet().iterator();
85          for (int i = 0; i < map.size(); i++)
86          {
87              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
88              assertTrue(map.containsValue("" + (char) ('a' + i)));
89              assertEquals("" + (char) ('a' + i), map.get(i + 1));
90              assertEquals(Integer.valueOf(i + 1), itKey.next());
91          }
92  
93          map = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f", 7, "g");
94          assertEquals(7, map.size());
95          itKey = map.keySet().iterator();
96          for (int i = 0; i < map.size(); i++)
97          {
98              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
99              assertTrue(map.containsValue("" + (char) ('a' + i)));
100             assertEquals("" + (char) ('a' + i), map.get(i + 1));
101             assertEquals(Integer.valueOf(i + 1), itKey.next());
102         }
103 
104         map = ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f", 7, "g", 8, "h");
105         assertEquals(8, map.size());
106         itKey = map.keySet().iterator();
107         for (int i = 0; i < map.size(); i++)
108         {
109             assertTrue(map.containsKey(Integer.valueOf(i + 1)));
110             assertTrue(map.containsValue("" + (char) ('a' + i)));
111             assertEquals("" + (char) ('a' + i), map.get(i + 1));
112             assertEquals(Integer.valueOf(i + 1), itKey.next());
113         }
114     }
115 
116 }