ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/NavigableMapCheck.java
(Generate patch)

Comparing jsr166/src/test/loops/NavigableMapCheck.java (file contents):
Revision 1.1 by dl, Mon May 2 19:19:38 2005 UTC vs.
Revision 1.14 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 1 | Line 1
1 + /*
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 + * http://creativecommons.org/publicdomain/zero/1.0/
5 + */
6   /**
7   * @test
8   * @synopsis Times and checks basic map operations
9   */
5 import java.util.*;
10   import java.io.*;
11 + import java.util.*;
12  
13   public class NavigableMapCheck {
14  
# Line 20 | Line 25 | public class NavigableMapCheck {
25      }
26  
27      public static void main(String[] args) throws Exception {
28 <        Class mapClass = null;
28 >        Class<?> mapClass = null;
29          int numTests = 50;
30          int size = 50000;
31  
32          if (args.length > 0) {
33              try {
34                  mapClass = Class.forName(args[0]);
35 <            } catch(ClassNotFoundException e) {
35 >            } catch (ClassNotFoundException e) {
36                  throw new RuntimeException("Class " + args[0] + " not found.");
37              }
38          }
39  
40 <
36 <        if (args.length > 1)
40 >        if (args.length > 1)
41              numTests = Integer.parseInt(args[1]);
42  
43 <        if (args.length > 2)
43 >        if (args.length > 2)
44              size = Integer.parseInt(args[2]);
45  
46          System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size);
47  
44
48          absentSize = 8;
49          while (absentSize < size) absentSize <<= 1;
50          absentMask = absentSize - 1;
51          absent = new Integer[absentSize];
52  
53 <        for (int i = 0; i < absentSize; ++i)
53 >        for (int i = 0; i < absentSize; ++i)
54              absent[i] = new Integer(i * 2);
55  
56          Integer[] key = new Integer[size];
57 <        for (int i = 0; i < size; ++i)
57 >        for (int i = 0; i < size; ++i)
58              key[i] = new Integer(i * 2 + 1);
59  
57
60          for (int rep = 0; rep < numTests; ++rep) {
61              runTest(newMap(mapClass), key);
62          }
# Line 63 | Line 65 | public class NavigableMapCheck {
65  
66      }
67  
68 <    static NavigableMap newMap(Class cl) {
68 >    static NavigableMap newMap(Class<?> cl) {
69          try {
70 <            NavigableMap m = (NavigableMap)cl.newInstance();
70 >            NavigableMap m = (NavigableMap) cl.newInstance();
71              return m;
72 <        } catch(Exception e) {
72 >        } catch (Exception e) {
73              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
74          }
75      }
76  
75
77      static void runTest(NavigableMap s, Integer[] key) {
78          shuffle(key);
79          int size = key.length;
# Line 90 | Line 91 | public class NavigableMapCheck {
91                  if (s.get(key[i]) != null) ++sum;
92              }
93          }
94 <        timer.finish();
95 <        reallyAssert (sum == expect * iters);
94 >        timer.finish();
95 >        reallyAssert(sum == expect * iters);
96      }
97  
98      static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 100 | Line 101 | public class NavigableMapCheck {
101          for (int i = 0; i < n; i++) {
102              if (s.remove(key[i]) != null) ++sum;
103          }
104 <        timer.finish();
105 <        reallyAssert (sum == expect);
104 >        timer.finish();
105 >        reallyAssert(sum == expect);
106      }
107  
108      static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 110 | Line 111 | public class NavigableMapCheck {
111          for (int i = 0; i < n; i++) {
112              if (s.put(key[i], absent[i & absentMask]) == null) ++sum;
113          }
114 <        timer.finish();
115 <        reallyAssert (sum == expect);
114 >        timer.finish();
115 >        reallyAssert(sum == expect);
116      }
117  
118      static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 120 | Line 121 | public class NavigableMapCheck {
121          for (int i = 0; i < n; i++) {
122              if (s.containsKey(key[i])) ++sum;
123          }
124 <        timer.finish();
125 <        reallyAssert (sum == expect);
124 >        timer.finish();
125 >        reallyAssert(sum == expect);
126      }
127  
128      static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 130 | Line 131 | public class NavigableMapCheck {
131          for (int i = n-2; i >= 0; i-=2) {
132              if (s.remove(key[i]) != null) ++sum;
133          }
134 <        timer.finish();
135 <        reallyAssert (sum == expect);
134 >        timer.finish();
135 >        reallyAssert(sum == expect);
136      }
137  
138      static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 141 | Line 142 | public class NavigableMapCheck {
142              if (s.get(k1[i]) != null) ++sum;
143              if (s.get(k2[i & absentMask]) != null) ++sum;
144          }
145 <        timer.finish();
146 <        reallyAssert (sum == n);
145 >        timer.finish();
146 >        reallyAssert(sum == n);
147      }
148  
149      static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 152 | Line 153 | public class NavigableMapCheck {
153              if (s.containsKey(k1[i])) ++sum;
154              if (s.containsKey(k2[i & absentMask])) ++sum;
155          }
156 <        timer.finish();
157 <        reallyAssert (sum == n);
156 >        timer.finish();
157 >        reallyAssert(sum == n);
158      }
159  
160      static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 162 | Line 163 | public class NavigableMapCheck {
163          for (int i = 0; i < n; i++) {
164              if (s.get(key[i]) != null) ++sum;
165          }
166 <        timer.finish();
167 <        reallyAssert (sum == expect);
166 >        timer.finish();
167 >        reallyAssert(sum == expect);
168      }
169  
169
170      static void t9(NavigableMap s) {
171          int sum = 0;
172          int iters = 20;
# Line 174 | Line 174 | public class NavigableMapCheck {
174          int step = absentSize / iters;
175          for (int i = 0; i < absentSize; i += step)
176              if (s.containsValue(absent[i])) ++sum;
177 <        timer.finish();
178 <        reallyAssert (sum != 0);
177 >        timer.finish();
178 >        reallyAssert(sum != 0);
179      }
180  
181      static void higherTest(NavigableMap s) {
# Line 187 | Line 187 | public class NavigableMapCheck {
187              ++sum;
188              e = s.higherEntry(e.getKey());
189          }
190 <        timer.finish();
191 <        reallyAssert (sum == iters);
190 >        timer.finish();
191 >        reallyAssert(sum == iters);
192      }
193  
194      static void lowerTest(NavigableMap s) {
# Line 200 | Line 200 | public class NavigableMapCheck {
200              ++sum;
201              e = s.higherEntry(e.getKey());
202          }
203 <        timer.finish();
204 <        reallyAssert (sum == iters);
203 >        timer.finish();
204 >        reallyAssert(sum == iters);
205      }
206  
207      static void ceilingTest(NavigableMap s) {
# Line 214 | Line 214 | public class NavigableMapCheck {
214              if (e != null)
215                  ++sum;
216          }
217 <        timer.finish();
218 <        reallyAssert (sum == iters);
217 >        timer.finish();
218 >        reallyAssert(sum == iters);
219      }
220  
221      static void floorTest(NavigableMap s) {
# Line 228 | Line 228 | public class NavigableMapCheck {
228              if (e != null)
229                  ++sum;
230          }
231 <        timer.finish();
232 <        reallyAssert (sum == iters-1);
231 >        timer.finish();
232 >        reallyAssert(sum == iters-1);
233      }
234  
235
235      static void ktest(NavigableMap s, int size, Integer[] key) {
236          timer.start("ContainsKey            ", size);
237          Set ks = s.keySet();
# Line 240 | Line 239 | public class NavigableMapCheck {
239          for (int i = 0; i < size; i++) {
240              if (ks.contains(key[i])) ++sum;
241          }
242 <        timer.finish();
243 <        reallyAssert (sum == size);
242 >        timer.finish();
243 >        reallyAssert(sum == size);
244      }
245  
247
246      static void ittest1(NavigableMap s, int size) {
247          int sum = 0;
248          timer.start("Iter Key               ", size);
249          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
250 <            if(it.next() != MISSING)
250 >            if (it.next() != MISSING)
251                  ++sum;
252          }
253 <        timer.finish();
254 <        reallyAssert (sum == size);
253 >        timer.finish();
254 >        reallyAssert(sum == size);
255      }
256  
257      static void ittest2(NavigableMap s, int size) {
258          int sum = 0;
259          timer.start("Iter Value             ", size);
260          for (Iterator it = s.values().iterator(); it.hasNext(); ) {
261 <            if(it.next() != MISSING)
261 >            if (it.next() != MISSING)
262                  ++sum;
263          }
264 <        timer.finish();
265 <        reallyAssert (sum == size);
264 >        timer.finish();
265 >        reallyAssert(sum == size);
266      }
267      static void ittest3(NavigableMap s, int size) {
268          int sum = 0;
269          timer.start("Iter Entry             ", size);
270          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
271 <            if(it.next() != MISSING)
271 >            if (it.next() != MISSING)
272                  ++sum;
273          }
274 <        timer.finish();
275 <        reallyAssert (sum == size);
274 >        timer.finish();
275 >        reallyAssert(sum == size);
276      }
277  
278      static void ittest(NavigableMap s, int size) {
# Line 287 | Line 285 | public class NavigableMapCheck {
285          int sum = 0;
286          timer.start("Desc Iter Key          ", size);
287          for (Iterator it = s.descendingKeySet().iterator(); it.hasNext(); ) {
288 <            if(it.next() != MISSING)
291 <                ++sum;
292 <        }
293 <        timer.finish();
294 <        reallyAssert (sum == size);
295 <    }
296 <
297 <    static void rittest2(NavigableMap s, int size) {
298 <        int sum = 0;
299 <        timer.start("Desc Iter Entry        ", size);
300 <        for (Iterator it = s.descendingEntrySet().iterator(); it.hasNext(); ) {
301 <            if(it.next() != MISSING)
288 >            if (it.next() != MISSING)
289                  ++sum;
290          }
291 <        timer.finish();
292 <        reallyAssert (sum == size);
291 >        timer.finish();
292 >        reallyAssert(sum == size);
293      }
294  
295      static void rittest(NavigableMap s, int size) {
296          rittest1(s, size);
297 <        rittest2(s, size);
297 >        //        rittest2(s, size);
298      }
299  
313
300      static void rtest(NavigableMap s, int size) {
301          timer.start("Remove (iterator)      ", size);
302          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
303              it.next();
304              it.remove();
305          }
306 <        timer.finish();
306 >        timer.finish();
307      }
308  
309      static void rvtest(NavigableMap s, int size) {
# Line 326 | Line 312 | public class NavigableMapCheck {
312              it.next();
313              it.remove();
314          }
315 <        timer.finish();
315 >        timer.finish();
316      }
317  
332
318      static void dtest(NavigableMap s, int size, Integer[] key) {
319          timer.start("Put (putAll)           ", size * 2);
320          NavigableMap s2 = null;
# Line 338 | Line 323 | public class NavigableMapCheck {
323              s2.putAll(s);
324          }
325          catch (Exception e) { e.printStackTrace(); return; }
326 <        timer.finish();
327 <    
326 >        timer.finish();
327 >
328          timer.start("Iter Equals            ", size * 2);
329          boolean eqt = s2.equals(s) && s.equals(s2);
330 <        reallyAssert (eqt);
331 <        timer.finish();
330 >        reallyAssert(eqt);
331 >        timer.finish();
332  
333          timer.start("Iter HashCode          ", size * 2);
334          int shc = s.hashCode();
335          int s2hc = s2.hashCode();
336 <        reallyAssert (shc == s2hc);
337 <        timer.finish();
336 >        reallyAssert(shc == s2hc);
337 >        timer.finish();
338  
339          timer.start("Put (present)          ", size);
340          s2.putAll(s);
341 <        timer.finish();
341 >        timer.finish();
342  
343          timer.start("Iter EntrySet contains ", size * 2);
344          Set es2 = s2.entrySet();
# Line 362 | Line 347 | public class NavigableMapCheck {
347              Object entry = i1.next();
348              if (es2.contains(entry)) ++sum;
349          }
350 <        timer.finish();
351 <        reallyAssert (sum == size);
350 >        timer.finish();
351 >        reallyAssert(sum == size);
352  
353          t6("Get                    ", size, s2, key, absent);
354  
# Line 371 | Line 356 | public class NavigableMapCheck {
356          s2.put(key[size-1], absent[0]);
357          timer.start("Iter Equals            ", size * 2);
358          eqt = s2.equals(s) && s.equals(s2);
359 <        reallyAssert (!eqt);
360 <        timer.finish();
359 >        reallyAssert(!eqt);
360 >        timer.finish();
361  
362          timer.start("Iter HashCode          ", size * 2);
363          int s1h = s.hashCode();
364          int s2h = s2.hashCode();
365 <        reallyAssert (s1h != s2h);
366 <        timer.finish();
365 >        reallyAssert(s1h != s2h);
366 >        timer.finish();
367  
368          s2.put(key[size-1], hold);
369          timer.start("Remove (iterator)      ", size * 2);
# Line 386 | Line 371 | public class NavigableMapCheck {
371          Set es = s.entrySet();
372          while (s2i.hasNext())
373              es.remove(s2i.next());
374 <        timer.finish();
374 >        timer.finish();
375  
376 <        reallyAssert (s.isEmpty());
376 >        reallyAssert(s.isEmpty());
377  
378          timer.start("Clear                  ", size);
379          s2.clear();
380 <        timer.finish();
381 <        reallyAssert (s2.isEmpty() && s.isEmpty());
380 >        timer.finish();
381 >        reallyAssert(s2.isEmpty() && s.isEmpty());
382      }
383  
399
400    
384      static void test(NavigableMap s, Integer[] key) {
385          int size = key.length;
386  
# Line 436 | Line 419 | public class NavigableMapCheck {
419          private String cname;
420  
421          static final java.util.TreeMap accum = new java.util.TreeMap();
422 <    
422 >
423          static void printStats() {
424              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
425 <                Map.Entry e = (Map.Entry)(it.next());
426 <                Stats stats = ((Stats)(e.getValue()));
425 >                Map.Entry e = (Map.Entry) it.next();
426 >                Stats stats = (Stats) e.getValue();
427                  int n = stats.number;
428                  double t;
429 <                if (n > 0)
429 >                if (n > 0)
430                      t = stats.sum / n;
431                  else
432                      t = stats.least;
# Line 451 | Line 434 | public class NavigableMapCheck {
434                  System.out.println(e.getKey() + ": " + nano);
435              }
436          }
437 <    
437 >
438          void start(String name, long numOps) {
439              this.name = name;
440              this.cname = classify();
441              this.numOps = numOps;
442              startTime = System.currentTimeMillis();
443          }
461    
444  
445          String classify() {
446              if (name.startsWith("Get"))
# Line 469 | Line 451 | public class NavigableMapCheck {
451                  return "Remove                 ";
452              else if (name.startsWith("Iter"))
453                  return "Iter                   ";
454 <            else
454 >            else
455                  return null;
456          }
457  
458          void finish() {
459              long endTime = System.currentTimeMillis();
460              long time = endTime - startTime;
461 <            double timePerOp = ((double)time)/numOps;
461 >            double timePerOp = (double) time /numOps;
462  
463              Object st = accum.get(name);
464              if (st == null)
# Line 511 | Line 493 | public class NavigableMapCheck {
493          Stats(double t) { least = t; }
494      }
495  
496 <    static Random rng = new Random();
496 >    static Random rng = new Random(111);
497  
498      static void shuffle(Integer[] keys) {
499          int size = keys.length;
500 <        for (int i=size; i>1; i--) {
500 >        for (int i = size; i > 1; i--) {
501              int r = rng.nextInt(i);
502              Integer t = keys[i-1];
503              keys[i-1] = keys[r];
# Line 524 | Line 506 | public class NavigableMapCheck {
506      }
507  
508   }
527

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines