1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import static com.google.common.collect.Iterables.getOnlyElement;
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import com.google.common.collect.testing.MapTestSuiteBuilder;
23 import com.google.common.collect.testing.TestStringMapGenerator;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import com.google.common.collect.testing.features.MapFeature;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32 
33 /**
34  * Tests for {@code CompactHashMap}.
35  *
36  * @author Louis Wasserman
37  */
38 public class CompactHashMapTest extends TestCase {
suite()39   public static Test suite() {
40     TestSuite suite = new TestSuite();
41     suite.addTest(
42         MapTestSuiteBuilder.using(
43                 new TestStringMapGenerator() {
44                   @Override
45                   protected Map<String, String> create(Entry<String, String>[] entries) {
46                     Map<String, String> map = CompactHashMap.create();
47                     for (Entry<String, String> entry : entries) {
48                       map.put(entry.getKey(), entry.getValue());
49                     }
50                     return map;
51                   }
52                 })
53             .named("CompactHashMap")
54             .withFeatures(
55                 CollectionSize.ANY,
56                 MapFeature.GENERAL_PURPOSE,
57                 MapFeature.ALLOWS_NULL_KEYS,
58                 MapFeature.ALLOWS_NULL_VALUES,
59                 CollectionFeature.SERIALIZABLE,
60                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
61             .createTestSuite());
62     suite.addTestSuite(CompactHashMapTest.class);
63     return suite;
64   }
65 
testTrimToSize()66   public void testTrimToSize() {
67     CompactHashMap<Integer, String> map = CompactHashMap.createWithExpectedSize(100);
68     for (int i = 0; i < 10; i++) {
69       map.put(i, Integer.toString(i));
70     }
71     map.trimToSize();
72     assertThat(map.entries).hasLength(10);
73     assertThat(map.keys).hasLength(10);
74     assertThat(map.values).hasLength(10);
75     assertEquals(10, map.size());
76     for (int i = 0; i < 10; i++) {
77       assertEquals(Integer.toString(i), map.get(i));
78     }
79   }
80 
testEntrySetValueAfterRemoved()81   public void testEntrySetValueAfterRemoved() {
82     CompactHashMap<Integer, String> map = CompactHashMap.create();
83     map.put(1, "1");
84     Entry<Integer, String> entry = getOnlyElement(map.entrySet());
85     map.remove(1);
86     entry.setValue("one");
87     assertThat(map).containsEntry(1, "one");
88   }
89 
testAllocArraysDefault()90   public void testAllocArraysDefault() {
91     CompactHashMap<Integer, String> map = CompactHashMap.create();
92     assertThat(map.needsAllocArrays()).isTrue();
93     assertThat(map.entries).isNull();
94     assertThat(map.keys).isNull();
95     assertThat(map.values).isNull();
96 
97     map.put(1, "1");
98     assertThat(map.needsAllocArrays()).isFalse();
99     assertThat(map.entries).hasLength(CompactHashing.DEFAULT_SIZE);
100     assertThat(map.keys).hasLength(CompactHashing.DEFAULT_SIZE);
101     assertThat(map.values).hasLength(CompactHashing.DEFAULT_SIZE);
102   }
103 
testAllocArraysExpectedSize()104   public void testAllocArraysExpectedSize() {
105     for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) {
106       CompactHashMap<Integer, String> map = CompactHashMap.createWithExpectedSize(i);
107       assertThat(map.needsAllocArrays()).isTrue();
108       assertThat(map.entries).isNull();
109       assertThat(map.keys).isNull();
110       assertThat(map.values).isNull();
111 
112       map.put(1, "1");
113       assertThat(map.needsAllocArrays()).isFalse();
114       int expectedSize = Math.max(1, i);
115       assertThat(map.entries).hasLength(expectedSize);
116       assertThat(map.keys).hasLength(expectedSize);
117       assertThat(map.values).hasLength(expectedSize);
118     }
119   }
120 }
121