]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.arrow.memory; | |
19 | ||
20 | import static junit.framework.TestCase.assertNotNull; | |
21 | import static junit.framework.TestCase.assertTrue; | |
22 | import static org.junit.Assert.assertEquals; | |
23 | import static org.junit.Assert.assertNull; | |
24 | ||
25 | import org.junit.Test; | |
26 | ||
27 | /** | |
28 | * To test simplified implementation of IdentityHashMap. | |
29 | */ | |
30 | public class TestLowCostIdentityHashMap { | |
31 | ||
32 | @Test | |
33 | public void testIdentityHashMap() throws Exception { | |
34 | LowCostIdentityHashMap<String, StringWithKey> hashMap = new LowCostIdentityHashMap<>(); | |
35 | ||
36 | StringWithKey obj1 = new StringWithKey("s1key", "s1value"); | |
37 | StringWithKey obj2 = new StringWithKey("s2key", "s2value"); | |
38 | StringWithKey obj3 = new StringWithKey("s3key", "s3value"); | |
39 | StringWithKey obj4 = new StringWithKey("s1key", "s4value"); | |
40 | StringWithKey obj5 = new StringWithKey("s5key", "s5value"); | |
41 | ||
42 | assertNull(hashMap.put(obj1)); | |
43 | assertNull(hashMap.put(obj2)); | |
44 | assertNull(hashMap.put(obj3)); | |
45 | assertEquals(obj1, hashMap.put(obj4)); | |
46 | assertNull(hashMap.put(obj5)); | |
47 | ||
48 | assertEquals(4, hashMap.size()); | |
49 | ||
50 | assertEquals(obj4, hashMap.get("s1key")); | |
51 | ||
52 | assertNull(hashMap.remove("abc")); | |
53 | ||
54 | assertEquals(obj3, hashMap.remove("s3key")); | |
55 | ||
56 | assertEquals(3, hashMap.size()); | |
57 | ||
58 | assertTrue(!hashMap.isEmpty()); | |
59 | ||
60 | StringWithKey nextValue = hashMap.getNextValue(); | |
61 | ||
62 | assertNotNull(nextValue); | |
63 | ||
64 | assertTrue((hashMap.get("s1key") == nextValue || hashMap.get("s2key") == nextValue || | |
65 | hashMap.get("s5key") == nextValue)); | |
66 | ||
67 | assertTrue(hashMap.containsValue(obj4)); | |
68 | assertTrue(hashMap.containsValue(obj2)); | |
69 | assertTrue(hashMap.containsValue(obj5)); | |
70 | ||
71 | assertEquals(obj4, hashMap.remove("s1key")); | |
72 | ||
73 | nextValue = hashMap.getNextValue(); | |
74 | ||
75 | assertNotNull(nextValue); | |
76 | ||
77 | assertTrue(hashMap.get("s2key") == nextValue || hashMap.get("s5key") == nextValue); | |
78 | ||
79 | assertEquals(2, hashMap.size()); | |
80 | ||
81 | assertEquals(obj2, hashMap.remove("s2key")); | |
82 | assertEquals(obj5, hashMap.remove("s5key")); | |
83 | ||
84 | assertEquals(0, hashMap.size()); | |
85 | ||
86 | assertTrue(hashMap.isEmpty()); | |
87 | } | |
88 | ||
89 | @Test | |
90 | public void testLargeMap() throws Exception { | |
91 | LowCostIdentityHashMap<String, StringWithKey> hashMap = new LowCostIdentityHashMap<>(); | |
92 | ||
93 | String [] keys = new String[200]; | |
94 | for (int i = 0; i < 200; i++) { | |
95 | keys[i] = "s" + i + "key"; | |
96 | } | |
97 | ||
98 | for (int i = 0; i < 100; i++) { | |
99 | if (i % 5 == 0 && i != 0) { | |
100 | StringWithKey obj = new StringWithKey(keys[i - 5], "s" + i + "value"); | |
101 | StringWithKey retObj = hashMap.put(obj); | |
102 | assertNotNull(retObj); | |
103 | StringWithKey obj1 = new StringWithKey(keys[i], "s" + 2 * i + "value"); | |
104 | StringWithKey retObj1 = hashMap.put(obj1); | |
105 | assertNull(retObj1); | |
106 | } else { | |
107 | StringWithKey obj = new StringWithKey(keys[i], "s" + i + "value"); | |
108 | StringWithKey retObj = hashMap.put(obj); | |
109 | assertNull(retObj); | |
110 | } | |
111 | } | |
112 | assertEquals(100, hashMap.size()); | |
113 | for (int i = 0; i < 100; i++) { | |
114 | StringWithKey returnObj = hashMap.get(keys[i]); | |
115 | assertNotNull(returnObj); | |
116 | if (i == 95) { | |
117 | assertEquals("s190value", returnObj.getValue()); | |
118 | continue; | |
119 | } | |
120 | if (i % 5 == 0) { | |
121 | assertEquals("s" + (i + 5) + "value", returnObj.getValue()); | |
122 | } else { | |
123 | assertEquals("s" + i + "value", returnObj.getValue()); | |
124 | } | |
125 | } | |
126 | ||
127 | for (int i = 0; i < 100; i++) { | |
128 | if (i % 4 == 0) { | |
129 | StringWithKey returnObj = hashMap.remove(keys[i]); | |
130 | assertNotNull(returnObj); | |
131 | assertTrue(!hashMap.containsKey(keys[i])); | |
132 | } | |
133 | StringWithKey obj = new StringWithKey(keys[100 + i], "s" + (100 + i) + "value"); | |
134 | StringWithKey retObj = hashMap.put(obj); | |
135 | assertNull(retObj); | |
136 | assertTrue(hashMap.containsKey(keys[100 + i])); | |
137 | } | |
138 | assertEquals(175, hashMap.size()); | |
139 | for (int i = 0; i < 100; i++) { | |
140 | StringWithKey retObj = hashMap.getNextValue(); | |
141 | assertNotNull(retObj); | |
142 | hashMap.remove(retObj.getKey()); | |
143 | } | |
144 | assertTrue(!hashMap.isEmpty()); | |
145 | assertEquals(75, hashMap.size()); | |
146 | hashMap.clear(); | |
147 | assertTrue(hashMap.isEmpty()); | |
148 | } | |
149 | ||
150 | private class StringWithKey implements ValueWithKeyIncluded<String> { | |
151 | ||
152 | private String myValue; | |
153 | private String myKey; | |
154 | ||
155 | StringWithKey(String myKey, String myValue) { | |
156 | this.myKey = myKey; | |
157 | this.myValue = myValue; | |
158 | } | |
159 | ||
160 | @Override | |
161 | public String getKey() { | |
162 | return myKey; | |
163 | } | |
164 | ||
165 | String getValue() { | |
166 | return myValue; | |
167 | } | |
168 | } | |
169 | } |