ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/NavigableSetCheck.java
Revision: 1.13
Committed: Mon Aug 10 03:13:33 2015 UTC (8 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +0 -1 lines
Log Message:
delete unwanted blank lines

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.8 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6 dl 1.1 /**
7     * @test
8     * @synopsis Times and checks basic set operations
9     */
10 jsr166 1.11 import java.io.*;
11 dl 1.1 import java.util.*;
12    
13     public class NavigableSetCheck {
14    
15     static int absentSize;
16     static int absentMask;
17     static Integer[] absent;
18    
19     static final Integer MISSING = new Integer(Integer.MIN_VALUE);
20    
21     static TestTimer timer = new TestTimer();
22    
23     static void reallyAssert(boolean b) {
24     if (!b) throw new Error("Failed Assertion");
25     }
26    
27     public static void main(String[] args) throws Exception {
28 jsr166 1.10 Class<?> setClass = null;
29 dl 1.1 int numTests = 50;
30     int size = 50000;
31    
32     if (args.length > 0) {
33     try {
34     setClass = Class.forName(args[0]);
35 jsr166 1.4 } catch (ClassNotFoundException e) {
36 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
37     }
38     }
39    
40 jsr166 1.3 if (args.length > 1)
41 dl 1.1 numTests = Integer.parseInt(args[1]);
42    
43 jsr166 1.3 if (args.length > 2)
44 dl 1.1 size = Integer.parseInt(args[2]);
45    
46     System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size);
47    
48     absentSize = 8;
49     while (absentSize < size) absentSize <<= 1;
50     absentMask = absentSize - 1;
51     absent = new Integer[absentSize];
52    
53 jsr166 1.3 for (int i = 0; i < absentSize; ++i)
54 dl 1.1 absent[i] = new Integer(i * 2);
55    
56     Integer[] key = new Integer[size];
57 jsr166 1.3 for (int i = 0; i < size; ++i)
58 dl 1.1 key[i] = new Integer(i * 2 + 1);
59    
60     for (int rep = 0; rep < numTests; ++rep) {
61     runTest(newSet(setClass), key);
62     }
63    
64     TestTimer.printStats();
65     }
66    
67 jsr166 1.10 static NavigableSet newSet(Class<?> cl) {
68 dl 1.1 try {
69 jsr166 1.4 NavigableSet m = (NavigableSet) cl.newInstance();
70 dl 1.1 return m;
71 jsr166 1.4 } catch (Exception e) {
72 dl 1.1 throw new RuntimeException("Can't instantiate " + cl + ": " + e);
73     }
74     }
75    
76     static void runTest(NavigableSet s, Integer[] key) {
77     shuffle(key);
78     int size = key.length;
79     long startTime = System.currentTimeMillis();
80     test(s, key);
81     long time = System.currentTimeMillis() - startTime;
82     }
83    
84     static void t1(String nm, int n, NavigableSet s, Integer[] key, int expect) {
85     int sum = 0;
86     int iters = 4;
87     timer.start(nm, n * iters);
88     for (int j = 0; j < iters; ++j) {
89     for (int i = 0; i < n; i++) {
90     if (s.contains(key[i])) ++sum;
91     }
92     }
93 jsr166 1.3 timer.finish();
94 jsr166 1.7 reallyAssert(sum == expect * iters);
95 dl 1.1 }
96    
97     static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) {
98     int sum = 0;
99     timer.start(nm, n);
100     for (int i = 0; i < n; i++) {
101     if (s.remove(key[i])) ++sum;
102     }
103 jsr166 1.3 timer.finish();
104 jsr166 1.7 reallyAssert(sum == expect);
105 dl 1.1 }
106    
107     static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) {
108     int sum = 0;
109     timer.start(nm, n);
110     for (int i = 0; i < n; i++) {
111     if (s.add(key[i])) ++sum;
112     }
113 jsr166 1.3 timer.finish();
114 jsr166 1.7 reallyAssert(sum == expect);
115 dl 1.1 }
116    
117     static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) {
118     int sum = 0;
119     timer.start(nm, n);
120     for (int i = 0; i < n; i++) {
121     if (s.contains(key[i])) ++sum;
122     }
123 jsr166 1.3 timer.finish();
124 jsr166 1.7 reallyAssert(sum == expect);
125 dl 1.1 }
126    
127     static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) {
128     int sum = 0;
129     timer.start(nm, n/2);
130     for (int i = n-2; i >= 0; i-=2) {
131     if (s.remove(key[i])) ++sum;
132     }
133 jsr166 1.3 timer.finish();
134 jsr166 1.7 reallyAssert(sum == expect);
135 dl 1.1 }
136    
137     static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
138     int sum = 0;
139     timer.start(nm, n * 2);
140     for (int i = 0; i < n; i++) {
141     if (s.contains(k1[i])) ++sum;
142     if (s.contains(k2[i & absentMask])) ++sum;
143     }
144 jsr166 1.3 timer.finish();
145 jsr166 1.7 reallyAssert(sum == n);
146 dl 1.1 }
147    
148     static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
149     int sum = 0;
150     timer.start(nm, n * 2);
151     for (int i = 0; i < n; i++) {
152     if (s.contains(k1[i])) ++sum;
153     if (s.contains(k2[i & absentMask])) ++sum;
154     }
155 jsr166 1.3 timer.finish();
156 jsr166 1.7 reallyAssert(sum == n);
157 dl 1.1 }
158    
159     static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) {
160     int sum = 0;
161     timer.start(nm, n);
162     for (int i = 0; i < n; i++) {
163     if (s.contains(key[i])) ++sum;
164     }
165 jsr166 1.3 timer.finish();
166 jsr166 1.7 reallyAssert(sum == expect);
167 dl 1.1 }
168    
169     static void higherTest(NavigableSet s) {
170     int sum = 0;
171     int iters = s.size();
172     timer.start("Higher ", iters);
173     Object e = s.first();
174     while (e != null) {
175     ++sum;
176     e = s.higher(e);
177     }
178 jsr166 1.3 timer.finish();
179 jsr166 1.7 reallyAssert(sum == iters);
180 dl 1.1 }
181    
182     static void lowerTest(NavigableSet s) {
183     int sum = 0;
184     int iters = s.size();
185     timer.start("Lower ", iters);
186     Object e = s.first();
187     while (e != null) {
188     ++sum;
189     e = s.higher(e);
190     }
191 jsr166 1.3 timer.finish();
192 jsr166 1.7 reallyAssert(sum == iters);
193 dl 1.1 }
194    
195     static void ceilingTest(NavigableSet s) {
196     int sum = 0;
197     int iters = s.size();
198     if (iters > absentSize) iters = absentSize;
199     timer.start("Ceiling ", iters);
200     for (int i = 0; i < iters; ++i) {
201     Object e = s.ceiling(absent[i]);
202     if (e != null)
203     ++sum;
204     }
205 jsr166 1.3 timer.finish();
206 jsr166 1.7 reallyAssert(sum == iters);
207 dl 1.1 }
208    
209     static void floorTest(NavigableSet s) {
210     int sum = 0;
211     int iters = s.size();
212     if (iters > absentSize) iters = absentSize;
213     timer.start("Floor ", iters);
214     for (int i = 1; i < iters; ++i) {
215     Object e = s.floor(absent[i]);
216     if (e != null)
217     ++sum;
218     }
219 jsr166 1.3 timer.finish();
220 jsr166 1.7 reallyAssert(sum == iters-1);
221 dl 1.1 }
222    
223     static void ktest(NavigableSet s, int size, Integer[] key) {
224     timer.start("Contains ", size);
225     int sum = 0;
226     for (int i = 0; i < size; i++) {
227     if (s.contains(key[i])) ++sum;
228     }
229 jsr166 1.3 timer.finish();
230 jsr166 1.7 reallyAssert(sum == size);
231 dl 1.1 }
232    
233     static void ittest1(NavigableSet s, int size) {
234     int sum = 0;
235     timer.start("Iter Key ", size);
236     for (Iterator it = s.iterator(); it.hasNext(); ) {
237 jsr166 1.4 if (it.next() != MISSING)
238 dl 1.1 ++sum;
239     }
240 jsr166 1.3 timer.finish();
241 jsr166 1.7 reallyAssert(sum == size);
242 dl 1.1 }
243    
244     static void ittest(NavigableSet s, int size) {
245     ittest1(s, size);
246     }
247    
248     static void rittest1(NavigableSet s, int size) {
249     int sum = 0;
250     timer.start("Desc Iter Key ", size);
251     for (Iterator it = s.descendingIterator(); it.hasNext(); ) {
252 jsr166 1.4 if (it.next() != MISSING)
253 dl 1.1 ++sum;
254     }
255 jsr166 1.3 timer.finish();
256 jsr166 1.7 reallyAssert(sum == size);
257 dl 1.1 }
258    
259     static void rittest(NavigableSet s, int size) {
260     rittest1(s, size);
261     }
262    
263     static void rtest(NavigableSet s, int size) {
264     timer.start("Remove (iterator) ", size);
265     for (Iterator it = s.iterator(); it.hasNext(); ) {
266     it.next();
267     it.remove();
268     }
269 jsr166 1.3 timer.finish();
270 dl 1.1 }
271    
272     static void dtest(NavigableSet s, int size, Integer[] key) {
273     timer.start("Add (addAll) ", size * 2);
274     NavigableSet s2 = null;
275     try {
276     s2 = (NavigableSet) (s.getClass().newInstance());
277     s2.addAll(s);
278     }
279     catch (Exception e) { e.printStackTrace(); return; }
280 jsr166 1.3 timer.finish();
281    
282 dl 1.1 timer.start("Iter Equals ", size * 2);
283     boolean eqt = s2.equals(s) && s.equals(s2);
284 jsr166 1.7 reallyAssert(eqt);
285 jsr166 1.3 timer.finish();
286 dl 1.1
287     timer.start("Iter HashCode ", size * 2);
288     int shc = s.hashCode();
289     int s2hc = s2.hashCode();
290 jsr166 1.7 reallyAssert(shc == s2hc);
291 jsr166 1.3 timer.finish();
292 dl 1.1
293     timer.start("Add (present) ", size);
294     s2.addAll(s);
295 jsr166 1.3 timer.finish();
296 dl 1.1
297     t6("Contains ", size, s2, key, absent);
298    
299     boolean as2 = s2.add(absent[absentSize-1]);
300     reallyAssert(as2);
301     timer.start("Iter Equals ", size * 2);
302     eqt = s2.equals(s) && s.equals(s2);
303     if (as2)
304 jsr166 1.7 reallyAssert(!eqt);
305 jsr166 1.3 timer.finish();
306 dl 1.1
307     timer.start("Iter HashCode ", size * 2);
308     int s1h = s.hashCode();
309     int s2h = s2.hashCode();
310     if (as2)
311 jsr166 1.7 reallyAssert(s1h != s2h);
312 jsr166 1.3 timer.finish();
313 dl 1.1
314     timer.start("Clear ", size);
315     s.clear();
316     s2.clear();
317 jsr166 1.3 timer.finish();
318 jsr166 1.7 reallyAssert(s2.isEmpty() && s.isEmpty());
319 dl 1.1 }
320    
321     static void test(NavigableSet s, Integer[] key) {
322     int size = key.length;
323    
324     t3("Add (absent) ", size, s, key, size);
325     t3("Add (present) ", size, s, key, 0);
326     t7("ContainsKey ", size, s, key, absent);
327     t4("ContainsKey ", size, s, key, size);
328     ktest(s, size, key);
329     t4("Contains ", absentSize, s, absent, 0);
330     t6("Contains ", size, s, key, absent);
331     t1("Contains (present) ", size, s, key, size);
332     t1("Contains (absent) ", absentSize, s, absent, 0);
333     t2("Remove (absent) ", absentSize, s, absent, 0);
334     t5("Remove (present) ", size, s, key, size / 2);
335     t3("Add (half present) ", size, s, key, size / 2);
336    
337     ittest(s, size);
338     rittest(s, size);
339     higherTest(s);
340     ceilingTest(s);
341     floorTest(s);
342     lowerTest(s);
343     rtest(s, size);
344    
345     t4("Contains ", size, s, key, 0);
346     t2("Remove (absent) ", size, s, key, 0);
347     t3("Add (presized) ", size, s, key, size);
348     dtest(s, size, key);
349     }
350    
351     static class TestTimer {
352     private String name;
353     private long numOps;
354     private long startTime;
355     private String cname;
356    
357     static final java.util.TreeMap accum = new java.util.TreeMap();
358 jsr166 1.3
359 dl 1.1 static void printStats() {
360     for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
361 jsr166 1.5 Map.Entry e = (Map.Entry) it.next();
362     Stats stats = (Stats) e.getValue();
363 dl 1.1 int n = stats.number;
364     double t;
365 jsr166 1.3 if (n > 0)
366 dl 1.1 t = stats.sum / n;
367     else
368     t = stats.least;
369     long nano = Math.round(1000000.0 * t);
370     System.out.println(e.getKey() + ": " + nano);
371     }
372     }
373 jsr166 1.3
374 dl 1.1 void start(String name, long numOps) {
375     this.name = name;
376     this.cname = classify();
377     this.numOps = numOps;
378     startTime = System.currentTimeMillis();
379     }
380 jsr166 1.3
381 dl 1.1 String classify() {
382     if (name.startsWith("Contains"))
383     return "Contains ";
384     else if (name.startsWith("Add"))
385     return "Add ";
386     else if (name.startsWith("Remove"))
387     return "Remove ";
388     else if (name.startsWith("Iter"))
389     return "Iter ";
390 jsr166 1.3 else
391 dl 1.1 return null;
392     }
393    
394     void finish() {
395     long endTime = System.currentTimeMillis();
396     long time = endTime - startTime;
397 jsr166 1.6 double timePerOp = (double) time / numOps;
398 dl 1.1
399     Object st = accum.get(name);
400     if (st == null)
401     accum.put(name, new Stats(timePerOp));
402     else {
403     Stats stats = (Stats) st;
404     stats.sum += timePerOp;
405     stats.number++;
406     if (timePerOp < stats.least) stats.least = timePerOp;
407     }
408    
409     if (cname != null) {
410     st = accum.get(cname);
411     if (st == null)
412     accum.put(cname, new Stats(timePerOp));
413     else {
414     Stats stats = (Stats) st;
415     stats.sum += timePerOp;
416     stats.number++;
417     if (timePerOp < stats.least) stats.least = timePerOp;
418     }
419     }
420    
421     }
422    
423     }
424    
425     static class Stats {
426     double sum = 0;
427     double least;
428     int number = 0;
429     Stats(double t) { least = t; }
430     }
431    
432     static Random rng = new Random();
433    
434     static void shuffle(Integer[] keys) {
435     int size = keys.length;
436 jsr166 1.9 for (int i = size; i > 1; i--) {
437 dl 1.1 int r = rng.nextInt(i);
438     Integer t = keys[i-1];
439     keys[i-1] = keys[r];
440     keys[r] = t;
441     }
442     }
443    
444     }