• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.addTest(
63         MapTestSuiteBuilder.using(
64                 new TestStringMapGenerator() {
65                   @Override
66                   protected Map<String, String> create(Entry<String, String>[] entries) {
67                     CompactHashMap<String, String> map = CompactHashMap.create();
68                     map.convertToHashFloodingResistantImplementation();
69                     for (Entry<String, String> entry : entries) {
70                       map.put(entry.getKey(), entry.getValue());
71                     }
72                     return map;
73                   }
74                 })
75             .named("CompactHashMap with flooding resistance")
76             .withFeatures(
77                 CollectionSize.ANY,
78                 MapFeature.GENERAL_PURPOSE,
79                 MapFeature.ALLOWS_NULL_KEYS,
80                 MapFeature.ALLOWS_NULL_VALUES,
81                 CollectionFeature.SERIALIZABLE,
82                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
83             .createTestSuite());
84     suite.addTestSuite(CompactHashMapTest.class);
85     return suite;
86   }
87 
testTrimToSize()88   public void testTrimToSize() {
89     CompactHashMap<Integer, String> map = CompactHashMap.createWithExpectedSize(100);
90     for (int i = 0; i < 10; i++) {
91       map.put(i, Integer.toString(i));
92     }
93     map.trimToSize();
94     assertThat(map.entries).hasLength(10);
95     assertThat(map.keys).hasLength(10);
96     assertThat(map.values).hasLength(10);
97     assertEquals(10, map.size());
98     for (int i = 0; i < 10; i++) {
99       assertEquals(Integer.toString(i), map.get(i));
100     }
101   }
102 
testEntrySetValueAfterRemoved()103   public void testEntrySetValueAfterRemoved() {
104     CompactHashMap<Integer, String> map = CompactHashMap.create();
105     map.put(1, "1");
106     Entry<Integer, String> entry = getOnlyElement(map.entrySet());
107     map.remove(1);
108     entry.setValue("one");
109     assertThat(map).containsEntry(1, "one");
110   }
111 
testAllocArraysDefault()112   public void testAllocArraysDefault() {
113     CompactHashMap<Integer, String> map = CompactHashMap.create();
114     assertThat(map.needsAllocArrays()).isTrue();
115     assertThat(map.entries).isNull();
116     assertThat(map.keys).isNull();
117     assertThat(map.values).isNull();
118 
119     map.put(1, "1");
120     assertThat(map.needsAllocArrays()).isFalse();
121     assertThat(map.entries).hasLength(CompactHashing.DEFAULT_SIZE);
122     assertThat(map.keys).hasLength(CompactHashing.DEFAULT_SIZE);
123     assertThat(map.values).hasLength(CompactHashing.DEFAULT_SIZE);
124   }
125 
testAllocArraysExpectedSize()126   public void testAllocArraysExpectedSize() {
127     for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) {
128       CompactHashMap<Integer, String> map = CompactHashMap.createWithExpectedSize(i);
129       assertThat(map.needsAllocArrays()).isTrue();
130       assertThat(map.entries).isNull();
131       assertThat(map.keys).isNull();
132       assertThat(map.values).isNull();
133 
134       map.put(1, "1");
135       assertThat(map.needsAllocArrays()).isFalse();
136       int expectedSize = Math.max(1, i);
137       assertThat(map.entries).hasLength(expectedSize);
138       assertThat(map.keys).hasLength(expectedSize);
139       assertThat(map.values).hasLength(expectedSize);
140     }
141   }
142 
143 }
144