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.3 by dl, Wed Apr 19 15:10:47 2006 UTC vs.
Revision 1.11 by jsr166, Sun Jul 14 22:29:33 2013 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
# Line 32 | Line 32 | public class NavigableMapCheck {
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  
41 <        if (args.length > 1)
41 >        if (args.length > 1)
42              numTests = Integer.parseInt(args[1]);
43  
44 <        if (args.length > 2)
44 >        if (args.length > 2)
45              size = Integer.parseInt(args[2]);
46  
47          System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size);
# Line 52 | Line 52 | public class NavigableMapCheck {
52          absentMask = absentSize - 1;
53          absent = new Integer[absentSize];
54  
55 <        for (int i = 0; i < absentSize; ++i)
55 >        for (int i = 0; i < absentSize; ++i)
56              absent[i] = new Integer(i * 2);
57  
58          Integer[] key = new Integer[size];
59 <        for (int i = 0; i < size; ++i)
59 >        for (int i = 0; i < size; ++i)
60              key[i] = new Integer(i * 2 + 1);
61  
62  
# Line 70 | Line 70 | public class NavigableMapCheck {
70  
71      static NavigableMap newMap(Class cl) {
72          try {
73 <            NavigableMap m = (NavigableMap)cl.newInstance();
73 >            NavigableMap m = (NavigableMap) cl.newInstance();
74              return m;
75 <        } catch(Exception e) {
75 >        } catch (Exception e) {
76              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
77          }
78      }
# Line 95 | Line 95 | public class NavigableMapCheck {
95                  if (s.get(key[i]) != null) ++sum;
96              }
97          }
98 <        timer.finish();
99 <        reallyAssert (sum == expect * iters);
98 >        timer.finish();
99 >        reallyAssert(sum == expect * iters);
100      }
101  
102      static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 105 | Line 105 | public class NavigableMapCheck {
105          for (int i = 0; i < n; i++) {
106              if (s.remove(key[i]) != null) ++sum;
107          }
108 <        timer.finish();
109 <        reallyAssert (sum == expect);
108 >        timer.finish();
109 >        reallyAssert(sum == expect);
110      }
111  
112      static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 115 | Line 115 | public class NavigableMapCheck {
115          for (int i = 0; i < n; i++) {
116              if (s.put(key[i], absent[i & absentMask]) == null) ++sum;
117          }
118 <        timer.finish();
119 <        reallyAssert (sum == expect);
118 >        timer.finish();
119 >        reallyAssert(sum == expect);
120      }
121  
122      static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 125 | Line 125 | public class NavigableMapCheck {
125          for (int i = 0; i < n; i++) {
126              if (s.containsKey(key[i])) ++sum;
127          }
128 <        timer.finish();
129 <        reallyAssert (sum == expect);
128 >        timer.finish();
129 >        reallyAssert(sum == expect);
130      }
131  
132      static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 135 | Line 135 | public class NavigableMapCheck {
135          for (int i = n-2; i >= 0; i-=2) {
136              if (s.remove(key[i]) != null) ++sum;
137          }
138 <        timer.finish();
139 <        reallyAssert (sum == expect);
138 >        timer.finish();
139 >        reallyAssert(sum == expect);
140      }
141  
142      static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 146 | Line 146 | public class NavigableMapCheck {
146              if (s.get(k1[i]) != null) ++sum;
147              if (s.get(k2[i & absentMask]) != null) ++sum;
148          }
149 <        timer.finish();
150 <        reallyAssert (sum == n);
149 >        timer.finish();
150 >        reallyAssert(sum == n);
151      }
152  
153      static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) {
# Line 157 | Line 157 | public class NavigableMapCheck {
157              if (s.containsKey(k1[i])) ++sum;
158              if (s.containsKey(k2[i & absentMask])) ++sum;
159          }
160 <        timer.finish();
161 <        reallyAssert (sum == n);
160 >        timer.finish();
161 >        reallyAssert(sum == n);
162      }
163  
164      static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) {
# Line 167 | Line 167 | public class NavigableMapCheck {
167          for (int i = 0; i < n; i++) {
168              if (s.get(key[i]) != null) ++sum;
169          }
170 <        timer.finish();
171 <        reallyAssert (sum == expect);
170 >        timer.finish();
171 >        reallyAssert(sum == expect);
172      }
173  
174  
# Line 179 | Line 179 | public class NavigableMapCheck {
179          int step = absentSize / iters;
180          for (int i = 0; i < absentSize; i += step)
181              if (s.containsValue(absent[i])) ++sum;
182 <        timer.finish();
183 <        reallyAssert (sum != 0);
182 >        timer.finish();
183 >        reallyAssert(sum != 0);
184      }
185  
186      static void higherTest(NavigableMap s) {
# Line 192 | Line 192 | public class NavigableMapCheck {
192              ++sum;
193              e = s.higherEntry(e.getKey());
194          }
195 <        timer.finish();
196 <        reallyAssert (sum == iters);
195 >        timer.finish();
196 >        reallyAssert(sum == iters);
197      }
198  
199      static void lowerTest(NavigableMap s) {
# Line 205 | Line 205 | public class NavigableMapCheck {
205              ++sum;
206              e = s.higherEntry(e.getKey());
207          }
208 <        timer.finish();
209 <        reallyAssert (sum == iters);
208 >        timer.finish();
209 >        reallyAssert(sum == iters);
210      }
211  
212      static void ceilingTest(NavigableMap s) {
# Line 219 | Line 219 | public class NavigableMapCheck {
219              if (e != null)
220                  ++sum;
221          }
222 <        timer.finish();
223 <        reallyAssert (sum == iters);
222 >        timer.finish();
223 >        reallyAssert(sum == iters);
224      }
225  
226      static void floorTest(NavigableMap s) {
# Line 233 | Line 233 | public class NavigableMapCheck {
233              if (e != null)
234                  ++sum;
235          }
236 <        timer.finish();
237 <        reallyAssert (sum == iters-1);
236 >        timer.finish();
237 >        reallyAssert(sum == iters-1);
238      }
239  
240  
# Line 245 | Line 245 | public class NavigableMapCheck {
245          for (int i = 0; i < size; i++) {
246              if (ks.contains(key[i])) ++sum;
247          }
248 <        timer.finish();
249 <        reallyAssert (sum == size);
248 >        timer.finish();
249 >        reallyAssert(sum == size);
250      }
251  
252  
# Line 254 | Line 254 | public class NavigableMapCheck {
254          int sum = 0;
255          timer.start("Iter Key               ", size);
256          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
257 <            if(it.next() != MISSING)
257 >            if (it.next() != MISSING)
258                  ++sum;
259          }
260 <        timer.finish();
261 <        reallyAssert (sum == size);
260 >        timer.finish();
261 >        reallyAssert(sum == size);
262      }
263  
264      static void ittest2(NavigableMap s, int size) {
265          int sum = 0;
266          timer.start("Iter Value             ", size);
267          for (Iterator it = s.values().iterator(); it.hasNext(); ) {
268 <            if(it.next() != MISSING)
268 >            if (it.next() != MISSING)
269                  ++sum;
270          }
271 <        timer.finish();
272 <        reallyAssert (sum == size);
271 >        timer.finish();
272 >        reallyAssert(sum == size);
273      }
274      static void ittest3(NavigableMap s, int size) {
275          int sum = 0;
276          timer.start("Iter Entry             ", size);
277          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
278 <            if(it.next() != MISSING)
278 >            if (it.next() != MISSING)
279                  ++sum;
280          }
281 <        timer.finish();
282 <        reallyAssert (sum == size);
281 >        timer.finish();
282 >        reallyAssert(sum == size);
283      }
284  
285      static void ittest(NavigableMap s, int size) {
# Line 292 | Line 292 | public class NavigableMapCheck {
292          int sum = 0;
293          timer.start("Desc Iter Key          ", size);
294          for (Iterator it = s.descendingKeySet().iterator(); it.hasNext(); ) {
295 <            if(it.next() != MISSING)
295 >            if (it.next() != MISSING)
296                  ++sum;
297          }
298 <        timer.finish();
299 <        reallyAssert (sum == size);
298 >        timer.finish();
299 >        reallyAssert(sum == size);
300      }
301  
302  
# Line 312 | Line 312 | public class NavigableMapCheck {
312              it.next();
313              it.remove();
314          }
315 <        timer.finish();
315 >        timer.finish();
316      }
317  
318      static void rvtest(NavigableMap s, int size) {
# Line 321 | Line 321 | public class NavigableMapCheck {
321              it.next();
322              it.remove();
323          }
324 <        timer.finish();
324 >        timer.finish();
325      }
326  
327  
# Line 333 | Line 333 | public class NavigableMapCheck {
333              s2.putAll(s);
334          }
335          catch (Exception e) { e.printStackTrace(); return; }
336 <        timer.finish();
337 <    
336 >        timer.finish();
337 >
338          timer.start("Iter Equals            ", size * 2);
339          boolean eqt = s2.equals(s) && s.equals(s2);
340 <        reallyAssert (eqt);
341 <        timer.finish();
340 >        reallyAssert(eqt);
341 >        timer.finish();
342  
343          timer.start("Iter HashCode          ", size * 2);
344          int shc = s.hashCode();
345          int s2hc = s2.hashCode();
346 <        reallyAssert (shc == s2hc);
347 <        timer.finish();
346 >        reallyAssert(shc == s2hc);
347 >        timer.finish();
348  
349          timer.start("Put (present)          ", size);
350          s2.putAll(s);
351 <        timer.finish();
351 >        timer.finish();
352  
353          timer.start("Iter EntrySet contains ", size * 2);
354          Set es2 = s2.entrySet();
# Line 357 | Line 357 | public class NavigableMapCheck {
357              Object entry = i1.next();
358              if (es2.contains(entry)) ++sum;
359          }
360 <        timer.finish();
361 <        reallyAssert (sum == size);
360 >        timer.finish();
361 >        reallyAssert(sum == size);
362  
363          t6("Get                    ", size, s2, key, absent);
364  
# Line 366 | Line 366 | public class NavigableMapCheck {
366          s2.put(key[size-1], absent[0]);
367          timer.start("Iter Equals            ", size * 2);
368          eqt = s2.equals(s) && s.equals(s2);
369 <        reallyAssert (!eqt);
370 <        timer.finish();
369 >        reallyAssert(!eqt);
370 >        timer.finish();
371  
372          timer.start("Iter HashCode          ", size * 2);
373          int s1h = s.hashCode();
374          int s2h = s2.hashCode();
375 <        reallyAssert (s1h != s2h);
376 <        timer.finish();
375 >        reallyAssert(s1h != s2h);
376 >        timer.finish();
377  
378          s2.put(key[size-1], hold);
379          timer.start("Remove (iterator)      ", size * 2);
# Line 381 | Line 381 | public class NavigableMapCheck {
381          Set es = s.entrySet();
382          while (s2i.hasNext())
383              es.remove(s2i.next());
384 <        timer.finish();
384 >        timer.finish();
385  
386 <        reallyAssert (s.isEmpty());
386 >        reallyAssert(s.isEmpty());
387  
388          timer.start("Clear                  ", size);
389          s2.clear();
390 <        timer.finish();
391 <        reallyAssert (s2.isEmpty() && s.isEmpty());
390 >        timer.finish();
391 >        reallyAssert(s2.isEmpty() && s.isEmpty());
392      }
393  
394  
395 <    
395 >
396      static void test(NavigableMap s, Integer[] key) {
397          int size = key.length;
398  
# Line 431 | Line 431 | public class NavigableMapCheck {
431          private String cname;
432  
433          static final java.util.TreeMap accum = new java.util.TreeMap();
434 <    
434 >
435          static void printStats() {
436              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
437 <                Map.Entry e = (Map.Entry)(it.next());
438 <                Stats stats = ((Stats)(e.getValue()));
437 >                Map.Entry e = (Map.Entry) it.next();
438 >                Stats stats = (Stats) e.getValue();
439                  int n = stats.number;
440                  double t;
441 <                if (n > 0)
441 >                if (n > 0)
442                      t = stats.sum / n;
443                  else
444                      t = stats.least;
# Line 446 | Line 446 | public class NavigableMapCheck {
446                  System.out.println(e.getKey() + ": " + nano);
447              }
448          }
449 <    
449 >
450          void start(String name, long numOps) {
451              this.name = name;
452              this.cname = classify();
453              this.numOps = numOps;
454              startTime = System.currentTimeMillis();
455          }
456 <    
456 >
457  
458          String classify() {
459              if (name.startsWith("Get"))
# Line 464 | Line 464 | public class NavigableMapCheck {
464                  return "Remove                 ";
465              else if (name.startsWith("Iter"))
466                  return "Iter                   ";
467 <            else
467 >            else
468                  return null;
469          }
470  
471          void finish() {
472              long endTime = System.currentTimeMillis();
473              long time = endTime - startTime;
474 <            double timePerOp = ((double)time)/numOps;
474 >            double timePerOp = (double) time /numOps;
475  
476              Object st = accum.get(name);
477              if (st == null)
# Line 506 | Line 506 | public class NavigableMapCheck {
506          Stats(double t) { least = t; }
507      }
508  
509 <    static Random rng = new Random();
509 >    static Random rng = new Random(111);
510  
511      static void shuffle(Integer[] keys) {
512          int size = keys.length;
513 <        for (int i=size; i>1; i--) {
513 >        for (int i = size; i > 1; i--) {
514              int r = rng.nextInt(i);
515              Integer t = keys[i-1];
516              keys[i-1] = keys[r];
# Line 519 | Line 519 | public class NavigableMapCheck {
519      }
520  
521   }
522

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines