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

Comparing jsr166/src/test/loops/NavigableSetCheck.java (file contents):
Revision 1.2 by dl, Mon May 9 19:33:30 2005 UTC vs.
Revision 1.14 by jsr166, Sun Oct 23 03:03:24 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 set operations
9   */
10 import java.util.*;
10   import java.io.*;
11 + import java.util.*;
12  
13   public class NavigableSetCheck {
14  
# Line 25 | Line 25 | public class NavigableSetCheck {
25      }
26  
27      public static void main(String[] args) throws Exception {
28 <        Class setClass = null;
28 >        Class<?> setClass = null;
29          int numTests = 50;
30          int size = 50000;
31  
32          if (args.length > 0) {
33              try {
34                  setClass = 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)
40 >        if (args.length > 1)
41              numTests = Integer.parseInt(args[1]);
42  
43 <        if (args.length > 2)
43 >        if (args.length > 2)
44              size = Integer.parseInt(args[2]);
45  
46          System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size);
47  
49
48          absentSize = 8;
49          while (absentSize < size) absentSize <<= 1;
50          absentMask = absentSize - 1;
51          absent = new Integer[absentSize];
52  
53 <        for (int i = 0; i < absentSize; ++i)
53 >        for (int i = 0; i < absentSize; ++i)
54              absent[i] = new Integer(i * 2);
55  
56          Integer[] key = new Integer[size];
57 <        for (int i = 0; i < size; ++i)
57 >        for (int i = 0; i < size; ++i)
58              key[i] = new Integer(i * 2 + 1);
59  
62
60          for (int rep = 0; rep < numTests; ++rep) {
61              runTest(newSet(setClass), key);
62          }
63  
64          TestTimer.printStats();
68
65      }
66  
67 <    static NavigableSet newSet(Class cl) {
67 >    static NavigableSet newSet(Class<?> cl) {
68          try {
69 <            NavigableSet m = (NavigableSet)cl.newInstance();
70 <            return m;
75 <        } catch(Exception e) {
69 >            return (NavigableSet) cl.getConstructor().newInstance();
70 >        } catch (Exception e) {
71              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
72          }
73      }
74  
80
75      static void runTest(NavigableSet s, Integer[] key) {
76          shuffle(key);
77          int size = key.length;
# Line 95 | Line 89 | public class NavigableSetCheck {
89                  if (s.contains(key[i])) ++sum;
90              }
91          }
92 <        timer.finish();
93 <        reallyAssert (sum == expect * iters);
92 >        timer.finish();
93 >        reallyAssert(sum == expect * iters);
94      }
95  
96      static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 105 | Line 99 | public class NavigableSetCheck {
99          for (int i = 0; i < n; i++) {
100              if (s.remove(key[i])) ++sum;
101          }
102 <        timer.finish();
103 <        reallyAssert (sum == expect);
102 >        timer.finish();
103 >        reallyAssert(sum == expect);
104      }
105  
106      static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 115 | Line 109 | public class NavigableSetCheck {
109          for (int i = 0; i < n; i++) {
110              if (s.add(key[i])) ++sum;
111          }
112 <        timer.finish();
113 <        reallyAssert (sum == expect);
112 >        timer.finish();
113 >        reallyAssert(sum == expect);
114      }
115  
116      static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 125 | Line 119 | public class NavigableSetCheck {
119          for (int i = 0; i < n; i++) {
120              if (s.contains(key[i])) ++sum;
121          }
122 <        timer.finish();
123 <        reallyAssert (sum == expect);
122 >        timer.finish();
123 >        reallyAssert(sum == expect);
124      }
125  
126      static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 135 | Line 129 | public class NavigableSetCheck {
129          for (int i = n-2; i >= 0; i-=2) {
130              if (s.remove(key[i])) ++sum;
131          }
132 <        timer.finish();
133 <        reallyAssert (sum == expect);
132 >        timer.finish();
133 >        reallyAssert(sum == expect);
134      }
135  
136      static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
# Line 146 | Line 140 | public class NavigableSetCheck {
140              if (s.contains(k1[i])) ++sum;
141              if (s.contains(k2[i & absentMask])) ++sum;
142          }
143 <        timer.finish();
144 <        reallyAssert (sum == n);
143 >        timer.finish();
144 >        reallyAssert(sum == n);
145      }
146  
147      static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
# Line 157 | Line 151 | public class NavigableSetCheck {
151              if (s.contains(k1[i])) ++sum;
152              if (s.contains(k2[i & absentMask])) ++sum;
153          }
154 <        timer.finish();
155 <        reallyAssert (sum == n);
154 >        timer.finish();
155 >        reallyAssert(sum == n);
156      }
157  
158      static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 167 | Line 161 | public class NavigableSetCheck {
161          for (int i = 0; i < n; i++) {
162              if (s.contains(key[i])) ++sum;
163          }
164 <        timer.finish();
165 <        reallyAssert (sum == expect);
164 >        timer.finish();
165 >        reallyAssert(sum == expect);
166      }
167  
174
168      static void higherTest(NavigableSet s) {
169          int sum = 0;
170          int iters = s.size();
# Line 181 | Line 174 | public class NavigableSetCheck {
174              ++sum;
175              e = s.higher(e);
176          }
177 <        timer.finish();
178 <        reallyAssert (sum == iters);
177 >        timer.finish();
178 >        reallyAssert(sum == iters);
179      }
180  
181      static void lowerTest(NavigableSet s) {
# Line 194 | Line 187 | public class NavigableSetCheck {
187              ++sum;
188              e = s.higher(e);
189          }
190 <        timer.finish();
191 <        reallyAssert (sum == iters);
190 >        timer.finish();
191 >        reallyAssert(sum == iters);
192      }
193  
194      static void ceilingTest(NavigableSet s) {
# Line 208 | Line 201 | public class NavigableSetCheck {
201              if (e != null)
202                  ++sum;
203          }
204 <        timer.finish();
205 <        reallyAssert (sum == iters);
204 >        timer.finish();
205 >        reallyAssert(sum == iters);
206      }
207  
208      static void floorTest(NavigableSet s) {
# Line 222 | Line 215 | public class NavigableSetCheck {
215              if (e != null)
216                  ++sum;
217          }
218 <        timer.finish();
219 <        reallyAssert (sum == iters-1);
218 >        timer.finish();
219 >        reallyAssert(sum == iters-1);
220      }
221  
229
222      static void ktest(NavigableSet s, int size, Integer[] key) {
223          timer.start("Contains               ", size);
224          int sum = 0;
225          for (int i = 0; i < size; i++) {
226              if (s.contains(key[i])) ++sum;
227          }
228 <        timer.finish();
229 <        reallyAssert (sum == size);
228 >        timer.finish();
229 >        reallyAssert(sum == size);
230      }
231  
240
232      static void ittest1(NavigableSet s, int size) {
233          int sum = 0;
234          timer.start("Iter Key               ", size);
235          for (Iterator it = s.iterator(); it.hasNext(); ) {
236 <            if(it.next() != MISSING)
236 >            if (it.next() != MISSING)
237                  ++sum;
238          }
239 <        timer.finish();
240 <        reallyAssert (sum == size);
239 >        timer.finish();
240 >        reallyAssert(sum == size);
241      }
242  
243      static void ittest(NavigableSet s, int size) {
# Line 257 | Line 248 | public class NavigableSetCheck {
248          int sum = 0;
249          timer.start("Desc Iter Key          ", size);
250          for (Iterator it = s.descendingIterator(); it.hasNext(); ) {
251 <            if(it.next() != MISSING)
251 >            if (it.next() != MISSING)
252                  ++sum;
253          }
254 <        timer.finish();
255 <        reallyAssert (sum == size);
254 >        timer.finish();
255 >        reallyAssert(sum == size);
256      }
257  
258      static void rittest(NavigableSet s, int size) {
259          rittest1(s, size);
260      }
261  
271
262      static void rtest(NavigableSet s, int size) {
263          timer.start("Remove (iterator)      ", size);
264          for (Iterator it = s.iterator(); it.hasNext(); ) {
265              it.next();
266              it.remove();
267          }
268 <        timer.finish();
268 >        timer.finish();
269      }
270  
281
282
271      static void dtest(NavigableSet s, int size, Integer[] key) {
272          timer.start("Add (addAll)           ", size * 2);
273 <        NavigableSet s2 = null;
273 >        final NavigableSet s2;
274          try {
275 <            s2 = (NavigableSet) (s.getClass().newInstance());
275 >            s2 = (NavigableSet) s.getClass().getConstructor().newInstance();
276              s2.addAll(s);
277          }
278          catch (Exception e) { e.printStackTrace(); return; }
279 <        timer.finish();
280 <    
279 >        timer.finish();
280 >
281          timer.start("Iter Equals            ", size * 2);
282          boolean eqt = s2.equals(s) && s.equals(s2);
283 <        reallyAssert (eqt);
284 <        timer.finish();
283 >        reallyAssert(eqt);
284 >        timer.finish();
285  
286          timer.start("Iter HashCode          ", size * 2);
287          int shc = s.hashCode();
288          int s2hc = s2.hashCode();
289 <        reallyAssert (shc == s2hc);
290 <        timer.finish();
289 >        reallyAssert(shc == s2hc);
290 >        timer.finish();
291  
292          timer.start("Add (present)          ", size);
293          s2.addAll(s);
294 <        timer.finish();
294 >        timer.finish();
295  
296          t6("Contains               ", size, s2, key, absent);
297  
# Line 312 | Line 300 | public class NavigableSetCheck {
300          timer.start("Iter Equals            ", size * 2);
301          eqt = s2.equals(s) && s.equals(s2);
302          if (as2)
303 <            reallyAssert (!eqt);
304 <        timer.finish();
303 >            reallyAssert(!eqt);
304 >        timer.finish();
305  
306          timer.start("Iter HashCode          ", size * 2);
307          int s1h = s.hashCode();
308          int s2h = s2.hashCode();
309          if (as2)
310 <            reallyAssert (s1h != s2h);
311 <        timer.finish();
310 >            reallyAssert(s1h != s2h);
311 >        timer.finish();
312  
313          timer.start("Clear                  ", size);
314          s.clear();
315          s2.clear();
316 <        timer.finish();
317 <        reallyAssert (s2.isEmpty() && s.isEmpty());
316 >        timer.finish();
317 >        reallyAssert(s2.isEmpty() && s.isEmpty());
318      }
319  
332
333    
320      static void test(NavigableSet s, Integer[] key) {
321          int size = key.length;
322  
# Line 368 | Line 354 | public class NavigableSetCheck {
354          private String cname;
355  
356          static final java.util.TreeMap accum = new java.util.TreeMap();
357 <    
357 >
358          static void printStats() {
359              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
360 <                Map.Entry e = (Map.Entry)(it.next());
361 <                Stats stats = ((Stats)(e.getValue()));
360 >                Map.Entry e = (Map.Entry) it.next();
361 >                Stats stats = (Stats) e.getValue();
362                  int n = stats.number;
363                  double t;
364 <                if (n > 0)
364 >                if (n > 0)
365                      t = stats.sum / n;
366                  else
367                      t = stats.least;
# Line 383 | Line 369 | public class NavigableSetCheck {
369                  System.out.println(e.getKey() + ": " + nano);
370              }
371          }
372 <    
372 >
373          void start(String name, long numOps) {
374              this.name = name;
375              this.cname = classify();
376              this.numOps = numOps;
377              startTime = System.currentTimeMillis();
378          }
393    
379  
380          String classify() {
381              if (name.startsWith("Contains"))
# Line 401 | Line 386 | public class NavigableSetCheck {
386                  return "Remove                 ";
387              else if (name.startsWith("Iter"))
388                  return "Iter                   ";
389 <            else
389 >            else
390                  return null;
391          }
392  
393          void finish() {
394              long endTime = System.currentTimeMillis();
395              long time = endTime - startTime;
396 <            double timePerOp = ((double)time)/numOps;
396 >            double timePerOp = (double) time / numOps;
397  
398              Object st = accum.get(name);
399              if (st == null)
# Line 447 | Line 432 | public class NavigableSetCheck {
432  
433      static void shuffle(Integer[] keys) {
434          int size = keys.length;
435 <        for (int i=size; i>1; i--) {
435 >        for (int i = size; i > 1; i--) {
436              int r = rng.nextInt(i);
437              Integer t = keys[i-1];
438              keys[i-1] = keys[r];
# Line 456 | Line 441 | public class NavigableSetCheck {
441      }
442  
443   }
459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines