• 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 package com.google.common.collect;
15 
16 import static com.google.common.truth.Truth.assertThat;
17 
18 import com.google.common.collect.testing.MapTestSuiteBuilder;
19 import com.google.common.collect.testing.TestStringMapGenerator;
20 import com.google.common.collect.testing.features.CollectionFeature;
21 import com.google.common.collect.testing.features.CollectionSize;
22 import com.google.common.collect.testing.features.MapFeature;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 
30 /**
31  * Tests for {@code CompactLinkedHashMap}.
32  *
33  * @author Louis Wasserman
34  */
35 public class CompactLinkedHashMapTest extends TestCase {
suite()36   public static Test suite() {
37     TestSuite suite = new TestSuite();
38     suite.addTest(
39         MapTestSuiteBuilder.using(
40                 new TestStringMapGenerator() {
41                   @Override
42                   protected Map<String, String> create(Entry<String, String>[] entries) {
43                     Map<String, String> map = CompactLinkedHashMap.create();
44                     for (Entry<String, String> entry : entries) {
45                       map.put(entry.getKey(), entry.getValue());
46                     }
47                     return map;
48                   }
49                 })
50             .named("CompactLinkedHashMap")
51             .withFeatures(
52                 CollectionSize.ANY,
53                 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
54                 MapFeature.GENERAL_PURPOSE,
55                 MapFeature.ALLOWS_NULL_KEYS,
56                 MapFeature.ALLOWS_NULL_VALUES,
57                 CollectionFeature.SERIALIZABLE,
58                 CollectionFeature.KNOWN_ORDER)
59             .createTestSuite());
60     suite.addTestSuite(CompactLinkedHashMapTest.class);
61     return suite;
62   }
63 
testInsertionOrder()64   public void testInsertionOrder() {
65     Map<Integer, String> map = CompactLinkedHashMap.create();
66     map.put(1, "a");
67     map.put(4, "b");
68     map.put(3, "d");
69     map.put(2, "c");
70     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d", 2, "c");
71   }
72 
testInsertionOrderAfterPutKeyTwice()73   public void testInsertionOrderAfterPutKeyTwice() {
74     Map<Integer, String> map = CompactLinkedHashMap.create();
75     map.put(1, "a");
76     map.put(4, "b");
77     map.put(3, "d");
78     map.put(2, "c");
79     map.put(1, "e");
80     testHasMapEntriesInOrder(map, 1, "e", 4, "b", 3, "d", 2, "c");
81   }
82 
testInsertionOrderAfterRemoveFirstEntry()83   public void testInsertionOrderAfterRemoveFirstEntry() {
84     Map<Integer, String> map = CompactLinkedHashMap.create();
85     map.put(1, "a");
86     map.put(4, "b");
87     map.put(3, "d");
88     map.put(2, "c");
89     map.remove(1);
90     testHasMapEntriesInOrder(map, 4, "b", 3, "d", 2, "c");
91   }
92 
testInsertionOrderAfterRemoveMiddleEntry()93   public void testInsertionOrderAfterRemoveMiddleEntry() {
94     Map<Integer, String> map = CompactLinkedHashMap.create();
95     map.put(1, "a");
96     map.put(4, "b");
97     map.put(3, "d");
98     map.put(2, "c");
99     map.remove(3);
100     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 2, "c");
101   }
102 
testInsertionOrderAfterRemoveLastEntry()103   public void testInsertionOrderAfterRemoveLastEntry() {
104     Map<Integer, String> map = CompactLinkedHashMap.create();
105     map.put(1, "a");
106     map.put(4, "b");
107     map.put(3, "d");
108     map.put(2, "c");
109     map.remove(2);
110     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d");
111   }
112 
testTrimToSize()113   public void testTrimToSize() {
114     CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.createWithExpectedSize(100);
115     map.put(1, "a");
116     map.put(4, "b");
117     map.put(3, "d");
118     map.put(2, "c");
119     map.trimToSize();
120     assertThat(map.entries).hasLength(4);
121     assertThat(map.keys).hasLength(4);
122     assertThat(map.values).hasLength(4);
123     assertThat(map.links).hasLength(4);
124     assertEquals(4, map.size());
125     testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d", 2, "c");
126   }
127 
testHasMapEntriesInOrder(Map<?, ?> map, Object... alternatingKeysAndValues)128   private void testHasMapEntriesInOrder(Map<?, ?> map, Object... alternatingKeysAndValues) {
129     List<? extends Entry<?, ?>> entries = Lists.newArrayList(map.entrySet());
130     List<Object> keys = Lists.newArrayList(map.keySet());
131     List<Object> values = Lists.newArrayList(map.values());
132     assertEquals(2 * entries.size(), alternatingKeysAndValues.length);
133     assertEquals(2 * keys.size(), alternatingKeysAndValues.length);
134     assertEquals(2 * values.size(), alternatingKeysAndValues.length);
135     for (int i = 0; i < map.size(); i++) {
136       Object expectedKey = alternatingKeysAndValues[2 * i];
137       Object expectedValue = alternatingKeysAndValues[2 * i + 1];
138       Entry<Object, Object> expectedEntry = Maps.immutableEntry(expectedKey, expectedValue);
139       assertEquals(expectedEntry, entries.get(i));
140       assertEquals(expectedKey, keys.get(i));
141       assertEquals(expectedValue, values.get(i));
142     }
143   }
144 }
145