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

Comparing jsr166/src/test/loops/MapCheck.java (file contents):
Revision 1.4 by dl, Wed Jul 1 12:11:44 2009 UTC vs.
Revision 1.19 by jsr166, Sun Oct 23 03:03:23 2016 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 9 | Line 9
9   *
10   * When run with "s" second arg, this requires file "testwords", which
11   * is best used with real words.  We can't check in this file, but you
12 < * can create one from a real dictonary (1 line per word) and then run
12 > * can create one from a real dictionary (1 line per word) and then run
13   * linux "shuf" to randomize entries.
14   */
15 import java.util.*;
15   import java.io.*;
16 + import java.util.*;
17  
18   public class MapCheck {
19      static final Object MISSING = new Object();
20      static TestTimer timer = new TestTimer();
21 <    static Class eclass;
22 <    static Class mapClass = java.util.concurrent.ConcurrentHashMap.class;
21 >    static Class<?> eclass;
22 >    static Class<?> mapClass = java.util.concurrent.ConcurrentHashMap.class;
23  
24      static final LoopHelpers.SimpleRandom srng = new LoopHelpers.SimpleRandom();
25      static final Random rng = new Random(3152688);
# Line 36 | Line 36 | public class MapCheck {
36  
37          if (args.length == 0)
38              System.out.println("Usage: MapCheck mapclass [int|float|string|object] [trials] [size] [serialtest]");
39 <        
39 >
40          if (args.length > 0) {
41              try {
42                  mapClass = Class.forName(args[0]);
43 <            } catch(ClassNotFoundException e) {
43 >            } catch (ClassNotFoundException e) {
44                  throw new RuntimeException("Class " + args[0] + " not found.");
45              }
46 <        }
46 >        }
47  
48          if (args.length > 1) {
49              String et = args[1].toLowerCase();
# Line 53 | Line 53 | public class MapCheck {
53                  eclass = java.lang.Float.class;
54              else if (et.startsWith("s"))
55                  eclass = java.lang.String.class;
56 +            else if (et.startsWith("d"))
57 +                eclass = java.lang.Double.class;
58          }
59          if (eclass == null)
60              eclass = Object.class;
61  
62 <        if (args.length > 2)
62 >        if (args.length > 2)
63              numTests = Integer.parseInt(args[2]);
64  
65 <        if (args.length > 3)
65 >        if (args.length > 3)
66              size = Integer.parseInt(args[3]);
67  
68          boolean doSerializeTest = args.length > 4;
# Line 77 | Line 79 | public class MapCheck {
79          Object[] absent = new Object[size];
80          initializeKeys(key, absent, size);
81  
82 <        precheck(size,  key, absent);
82 >        precheck(size, key, absent);
83  
84          for (int rep = 0; rep < numTests; ++rep) {
85              mainTest(newMap(), key, absent);
# Line 97 | Line 99 | public class MapCheck {
99  
100      static Map newMap() {
101          try {
102 <            return (Map)mapClass.newInstance();
103 <        } catch(Exception e) {
102 >            return (Map) mapClass.getConstructor().newInstance();
103 >        } catch (Exception e) {
104              throw new RuntimeException("Can't instantiate " + mapClass + ": " + e);
105          }
106      }
# Line 132 | Line 134 | public class MapCheck {
134          try {
135              m.put(null, x);
136              v = m.get(null);
137 <        } catch(NullPointerException npe) {
137 >        } catch (NullPointerException npe) {
138              System.out.println("Map does not allow null keys");
139              return;
140          }
# Line 141 | Line 143 | public class MapCheck {
143          if (m.get(null) != null) throw new Error();
144      }
145  
144
146      static void getTest(String nm, int n, Map s, Object[] key, int expect) {
147          int sum = 0;
148          timer.start(nm, n);
# Line 150 | Line 151 | public class MapCheck {
151              if (v != null && v.getClass() == eclass)
152                  ++sum;
153          }
154 <        timer.finish();
155 <        reallyAssert (sum == expect);
154 >        timer.finish();
155 >        reallyAssert(sum == expect);
156          checkSum += sum;
157      }
158  
158
159      // unused
160      static void getTestBoxed(String nm, int n, Map s, Object[] key, int expect) {
161          int sum = 0;
162          Map<Integer,Integer> intMap = (Map<Integer,Integer>)s;
163          timer.start(nm, n);
164          for (int i = 0; i < n; i++) {
165 <            if ((Integer)(intMap.get(i)) != i) ++sum;
165 >            if (intMap.get(i) != i) ++sum;
166          }
167 <        timer.finish();
168 <        reallyAssert (sum == expect);
167 >        timer.finish();
168 >        reallyAssert(sum == expect);
169      }
170  
171      static void remTest(String nm, int n, Map s, Object[] key, int expect) {
# Line 174 | Line 174 | public class MapCheck {
174          for (int i = 0; i < n; i++) {
175              if (s.remove(key[i]) != null) ++sum;
176          }
177 <        timer.finish();
178 <        reallyAssert (sum == expect);
177 >        timer.finish();
178 >        reallyAssert(sum == expect);
179          checkSum += sum;
180      }
181  
# Line 183 | Line 183 | public class MapCheck {
183          String nm = "Remove Present         ";
184          timer.start(nm, n);
185          s.clear();
186 <        timer.finish();
187 <        reallyAssert (s.isEmpty());
186 >        timer.finish();
187 >        reallyAssert(s.isEmpty());
188      }
189  
190      static void putTest(String nm, int n, Map s, Object[] key, int expect) {
# Line 195 | Line 195 | public class MapCheck {
195              Object v = s.put(k, k);
196              if (v == null) ++sum;
197          }
198 <        timer.finish();
199 <        reallyAssert (sum == expect);
198 >        timer.finish();
199 >        reallyAssert(sum == expect);
200          checkSum += sum;
201      }
202  
# Line 206 | Line 206 | public class MapCheck {
206          for (int i = 0; i < n; i++) {
207              if (s.containsKey(key[i])) ++sum;
208          }
209 <        timer.finish();
210 <        reallyAssert (sum == expect);
209 >        timer.finish();
210 >        reallyAssert(sum == expect);
211          checkSum += sum;
212      }
213  
# Line 217 | Line 217 | public class MapCheck {
217          for (int i = 0; i < n; i++) {
218              if (s.containsKey(key[i])) ++sum;
219          }
220 <        reallyAssert (sum == expect);
220 >        reallyAssert(sum == expect);
221          checkSum += sum;
222      }
223  
# Line 227 | Line 227 | public class MapCheck {
227          for (int i = n-2; i >= 0; i-=2) {
228              if (s.remove(key[i]) != null) ++sum;
229          }
230 <        timer.finish();
231 <        reallyAssert (sum == expect);
230 >        timer.finish();
231 >        reallyAssert(sum == expect);
232          checkSum += sum;
233      }
234  
235      static void valTest(Map s, Object[] key) {
236          int size = s.size();
237          int sum = 0;
238 <        timer.start("Traverse access key/val", size);
238 >        timer.start("Traverse key or value  ", size);
239          if (s.containsValue(MISSING)) ++sum;
240 <        timer.finish();
241 <        reallyAssert (sum == 0);
240 >        timer.finish();
241 >        reallyAssert(sum == 0);
242          checkSum += sum;
243      }
244  
245
245      static Object kitTest(Map s, int size) {
246          Object last = null;
247          int sum = 0;
248 <        timer.start("Traverse access key/val", size);
248 >        timer.start("Traverse key or value  ", size);
249          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
250              Object x = it.next();
251              if (x != last && x != null && x.getClass() == eclass)
252                  ++sum;
253              last = x;
254          }
255 <        timer.finish();
256 <        reallyAssert (sum == size);
255 >        timer.finish();
256 >        reallyAssert(sum == size);
257          checkSum += sum;
258          return last;
259      }
# Line 262 | Line 261 | public class MapCheck {
261      static Object vitTest(Map s, int size) {
262          Object last = null;
263          int sum = 0;
264 <        timer.start("Traverse access key/val", size);
264 >        timer.start("Traverse key or value  ", size);
265          for (Iterator it = s.values().iterator(); it.hasNext(); ) {
266              Object x = it.next();
267              if (x != last && x != null && x.getClass() == eclass)
268                  ++sum;
269              last = x;
270          }
271 <        timer.finish();
272 <        reallyAssert (sum == size);
271 >        timer.finish();
272 >        reallyAssert(sum == size);
273          checkSum += sum;
274          return last;
275      }
276  
277      static void eitTest(Map s, int size) {
278          int sum = 0;
279 <        timer.start("Traverse access entry  ", size);
279 >        timer.start("Traverse entry         ", size);
280          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
281              Map.Entry e = (Map.Entry)it.next();
282              Object k = e.getKey();
# Line 286 | Line 285 | public class MapCheck {
285                  v != null && v.getClass() == eclass)
286                  ++sum;
287          }
288 <        timer.finish();
289 <        reallyAssert (sum == size);
288 >        timer.finish();
289 >        reallyAssert(sum == size);
290          checkSum += sum;
291      }
292  
293      static void itRemTest(Map s, int size) {
294          int sz = s.size();
295 <        reallyAssert (sz == size);
295 >        reallyAssert(sz == size);
296          timer.start("Remove Present         ", size);
297          int sum = 0;
298          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
# Line 301 | Line 300 | public class MapCheck {
300              it.remove();
301              ++sum;
302          }
303 <        timer.finish();
304 <        reallyAssert (sum == sz);
303 >        timer.finish();
304 >        reallyAssert(sum == sz);
305          checkSum += sum;
306      }
307  
308      static void itHalfRemTest(Map s, int size) {
309          int sz = s.size();
310 <        reallyAssert (sz == size);
310 >        reallyAssert(sz == size);
311          timer.start("Remove Present         ", size);
312          int sum = 0;
313          for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
# Line 318 | Line 317 | public class MapCheck {
317                  it.next();
318              ++sum;
319          }
320 <        timer.finish();
321 <        reallyAssert (sum == sz / 2);
320 >        timer.finish();
321 >        reallyAssert(sum == sz / 2);
322          checkSum += sum;
323      }
324  
325      static void putAllTest(String nm, int n, Map src, Map dst) {
326          timer.start(nm, n);
327          dst.putAll(src);
328 <        timer.finish();
329 <        reallyAssert (src.size() == dst.size());
328 >        timer.finish();
329 >        reallyAssert(src.size() == dst.size());
330      }
331  
332      static void serTest(Map s, int size) throws Exception {
333 <        if (!(s instanceof Serializable))
333 >        if (!(s instanceof Serializable))
334              return;
335          System.out.print("Serialize              : ");
336 <      
336 >
337          for (int i = 0; i < size; i++) {
338              s.put(new Integer(i), Boolean.TRUE);
339          }
# Line 348 | Line 347 | public class MapCheck {
347  
348          FileInputStream is = new FileInputStream("MapCheck.dat");
349          ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is));
350 <        Map m = (Map)in.readObject();
350 >        Map m = (Map) in.readObject();
351  
352          long endTime = System.currentTimeMillis();
353          long time = endTime - startTime;
# Line 356 | Line 355 | public class MapCheck {
355          System.out.print(time + "ms");
356  
357          if (s instanceof IdentityHashMap) return;
358 <        reallyAssert (s.equals(m));
358 >        reallyAssert(s.equals(m));
359      }
360  
361      static void mainTest(Map s, Object[] key, Object[] absent) {
# Line 391 | Line 390 | public class MapCheck {
390          kitTest(s, size);
391          vitTest(s, size);
392          eitTest(s, size);
393 <        twoMapTest1(s, key, absent);
393 >        twoMapTest1(s, key, absent);
394          twoMapTest2(s, key, absent);
395      }
396  
# Line 408 | Line 407 | public class MapCheck {
407          putTest("Add    Absent          ", size, s2, key, size * 3 / 4);
408          reallyAssert(s2.size() == size * 2);
409          clrTest(size, s2);
410 <    }        
410 >    }
411  
412      static void twoMapTest2(Map s, Object[] key, Object[] absent) {
413          int size = key.length;
# Line 421 | Line 420 | public class MapCheck {
420          Object hold = s2.get(lastkey);
421          int sum = 0;
422  
423 <        timer.start("Traverse access entry  ", size * 12); // 12 until finish
423 >        timer.start("Traverse entry         ", size * 12); // 12 until finish
424  
425          int sh1 = s.hashCode() - s2.hashCode();
426 <        reallyAssert (sh1 == 0);
426 >        reallyAssert(sh1 == 0);
427  
428          boolean eq1 = s2.equals(s);
429          boolean eq2 = s.equals(s2);
430 <        reallyAssert (eq1 && eq2);
430 >        reallyAssert(eq1 && eq2);
431  
432          Set es2 = s2.entrySet();
433          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
434              Object entry = it.next();
435              if (es2.contains(entry)) ++sum;
436          }
437 <        reallyAssert (sum == size);
437 >        reallyAssert(sum == size);
438  
439          s2.put(lastkey, MISSING);
440  
441          int sh2 = s.hashCode() - s2.hashCode();
442 <        reallyAssert (sh2 != 0);
442 >        reallyAssert(sh2 != 0);
443  
444          eq1 = s2.equals(s);
445          eq2 = s.equals(s2);
446 <        reallyAssert (!eq1 && !eq2);
446 >        reallyAssert(!eq1 && !eq2);
447  
448          sum = 0;
449          for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
450              Map.Entry e = (Map.Entry)it.next();
451              e.setValue(absent[sum++]);
452          }
453 <        reallyAssert (sum == size);
453 >        reallyAssert(sum == size);
454          for (Iterator it = s2.entrySet().iterator(); it.hasNext(); ) {
455              Map.Entry e = (Map.Entry)it.next();
456              e.setValue(s.get(e.getKey()));
457          }
458  
459 <        timer.finish();
459 >        timer.finish();
460  
461          int rmiss = 0;
462          timer.start("Remove Present         ", size * 2);
# Line 467 | Line 466 | public class MapCheck {
466              if (!es.remove(s2i.next()))
467                  ++rmiss;
468          }
469 <        timer.finish();
469 >        timer.finish();
470          reallyAssert(rmiss == 0);
471  
472          clrTest(size, s2);
473 <        reallyAssert (s2.isEmpty() && s.isEmpty());
473 >        reallyAssert(s2.isEmpty() && s.isEmpty());
474      }
475  
476      static void itTest4(Map s, int size, int pos) {
477          IdentityHashMap seen = new IdentityHashMap(size);
478 <        reallyAssert (s.size() == size);
478 >        reallyAssert(s.size() == size);
479          int sum = 0;
480          timer.start("Iter XEntry            ", size);
481 <        Iterator it = s.entrySet().iterator();
481 >        Iterator it = s.entrySet().iterator();
482          Object k = null;
483          Object v = null;
484          for (int i = 0; i < size-pos; ++i) {
# Line 490 | Line 489 | public class MapCheck {
489              if (x != MISSING)
490                  ++sum;
491          }
492 <        reallyAssert (s.containsKey(k));
492 >        reallyAssert(s.containsKey(k));
493          it.remove();
494 <        reallyAssert (!s.containsKey(k));
494 >        reallyAssert(!s.containsKey(k));
495          while (it.hasNext()) {
496              Map.Entry x = (Map.Entry)(it.next());
497              Object k2 = x.getKey();
# Line 501 | Line 500 | public class MapCheck {
500                  ++sum;
501          }
502  
503 <        reallyAssert (s.size() == size-1);
503 >        reallyAssert(s.size() == size-1);
504          s.put(k, v);
505 <        reallyAssert (seen.size() == size);
506 <        timer.finish();
507 <        reallyAssert (sum == size);
508 <        reallyAssert (s.size() == size);
505 >        reallyAssert(seen.size() == size);
506 >        timer.finish();
507 >        reallyAssert(sum == size);
508 >        reallyAssert(s.size() == size);
509      }
510  
512
513
511      static void initializeKeys(Object[] key, Object[] absent, int size) {
512          if (eclass == Object.class) {
513              for (int i = 0; i < size; ++i) key[i] = new Object();
# Line 522 | Line 519 | public class MapCheck {
519          else if (eclass == Float.class) {
520              initFloats(key, absent, size);
521          }
522 +        else if (eclass == Double.class) {
523 +            initDoubles(key, absent, size);
524 +        }
525          else if (eclass == String.class) {
526              initWords(size, key, absent);
527          }
528 <        else
528 >        else
529              throw new Error("unknown type");
530      }
531  
532      static void initInts(Object[] key, Object[] absent, int size) {
533 <        for (int i = 0; i < size; ++i)
533 >        for (int i = 0; i < size; ++i)
534              key[i] = Integer.valueOf(i);
535          Map m = newMap();
536          int k = 0;
# Line 560 | Line 560 | public class MapCheck {
560          }
561      }
562  
563 +    static void initDoubles(Object[] key, Object[] absent, int size) {
564 +        Map m = newMap();
565 +        for (int i = 0; i < size; ++i) {
566 +            double r = Double.valueOf(i);
567 +            key[i] = r;
568 +            m.put(r, r);
569 +        }
570 +        int k = 0;
571 +        while (k < size) {
572 +            double r = rng.nextDouble();
573 +            Double ir = Double.valueOf(r);
574 +            if (m.put(ir, ir) == null)
575 +                absent[k++] = ir;
576 +        }
577 +    }
578 +
579      // Use as many real words as possible, then use fake random words
580  
581      static void initWords(int size, Object[] key, Object[] abs) {
# Line 574 | Line 590 | public class MapCheck {
590                  for (;;) {
591                      int c = in.read();
592                      if (c < 0) {
593 <                        if (ki < size)
593 >                        if (ki < size)
594                              randomWords(key, ki, size);
595                          if (ai < size)
596                              randomWords(abs, ai, size);
# Line 589 | Line 605 | public class MapCheck {
605                              abs[ai++] = s;
606                          break;
607                      }
608 <                    sb.append((char)c);
608 >                    sb.append((char) c);
609                  }
610              }
611              in.close();
# Line 600 | Line 616 | public class MapCheck {
616          }
617      }
618  
603
619      static void randomWords(Object[] ws, int origin, int size) {
620          for (int i = origin; i < size; ++i) {
621              int k = 0;
# Line 608 | Line 623 | public class MapCheck {
623              char[] c = new char[len * 4 + 1];
624              for (int j = 1; j < len; ++j) {
625                  int r = srng.next();
626 <                c[k++] = (char)(' ' + (r & 0x7f));
626 >                c[k++] = (char) (' ' + (r & 0x7f));
627                  r >>>= 8;
628 <                c[k++] = (char)(' ' + (r & 0x7f));
628 >                c[k++] = (char) (' ' + (r & 0x7f));
629                  r >>>= 8;
630 <                c[k++] = (char)(' ' + (r & 0x7f));
630 >                c[k++] = (char) (' ' + (r & 0x7f));
631                  r >>>= 8;
632 <                c[k++] = (char)(' ' + (r & 0x7f));
632 >                c[k++] = (char) (' ' + (r & 0x7f));
633              }
634 <            c[k++] = (char)((i & 31) | 1); // never == to any testword
634 >            c[k++] = (char) ((i & 31) | 1); // never == to any testword
635              ws[i] = new String(c);
636          }
637      }
# Line 627 | Line 642 | public class MapCheck {
642          private long startTime;
643  
644          static final java.util.TreeMap accum = new java.util.TreeMap();
645 <    
645 >
646          static void printStats() {
647              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
648 <                Map.Entry e = (Map.Entry)(it.next());
649 <                Stats stats = ((Stats)(e.getValue()));
648 >                Map.Entry e = (Map.Entry) it.next();
649 >                Stats stats = (Stats) e.getValue();
650                  System.out.print(e.getKey() + ": ");
651                  long s;
652                  long n = stats.number;
# Line 639 | Line 654 | public class MapCheck {
654                      n = stats.firstn;
655                      s = stats.first;
656                  }
657 <                else
657 >                else
658                      s = stats.sum;
659 <                
660 <                double t = ((double)s) / n;
659 >
660 >                double t = ((double) s) / n;
661                  long nano = Math.round(t);
662                  System.out.printf("%6d", + nano);
663                  System.out.println();
664              }
665          }
666 <    
666 >
667          void start(String name, long numOps) {
668              this.name = name;
669              this.numOps = numOps;
# Line 660 | Line 675 | public class MapCheck {
675              Object st = accum.get(name);
676              if (st == null)
677                  accum.put(name, new Stats(elapsed, numOps));
678 <            else
679 <                ((Stats)st).addTime(elapsed, numOps);
678 >            else
679 >                ((Stats) st).addTime(elapsed, numOps);
680          }
681  
682      }
# Line 671 | Line 686 | public class MapCheck {
686          long number;
687          long first;
688          long firstn;
689 <        Stats(long t, long n) {
690 <            first = t;
689 >        Stats(long t, long n) {
690 >            first = t;
691              firstn = n;
692          }
693          void addTime(long t, long n) {
# Line 681 | Line 696 | public class MapCheck {
696          }
697      }
698  
684
699      static void shuffle(Object[] keys) {
700          int size = keys.length;
701 <        for (int i= size; i>1; i--) {
701 >        for (int i = size; i > 1; i--) {
702              int r = rng.nextInt(i);
703              Object t = keys[i-1];
704              keys[i-1] = keys[r];
# Line 694 | Line 708 | public class MapCheck {
708  
709      static void shuffle(ArrayList keys) {
710          int size = keys.size();
711 <        for (int i= size; i>1; i--) {
711 >        for (int i = size; i > 1; i--) {
712              int r = rng.nextInt(i);
713              Object t = keys.get(i-1);
714              keys.set(i-1, keys.get(r));
# Line 703 | Line 717 | public class MapCheck {
717      }
718  
719   }
706

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines