• 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"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import static com.google.common.truth.Truth.assertThat;
18 
19 import com.google.common.collect.testing.MapTestSuiteBuilder;
20 import com.google.common.collect.testing.TestStringMapGenerator;
21 import com.google.common.collect.testing.features.CollectionFeature;
22 import com.google.common.collect.testing.features.CollectionSize;
23 import com.google.common.collect.testing.features.MapFeature;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 
31 /**
32  * Tests for {@code CompactLinkedHashMap}.
33  *
34  * @author Louis Wasserman
35  */
36 public class CompactLinkedHashMapTest extends TestCase {
suite()37   public static Test suite() {
38     TestSuite suite = new TestSuite();
39     suite.addTest(
40         MapTestSuiteBuilder.using(
41                 new TestStringMapGenerator() {
42                   @Override
43                   protected Map<String, String> create(Entry<String, String>[] entries) {
44                     Map<String, String> map = CompactLinkedHashMap.create();
45                     for (Entry<String, String> entry : entries) {
46                       map.put(entry.getKey(), entry.getValue());
47                     }
48                     return map;
49                   }
50                 })
51             .named("CompactLinkedHashMap")
52             .withFeatures(
53                 CollectionSize.ANY,
54                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
55                 MapFeature.GENERAL_PURPOSE,
56                 MapFeature.ALLOWS_NULL_KEYS,
57                 MapFeature.ALLOWS_NULL_VALUES,
58                 CollectionFeature.SERIALIZABLE,
59                 CollectionFeature.KNOWN_ORDER)
60             .createTestSuite());
61     suite.addTest(
62         MapTestSuiteBuilder.using(
63                 new TestStringMapGenerator() {
64                   @Override
65                   protected Map<String, String> create(Entry<String, String>[] entries) {
66                     CompactLinkedHashMap<String, String> map = CompactLinkedHashMap.create();
67                     map.convertToHashFloodingResistantImplementation();
68                     for (Entry<String, String> entry : entries) {
69                       map.put(entry.getKey(), entry.getValue());
70                     }
71                     return map;
72                   }
73                 })
74             .named("CompactLinkedHashMap with flooding resistance")
75             .withFeatures(
76                 CollectionSize.ANY,
77                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
78                 MapFeature.GENERAL_PURPOSE,
79                 MapFeature.ALLOWS_NULL_KEYS,
80                 MapFeature.ALLOWS_NULL_VALUES,
81                 CollectionFeature.SERIALIZABLE,
82                 CollectionFeature.KNOWN_ORDER)
83             .createTestSuite());
84     suite.addTestSuite(CompactLinkedHashMapTest.class);
85     return suite;
86   }
87 
testInsertionOrder()88   public void testInsertionOrder() {
89     Map<Integer, String> map = CompactLinkedHashMap.create();
90     map.put(1, "a");
91     map.put(4, "b");
92     map.put(3, "d");
93     map.put(2, "c");
94     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d", 2, "c");
95   }
96 
testInsertionOrderAfterPutKeyTwice()97   public void testInsertionOrderAfterPutKeyTwice() {
98     Map<Integer, String> map = CompactLinkedHashMap.create();
99     map.put(1, "a");
100     map.put(4, "b");
101     map.put(3, "d");
102     map.put(2, "c");
103     map.put(1, "e");
104     testHasMapEntriesInOrder(map, 1, "e", 4, "b", 3, "d", 2, "c");
105   }
106 
testInsertionOrderAfterRemoveFirstEntry()107   public void testInsertionOrderAfterRemoveFirstEntry() {
108     Map<Integer, String> map = CompactLinkedHashMap.create();
109     map.put(1, "a");
110     map.put(4, "b");
111     map.put(3, "d");
112     map.put(2, "c");
113     map.remove(1);
114     testHasMapEntriesInOrder(map, 4, "b", 3, "d", 2, "c");
115   }
116 
testInsertionOrderAfterRemoveMiddleEntry()117   public void testInsertionOrderAfterRemoveMiddleEntry() {
118     Map<Integer, String> map = CompactLinkedHashMap.create();
119     map.put(1, "a");
120     map.put(4, "b");
121     map.put(3, "d");
122     map.put(2, "c");
123     map.remove(4);
124     testHasMapEntriesInOrder(map, 1, "a", 3, "d", 2, "c");
125   }
126 
testInsertionOrderAfterRemoveLastEntry()127   public void testInsertionOrderAfterRemoveLastEntry() {
128     Map<Integer, String> map = CompactLinkedHashMap.create();
129     map.put(1, "a");
130     map.put(4, "b");
131     map.put(3, "d");
132     map.put(2, "c");
133     map.remove(2);
134     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d");
135   }
136 
testTrimToSize()137   public void testTrimToSize() {
138     CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.createWithExpectedSize(100);
139     map.put(1, "a");
140     map.put(4, "b");
141     map.put(3, "d");
142     map.put(2, "c");
143     map.trimToSize();
144     assertThat(map.entries).hasLength(4);
145     assertThat(map.keys).hasLength(4);
146     assertThat(map.values).hasLength(4);
147     assertThat(map.links).hasLength(4);
148     assertEquals(4, map.size());
149     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d", 2, "c");
150   }
151 
testHasMapEntriesInOrder(Map<?, ?> map, Object... alternatingKeysAndValues)152   private void testHasMapEntriesInOrder(Map<?, ?> map, Object... alternatingKeysAndValues) {
153     List<? extends Entry<?, ?>> entries = Lists.newArrayList(map.entrySet());
154     List<Object> keys = Lists.newArrayList(map.keySet());
155     List<Object> values = Lists.newArrayList(map.values());
156     assertEquals(2 * entries.size(), alternatingKeysAndValues.length);
157     assertEquals(2 * keys.size(), alternatingKeysAndValues.length);
158     assertEquals(2 * values.size(), alternatingKeysAndValues.length);
159     for (int i = 0; i < map.size(); i++) {
160       Object expectedKey = alternatingKeysAndValues[2 * i];
161       Object expectedValue = alternatingKeysAndValues[2 * i + 1];
162       Entry<Object, Object> expectedEntry = Maps.immutableEntry(expectedKey, expectedValue);
163       assertEquals(expectedEntry, entries.get(i));
164       assertEquals(expectedKey, keys.get(i));
165       assertEquals(expectedValue, values.get(i));
166     }
167   }
168 
testAllocArraysDefault()169   public void testAllocArraysDefault() {
170     CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.create();
171     assertThat(map.needsAllocArrays()).isTrue();
172     assertThat(map.entries).isNull();
173     assertThat(map.keys).isNull();
174     assertThat(map.values).isNull();
175     assertThat(map.links).isNull();
176 
177     map.put(1, Integer.toString(1));
178     assertThat(map.needsAllocArrays()).isFalse();
179     assertThat(map.entries).hasLength(CompactHashing.DEFAULT_SIZE);
180     assertThat(map.keys).hasLength(CompactHashing.DEFAULT_SIZE);
181     assertThat(map.values).hasLength(CompactHashing.DEFAULT_SIZE);
182     assertThat(map.links).hasLength(CompactHashing.DEFAULT_SIZE);
183   }
184 
testAllocArraysExpectedSize()185   public void testAllocArraysExpectedSize() {
186     for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) {
187       CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.createWithExpectedSize(i);
188       assertThat(map.needsAllocArrays()).isTrue();
189       assertThat(map.entries).isNull();
190       assertThat(map.keys).isNull();
191       assertThat(map.values).isNull();
192       assertThat(map.links).isNull();
193 
194       map.put(1, Integer.toString(1));
195       assertThat(map.needsAllocArrays()).isFalse();
196       int expectedSize = Math.max(1, i);
197       assertThat(map.entries).hasLength(expectedSize);
198       assertThat(map.keys).hasLength(expectedSize);
199       assertThat(map.values).hasLength(expectedSize);
200       assertThat(map.links).hasLength(expectedSize);
201     }
202   }
203 
204 }
205