View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.map;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * JUnit tests.
26   */
27  public class HashedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
28  
29      public HashedMapTest() {
30          super(HashedMapTest.class.getSimpleName());
31      }
32  
33      @Override
34      public String getCompatibilityVersion() {
35          return "4";
36      }
37  
38      @Override
39      public HashedMap<K, V> makeObject() {
40          return new HashedMap<>();
41      }
42  
43      @Test
44      @SuppressWarnings("unchecked")
45      public void testClone() {
46          final HashedMap<K, V> map = new HashedMap<>(10);
47          map.put((K) "1", (V) "1");
48          final HashedMap<K, V> cloned = map.clone();
49          assertEquals(map.size(), cloned.size());
50          assertSame(map.get("1"), cloned.get("1"));
51      }
52  
53      /**
54       * Test for <a href="https://issues.apache.org/jira/browse/COLLECTIONS-323">COLLECTIONS-323</a>.
55       */
56      @Test
57      public void testInitialCapacityZero() {
58          final HashedMap<String, String> map = new HashedMap<>(0);
59          assertEquals(1, map.data.length);
60      }
61  
62  //    public void testCreate() throws Exception {
63  //        resetEmpty();
64  //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/HashedMap.emptyCollection.version4.obj");
65  //        resetFull();
66  //        writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/HashedMap.fullCollection.version4.obj");
67  //    }
68  
69      @Test
70      public void testInternalState() {
71          final HashedMap<Integer, Integer> map = new HashedMap<>(42, 0.75f);
72          assertEquals(0.75f, map.loadFactor, 0.1f);
73          assertEquals(0, map.size);
74          assertEquals(64, map.data.length);
75          assertEquals(48, map.threshold);
76          assertEquals(0, map.modCount);
77  
78          // contract: the capacity is ensured when too many elements are added
79          final HashedMap<Integer, Integer> tmpMap = new HashedMap<>();
80          // we need to put at least the "threshold" number of elements
81          // in order to double the capacity
82          for (int i = 1; i <= map.threshold; i++) {
83              tmpMap.put(i, i);
84          }
85          map.putAll(tmpMap);
86          // the threshold has changed due to calling ensureCapacity
87          assertEquals(96, map.threshold);
88      }
89  }