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    * TestImmutableMap tests the static of() methods of the ImmutableMap. <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 TestImmutableMap
19  {
20      /** test the of() methods. */
21      @Test
22      public void testOf()
23      {
24          ImmutableMap<Integer, String> map = ImmutableMap.of();
25          assertTrue(map.isEmpty());
26  
27          map = ImmutableMap.of(1, "a");
28          assertEquals(1, map.size());
29          assertTrue(map.containsKey(Integer.valueOf(1)));
30          assertTrue(map.containsValue("a"));
31          assertEquals("a", map.get(1));
32  
33          map = ImmutableMap.of(1, "a", 2, "b");
34          assertEquals(2, map.size());
35          assertTrue(map.containsKey(Integer.valueOf(1)));
36          assertTrue(map.containsValue("a"));
37          assertEquals("a", map.get(1));
38          assertTrue(map.containsKey(Integer.valueOf(2)));
39          assertTrue(map.containsValue("b"));
40          assertEquals("b", map.get(2));
41  
42          map = ImmutableMap.of(1, "a", 2, "b", 3, "c");
43          assertEquals(3, map.size());
44          for (int i = 0; i < map.size(); i++)
45          {
46              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
47              assertTrue(map.containsValue("" + (char) ('a' + i)));
48              assertEquals("" + (char) ('a' + i), map.get(i + 1));
49          }
50  
51          map = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d");
52          assertEquals(4, map.size());
53          for (int i = 0; i < map.size(); i++)
54          {
55              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
56              assertTrue(map.containsValue("" + (char) ('a' + i)));
57              assertEquals("" + (char) ('a' + i), map.get(i + 1));
58          }
59  
60          map = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e");
61          assertEquals(5, map.size());
62          for (int i = 0; i < map.size(); i++)
63          {
64              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
65              assertTrue(map.containsValue("" + (char) ('a' + i)));
66              assertEquals("" + (char) ('a' + i), map.get(i + 1));
67          }
68  
69          map = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f");
70          assertEquals(6, map.size());
71          for (int i = 0; i < map.size(); i++)
72          {
73              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
74              assertTrue(map.containsValue("" + (char) ('a' + i)));
75              assertEquals("" + (char) ('a' + i), map.get(i + 1));
76          }
77  
78          map = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f", 7, "g");
79          assertEquals(7, map.size());
80          for (int i = 0; i < map.size(); i++)
81          {
82              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
83              assertTrue(map.containsValue("" + (char) ('a' + i)));
84              assertEquals("" + (char) ('a' + i), map.get(i + 1));
85          }
86  
87          map = ImmutableMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e", 6, "f", 7, "g", 8, "h");
88          assertEquals(8, map.size());
89          for (int i = 0; i < map.size(); i++)
90          {
91              assertTrue(map.containsKey(Integer.valueOf(i + 1)));
92              assertTrue(map.containsValue("" + (char) ('a' + i)));
93              assertEquals("" + (char) ('a' + i), map.get(i + 1));
94          }
95      }
96  
97  }