author | dl |
Fri, 03 Feb 2017 13:24:59 -0800 | |
changeset 43522 | f9c6f543c4db |
parent 42338 | a60f280f803c |
permissions | -rw-r--r-- |
25413
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
1 |
/* |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
4 |
* |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
8 |
* |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
13 |
* accompanied this code). |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
14 |
* |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
18 |
* |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
21 |
* questions. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
22 |
*/ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
23 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
24 |
import java.lang.reflect.Field; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
25 |
import java.util.ArrayList; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
26 |
import java.util.Collections; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
27 |
import java.util.IdentityHashMap; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
28 |
import java.util.List; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
29 |
import java.util.Random; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
30 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
31 |
import org.testng.annotations.DataProvider; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
32 |
import org.testng.annotations.Test; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
33 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
34 |
import static org.testng.Assert.*; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
35 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
36 |
/* |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
37 |
* @test |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
38 |
* @bug 6904367 |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
39 |
* @summary IdentityHashMap reallocates storage when inserting expected |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
40 |
* number of elements |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
30046
diff
changeset
|
41 |
* @modules java.base/java.util:open |
25413
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
42 |
* @run testng Capacity |
30046 | 43 |
* @key randomness |
25413
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
44 |
*/ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
45 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
46 |
@Test |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
47 |
public class Capacity { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
48 |
static final Field tableField; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
49 |
static final Random random = new Random(); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
50 |
static final Object[][] sizesData; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
51 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
52 |
@DataProvider(name="sizes", parallel = true) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
53 |
public Object[][] sizesToTest() { return sizesData; } |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
54 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
55 |
static { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
56 |
try { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
57 |
tableField = IdentityHashMap.class.getDeclaredField("table"); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
58 |
tableField.setAccessible(true); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
59 |
} catch (NoSuchFieldException e) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
60 |
throw new LinkageError("table", e); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
61 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
62 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
63 |
ArrayList<Object[]> sizes = new ArrayList<>(); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
64 |
for (int size = 0; size < 200; size++) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
65 |
sizes.add(new Object[] { size }); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
66 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
67 |
// some numbers known to demonstrate bug 6904367 |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
68 |
for (int size : new int[] {682, 683, 1365, 2730, 2731, 5461}) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
69 |
sizes.add(new Object[] { size }); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
70 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
71 |
// a few more random sizes to try |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
72 |
for (int i = 0; i != 128; i++) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
73 |
sizes.add(new Object[] { random.nextInt(5000) }); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
74 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
75 |
sizesData = sizes.toArray(new Object[0][]); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
76 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
77 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
78 |
static int capacity(IdentityHashMap<?,?> map) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
79 |
try { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
80 |
return ((Object[]) tableField.get(map)).length / 2; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
81 |
} catch (Throwable t) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
82 |
throw new LinkageError("table", t); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
83 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
84 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
85 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
86 |
static void assertCapacity(IdentityHashMap<?,?> map, |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
87 |
int expectedCapacity) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
88 |
assertEquals(capacity(map), expectedCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
89 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
90 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
91 |
static void growUsingPut(IdentityHashMap<Object,Object> map, |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
92 |
int elementsToAdd) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
93 |
for (int i = 0; i < elementsToAdd; i++) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
94 |
map.put(new Object(), new Object()); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
95 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
96 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
97 |
static void growUsingPutAll(IdentityHashMap<Object,Object> map, |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
98 |
int elementsToAdd) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
99 |
IdentityHashMap<Object,Object> other = new IdentityHashMap<>(); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
100 |
growUsingPut(other, elementsToAdd); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
101 |
map.putAll(other); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
102 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
103 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
104 |
static void growUsingRepeatedPutAll(IdentityHashMap<Object,Object> map, |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
105 |
int elementsToAdd) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
106 |
for (int i = 0; i < elementsToAdd; i++) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
107 |
map.putAll(Collections.singletonMap(new Object(), |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
108 |
new Object())); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
109 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
110 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
111 |
/** |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
112 |
* Checks that expected number of items can be inserted into |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
113 |
* the map without resizing of the internal storage |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
114 |
*/ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
115 |
@Test(dataProvider = "sizes") |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
116 |
public void canInsertExpectedItemsWithoutResizing(int size) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
117 |
throws Throwable { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
118 |
// First try growing using put() |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
119 |
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
120 |
int initialCapacity = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
121 |
growUsingPut(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
122 |
assertCapacity(m, initialCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
123 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
124 |
// Doubling from the expected size will cause exactly one |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
125 |
// resize, except near minimum capacity. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
126 |
if (size > 1) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
127 |
growUsingPut(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
128 |
assertCapacity(m, 2 * initialCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
129 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
130 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
131 |
// Try again, growing with putAll() |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
132 |
m = new IdentityHashMap<>(size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
133 |
initialCapacity = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
134 |
growUsingPutAll(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
135 |
assertCapacity(m, initialCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
136 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
137 |
// Doubling from the expected size will cause exactly one |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
138 |
// resize, except near minimum capacity. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
139 |
if (size > 1) { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
140 |
growUsingPutAll(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
141 |
assertCapacity(m, 2 * initialCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
142 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
143 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
144 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
145 |
/** |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
146 |
* Given the expected size, computes such a number N of items that |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
147 |
* inserting (N+1) items will trigger resizing of the internal storage |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
148 |
*/ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
149 |
static int threshold(int size) throws Throwable { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
150 |
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
151 |
int initialCapacity = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
152 |
while (capacity(m) == initialCapacity) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
153 |
growUsingPut(m, 1); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
154 |
return m.size() - 1; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
155 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
156 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
157 |
/** |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
158 |
* Checks that inserting (threshold+1) item causes resizing |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
159 |
* of the internal storage |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
160 |
*/ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
161 |
@Test(dataProvider = "sizes") |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
162 |
public void passingThresholdCausesResize(int size) throws Throwable { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
163 |
final int threshold = threshold(size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
164 |
IdentityHashMap<Object,Object> m = new IdentityHashMap<>(threshold); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
165 |
int initialCapacity = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
166 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
167 |
growUsingPut(m, threshold); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
168 |
assertCapacity(m, initialCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
169 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
170 |
growUsingPut(m, 1); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
171 |
assertCapacity(m, 2 * initialCapacity); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
172 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
173 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
174 |
/** |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
175 |
* Checks that 4 methods of requiring capacity lead to the same |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
176 |
* internal capacity, unless sized below default capacity. |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
177 |
*/ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
178 |
@Test(dataProvider = "sizes") |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
179 |
public void differentGrowthPatternsResultInSameCapacity(int size) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
180 |
throws Throwable { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
181 |
if (size < 21) // 21 is default maxExpectedSize |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
182 |
return; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
183 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
184 |
IdentityHashMap<Object,Object> m; |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
185 |
m = new IdentityHashMap<Object,Object>(size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
186 |
int capacity1 = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
187 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
188 |
m = new IdentityHashMap<>(); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
189 |
growUsingPut(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
190 |
int capacity2 = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
191 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
192 |
m = new IdentityHashMap<>(); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
193 |
growUsingPutAll(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
194 |
int capacity3 = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
195 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
196 |
m = new IdentityHashMap<>(); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
197 |
growUsingRepeatedPutAll(m, size); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
198 |
int capacity4 = capacity(m); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
199 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
200 |
if (capacity1 != capacity2 || |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
201 |
capacity2 != capacity3 || |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
202 |
capacity3 != capacity4) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
203 |
throw new AssertionError("Capacities not equal: " |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
204 |
+ capacity1 + " " |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
205 |
+ capacity2 + " " |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
206 |
+ capacity3 + " " |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
207 |
+ capacity4); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
208 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
209 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
210 |
public void defaultExpectedMaxSizeIs21() { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
211 |
assertCapacity(new IdentityHashMap<Long,Long>(), 32); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
212 |
assertCapacity(new IdentityHashMap<Long,Long>(21), 32); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
213 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
214 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
215 |
public void minimumCapacityIs4() { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
216 |
assertCapacity(new IdentityHashMap<Long,Long>(0), 4); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
217 |
assertCapacity(new IdentityHashMap<Long,Long>(1), 4); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
218 |
assertCapacity(new IdentityHashMap<Long,Long>(2), 4); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
219 |
assertCapacity(new IdentityHashMap<Long,Long>(3), 8); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
220 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
221 |
|
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
222 |
@Test(enabled = false) |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
223 |
/** needs too much memory to run normally */ |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
224 |
public void maximumCapacityIs2ToThe29() { |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
225 |
assertCapacity(new IdentityHashMap<Long,Long>(Integer.MAX_VALUE), |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
226 |
1 << 29); |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
227 |
} |
bb827716b9b9
6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents:
diff
changeset
|
228 |
} |