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.6 by jsr166, Mon Nov 2 23:42:46 2009 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
5 + */
6   /**
7   * @test
8   * @synopsis Times and checks basic map operations
# Line 27 | 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 47 | 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 65 | 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 90 | Line 95 | public class NavigableMapCheck {
95                  if (s.get(key[i]) != null) ++sum;
96              }
97          }
98 <        timer.finish();
98 >        timer.finish();
99          reallyAssert (sum == expect * iters);
100      }
101  
# Line 100 | 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();
108 >        timer.finish();
109          reallyAssert (sum == expect);
110      }
111  
# Line 110 | 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();
118 >        timer.finish();
119          reallyAssert (sum == expect);
120      }
121  
# Line 120 | Line 125 | public class NavigableMapCheck {
125          for (int i = 0; i < n; i++) {
126              if (s.containsKey(key[i])) ++sum;
127          }
128 <        timer.finish();
128 >        timer.finish();
129          reallyAssert (sum == expect);
130      }
131  
# Line 130 | 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();
138 >        timer.finish();
139          reallyAssert (sum == expect);
140      }
141  
# Line 141 | 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();
149 >        timer.finish();
150          reallyAssert (sum == n);
151      }
152  
# Line 152 | Line 157 | public class NavigableMapCheck {
157              if (s.containsKey(k1[i])) ++sum;
158              if (s.containsKey(k2[i & absentMask])) ++sum;
159          }
160 <        timer.finish();
160 >        timer.finish();
161          reallyAssert (sum == n);
162      }
163  
# Line 162 | 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();
170 >        timer.finish();
171          reallyAssert (sum == expect);
172      }
173  
# Line 174 | 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();
182 >        timer.finish();
183          reallyAssert (sum != 0);
184      }
185  
# Line 187 | Line 192 | public class NavigableMapCheck {
192              ++sum;
193              e = s.higherEntry(e.getKey());
194          }
195 <        timer.finish();
195 >        timer.finish();
196          reallyAssert (sum == iters);
197      }
198  
# Line 200 | Line 205 | public class NavigableMapCheck {
205              ++sum;
206              e = s.higherEntry(e.getKey());
207          }
208 <        timer.finish();
208 >        timer.finish();
209          reallyAssert (sum == iters);
210      }
211  
# Line 214 | Line 219 | public class NavigableMapCheck {
219              if (e != null)
220                  ++sum;
221          }
222 <        timer.finish();
222 >        timer.finish();
223          reallyAssert (sum == iters);
224      }
225  
# Line 228 | Line 233 | public class NavigableMapCheck {
233              if (e != null)
234                  ++sum;
235          }
236 <        timer.finish();
236 >        timer.finish();
237          reallyAssert (sum == iters-1);
238      }
239  
# Line 240 | Line 245 | public class NavigableMapCheck {
245          for (int i = 0; i < size; i++) {
246              if (ks.contains(key[i])) ++sum;
247          }
248 <        timer.finish();
248 >        timer.finish();
249          reallyAssert (sum == size);
250      }
251  
# Line 249 | 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();
260 >        timer.finish();
261          reallyAssert (sum == size);
262      }
263  
# Line 260 | Line 265 | public class NavigableMapCheck {
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();
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();
281 >        timer.finish();
282          reallyAssert (sum == size);
283      }
284  
# Line 287 | 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();
298 >        timer.finish();
299          reallyAssert (sum == size);
300      }
301  
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)
302                ++sum;
303        }
304        timer.finish();
305        reallyAssert (sum == size);
306    }
302  
303      static void rittest(NavigableMap s, int size) {
304          rittest1(s, size);
305 <        rittest2(s, size);
305 >        //        rittest2(s, size);
306      }
307  
308  
# Line 317 | 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 326 | Line 321 | public class NavigableMapCheck {
321              it.next();
322              it.remove();
323          }
324 <        timer.finish();
324 >        timer.finish();
325      }
326  
327  
# Line 338 | 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();
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();
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 362 | Line 357 | public class NavigableMapCheck {
357              Object entry = i1.next();
358              if (es2.contains(entry)) ++sum;
359          }
360 <        timer.finish();
360 >        timer.finish();
361          reallyAssert (sum == size);
362  
363          t6("Get                    ", size, s2, key, absent);
# Line 372 | Line 367 | public class NavigableMapCheck {
367          timer.start("Iter Equals            ", size * 2);
368          eqt = s2.equals(s) && s.equals(s2);
369          reallyAssert (!eqt);
370 <        timer.finish();
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();
376 >        timer.finish();
377  
378          s2.put(key[size-1], hold);
379          timer.start("Remove (iterator)      ", size * 2);
# Line 386 | 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());
387  
388          timer.start("Clear                  ", size);
389          s2.clear();
390 <        timer.finish();
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 436 | 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()));
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 451 | 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 469 | 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  
# Line 511 | 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;
# Line 524 | Line 519 | public class NavigableMapCheck {
519      }
520  
521   }
527

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines