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.8 by jsr166, Mon Nov 2 23:42:46 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 12 | Line 12
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 79 | 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 99 | Line 99 | public class MapCheck {
99  
100      static Map newMap() {
101          try {
102 <            return (Map) mapClass.newInstance();
102 >            return (Map) mapClass.getConstructor().newInstance();
103          } catch (Exception e) {
104              throw new RuntimeException("Can't instantiate " + mapClass + ": " + e);
105          }
# Line 143 | Line 143 | public class MapCheck {
143          if (m.get(null) != null) throw new Error();
144      }
145  
146
146      static void getTest(String nm, int n, Map s, Object[] key, int expect) {
147          int sum = 0;
148          timer.start(nm, n);
# Line 153 | Line 152 | public class MapCheck {
152                  ++sum;
153          }
154          timer.finish();
155 <        reallyAssert (sum == expect);
155 >        reallyAssert(sum == expect);
156          checkSum += sum;
157      }
158  
160
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);
168 >        reallyAssert(sum == expect);
169      }
170  
171      static void remTest(String nm, int n, Map s, Object[] key, int expect) {
# Line 177 | Line 175 | public class MapCheck {
175              if (s.remove(key[i]) != null) ++sum;
176          }
177          timer.finish();
178 <        reallyAssert (sum == expect);
178 >        reallyAssert(sum == expect);
179          checkSum += sum;
180      }
181  
# Line 186 | Line 184 | public class MapCheck {
184          timer.start(nm, n);
185          s.clear();
186          timer.finish();
187 <        reallyAssert (s.isEmpty());
187 >        reallyAssert(s.isEmpty());
188      }
189  
190      static void putTest(String nm, int n, Map s, Object[] key, int expect) {
# Line 198 | Line 196 | public class MapCheck {
196              if (v == null) ++sum;
197          }
198          timer.finish();
199 <        reallyAssert (sum == expect);
199 >        reallyAssert(sum == expect);
200          checkSum += sum;
201      }
202  
# Line 209 | Line 207 | public class MapCheck {
207              if (s.containsKey(key[i])) ++sum;
208          }
209          timer.finish();
210 <        reallyAssert (sum == expect);
210 >        reallyAssert(sum == expect);
211          checkSum += sum;
212      }
213  
# Line 219 | 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 230 | Line 228 | public class MapCheck {
228              if (s.remove(key[i]) != null) ++sum;
229          }
230          timer.finish();
231 <        reallyAssert (sum == expect);
231 >        reallyAssert(sum == expect);
232          checkSum += sum;
233      }
234  
# Line 240 | Line 238 | public class MapCheck {
238          timer.start("Traverse key or value  ", size);
239          if (s.containsValue(MISSING)) ++sum;
240          timer.finish();
241 <        reallyAssert (sum == 0);
241 >        reallyAssert(sum == 0);
242          checkSum += sum;
243      }
244  
247
245      static Object kitTest(Map s, int size) {
246          Object last = null;
247          int sum = 0;
# Line 256 | Line 253 | public class MapCheck {
253              last = x;
254          }
255          timer.finish();
256 <        reallyAssert (sum == size);
256 >        reallyAssert(sum == size);
257          checkSum += sum;
258          return last;
259      }
# Line 272 | Line 269 | public class MapCheck {
269              last = x;
270          }
271          timer.finish();
272 <        reallyAssert (sum == size);
272 >        reallyAssert(sum == size);
273          checkSum += sum;
274          return last;
275      }
# Line 289 | Line 286 | public class MapCheck {
286                  ++sum;
287          }
288          timer.finish();
289 <        reallyAssert (sum == size);
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 304 | Line 301 | public class MapCheck {
301              ++sum;
302          }
303          timer.finish();
304 <        reallyAssert (sum == sz);
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 321 | Line 318 | public class MapCheck {
318              ++sum;
319          }
320          timer.finish();
321 <        reallyAssert (sum == sz / 2);
321 >        reallyAssert(sum == sz / 2);
322          checkSum += sum;
323      }
324  
# Line 329 | Line 326 | public class MapCheck {
326          timer.start(nm, n);
327          dst.putAll(src);
328          timer.finish();
329 <        reallyAssert (src.size() == dst.size());
329 >        reallyAssert(src.size() == dst.size());
330      }
331  
332      static void serTest(Map s, int size) throws Exception {
# Line 350 | 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 358 | 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 426 | Line 423 | public class MapCheck {
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()));
# Line 473 | Line 470 | public class MapCheck {
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();
# Line 492 | 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 503 | 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);
505 >        reallyAssert(seen.size() == size);
506          timer.finish();
507 <        reallyAssert (sum == size);
508 <        reallyAssert (s.size() == size);
507 >        reallyAssert(sum == size);
508 >        reallyAssert(s.size() == size);
509      }
510  
514
515
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 610 | 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 621 | Line 616 | public class MapCheck {
616          }
617      }
618  
624
619      static void randomWords(Object[] ws, int origin, int size) {
620          for (int i = origin; i < size; ++i) {
621              int k = 0;
# Line 629 | 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 651 | Line 645 | public class MapCheck {
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 663 | Line 657 | public class MapCheck {
657                  else
658                      s = stats.sum;
659  
660 <                double t = ((double)s) / n;
660 >                double t = ((double) s) / n;
661                  long nano = Math.round(t);
662                  System.out.printf("%6d", + nano);
663                  System.out.println();
# Line 682 | Line 676 | public class MapCheck {
676              if (st == null)
677                  accum.put(name, new Stats(elapsed, numOps));
678              else
679 <                ((Stats)st).addTime(elapsed, numOps);
679 >                ((Stats) st).addTime(elapsed, numOps);
680          }
681  
682      }
# Line 702 | Line 696 | public class MapCheck {
696          }
697      }
698  
705
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 715 | 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));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines