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.5 by jsr166, Thu Oct 29 23:09:07 2009 UTC vs.
Revision 1.15 by jsr166, Mon Aug 10 03:13:33 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/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6   /**
7   * @test
8   * @synopsis Times and checks basic map operations
9   */
10 import java.util.*;
10   import java.io.*;
11 + import java.util.*;
12  
13   public class NavigableMapCheck {
14  
# Line 25 | 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
40          if (args.length > 1)
41              numTests = Integer.parseInt(args[1]);
42  
# Line 46 | Line 45 | public class NavigableMapCheck {
45  
46          System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size);
47  
49
48          absentSize = 8;
49          while (absentSize < size) absentSize <<= 1;
50          absentMask = absentSize - 1;
# Line 59 | Line 57 | public class NavigableMapCheck {
57          for (int i = 0; i < size; ++i)
58              key[i] = new Integer(i * 2 + 1);
59  
62
60          for (int rep = 0; rep < numTests; ++rep) {
61              runTest(newMap(mapClass), key);
62          }
63  
64          TestTimer.printStats();
68
65      }
66  
67 <    static NavigableMap newMap(Class cl) {
67 >    static NavigableMap newMap(Class<?> cl) {
68          try {
69 <            NavigableMap m = (NavigableMap)cl.newInstance();
69 >            NavigableMap m = (NavigableMap) cl.newInstance();
70              return m;
71 <        } catch(Exception e) {
71 >        } catch (Exception e) {
72              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
73          }
74      }
75  
80
76      static void runTest(NavigableMap s, Integer[] key) {
77          shuffle(key);
78          int size = key.length;
# Line 96 | Line 91 | public class NavigableMapCheck {
91              }
92          }
93          timer.finish();
94 <        reallyAssert (sum == expect * iters);
94 >        reallyAssert(sum == expect * iters);
95      }
96  
97      static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 106 | Line 101 | public class NavigableMapCheck {
101              if (s.remove(key[i]) != null) ++sum;
102          }
103          timer.finish();
104 <        reallyAssert (sum == expect);
104 >        reallyAssert(sum == expect);
105      }
106  
107      static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 116 | Line 111 | public class NavigableMapCheck {
111              if (s.put(key[i], absent[i & absentMask]) == null) ++sum;
112          }
113          timer.finish();
114 <        reallyAssert (sum == expect);
114 >        reallyAssert(sum == expect);
115      }
116  
117      static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 126 | Line 121 | public class NavigableMapCheck {
121              if (s.containsKey(key[i])) ++sum;
122          }
123          timer.finish();
124 <        reallyAssert (sum == expect);
124 >        reallyAssert(sum == expect);
125      }
126  
127      static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 136 | Line 131 | public class NavigableMapCheck {
131              if (s.remove(key[i]) != null) ++sum;
132          }
133          timer.finish();
134 <        reallyAssert (sum == expect);
134 >        reallyAssert(sum == expect);
135      }
136  
137      static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 147 | Line 142 | public class NavigableMapCheck {
142              if (s.get(k2[i & absentMask]) != null) ++sum;
143          }
144          timer.finish();
145 <        reallyAssert (sum == n);
145 >        reallyAssert(sum == n);
146      }
147  
148      static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 158 | Line 153 | public class NavigableMapCheck {
153              if (s.containsKey(k2[i & absentMask])) ++sum;
154          }
155          timer.finish();
156 <        reallyAssert (sum == n);
156 >        reallyAssert(sum == n);
157      }
158  
159      static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 168 | Line 163 | public class NavigableMapCheck {
163              if (s.get(key[i]) != null) ++sum;
164          }
165          timer.finish();
166 <        reallyAssert (sum == expect);
166 >        reallyAssert(sum == expect);
167      }
168  
174
169      static void t9(NavigableMap s) {
170          int sum = 0;
171          int iters = 20;
# Line 180 | Line 174 | public class NavigableMapCheck {
174          for (int i = 0; i < absentSize; i += step)
175              if (s.containsValue(absent[i])) ++sum;
176          timer.finish();
177 <        reallyAssert (sum != 0);
177 >        reallyAssert(sum != 0);
178      }
179  
180      static void higherTest(NavigableMap s) {
# Line 193 | Line 187 | public class NavigableMapCheck {
187              e = s.higherEntry(e.getKey());
188          }
189          timer.finish();
190 <        reallyAssert (sum == iters);
190 >        reallyAssert(sum == iters);
191      }
192  
193      static void lowerTest(NavigableMap s) {
# Line 206 | Line 200 | public class NavigableMapCheck {
200              e = s.higherEntry(e.getKey());
201          }
202          timer.finish();
203 <        reallyAssert (sum == iters);
203 >        reallyAssert(sum == iters);
204      }
205  
206      static void ceilingTest(NavigableMap s) {
# Line 220 | Line 214 | public class NavigableMapCheck {
214                  ++sum;
215          }
216          timer.finish();
217 <        reallyAssert (sum == iters);
217 >        reallyAssert(sum == iters);
218      }
219  
220      static void floorTest(NavigableMap s) {
# Line 234 | Line 228 | public class NavigableMapCheck {
228                  ++sum;
229          }
230          timer.finish();
231 <        reallyAssert (sum == iters-1);
231 >        reallyAssert(sum == iters-1);
232      }
233  
240
234      static void ktest(NavigableMap s, int size, Integer[] key) {
235          timer.start("ContainsKey            ", size);
236          Set ks = s.keySet();
# Line 246 | Line 239 | public class NavigableMapCheck {
239              if (ks.contains(key[i])) ++sum;
240          }
241          timer.finish();
242 <        reallyAssert (sum == size);
242 >        reallyAssert(sum == size);
243      }
244  
252
245      static void ittest1(NavigableMap s, int size) {
246          int sum = 0;
247          timer.start("Iter Key               ", size);
248          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
249 <            if(it.next() != MISSING)
249 >            if (it.next() != MISSING)
250                  ++sum;
251          }
252          timer.finish();
253 <        reallyAssert (sum == size);
253 >        reallyAssert(sum == size);
254      }
255  
256      static void ittest2(NavigableMap s, int size) {
257          int sum = 0;
258          timer.start("Iter Value             ", size);
259          for (Iterator it = s.values().iterator(); it.hasNext(); ) {
260 <            if(it.next() != MISSING)
260 >            if (it.next() != MISSING)
261                  ++sum;
262          }
263          timer.finish();
264 <        reallyAssert (sum == size);
264 >        reallyAssert(sum == size);
265      }
266      static void ittest3(NavigableMap s, int size) {
267          int sum = 0;
268          timer.start("Iter Entry             ", size);
269          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
270 <            if(it.next() != MISSING)
270 >            if (it.next() != MISSING)
271                  ++sum;
272          }
273          timer.finish();
274 <        reallyAssert (sum == size);
274 >        reallyAssert(sum == size);
275      }
276  
277      static void ittest(NavigableMap s, int size) {
# Line 292 | Line 284 | public class NavigableMapCheck {
284          int sum = 0;
285          timer.start("Desc Iter Key          ", size);
286          for (Iterator it = s.descendingKeySet().iterator(); it.hasNext(); ) {
287 <            if(it.next() != MISSING)
287 >            if (it.next() != MISSING)
288                  ++sum;
289          }
290          timer.finish();
291 <        reallyAssert (sum == size);
291 >        reallyAssert(sum == size);
292      }
293  
302
294      static void rittest(NavigableMap s, int size) {
295          rittest1(s, size);
296          //        rittest2(s, size);
297      }
298  
308
299      static void rtest(NavigableMap s, int size) {
300          timer.start("Remove (iterator)      ", size);
301          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
# Line 324 | Line 314 | public class NavigableMapCheck {
314          timer.finish();
315      }
316  
327
317      static void dtest(NavigableMap s, int size, Integer[] key) {
318          timer.start("Put (putAll)           ", size * 2);
319          NavigableMap s2 = null;
# Line 337 | Line 326 | public class NavigableMapCheck {
326  
327          timer.start("Iter Equals            ", size * 2);
328          boolean eqt = s2.equals(s) && s.equals(s2);
329 <        reallyAssert (eqt);
329 >        reallyAssert(eqt);
330          timer.finish();
331  
332          timer.start("Iter HashCode          ", size * 2);
333          int shc = s.hashCode();
334          int s2hc = s2.hashCode();
335 <        reallyAssert (shc == s2hc);
335 >        reallyAssert(shc == s2hc);
336          timer.finish();
337  
338          timer.start("Put (present)          ", size);
# Line 358 | Line 347 | public class NavigableMapCheck {
347              if (es2.contains(entry)) ++sum;
348          }
349          timer.finish();
350 <        reallyAssert (sum == size);
350 >        reallyAssert(sum == size);
351  
352          t6("Get                    ", size, s2, key, absent);
353  
# Line 366 | Line 355 | public class NavigableMapCheck {
355          s2.put(key[size-1], absent[0]);
356          timer.start("Iter Equals            ", size * 2);
357          eqt = s2.equals(s) && s.equals(s2);
358 <        reallyAssert (!eqt);
358 >        reallyAssert(!eqt);
359          timer.finish();
360  
361          timer.start("Iter HashCode          ", size * 2);
362          int s1h = s.hashCode();
363          int s2h = s2.hashCode();
364 <        reallyAssert (s1h != s2h);
364 >        reallyAssert(s1h != s2h);
365          timer.finish();
366  
367          s2.put(key[size-1], hold);
# Line 383 | Line 372 | public class NavigableMapCheck {
372              es.remove(s2i.next());
373          timer.finish();
374  
375 <        reallyAssert (s.isEmpty());
375 >        reallyAssert(s.isEmpty());
376  
377          timer.start("Clear                  ", size);
378          s2.clear();
379          timer.finish();
380 <        reallyAssert (s2.isEmpty() && s.isEmpty());
380 >        reallyAssert(s2.isEmpty() && s.isEmpty());
381      }
382  
394
395
383      static void test(NavigableMap s, Integer[] key) {
384          int size = key.length;
385  
# Line 434 | Line 421 | public class NavigableMapCheck {
421  
422          static void printStats() {
423              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
424 <                Map.Entry e = (Map.Entry)(it.next());
425 <                Stats stats = ((Stats)(e.getValue()));
424 >                Map.Entry e = (Map.Entry) it.next();
425 >                Stats stats = (Stats) e.getValue();
426                  int n = stats.number;
427                  double t;
428                  if (n > 0)
# Line 454 | Line 441 | public class NavigableMapCheck {
441              startTime = System.currentTimeMillis();
442          }
443  
457
444          String classify() {
445              if (name.startsWith("Get"))
446                  return "Get                    ";
# Line 471 | Line 457 | public class NavigableMapCheck {
457          void finish() {
458              long endTime = System.currentTimeMillis();
459              long time = endTime - startTime;
460 <            double timePerOp = ((double)time)/numOps;
460 >            double timePerOp = (double) time /numOps;
461  
462              Object st = accum.get(name);
463              if (st == null)
# Line 494 | Line 480 | public class NavigableMapCheck {
480                      if (timePerOp < stats.least) stats.least = timePerOp;
481                  }
482              }
497
483          }
499
484      }
485  
486      static class Stats {
# Line 510 | Line 494 | public class NavigableMapCheck {
494  
495      static void shuffle(Integer[] keys) {
496          int size = keys.length;
497 <        for (int i=size; i>1; i--) {
497 >        for (int i = size; i > 1; i--) {
498              int r = rng.nextInt(i);
499              Integer t = keys[i-1];
500              keys[i-1] = keys[r];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines