ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/NavigableSetCheck.java
Revision: 1.12
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +0 -13 lines
Log Message:
delete extraneous 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    
68 jsr166 1.10 static NavigableSet newSet(Class<?> cl) {
69 dl 1.1 try {
70 jsr166 1.4 NavigableSet m = (NavigableSet) cl.newInstance();
71 dl 1.1 return m;
72 jsr166 1.4 } catch (Exception e) {
73 dl 1.1 throw new RuntimeException("Can't instantiate " + cl + ": " + e);
74     }
75     }
76    
77     static void runTest(NavigableSet s, Integer[] key) {
78     shuffle(key);
79     int size = key.length;
80     long startTime = System.currentTimeMillis();
81     test(s, key);
82     long time = System.currentTimeMillis() - startTime;
83     }
84    
85     static void t1(String nm, int n, NavigableSet s, Integer[] key, int expect) {
86     int sum = 0;
87     int iters = 4;
88     timer.start(nm, n * iters);
89     for (int j = 0; j < iters; ++j) {
90     for (int i = 0; i < n; i++) {
91     if (s.contains(key[i])) ++sum;
92     }
93     }
94 jsr166 1.3 timer.finish();
95 jsr166 1.7 reallyAssert(sum == expect * iters);
96 dl 1.1 }
97    
98     static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) {
99     int sum = 0;
100     timer.start(nm, n);
101     for (int i = 0; i < n; i++) {
102     if (s.remove(key[i])) ++sum;
103     }
104 jsr166 1.3 timer.finish();
105 jsr166 1.7 reallyAssert(sum == expect);
106 dl 1.1 }
107    
108     static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) {
109     int sum = 0;
110     timer.start(nm, n);
111     for (int i = 0; i < n; i++) {
112     if (s.add(key[i])) ++sum;
113     }
114 jsr166 1.3 timer.finish();
115 jsr166 1.7 reallyAssert(sum == expect);
116 dl 1.1 }
117    
118     static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) {
119     int sum = 0;
120     timer.start(nm, n);
121     for (int i = 0; i < n; i++) {
122     if (s.contains(key[i])) ++sum;
123     }
124 jsr166 1.3 timer.finish();
125 jsr166 1.7 reallyAssert(sum == expect);
126 dl 1.1 }
127    
128     static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) {
129     int sum = 0;
130     timer.start(nm, n/2);
131     for (int i = n-2; i >= 0; i-=2) {
132     if (s.remove(key[i])) ++sum;
133     }
134 jsr166 1.3 timer.finish();
135 jsr166 1.7 reallyAssert(sum == expect);
136 dl 1.1 }
137    
138     static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
139     int sum = 0;
140     timer.start(nm, n * 2);
141     for (int i = 0; i < n; i++) {
142     if (s.contains(k1[i])) ++sum;
143     if (s.contains(k2[i & absentMask])) ++sum;
144     }
145 jsr166 1.3 timer.finish();
146 jsr166 1.7 reallyAssert(sum == n);
147 dl 1.1 }
148    
149     static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
150     int sum = 0;
151     timer.start(nm, n * 2);
152     for (int i = 0; i < n; i++) {
153     if (s.contains(k1[i])) ++sum;
154     if (s.contains(k2[i & absentMask])) ++sum;
155     }
156 jsr166 1.3 timer.finish();
157 jsr166 1.7 reallyAssert(sum == n);
158 dl 1.1 }
159    
160     static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) {
161     int sum = 0;
162     timer.start(nm, n);
163     for (int i = 0; i < n; i++) {
164     if (s.contains(key[i])) ++sum;
165     }
166 jsr166 1.3 timer.finish();
167 jsr166 1.7 reallyAssert(sum == expect);
168 dl 1.1 }
169    
170     static void higherTest(NavigableSet s) {
171     int sum = 0;
172     int iters = s.size();
173     timer.start("Higher ", iters);
174     Object e = s.first();
175     while (e != null) {
176     ++sum;
177     e = s.higher(e);
178     }
179 jsr166 1.3 timer.finish();
180 jsr166 1.7 reallyAssert(sum == iters);
181 dl 1.1 }
182    
183     static void lowerTest(NavigableSet s) {
184     int sum = 0;
185     int iters = s.size();
186     timer.start("Lower ", iters);
187     Object e = s.first();
188     while (e != null) {
189     ++sum;
190     e = s.higher(e);
191     }
192 jsr166 1.3 timer.finish();
193 jsr166 1.7 reallyAssert(sum == iters);
194 dl 1.1 }
195    
196     static void ceilingTest(NavigableSet s) {
197     int sum = 0;
198     int iters = s.size();
199     if (iters > absentSize) iters = absentSize;
200     timer.start("Ceiling ", iters);
201     for (int i = 0; i < iters; ++i) {
202     Object e = s.ceiling(absent[i]);
203     if (e != null)
204     ++sum;
205     }
206 jsr166 1.3 timer.finish();
207 jsr166 1.7 reallyAssert(sum == iters);
208 dl 1.1 }
209    
210     static void floorTest(NavigableSet s) {
211     int sum = 0;
212     int iters = s.size();
213     if (iters > absentSize) iters = absentSize;
214     timer.start("Floor ", iters);
215     for (int i = 1; i < iters; ++i) {
216     Object e = s.floor(absent[i]);
217     if (e != null)
218     ++sum;
219     }
220 jsr166 1.3 timer.finish();
221 jsr166 1.7 reallyAssert(sum == iters-1);
222 dl 1.1 }
223    
224     static void ktest(NavigableSet s, int size, Integer[] key) {
225     timer.start("Contains ", size);
226     int sum = 0;
227     for (int i = 0; i < size; i++) {
228     if (s.contains(key[i])) ++sum;
229     }
230 jsr166 1.3 timer.finish();
231 jsr166 1.7 reallyAssert(sum == size);
232 dl 1.1 }
233    
234     static void ittest1(NavigableSet s, int size) {
235     int sum = 0;
236     timer.start("Iter Key ", size);
237     for (Iterator it = s.iterator(); it.hasNext(); ) {
238 jsr166 1.4 if (it.next() != MISSING)
239 dl 1.1 ++sum;
240     }
241 jsr166 1.3 timer.finish();
242 jsr166 1.7 reallyAssert(sum == size);
243 dl 1.1 }
244    
245     static void ittest(NavigableSet s, int size) {
246     ittest1(s, size);
247     }
248    
249     static void rittest1(NavigableSet s, int size) {
250     int sum = 0;
251     timer.start("Desc Iter Key ", size);
252     for (Iterator it = s.descendingIterator(); it.hasNext(); ) {
253 jsr166 1.4 if (it.next() != MISSING)
254 dl 1.1 ++sum;
255     }
256 jsr166 1.3 timer.finish();
257 jsr166 1.7 reallyAssert(sum == size);
258 dl 1.1 }
259    
260     static void rittest(NavigableSet s, int size) {
261     rittest1(s, size);
262     }
263    
264     static void rtest(NavigableSet s, int size) {
265     timer.start("Remove (iterator) ", size);
266     for (Iterator it = s.iterator(); it.hasNext(); ) {
267     it.next();
268     it.remove();
269     }
270 jsr166 1.3 timer.finish();
271 dl 1.1 }
272    
273     static void dtest(NavigableSet s, int size, Integer[] key) {
274     timer.start("Add (addAll) ", size * 2);
275     NavigableSet s2 = null;
276     try {
277     s2 = (NavigableSet) (s.getClass().newInstance());
278     s2.addAll(s);
279     }
280     catch (Exception e) { e.printStackTrace(); return; }
281 jsr166 1.3 timer.finish();
282    
283 dl 1.1 timer.start("Iter Equals ", size * 2);
284     boolean eqt = s2.equals(s) && s.equals(s2);
285 jsr166 1.7 reallyAssert(eqt);
286 jsr166 1.3 timer.finish();
287 dl 1.1
288     timer.start("Iter HashCode ", size * 2);
289     int shc = s.hashCode();
290     int s2hc = s2.hashCode();
291 jsr166 1.7 reallyAssert(shc == s2hc);
292 jsr166 1.3 timer.finish();
293 dl 1.1
294     timer.start("Add (present) ", size);
295     s2.addAll(s);
296 jsr166 1.3 timer.finish();
297 dl 1.1
298     t6("Contains ", size, s2, key, absent);
299    
300     boolean as2 = s2.add(absent[absentSize-1]);
301     reallyAssert(as2);
302     timer.start("Iter Equals ", size * 2);
303     eqt = s2.equals(s) && s.equals(s2);
304     if (as2)
305 jsr166 1.7 reallyAssert(!eqt);
306 jsr166 1.3 timer.finish();
307 dl 1.1
308     timer.start("Iter HashCode ", size * 2);
309     int s1h = s.hashCode();
310     int s2h = s2.hashCode();
311     if (as2)
312 jsr166 1.7 reallyAssert(s1h != s2h);
313 jsr166 1.3 timer.finish();
314 dl 1.1
315     timer.start("Clear ", size);
316     s.clear();
317     s2.clear();
318 jsr166 1.3 timer.finish();
319 jsr166 1.7 reallyAssert(s2.isEmpty() && s.isEmpty());
320 dl 1.1 }
321    
322     static void test(NavigableSet s, Integer[] key) {
323     int size = key.length;
324    
325     t3("Add (absent) ", size, s, key, size);
326     t3("Add (present) ", size, s, key, 0);
327     t7("ContainsKey ", size, s, key, absent);
328     t4("ContainsKey ", size, s, key, size);
329     ktest(s, size, key);
330     t4("Contains ", absentSize, s, absent, 0);
331     t6("Contains ", size, s, key, absent);
332     t1("Contains (present) ", size, s, key, size);
333     t1("Contains (absent) ", absentSize, s, absent, 0);
334     t2("Remove (absent) ", absentSize, s, absent, 0);
335     t5("Remove (present) ", size, s, key, size / 2);
336     t3("Add (half present) ", size, s, key, size / 2);
337    
338     ittest(s, size);
339     rittest(s, size);
340     higherTest(s);
341     ceilingTest(s);
342     floorTest(s);
343     lowerTest(s);
344     rtest(s, size);
345    
346     t4("Contains ", size, s, key, 0);
347     t2("Remove (absent) ", size, s, key, 0);
348     t3("Add (presized) ", size, s, key, size);
349     dtest(s, size, key);
350     }
351    
352     static class TestTimer {
353     private String name;
354     private long numOps;
355     private long startTime;
356     private String cname;
357    
358     static final java.util.TreeMap accum = new java.util.TreeMap();
359 jsr166 1.3
360 dl 1.1 static void printStats() {
361     for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
362 jsr166 1.5 Map.Entry e = (Map.Entry) it.next();
363     Stats stats = (Stats) e.getValue();
364 dl 1.1 int n = stats.number;
365     double t;
366 jsr166 1.3 if (n > 0)
367 dl 1.1 t = stats.sum / n;
368     else
369     t = stats.least;
370     long nano = Math.round(1000000.0 * t);
371     System.out.println(e.getKey() + ": " + nano);
372     }
373     }
374 jsr166 1.3
375 dl 1.1 void start(String name, long numOps) {
376     this.name = name;
377     this.cname = classify();
378     this.numOps = numOps;
379     startTime = System.currentTimeMillis();
380     }
381 jsr166 1.3
382 dl 1.1 String classify() {
383     if (name.startsWith("Contains"))
384     return "Contains ";
385     else if (name.startsWith("Add"))
386     return "Add ";
387     else if (name.startsWith("Remove"))
388     return "Remove ";
389     else if (name.startsWith("Iter"))
390     return "Iter ";
391 jsr166 1.3 else
392 dl 1.1 return null;
393     }
394    
395     void finish() {
396     long endTime = System.currentTimeMillis();
397     long time = endTime - startTime;
398 jsr166 1.6 double timePerOp = (double) time / numOps;
399 dl 1.1
400     Object st = accum.get(name);
401     if (st == null)
402     accum.put(name, new Stats(timePerOp));
403     else {
404     Stats stats = (Stats) st;
405     stats.sum += timePerOp;
406     stats.number++;
407     if (timePerOp < stats.least) stats.least = timePerOp;
408     }
409    
410     if (cname != null) {
411     st = accum.get(cname);
412     if (st == null)
413     accum.put(cname, new Stats(timePerOp));
414     else {
415     Stats stats = (Stats) st;
416     stats.sum += timePerOp;
417     stats.number++;
418     if (timePerOp < stats.least) stats.least = timePerOp;
419     }
420     }
421    
422     }
423    
424     }
425    
426     static class Stats {
427     double sum = 0;
428     double least;
429     int number = 0;
430     Stats(double t) { least = t; }
431     }
432    
433     static Random rng = new Random();
434    
435     static void shuffle(Integer[] keys) {
436     int size = keys.length;
437 jsr166 1.9 for (int i = size; i > 1; i--) {
438 dl 1.1 int r = rng.nextInt(i);
439     Integer t = keys[i-1];
440     keys[i-1] = keys[r];
441     keys[r] = t;
442     }
443     }
444    
445     }