Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/ArrayDeque/WhiteBox.java
41149 views
1
/*
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
*
4
* This code is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation.
7
*
8
* This code is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11
* version 2 for more details (a copy is included in the LICENSE file that
12
* accompanied this code).
13
*
14
* You should have received a copy of the GNU General Public License version
15
* 2 along with this work; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*
18
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19
* or visit www.oracle.com if you need additional information or have any
20
* questions.
21
*/
22
23
/*
24
* This file is available under and governed by the GNU General Public
25
* License version 2 only, as published by the Free Software Foundation.
26
* However, the following notice accompanied the original version of this
27
* file:
28
*
29
* Written by Martin Buchholz with assistance from members of JCP
30
* JSR-166 Expert Group and released to the public domain, as
31
* explained at http://creativecommons.org/publicdomain/zero/1.0/
32
*/
33
34
/*
35
* @test
36
* @modules java.base/java.util:open
37
* @run testng WhiteBox
38
* @summary White box tests of implementation details
39
*/
40
41
import static org.testng.Assert.*;
42
import org.testng.annotations.Test;
43
44
import java.io.ByteArrayInputStream;
45
import java.io.ByteArrayOutputStream;
46
import java.io.ObjectInputStream;
47
import java.io.ObjectOutputStream;
48
import java.lang.invoke.MethodHandles;
49
import java.lang.invoke.VarHandle;
50
import java.util.ArrayDeque;
51
import java.util.concurrent.ThreadLocalRandom;
52
53
@Test
54
public class WhiteBox {
55
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
56
final VarHandle ELEMENTS, HEAD, TAIL;
57
58
WhiteBox() throws ReflectiveOperationException {
59
Class<?> klazz = ArrayDeque.class;
60
MethodHandles.Lookup lookup
61
= MethodHandles.privateLookupIn(klazz, MethodHandles.lookup());
62
ELEMENTS = lookup.findVarHandle(klazz, "elements", Object[].class);
63
HEAD = lookup.findVarHandle(klazz, "head", int.class);
64
TAIL = lookup.findVarHandle(klazz, "tail", int.class);
65
}
66
67
Object[] elements(ArrayDeque d) { return (Object[]) ELEMENTS.get(d); }
68
int head(ArrayDeque d) { return (int) HEAD.get(d); }
69
int tail(ArrayDeque d) { return (int) TAIL.get(d); }
70
71
void checkCapacity(ArrayDeque d, int capacity) {
72
assertTrue(d.isEmpty());
73
assertEquals(0, head(d));
74
assertEquals(0, tail(d));
75
Object[] initialElements = elements(d);
76
77
assertInvariants(d);
78
for (int i = capacity; i--> 0; ) {
79
d.add(rnd.nextInt(42));
80
assertSame(elements(d), initialElements);
81
assertInvariants(d);
82
}
83
84
d.add(rnd.nextInt(42));
85
assertNotSame(elements(d), initialElements);
86
assertInvariants(d);
87
}
88
89
@Test
90
public void defaultConstructor() {
91
checkCapacity(new ArrayDeque(), 16);
92
}
93
94
@Test
95
public void shouldNotResizeWhenInitialCapacityProvided() {
96
int initialCapacity = rnd.nextInt(20);
97
checkCapacity(new ArrayDeque(initialCapacity), initialCapacity);
98
}
99
100
byte[] serialBytes(Object o) {
101
try {
102
ByteArrayOutputStream bos = new ByteArrayOutputStream();
103
ObjectOutputStream oos = new ObjectOutputStream(bos);
104
oos.writeObject(o);
105
oos.flush();
106
oos.close();
107
return bos.toByteArray();
108
} catch (Exception fail) {
109
throw new AssertionError(fail);
110
}
111
}
112
113
@SuppressWarnings("unchecked")
114
<T> T serialClone(T o) {
115
try {
116
ObjectInputStream ois = new ObjectInputStream
117
(new ByteArrayInputStream(serialBytes(o)));
118
T clone = (T) ois.readObject();
119
assertNotSame(o, clone);
120
assertSame(o.getClass(), clone.getClass());
121
return clone;
122
} catch (Exception fail) {
123
throw new AssertionError(fail);
124
}
125
}
126
127
@Test
128
public void testSerialization() {
129
ArrayDeque[] ds = { new ArrayDeque(), new ArrayDeque(rnd.nextInt(20)) };
130
for (ArrayDeque d : ds) {
131
if (rnd.nextBoolean()) d.add(99);
132
ArrayDeque clone = serialClone(d);
133
assertInvariants(clone);
134
assertNotSame(elements(d), elements(clone));
135
assertEquals(d, clone);
136
}
137
}
138
139
/** Checks conditions which should always be true. */
140
void assertInvariants(ArrayDeque d) {
141
final Object[] elements = elements(d);
142
final int head = head(d);
143
final int tail = tail(d);
144
final int capacity = elements.length;
145
assertTrue(0 <= head && head < capacity);
146
assertTrue(0 <= tail && tail < capacity);
147
assertTrue(capacity > 0);
148
assertTrue(d.size() < capacity);
149
assertTrue((head == tail) ^ (elements[head] != null));
150
assertNull(elements[tail]);
151
assertTrue((head == tail) ^ (elements[Math.floorMod(tail - 1, capacity)] != null));
152
}
153
}
154
155