Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collection/RandomizedIteration.java
41149 views
1
/*
2
* Copyright (c) 2018, 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
/*
25
* @test
26
* @bug 8201518
27
* @key randomness
28
* @summary Ensure that randomized iteration order of unmodifiable sets
29
* and maps is actually randomized. Must be run othervm so that
30
* the per-VM-instance salt value differs.
31
* @run main/othervm RandomizedIteration 0
32
* @run main/othervm RandomizedIteration 1
33
* @run main/othervm RandomizedIteration 2
34
* @run main/othervm RandomizedIteration 3
35
* @run main/othervm RandomizedIteration 4
36
* @run main/othervm RandomizedIteration verify 5
37
*/
38
39
import java.io.IOException;
40
import java.io.PrintStream;
41
import java.nio.file.Files;
42
import java.nio.file.Paths;
43
import java.util.Arrays;
44
import java.util.HashSet;
45
import java.util.Map;
46
import java.util.Set;
47
48
import static java.util.stream.Collectors.toUnmodifiableMap;
49
50
/**
51
* Test of randomized iteration of unmodifiable sets and maps.
52
*
53
* Usage: RandomizedIteration n
54
* - writes files suffixed with 'n' containing set elements and map keys
55
* in iteration order
56
* RandomizedIteration "verify" count
57
* - reads files 0..count-1 and checks to ensure that their orders differ
58
*
59
* The idea is to generate several test files by invoking this test with an arg
60
* of 0 through count-1. Then invoke the test once more with two args, the first being
61
* the word "verify" and the second arg being the count. This will read all the generated
62
* files and perform verification.
63
*
64
* The test is considered to pass if any of the runs result in different iteration
65
* orders. The randomization is not actually very random, so over many runs there is
66
* the possibility of a couple of the test files having the same order. That's ok, as
67
* long as the iteration order is usually different. The test fails if *all* of the
68
* iteration orders are the same.
69
*/
70
public class RandomizedIteration {
71
/**
72
* Generates a set and a map from the word array, and then writes
73
* text files "set.#" and "map.#" containing the set elements and
74
* map keys in iteration order.
75
*
76
* @param suffix number used for the file suffix
77
*/
78
static void writeFiles(int suffix) throws IOException {
79
try (PrintStream setOut = new PrintStream("set." + suffix)) {
80
Set.of(WORDS)
81
.forEach(setOut::println);
82
}
83
84
try (PrintStream mapOut = new PrintStream("map." + suffix)) {
85
var map = Map.ofEntries(Arrays.stream(WORDS)
86
.map(word -> Map.entry(word, ""))
87
.toArray(Map.Entry<?, ?>[]::new));
88
map.keySet()
89
.forEach(mapOut::println);
90
}
91
}
92
93
/**
94
* Reads lines from each file derived from the prefix and index from 0..count-1
95
* into a list, computes its hashcode, and returns a set of those hashcodes.
96
* The hashcode of the list is order sensitive, so the same lines in a different
97
* order should have different hashcodes.
98
*
99
* @param prefix the file prefix
100
* @param count the number of files to read
101
* @return a set of hashcodes of each file
102
*/
103
static Set<Integer> readFiles(String prefix, int count) throws IOException {
104
Set<Integer> hashes = new HashSet<>();
105
for (int suffix = 0; suffix < count; suffix++) {
106
String name = prefix + suffix;
107
int hash = Files.readAllLines(Paths.get(name)).hashCode();
108
System.out.println(name + ": " + hash);
109
hashes.add(hash);
110
}
111
return hashes;
112
}
113
114
/**
115
* Test main routine.
116
*
117
* @param args n | "verify" count
118
* @throws IOException if an error occurred
119
*/
120
public static void main(String[] args) throws IOException {
121
if ("verify".equals(args[0])) {
122
int count = Integer.parseInt(args[1]);
123
System.out.println("Verifying " + count + " files.");
124
Set<Integer> setHashes = readFiles("set.", count);
125
Set<Integer> mapHashes = readFiles("map.", count);
126
if (setHashes.size() > 1 && mapHashes.size() > 1) {
127
System.out.println("Passed: differing iteration orders were detected.");
128
} else {
129
throw new AssertionError("FAILED: iteration order not randomized!");
130
}
131
} else {
132
int suffix = Integer.parseInt(args[0]);
133
System.out.println("Generating files: " + suffix);
134
writeFiles(suffix);
135
}
136
}
137
138
/**
139
* List of 63 words of 22 or more letters from BSD /usr/share/dict/words.
140
*/
141
static final String[] WORDS = {
142
"anatomicophysiological",
143
"anthropomorphologically",
144
"aquopentamminecobaltic",
145
"blepharoconjunctivitis",
146
"blepharosphincterectomy",
147
"cholecystenterorrhaphy",
148
"cholecystoduodenostomy",
149
"choledochoduodenostomy",
150
"counterexcommunication",
151
"dacryocystoblennorrhea",
152
"dacryocystosyringotomy",
153
"deanthropomorphization",
154
"duodenocholecystostomy",
155
"electroencephalography",
156
"electrotelethermometer",
157
"epididymodeferentectomy",
158
"formaldehydesulphoxylate",
159
"formaldehydesulphoxylic",
160
"gastroenteroanastomosis",
161
"hematospectrophotometer",
162
"hexamethylenetetramine",
163
"hexanitrodiphenylamine",
164
"historicocabbalistical",
165
"hydropneumopericardium",
166
"hyperconscientiousness",
167
"laparocolpohysterotomy",
168
"lymphangioendothelioma",
169
"macracanthrorhynchiasis",
170
"microcryptocrystalline",
171
"naphthylaminesulphonic",
172
"nonrepresentationalism",
173
"omnirepresentativeness",
174
"pancreaticoduodenostomy",
175
"pancreaticogastrostomy",
176
"pathologicohistological",
177
"pathologicopsychological",
178
"pericardiomediastinitis",
179
"phenolsulphonephthalein",
180
"philosophicohistorical",
181
"philosophicotheological",
182
"photochronographically",
183
"photospectroheliograph",
184
"pneumohydropericardium",
185
"pneumoventriculography",
186
"polioencephalomyelitis",
187
"Prorhipidoglossomorpha",
188
"Pseudolamellibranchiata",
189
"pseudolamellibranchiate",
190
"pseudomonocotyledonous",
191
"pyopneumocholecystitis",
192
"scientificogeographical",
193
"scientificophilosophical",
194
"scleroticochorioiditis",
195
"stereophotomicrography",
196
"tetraiodophenolphthalein",
197
"theologicoastronomical",
198
"theologicometaphysical",
199
"thymolsulphonephthalein",
200
"thyroparathyroidectomize",
201
"thyroparathyroidectomy",
202
"transubstantiationalist",
203
"ureterocystanastomosis",
204
"zoologicoarchaeologist"
205
};
206
}
207
208