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 * @param <K> the key type.
28 * @param <V> the value type.
29 */
30 public class HashedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
31
32 @Override
33 public String getCompatibilityVersion() {
34 return "4";
35 }
36
37 @Override
38 public HashedMap<K, V> makeObject() {
39 return new HashedMap<>();
40 }
41
42 @Test
43 @SuppressWarnings("unchecked")
44 public void testClone() {
45 final HashedMap<K, V> map = new HashedMap<>(10);
46 map.put((K) "1", (V) "1");
47 final HashedMap<K, V> cloned = map.clone();
48 assertEquals(map.size(), cloned.size());
49 assertSame(map.get("1"), cloned.get("1"));
50 }
51
52 /**
53 * Test for <a href="https://issues.apache.org/jira/browse/COLLECTIONS-323">COLLECTIONS-323</a>.
54 */
55 @Test
56 public void testInitialCapacityZero() {
57 final HashedMap<String, String> map = new HashedMap<>(0);
58 assertEquals(1, map.data.length);
59 }
60
61 // public void testCreate() throws Exception {
62 // resetEmpty();
63 // writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/HashedMap.emptyCollection.version4.obj");
64 // resetFull();
65 // writeExternalFormToDisk((java.io.Serializable) map, "src/test/resources/data/test/HashedMap.fullCollection.version4.obj");
66 // }
67
68 @Test
69 public void testInternalState() {
70 final HashedMap<Integer, Integer> map = new HashedMap<>(42, 0.75f);
71 assertEquals(0.75f, map.loadFactor, 0.1f);
72 assertEquals(0, map.size);
73 assertEquals(64, map.data.length);
74 assertEquals(48, map.threshold);
75 assertEquals(0, map.modCount);
76
77 // contract: the capacity is ensured when too many elements are added
78 final HashedMap<Integer, Integer> tmpMap = new HashedMap<>();
79 // we need to put at least the "threshold" number of elements
80 // in order to double the capacity
81 for (int i = 1; i <= map.threshold; i++) {
82 tmpMap.put(i, i);
83 }
84 map.putAll(tmpMap);
85 // the threshold has changed due to calling ensureCapacity
86 assertEquals(96, map.threshold);
87 }
88 }