ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DequeBash.java
Revision: 1.15
Committed: Sun Oct 23 02:21:56 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +2 -1 lines
Log Message:
fix deprecation warning from Class#newInstance

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Josh Bloch of Google Inc. and released to the public domain,
3 jsr166 1.7 * as explained at http://creativecommons.org/publicdomain/zero/1.0/.
4 dl 1.1 */
5    
6 jsr166 1.10 import java.io.*;
7 dl 1.1 import java.util.*;
8    
9     /**
10     * Interface-based Deque tester. This test currently makes three
11     * assumptions about the implementation under test:
12     *
13     * 1) It has no size limitation.
14     * 2) It implements Serializable.
15     * 3) It has a conventional (Collection) constructor.
16     *
17     * All of these assumptions could be relaxed.
18     */
19     public class DequeBash {
20     static int seed = 7;
21     static int nextTail = 0;
22     static int nextHead = -1;
23     static int size() { return nextTail - nextHead - 1; }
24    
25 dl 1.13 static int random() {
26     int r = seed;
27     r ^= r << 13; // xorshift
28     r ^= r >>> 17;
29     return seed = r ^ (r << 5);
30 dl 1.1 }
31    
32 dl 1.13 static int random(int bound) {
33     return (random() & 0x7fffffff) % bound;
34 dl 1.1 }
35 jsr166 1.14
36 jsr166 1.9 public static void main(String[] args) throws Exception {
37 jsr166 1.6 Class<?> cls = Class.forName(args[0]);
38 dl 1.13 long startTime = System.nanoTime();
39     for (int j = 0; j < 200; ++j) {
40 jsr166 1.6 @SuppressWarnings("unchecked")
41 jsr166 1.15 Deque<Integer> deque =
42     (Deque<Integer>) cls.getConstructor().newInstance();
43 dl 1.1 nextTail = 0;
44     nextHead = -1;
45 dl 1.13 mainTest(deque, (random() & ((1 << 20) - 1)));
46 dl 1.1 if (deque.isEmpty()) System.out.print(" ");
47 dl 1.13 if ((j % 10) == 9) System.out.print(".");
48 dl 1.1 }
49 dl 1.13 long now = System.nanoTime();
50     long elapsedTimeMillis = (now - startTime) / (1000L * 1000L);
51     System.out.printf("\ntotal time %d ms\n", elapsedTimeMillis);
52 jsr166 1.14
53 dl 1.1 }
54    
55     static void mainTest(Deque<Integer> deque, int n) throws Exception {
56     /*
57     * Perform a random sequence of operations, keeping contiguous
58     * sequence of integers on the deque.
59     */
60     for (int i = 0; i < n; i++) {
61     sizeTests(deque);
62     randomOp(deque);
63    
64     // Test iterator occasionally
65 dl 1.2 if ((i & 1023) == 0) {
66 dl 1.1 testIter(deque);
67 dl 1.2 testDescendingIter(deque);
68     }
69 dl 1.1
70     // Test serialization and copying
71     if ((i & 4095) == 0) {
72 jsr166 1.6 testEqual(deque, serialClone(deque));
73     testEqual(deque, copyConstructorClone(deque));
74 dl 1.1 }
75 jsr166 1.3
76 dl 1.1 // Test fancy removal stuff once in a blue moon
77     if ((i & 8191) == 0)
78     testRemove(deque);
79 jsr166 1.8 }
80 dl 1.1
81     // Stupid tests for clear, toString
82     deque.clear();
83     testEmpty(deque);
84     Collection<Integer> c = Arrays.asList(1, 2, 3);
85     deque.addAll(c);
86     if (!deque.toString().equals("[1, 2, 3]"))
87     throw new Exception("Deque.toString(): " + deque.toString());
88     }
89    
90 jsr166 1.3 static void testIter(Deque<Integer> deque) throws Exception {
91 dl 1.1 int next = nextHead + 1;
92     int count = 0;
93     for (int j : deque) {
94     if (j != next++)
95     throw new Exception("Element "+ j + " != " + (next-1));
96     count++;
97     }
98     if (count != size())
99     throw new Exception("Count " + count + " != " + size());
100     }
101    
102 jsr166 1.3 static void testDescendingIter(Deque<Integer> deque) throws Exception {
103 dl 1.2 int next = deque.size() + nextHead;
104     int count = 0;
105     for (Iterator<Integer> it = deque.descendingIterator(); it.hasNext();) {
106     int j = it.next();
107     if (j != next--)
108     throw new Exception("Element "+ j + " != " + (next-1));
109     count++;
110     }
111     if (count != size())
112     throw new Exception("Count " + count + " != " + size());
113     }
114    
115 dl 1.1 static void sizeTests(Deque<Integer> deque) throws Exception {
116     if (deque.size() != size())
117     throw new Exception("Size: " + deque.size() +
118     ", expecting " + size());
119     if (deque.isEmpty() != (size() == 0))
120     throw new Exception(
121     "IsEmpty " + deque.isEmpty() + ", size " + size());
122     // Check head and tail
123 jsr166 1.3 if (size() == 0)
124 dl 1.1 testEmpty(deque);
125 jsr166 1.3 else
126 dl 1.1 nonEmptyTest(deque);
127    
128     }
129    
130 jsr166 1.3 static void nonEmptyTest(Deque<Integer> deque) throws Exception {
131 dl 1.1 if (deque.getFirst() != nextHead + 1)
132 jsr166 1.3 throw new Exception("getFirst got: " +
133 dl 1.1 deque.getFirst() + " expecting " + (nextHead + 1));
134     if (deque.element() != nextHead + 1)
135     throw new Exception("element got: " + deque.element() +
136     " expecting " + (nextHead + 1));
137     if (deque.peekFirst() != nextHead + 1)
138     throw new Exception("peekFirst got: "+deque.peekFirst() +
139     " expecting " + (nextHead + 1));
140     if (deque.peek() != nextHead + 1)
141     throw new Exception("peek got: " + deque.peek() +
142     " expecting " + (nextHead + 1));
143 jsr166 1.3
144 dl 1.1 if (deque.peekLast() != nextTail - 1)
145     throw new Exception("peekLast got: " + deque.peekLast() +
146     " expecting " + (nextTail - 1));
147     if (deque.getLast() != nextTail - 1)
148 jsr166 1.3 throw new Exception("getLast got: " +
149 dl 1.1 deque.getLast() + " expecting " + (nextTail - 1));
150 jsr166 1.3 }
151    
152 dl 1.1 static void randomOp(Deque<Integer> deque) throws Exception {
153 jsr166 1.3
154 dl 1.1 // Perform a random operation
155 jsr166 1.5 switch (random() & 3) {
156 dl 1.1 case 0:
157 jsr166 1.5 switch (random() & 3) {
158 dl 1.1 case 0: deque.addLast(nextTail++); break;
159     case 1: deque.offerLast(nextTail++); break;
160     case 2: deque.offer(nextTail++); break;
161     case 3: deque.add(nextTail++); break;
162     default: throw new Exception("How'd we get here");
163     }
164     break;
165     case 1:
166     if (size() == 0) {
167     int result = 666;
168     boolean threw = false;
169     try {
170 jsr166 1.5 switch (random(3)) {
171 dl 1.1 case 0: result = deque.removeFirst(); break;
172     case 1: result = deque.remove(); break;
173     case 2: result = deque.pop(); break;
174     default: throw new Exception("How'd we get here");
175     }
176 jsr166 1.4 } catch (NoSuchElementException e) {
177 dl 1.1 threw = true;
178     }
179     if (!threw)
180     throw new Exception("Remove-no exception: " + result);
181     } else { // deque nonempty
182     int result = -1;
183 jsr166 1.5 switch (random(5)) {
184 dl 1.1 case 0: result = deque.removeFirst(); break;
185     case 1: result = deque.remove(); break;
186     case 2: result = deque.pop(); break;
187     case 3: result = deque.pollFirst(); break;
188     case 4: result = deque.poll(); break;
189     default: throw new Exception("How'd we get here");
190     }
191     if (result != ++nextHead)
192     throw new Exception(
193     "Removed "+ result + " expecting "+(nextHead - 1));
194     }
195     break;
196     case 2:
197 jsr166 1.5 switch (random(3)) {
198 dl 1.1 case 0: deque.addFirst(nextHead--); break;
199     case 1: deque.offerFirst(nextHead--); break;
200     case 2: deque.push(nextHead--); break;
201     default: throw new Exception("How'd we get here");
202     }
203     break;
204     case 3:
205     if (size() == 0) {
206     int result = -1;
207     boolean threw = false;
208     try {
209     result = deque.removeLast();
210 jsr166 1.4 } catch (NoSuchElementException e) {
211 dl 1.1 threw = true;
212     }
213     if (!threw)
214     throw new Exception("Remove-no exception: " + result);
215     } else { // deque nonempty
216 jsr166 1.4 int result = ((random() & 1) == 0 ?
217 dl 1.1 deque.removeLast() : deque.pollLast());
218     if (result != --nextTail)
219     throw new Exception(
220 jsr166 1.4 "Removed "+ result + " expecting "+(nextTail + 1));
221 dl 1.1 }
222     break;
223     default:
224     throw new Exception("How'd we get here");
225     }
226     }
227    
228     private static void testEqual(Deque<Integer> d1, Deque<Integer> d2)
229     throws Exception
230     {
231     if (d1.size() != d2.size())
232     throw new Exception("Size " + d1.size() + " != " + d2.size());
233     Iterator<Integer> it = d2.iterator();
234 jsr166 1.4 for (int i : d1) {
235 dl 1.1 int j = it.next();
236     if (j != i)
237     throw new Exception("Element " + j + " != " + i);
238     }
239    
240 jsr166 1.4 for (int i : d1)
241 dl 1.1 if (!d2.contains(i))
242     throw new Exception("d2 doesn't contain " + i);
243 jsr166 1.4 for (int i : d2)
244 dl 1.1 if (!d1.contains(i))
245     throw new Exception("d2 doesn't contain " + i);
246    
247     if (d1.contains(Integer.MIN_VALUE))
248     throw new Exception("d2 contains Integer.MIN_VALUE");
249     if (d2.contains(Integer.MIN_VALUE))
250     throw new Exception("d2 contains Integer.MIN_VALUE");
251     if (d1.contains(null))
252     throw new Exception("d1 contains null");
253     if (d2.contains(null))
254     throw new Exception("d2 contains null");
255    
256     if (!d1.containsAll(d2))
257     throw new Exception("d1 doesn't contain all of d2");
258     if (!d2.containsAll(d1))
259     throw new Exception("d2 doesn't contain all of d1");
260     Collection<Integer> c = Collections.singleton(Integer.MIN_VALUE);
261     if (d1.containsAll(c))
262     throw new Exception("d1 contains all of {Integer.MIN_VALUE }");
263     if (d2.containsAll(c))
264     throw new Exception("d2 contains all of {Integer.MIN_VALUE }");
265     }
266    
267 jsr166 1.6 @SuppressWarnings("unchecked")
268     private static <T> T copyConstructorClone(T o) throws Exception {
269     return (T) o.getClass().getConstructor(Collection.class).newInstance(o);
270     }
271    
272     @SuppressWarnings("unchecked")
273     private static <T> T serialClone(T o) throws Exception {
274     ByteArrayOutputStream bos = new ByteArrayOutputStream();
275     ObjectOutputStream oos = new ObjectOutputStream(bos);
276     oos.writeObject(o);
277     oos.flush();
278     oos.close();
279     ByteArrayInputStream bin =
280     new ByteArrayInputStream(bos.toByteArray());
281     ObjectInputStream ois = new ObjectInputStream(bin);
282 dl 1.1 return (T) ois.readObject();
283     }
284    
285     private static void testRemove(Deque<Integer> deque) throws Exception {
286 jsr166 1.6 Deque<Integer> copy = ((random() & 1) == 0) ?
287     copyConstructorClone(deque) :
288     serialClone(deque);
289 dl 1.1
290     int numRemoved = 0;
291     for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) {
292     if ((it.next() & 1) == 0) {
293     it.remove();
294     numRemoved++;
295     }
296     }
297    
298     if (copy.size() + numRemoved != size())
299     throw new Exception((copy.size() + numRemoved) + " != " + size());
300     for (int i : copy)
301     if ((i & 1) == 0)
302     throw new Exception("Even number still present: " + i);
303    
304     List<Integer> elements = Arrays.asList(copy.toArray(new Integer[0]));
305     Collections.shuffle(elements);
306     for (int e : elements) {
307     if (!copy.contains(e))
308     throw new Exception(e + " missing.");
309    
310     boolean removed = false;
311 jsr166 1.5 switch (random(3)) {
312 dl 1.1 case 0: removed = copy.remove(e); break;
313     case 1: removed = copy.removeFirstOccurrence(e); break;
314     case 2: removed = copy.removeLastOccurrence(e); break;
315 jsr166 1.3 default: throw new Exception("How'd we get here");
316 dl 1.1 }
317     if (!removed)
318     throw new Exception(e + " not removed.");
319    
320     if (copy.contains(e))
321     throw new Exception(e + " present after removal.");
322     }
323    
324     testEmpty(copy);
325    
326 jsr166 1.6 copy = copyConstructorClone(deque);
327 dl 1.1 copy.retainAll(deque);
328     testEqual(deque, copy);
329     copy.removeAll(deque);
330     testEmpty(copy);
331     }
332    
333     static boolean checkedThrows;
334    
335     private static void testEmpty(Deque<Integer> deque) throws Exception {
336     if (!deque.isEmpty())
337     throw new Exception("Deque isn't empty");
338     if (deque.size() != 0)
339     throw new Exception("Deque size isn't zero");
340     if (!(deque.pollFirst() == null))
341     throw new Exception("pollFirst lies");
342     if (!(deque.poll() == null))
343     throw new Exception("poll lies");
344     if (!(deque.peekFirst() == null))
345     throw new Exception("peekFirst lies");
346     if (!(deque.peek() == null))
347     throw new Exception("peek lies");
348     if (!(deque.pollLast() == null))
349     throw new Exception("pollLast lies");
350     if (!(deque.peekLast() == null))
351     throw new Exception("peekLast lies");
352    
353     if (!checkedThrows) {
354     checkedThrows = true;
355     boolean threw = false;
356     int result = 666;
357     try {
358 jsr166 1.4 result = ((random() & 1) == 0 ?
359 dl 1.1 deque.getFirst() : deque.element());
360 jsr166 1.4 } catch (NoSuchElementException e) {
361 dl 1.1 threw = true;
362     }
363     if (!threw)
364     throw new Exception("getFirst-no exception: "+result);
365     threw = false;
366     try {
367     result = deque.getLast();
368 jsr166 1.4 } catch (NoSuchElementException e) {
369 dl 1.1 threw = true;
370     }
371     if (!threw)
372     throw new Exception("getLast-no exception: "+result);
373     }
374    
375     }
376     }