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.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/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          }
# Line 68 | 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  
80
77      static void runTest(NavigableMap s, Integer[] key) {
78          shuffle(key);
79          int size = key.length;
# Line 96 | Line 92 | public class NavigableMapCheck {
92              }
93          }
94          timer.finish();
95 <        reallyAssert (sum == expect * iters);
95 >        reallyAssert(sum == expect * iters);
96      }
97  
98      static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 106 | Line 102 | public class NavigableMapCheck {
102              if (s.remove(key[i]) != null) ++sum;
103          }
104          timer.finish();
105 <        reallyAssert (sum == expect);
105 >        reallyAssert(sum == expect);
106      }
107  
108      static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 116 | Line 112 | public class NavigableMapCheck {
112              if (s.put(key[i], absent[i & absentMask]) == null) ++sum;
113          }
114          timer.finish();
115 <        reallyAssert (sum == expect);
115 >        reallyAssert(sum == expect);
116      }
117  
118      static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 126 | Line 122 | public class NavigableMapCheck {
122              if (s.containsKey(key[i])) ++sum;
123          }
124          timer.finish();
125 <        reallyAssert (sum == expect);
125 >        reallyAssert(sum == expect);
126      }
127  
128      static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 136 | Line 132 | public class NavigableMapCheck {
132              if (s.remove(key[i]) != null) ++sum;
133          }
134          timer.finish();
135 <        reallyAssert (sum == expect);
135 >        reallyAssert(sum == expect);
136      }
137  
138      static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 147 | Line 143 | public class NavigableMapCheck {
143              if (s.get(k2[i & absentMask]) != null) ++sum;
144          }
145          timer.finish();
146 <        reallyAssert (sum == n);
146 >        reallyAssert(sum == n);
147      }
148  
149      static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 158 | Line 154 | public class NavigableMapCheck {
154              if (s.containsKey(k2[i & absentMask])) ++sum;
155          }
156          timer.finish();
157 <        reallyAssert (sum == n);
157 >        reallyAssert(sum == n);
158      }
159  
160      static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 168 | Line 164 | public class NavigableMapCheck {
164              if (s.get(key[i]) != null) ++sum;
165          }
166          timer.finish();
167 <        reallyAssert (sum == expect);
167 >        reallyAssert(sum == expect);
168      }
169  
174
170      static void t9(NavigableMap s) {
171          int sum = 0;
172          int iters = 20;
# Line 180 | Line 175 | public class NavigableMapCheck {
175          for (int i = 0; i < absentSize; i += step)
176              if (s.containsValue(absent[i])) ++sum;
177          timer.finish();
178 <        reallyAssert (sum != 0);
178 >        reallyAssert(sum != 0);
179      }
180  
181      static void higherTest(NavigableMap s) {
# Line 193 | Line 188 | public class NavigableMapCheck {
188              e = s.higherEntry(e.getKey());
189          }
190          timer.finish();
191 <        reallyAssert (sum == iters);
191 >        reallyAssert(sum == iters);
192      }
193  
194      static void lowerTest(NavigableMap s) {
# Line 206 | Line 201 | public class NavigableMapCheck {
201              e = s.higherEntry(e.getKey());
202          }
203          timer.finish();
204 <        reallyAssert (sum == iters);
204 >        reallyAssert(sum == iters);
205      }
206  
207      static void ceilingTest(NavigableMap s) {
# Line 220 | Line 215 | public class NavigableMapCheck {
215                  ++sum;
216          }
217          timer.finish();
218 <        reallyAssert (sum == iters);
218 >        reallyAssert(sum == iters);
219      }
220  
221      static void floorTest(NavigableMap s) {
# Line 234 | Line 229 | public class NavigableMapCheck {
229                  ++sum;
230          }
231          timer.finish();
232 <        reallyAssert (sum == iters-1);
232 >        reallyAssert(sum == iters-1);
233      }
234  
240
235      static void ktest(NavigableMap s, int size, Integer[] key) {
236          timer.start("ContainsKey            ", size);
237          Set ks = s.keySet();
# Line 246 | Line 240 | public class NavigableMapCheck {
240              if (ks.contains(key[i])) ++sum;
241          }
242          timer.finish();
243 <        reallyAssert (sum == size);
243 >        reallyAssert(sum == size);
244      }
245  
252
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);
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);
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);
275 >        reallyAssert(sum == size);
276      }
277  
278      static void ittest(NavigableMap s, int size) {
# Line 292 | 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)
288 >            if (it.next() != MISSING)
289                  ++sum;
290          }
291          timer.finish();
292 <        reallyAssert (sum == size);
292 >        reallyAssert(sum == size);
293      }
294  
302
295      static void rittest(NavigableMap s, int size) {
296          rittest1(s, size);
297          //        rittest2(s, size);
298      }
299  
308
300      static void rtest(NavigableMap s, int size) {
301          timer.start("Remove (iterator)      ", size);
302          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
# Line 324 | Line 315 | public class NavigableMapCheck {
315          timer.finish();
316      }
317  
327
318      static void dtest(NavigableMap s, int size, Integer[] key) {
319          timer.start("Put (putAll)           ", size * 2);
320          NavigableMap s2 = null;
# Line 337 | Line 327 | public class NavigableMapCheck {
327  
328          timer.start("Iter Equals            ", size * 2);
329          boolean eqt = s2.equals(s) && s.equals(s2);
330 <        reallyAssert (eqt);
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);
336 >        reallyAssert(shc == s2hc);
337          timer.finish();
338  
339          timer.start("Put (present)          ", size);
# Line 358 | Line 348 | public class NavigableMapCheck {
348              if (es2.contains(entry)) ++sum;
349          }
350          timer.finish();
351 <        reallyAssert (sum == size);
351 >        reallyAssert(sum == size);
352  
353          t6("Get                    ", size, s2, key, absent);
354  
# Line 366 | 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);
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);
365 >        reallyAssert(s1h != s2h);
366          timer.finish();
367  
368          s2.put(key[size-1], hold);
# Line 383 | Line 373 | public class NavigableMapCheck {
373              es.remove(s2i.next());
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());
381 >        reallyAssert(s2.isEmpty() && s.isEmpty());
382      }
383  
394
395
384      static void test(NavigableMap s, Integer[] key) {
385          int size = key.length;
386  
# Line 434 | Line 422 | public class NavigableMapCheck {
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)
# Line 454 | Line 442 | public class NavigableMapCheck {
442              startTime = System.currentTimeMillis();
443          }
444  
457
445          String classify() {
446              if (name.startsWith("Get"))
447                  return "Get                    ";
# Line 471 | Line 458 | public class NavigableMapCheck {
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 510 | Line 497 | public class NavigableMapCheck {
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];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines