Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Hashtable/DeserializedLength.java
41149 views
1
/*
2
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.*;
25
import java.lang.reflect.Field;
26
import java.util.Hashtable;
27
28
/**
29
* @test
30
* @bug 8068427
31
* @summary Hashtable deserialization reconstitutes table with wrong capacity
32
* @modules java.base/java.util:open
33
*/
34
public class DeserializedLength {
35
36
static boolean testDeserializedLength(int elements, float loadFactor) throws Exception {
37
38
// construct Hashtable with minimal initial capacity and given loadFactor
39
Hashtable<Integer, Integer> ht1 = new Hashtable<>(1, loadFactor);
40
41
// add given number of unique elements
42
for (int i = 0; i < elements; i++) {
43
ht1.put(i, i);
44
}
45
46
// serialize and deserialize into a deep clone
47
Hashtable<Integer, Integer> ht2 = serialClone(ht1);
48
49
// compare lengths of internal tables
50
Object[] table1 = (Object[]) hashtableTableField.get(ht1);
51
Object[] table2 = (Object[]) hashtableTableField.get(ht2);
52
assert table1 != null;
53
assert table2 != null;
54
55
int minLength = (int) (ht1.size() / loadFactor) + 1;
56
int maxLength = minLength * 2;
57
58
boolean ok = (table2.length >= minLength && table2.length <= maxLength);
59
60
System.out.printf(
61
"%7d %5.2f %7d %7d %7d...%7d %s\n",
62
ht1.size(), loadFactor,
63
table1.length, table2.length,
64
minLength, maxLength,
65
(ok ? "OK" : "NOT-OK")
66
);
67
68
return ok;
69
}
70
71
static <T> T serialClone(T o) throws IOException, ClassNotFoundException {
72
ByteArrayOutputStream bos = new ByteArrayOutputStream();
73
try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
74
oos.writeObject(o);
75
}
76
@SuppressWarnings("unchecked")
77
T clone = (T) new ObjectInputStream(
78
new ByteArrayInputStream(bos.toByteArray())).readObject();
79
return clone;
80
}
81
82
private static final Field hashtableTableField;
83
84
static {
85
try {
86
hashtableTableField = Hashtable.class.getDeclaredField("table");
87
hashtableTableField.setAccessible(true);
88
} catch (NoSuchFieldException e) {
89
throw new Error(e);
90
}
91
}
92
93
public static void main(String[] args) throws Exception {
94
boolean ok = true;
95
96
System.out.printf("Results:\n" +
97
" ser. deser.\n" +
98
" size load lentgh length valid range ok?\n" +
99
"------- ----- ------- ------- ----------------- ------\n"
100
);
101
102
for (int elements : new int[]{10, 50, 500, 5000}) {
103
for (float loadFactor : new float[]{0.15f, 0.5f, 0.75f, 1.0f, 2.5f}) {
104
ok &= testDeserializedLength(elements, loadFactor);
105
}
106
}
107
if (!ok) {
108
throw new AssertionError("Test failed.");
109
}
110
}
111
}
112
113