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

Comparing jsr166/src/test/loops/IntMapCheck.java (file contents):
Revision 1.8 by jsr166, Tue Nov 3 01:04:02 2009 UTC vs.
Revision 1.15 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
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 IntMapCheck {
14      static int absentSize;
# Line 22 | Line 22 | public class IntMapCheck {
22      }
23  
24      public static void main(String[] args) throws Exception {
25 <        Class mapClass = java.util.concurrent.ConcurrentHashMap.class;
25 >        Class<?> mapClass = java.util.concurrent.ConcurrentHashMap.class;
26          int numTests = 50;
27          int size = 75000;
28  
# Line 34 | Line 34 | public class IntMapCheck {
34              }
35          }
36  
37
37          if (args.length > 1)
38              numTests = Integer.parseInt(args[1]);
39  
# Line 68 | Line 67 | public class IntMapCheck {
67  
68          TestTimer.printStats();
69  
71
70          if (doSerializeTest)
71              stest(newMap(mapClass), size);
72      }
73  
74 <    static Map<Integer,Integer> newMap(Class cl) {
74 >    static Map<Integer,Integer> newMap(Class<?> cl) {
75          try {
76 <            Map m = (Map<Integer,Integer>)cl.newInstance();
79 <            return m;
76 >            return (Map<Integer,Integer>) cl.getConstructor().newInstance();
77          } catch (Exception e) {
78              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
79          }
80      }
81  
85
82      static void runTest(Map<Integer,Integer> s, Integer[] key) {
83          int size = key.length;
84          long startTime = System.nanoTime();
# Line 99 | Line 95 | public class IntMapCheck {
95              }
96          }
97          timer.finish();
98 <        reallyAssert (sum == expect * iters);
98 >        reallyAssert(sum == expect * iters);
99      }
100  
101      static void t1Boxed(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 112 | Line 108 | public class IntMapCheck {
108              }
109          }
110          timer.finish();
111 <        reallyAssert (sum == expect * iters);
111 >        reallyAssert(sum == expect * iters);
112      }
113  
118
114      static void t2(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
115          int sum = 0;
116          timer.start(nm, n);
# Line 123 | Line 118 | public class IntMapCheck {
118              if (s.remove(key[i]) != null) ++sum;
119          }
120          timer.finish();
121 <        reallyAssert (sum == expect);
121 >        reallyAssert(sum == expect);
122      }
123  
124      static void t3(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 135 | Line 130 | public class IntMapCheck {
130              if (s.put(k, v) == null) ++sum;
131          }
132          timer.finish();
133 <        reallyAssert (sum == expect);
133 >        reallyAssert(sum == expect);
134      }
135  
136      static void t4(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 145 | Line 140 | public class IntMapCheck {
140              if (s.containsKey(key[i])) ++sum;
141          }
142          timer.finish();
143 <        reallyAssert (sum == expect);
143 >        reallyAssert(sum == expect);
144      }
145  
146      static void t5(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 155 | Line 150 | public class IntMapCheck {
150              if (s.remove(key[i]) != null) ++sum;
151          }
152          timer.finish();
153 <        reallyAssert (sum == expect);
153 >        reallyAssert(sum == expect);
154      }
155  
156      static void t6(String nm, int n, Map<Integer,Integer> s, Integer[] k1, Integer[] k2) {
# Line 166 | Line 161 | public class IntMapCheck {
161              if (s.get(k2[i & absentMask]) != null) ++sum;
162          }
163          timer.finish();
164 <        reallyAssert (sum == n);
164 >        reallyAssert(sum == n);
165      }
166  
167      static void t7(String nm, int n, Map<Integer,Integer> s, Integer[] k1, Integer[] k2) {
# Line 177 | Line 172 | public class IntMapCheck {
172              if (s.containsKey(k2[i & absentMask])) ++sum;
173          }
174          timer.finish();
175 <        reallyAssert (sum == n);
175 >        reallyAssert(sum == n);
176      }
177  
178      static void t8(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) {
# Line 187 | Line 182 | public class IntMapCheck {
182              if (s.get(key[i]) != null) ++sum;
183          }
184          timer.finish();
185 <        reallyAssert (sum == expect);
185 >        reallyAssert(sum == expect);
186      }
187  
193
188      static void t9(Map<Integer,Integer> s) {
189          int sum = 0;
190          int iters = 20;
# Line 199 | Line 193 | public class IntMapCheck {
193          for (int i = 0; i < absentSize; i += step)
194              if (s.containsValue(absent[i])) ++sum;
195          timer.finish();
196 <        reallyAssert (sum != 0);
196 >        reallyAssert(sum != 0);
197      }
198  
205
199      static void ktest(Map<Integer,Integer> s, int size, Integer[] key) {
200          timer.start("ContainsKey            ", size);
201          Set ks = s.keySet();
# Line 211 | Line 204 | public class IntMapCheck {
204              if (ks.contains(key[i])) ++sum;
205          }
206          timer.finish();
207 <        reallyAssert (sum == size);
207 >        reallyAssert(sum == size);
208      }
209  
217
210      static void ittest1(Map<Integer,Integer> s, int size) {
211          int sum = 0;
212          timer.start("Iter Key               ", size);
# Line 225 | Line 217 | public class IntMapCheck {
217          timer.finish();
218          //        if (sum != size)
219          //            System.out.println("iters " + sum + " size " + size);
220 <        reallyAssert (sum == size);
220 >        reallyAssert(sum == size);
221      }
222  
223      static void ittest2(Map<Integer,Integer> s, int size) {
# Line 238 | Line 230 | public class IntMapCheck {
230          timer.finish();
231          //        if (sum != size)
232          //            System.out.println("iters " + sum + " size " + size);
233 <        reallyAssert (sum == size);
233 >        reallyAssert(sum == size);
234      }
235      static void ittest3(Map<Integer,Integer> s, int size) {
236          int sum = 0;
# Line 248 | Line 240 | public class IntMapCheck {
240                  ++sum;
241          }
242          timer.finish();
243 <        reallyAssert (sum == size);
243 >        reallyAssert(sum == size);
244      }
245  
246      static void ittest4(Map<Integer,Integer> s, int size, int pos) {
247          IdentityHashMap seen = new IdentityHashMap(size);
248 <        reallyAssert (s.size() == size);
248 >        reallyAssert(s.size() == size);
249          int sum = 0;
250          timer.start("Iter XEntry            ", size);
251          Iterator it = s.entrySet().iterator();
# Line 267 | Line 259 | public class IntMapCheck {
259              if (v != MISSING)
260                  ++sum;
261          }
262 <        reallyAssert (s.containsKey(k));
262 >        reallyAssert(s.containsKey(k));
263          it.remove();
264 <        reallyAssert (!s.containsKey(k));
264 >        reallyAssert(!s.containsKey(k));
265          while (it.hasNext()) {
266              Map.Entry<Integer,Integer> x = (Map.Entry<Integer,Integer>)(it.next());
267              Integer k2 = x.getKey();
# Line 278 | Line 270 | public class IntMapCheck {
270                  ++sum;
271          }
272  
273 <        reallyAssert (s.size() == size-1);
273 >        reallyAssert(s.size() == size-1);
274          s.put(k, v);
275 <        reallyAssert (seen.size() == size);
275 >        reallyAssert(seen.size() == size);
276          timer.finish();
277 <        reallyAssert (sum == size);
278 <        reallyAssert (s.size() == size);
277 >        reallyAssert(sum == size);
278 >        reallyAssert(s.size() == size);
279      }
280  
289
281      static void ittest(Map<Integer,Integer> s, int size) {
282          for (int i = 0; i < 4; ++i) {
283              ittest1(s, size);
# Line 306 | Line 297 | public class IntMapCheck {
297                  ++sum;
298          }
299          timer.finish();
300 <        reallyAssert (sum == size);
300 >        reallyAssert(sum == size);
301      }
302  
303      static void entest2(Hashtable ht, int size) {
# Line 317 | Line 308 | public class IntMapCheck {
308                  ++sum;
309          }
310          timer.finish();
311 <        reallyAssert (sum == size);
311 >        reallyAssert(sum == size);
312      }
313  
323
314      static void entest3(Hashtable ht, int size) {
315          int sum = 0;
316  
# Line 331 | Line 321 | public class IntMapCheck {
321                  ++sum;
322          }
323          timer.finish();
324 <        reallyAssert (sum == size);
324 >        reallyAssert(sum == size);
325      }
326  
327      static void entest4(Hashtable ht, int size) {
# Line 343 | Line 333 | public class IntMapCheck {
333                  ++sum;
334          }
335          timer.finish();
336 <        reallyAssert (sum == size);
336 >        reallyAssert(sum == size);
337      }
338  
339      static void entest(Map<Integer,Integer> s, int size) {
# Line 396 | Line 386 | public class IntMapCheck {
386          System.out.print(time + "ms");
387  
388          if (s instanceof IdentityHashMap) return;
389 <        reallyAssert (s.equals(m));
389 >        reallyAssert(s.equals(m));
390      }
391  
402
392      static void test(Map<Integer,Integer> s, Integer[] key) {
393          int size = key.length;
394  
# Line 449 | Line 438 | public class IntMapCheck {
438          t3("Put (presized)         ", size, s, key, size);
439  
440          timer.start("Put (putAll)           ", size * 2);
441 <        Map<Integer,Integer> s2 = null;
441 >        final Map<Integer,Integer> s2;
442          try {
443 <            s2 = (Map<Integer,Integer>) (s.getClass().newInstance());
443 >            s2 = (Map<Integer,Integer>)
444 >                s.getClass().getConstructor().newInstance();
445              s2.putAll(s);
446          }
447          catch (Exception e) { e.printStackTrace(); return; }
# Line 459 | Line 449 | public class IntMapCheck {
449  
450          timer.start("Iter Equals            ", size * 2);
451          boolean eqt = s2.equals(s) && s.equals(s2);
452 <        reallyAssert (eqt);
452 >        reallyAssert(eqt);
453          timer.finish();
454  
455          timer.start("Iter HashCode          ", size * 2);
456          int shc = s.hashCode();
457          int s2hc = s2.hashCode();
458 <        reallyAssert (shc == s2hc);
458 >        reallyAssert(shc == s2hc);
459          timer.finish();
460  
461          timer.start("Put (present)          ", size * 2);
# Line 479 | Line 469 | public class IntMapCheck {
469              if (s2.put(me.getKey(), me.getValue()) != null)
470                  ++ipsum;
471          }
472 <        reallyAssert (ipsum == s.size());
472 >        reallyAssert(ipsum == s.size());
473          timer.finish();
474  
475          timer.start("Iter EntrySet contains ", size * 2);
# Line 490 | Line 480 | public class IntMapCheck {
480              if (es2.contains(entry)) ++sum;
481          }
482          timer.finish();
483 <        reallyAssert (sum == size);
483 >        reallyAssert(sum == size);
484  
485          Integer hold = s2.get(key[size-1]);
486          s2.put(key[size-1], absent[0]);
487          timer.start("Iter Equals            ", size * 2);
488          eqt = s2.equals(s) && s.equals(s2);
489 <        reallyAssert (!eqt);
489 >        reallyAssert(!eqt);
490          timer.finish();
491  
492          timer.start("Iter HashCode          ", size * 2);
493          int s1h = s.hashCode();
494          int s2h = s2.hashCode();
495 <        reallyAssert (s1h != s2h);
495 >        reallyAssert(s1h != s2h);
496          timer.finish();
497  
498          s2.put(key[size-1], hold);
# Line 513 | Line 503 | public class IntMapCheck {
503              reallyAssert(es.remove(s2i.next()));
504          timer.finish();
505  
506 <        reallyAssert (s.isEmpty());
506 >        reallyAssert(s.isEmpty());
507  
508          timer.start("Clear                  ", size);
509          s2.clear();
510          timer.finish();
511 <        reallyAssert (s2.isEmpty() && s.isEmpty());
511 >        reallyAssert(s2.isEmpty() && s.isEmpty());
512      }
513  
514      static class TestTimer {
# Line 551 | Line 541 | public class IntMapCheck {
541              startTime = System.nanoTime();
542          }
543  
554
544          String classify() {
545              if (name.startsWith("Get"))
546                  return "Get                    ";
# Line 607 | Line 596 | public class IntMapCheck {
596  
597      static void shuffle(Integer[] keys) {
598          int size = keys.length;
599 <        for (int i=size; i>1; i--) {
599 >        for (int i = size; i > 1; i--) {
600              int r = rng.nextInt(i);
601              Integer t = keys[i-1];
602              keys[i-1] = keys[r];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines