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.addTestSuite(CompactLinkedHashMapTest.class); 62 return suite; 63 } 64 testInsertionOrder()65 public void testInsertionOrder() { 66 Map<Integer, String> map = CompactLinkedHashMap.create(); 67 map.put(1, "a"); 68 map.put(4, "b"); 69 map.put(3, "d"); 70 map.put(2, "c"); 71 testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d", 2, "c"); 72 } 73 testInsertionOrderAfterPutKeyTwice()74 public void testInsertionOrderAfterPutKeyTwice() { 75 Map<Integer, String> map = CompactLinkedHashMap.create(); 76 map.put(1, "a"); 77 map.put(4, "b"); 78 map.put(3, "d"); 79 map.put(2, "c"); 80 map.put(1, "e"); 81 testHasMapEntriesInOrder(map, 1, "e", 4, "b", 3, "d", 2, "c"); 82 } 83 testInsertionOrderAfterRemoveFirstEntry()84 public void testInsertionOrderAfterRemoveFirstEntry() { 85 Map<Integer, String> map = CompactLinkedHashMap.create(); 86 map.put(1, "a"); 87 map.put(4, "b"); 88 map.put(3, "d"); 89 map.put(2, "c"); 90 map.remove(1); 91 testHasMapEntriesInOrder(map, 4, "b", 3, "d", 2, "c"); 92 } 93 testInsertionOrderAfterRemoveMiddleEntry()94 public void testInsertionOrderAfterRemoveMiddleEntry() { 95 Map<Integer, String> map = CompactLinkedHashMap.create(); 96 map.put(1, "a"); 97 map.put(4, "b"); 98 map.put(3, "d"); 99 map.put(2, "c"); 100 map.remove(4); 101 testHasMapEntriesInOrder(map, 1, "a", 3, "d", 2, "c"); 102 } 103 testInsertionOrderAfterRemoveLastEntry()104 public void testInsertionOrderAfterRemoveLastEntry() { 105 Map<Integer, String> map = CompactLinkedHashMap.create(); 106 map.put(1, "a"); 107 map.put(4, "b"); 108 map.put(3, "d"); 109 map.put(2, "c"); 110 map.remove(2); 111 testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d"); 112 } 113 testTrimToSize()114 public void testTrimToSize() { 115 CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.createWithExpectedSize(100); 116 map.put(1, "a"); 117 map.put(4, "b"); 118 map.put(3, "d"); 119 map.put(2, "c"); 120 map.trimToSize(); 121 assertThat(map.entries).hasLength(4); 122 assertThat(map.keys).hasLength(4); 123 assertThat(map.values).hasLength(4); 124 assertThat(map.links).hasLength(4); 125 assertEquals(4, map.size()); 126 testHasMapEntriesInOrder(map, 1, "a", 4, "b", 3, "d", 2, "c"); 127 } 128 testHasMapEntriesInOrder(Map<?, ?> map, Object... alternatingKeysAndValues)129 private void testHasMapEntriesInOrder(Map<?, ?> map, Object... alternatingKeysAndValues) { 130 List<? extends Entry<?, ?>> entries = Lists.newArrayList(map.entrySet()); 131 List<Object> keys = Lists.newArrayList(map.keySet()); 132 List<Object> values = Lists.newArrayList(map.values()); 133 assertEquals(2 * entries.size(), alternatingKeysAndValues.length); 134 assertEquals(2 * keys.size(), alternatingKeysAndValues.length); 135 assertEquals(2 * values.size(), alternatingKeysAndValues.length); 136 for (int i = 0; i < map.size(); i++) { 137 Object expectedKey = alternatingKeysAndValues[2 * i]; 138 Object expectedValue = alternatingKeysAndValues[2 * i + 1]; 139 Entry<Object, Object> expectedEntry = Maps.immutableEntry(expectedKey, expectedValue); 140 assertEquals(expectedEntry, entries.get(i)); 141 assertEquals(expectedKey, keys.get(i)); 142 assertEquals(expectedValue, values.get(i)); 143 } 144 } 145 testAllocArraysDefault()146 public void testAllocArraysDefault() { 147 CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.create(); 148 assertThat(map.needsAllocArrays()).isTrue(); 149 assertThat(map.entries).isNull(); 150 assertThat(map.keys).isNull(); 151 assertThat(map.values).isNull(); 152 assertThat(map.links).isNull(); 153 154 map.put(1, Integer.toString(1)); 155 assertThat(map.needsAllocArrays()).isFalse(); 156 assertThat(map.entries).hasLength(CompactHashing.DEFAULT_SIZE); 157 assertThat(map.keys).hasLength(CompactHashing.DEFAULT_SIZE); 158 assertThat(map.values).hasLength(CompactHashing.DEFAULT_SIZE); 159 assertThat(map.links).hasLength(CompactHashing.DEFAULT_SIZE); 160 } 161 testAllocArraysExpectedSize()162 public void testAllocArraysExpectedSize() { 163 for (int i = 0; i <= CompactHashing.DEFAULT_SIZE; i++) { 164 CompactLinkedHashMap<Integer, String> map = CompactLinkedHashMap.createWithExpectedSize(i); 165 assertThat(map.needsAllocArrays()).isTrue(); 166 assertThat(map.entries).isNull(); 167 assertThat(map.keys).isNull(); 168 assertThat(map.values).isNull(); 169 assertThat(map.links).isNull(); 170 171 map.put(1, Integer.toString(1)); 172 assertThat(map.needsAllocArrays()).isFalse(); 173 int expectedSize = Math.max(1, i); 174 assertThat(map.entries).hasLength(expectedSize); 175 assertThat(map.keys).hasLength(expectedSize); 176 assertThat(map.values).hasLength(expectedSize); 177 assertThat(map.links).hasLength(expectedSize); 178 } 179 } 180 } 181