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.3 by jsr166, Thu Oct 29 23:09:07 2009 UTC vs.
Revision 1.12 by jsr166, Thu Jan 15 18:34:19 2015 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
40          if (args.length > 1)
41              numTests = Integer.parseInt(args[1]);
42  
# Line 46 | Line 45 | public class NavigableSetCheck {
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;
# Line 59 | Line 57 | public class NavigableSetCheck {
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          }
# Line 68 | Line 65 | public class NavigableSetCheck {
65  
66      }
67  
68 <    static NavigableSet newSet(Class cl) {
68 >    static NavigableSet newSet(Class<?> cl) {
69          try {
70 <            NavigableSet m = (NavigableSet)cl.newInstance();
70 >            NavigableSet m = (NavigableSet) cl.newInstance();
71              return m;
72 <        } catch(Exception e) {
72 >        } catch (Exception e) {
73              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
74          }
75      }
76  
80
77      static void runTest(NavigableSet s, Integer[] key) {
78          shuffle(key);
79          int size = key.length;
# Line 96 | Line 92 | public class NavigableSetCheck {
92              }
93          }
94          timer.finish();
95 <        reallyAssert (sum == expect * iters);
95 >        reallyAssert(sum == expect * iters);
96      }
97  
98      static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 106 | Line 102 | public class NavigableSetCheck {
102              if (s.remove(key[i])) ++sum;
103          }
104          timer.finish();
105 <        reallyAssert (sum == expect);
105 >        reallyAssert(sum == expect);
106      }
107  
108      static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 116 | Line 112 | public class NavigableSetCheck {
112              if (s.add(key[i])) ++sum;
113          }
114          timer.finish();
115 <        reallyAssert (sum == expect);
115 >        reallyAssert(sum == expect);
116      }
117  
118      static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 126 | Line 122 | public class NavigableSetCheck {
122              if (s.contains(key[i])) ++sum;
123          }
124          timer.finish();
125 <        reallyAssert (sum == expect);
125 >        reallyAssert(sum == expect);
126      }
127  
128      static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 136 | Line 132 | public class NavigableSetCheck {
132              if (s.remove(key[i])) ++sum;
133          }
134          timer.finish();
135 <        reallyAssert (sum == expect);
135 >        reallyAssert(sum == expect);
136      }
137  
138      static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
# Line 147 | Line 143 | public class NavigableSetCheck {
143              if (s.contains(k2[i & absentMask])) ++sum;
144          }
145          timer.finish();
146 <        reallyAssert (sum == n);
146 >        reallyAssert(sum == n);
147      }
148  
149      static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
# Line 158 | Line 154 | public class NavigableSetCheck {
154              if (s.contains(k2[i & absentMask])) ++sum;
155          }
156          timer.finish();
157 <        reallyAssert (sum == n);
157 >        reallyAssert(sum == n);
158      }
159  
160      static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 168 | Line 164 | public class NavigableSetCheck {
164              if (s.contains(key[i])) ++sum;
165          }
166          timer.finish();
167 <        reallyAssert (sum == expect);
167 >        reallyAssert(sum == expect);
168      }
169  
174
170      static void higherTest(NavigableSet s) {
171          int sum = 0;
172          int iters = s.size();
# Line 182 | Line 177 | public class NavigableSetCheck {
177              e = s.higher(e);
178          }
179          timer.finish();
180 <        reallyAssert (sum == iters);
180 >        reallyAssert(sum == iters);
181      }
182  
183      static void lowerTest(NavigableSet s) {
# Line 195 | Line 190 | public class NavigableSetCheck {
190              e = s.higher(e);
191          }
192          timer.finish();
193 <        reallyAssert (sum == iters);
193 >        reallyAssert(sum == iters);
194      }
195  
196      static void ceilingTest(NavigableSet s) {
# Line 209 | Line 204 | public class NavigableSetCheck {
204                  ++sum;
205          }
206          timer.finish();
207 <        reallyAssert (sum == iters);
207 >        reallyAssert(sum == iters);
208      }
209  
210      static void floorTest(NavigableSet s) {
# Line 223 | Line 218 | public class NavigableSetCheck {
218                  ++sum;
219          }
220          timer.finish();
221 <        reallyAssert (sum == iters-1);
221 >        reallyAssert(sum == iters-1);
222      }
223  
229
224      static void ktest(NavigableSet s, int size, Integer[] key) {
225          timer.start("Contains               ", size);
226          int sum = 0;
# Line 234 | Line 228 | public class NavigableSetCheck {
228              if (s.contains(key[i])) ++sum;
229          }
230          timer.finish();
231 <        reallyAssert (sum == size);
231 >        reallyAssert(sum == size);
232      }
233  
240
234      static void ittest1(NavigableSet s, int size) {
235          int sum = 0;
236          timer.start("Iter Key               ", size);
237          for (Iterator it = s.iterator(); it.hasNext(); ) {
238 <            if(it.next() != MISSING)
238 >            if (it.next() != MISSING)
239                  ++sum;
240          }
241          timer.finish();
242 <        reallyAssert (sum == size);
242 >        reallyAssert(sum == size);
243      }
244  
245      static void ittest(NavigableSet s, int size) {
# Line 257 | Line 250 | public class NavigableSetCheck {
250          int sum = 0;
251          timer.start("Desc Iter Key          ", size);
252          for (Iterator it = s.descendingIterator(); it.hasNext(); ) {
253 <            if(it.next() != MISSING)
253 >            if (it.next() != MISSING)
254                  ++sum;
255          }
256          timer.finish();
257 <        reallyAssert (sum == size);
257 >        reallyAssert(sum == size);
258      }
259  
260      static void rittest(NavigableSet s, int size) {
261          rittest1(s, size);
262      }
263  
271
264      static void rtest(NavigableSet s, int size) {
265          timer.start("Remove (iterator)      ", size);
266          for (Iterator it = s.iterator(); it.hasNext(); ) {
# Line 278 | Line 270 | public class NavigableSetCheck {
270          timer.finish();
271      }
272  
281
282
273      static void dtest(NavigableSet s, int size, Integer[] key) {
274          timer.start("Add (addAll)           ", size * 2);
275          NavigableSet s2 = null;
# Line 292 | Line 282 | public class NavigableSetCheck {
282  
283          timer.start("Iter Equals            ", size * 2);
284          boolean eqt = s2.equals(s) && s.equals(s2);
285 <        reallyAssert (eqt);
285 >        reallyAssert(eqt);
286          timer.finish();
287  
288          timer.start("Iter HashCode          ", size * 2);
289          int shc = s.hashCode();
290          int s2hc = s2.hashCode();
291 <        reallyAssert (shc == s2hc);
291 >        reallyAssert(shc == s2hc);
292          timer.finish();
293  
294          timer.start("Add (present)          ", size);
# Line 312 | Line 302 | public class NavigableSetCheck {
302          timer.start("Iter Equals            ", size * 2);
303          eqt = s2.equals(s) && s.equals(s2);
304          if (as2)
305 <            reallyAssert (!eqt);
305 >            reallyAssert(!eqt);
306          timer.finish();
307  
308          timer.start("Iter HashCode          ", size * 2);
309          int s1h = s.hashCode();
310          int s2h = s2.hashCode();
311          if (as2)
312 <            reallyAssert (s1h != s2h);
312 >            reallyAssert(s1h != s2h);
313          timer.finish();
314  
315          timer.start("Clear                  ", size);
316          s.clear();
317          s2.clear();
318          timer.finish();
319 <        reallyAssert (s2.isEmpty() && s.isEmpty());
319 >        reallyAssert(s2.isEmpty() && s.isEmpty());
320      }
321  
332
333
322      static void test(NavigableSet s, Integer[] key) {
323          int size = key.length;
324  
# Line 371 | Line 359 | public class NavigableSetCheck {
359  
360          static void printStats() {
361              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
362 <                Map.Entry e = (Map.Entry)(it.next());
363 <                Stats stats = ((Stats)(e.getValue()));
362 >                Map.Entry e = (Map.Entry) it.next();
363 >                Stats stats = (Stats) e.getValue();
364                  int n = stats.number;
365                  double t;
366                  if (n > 0)
# Line 391 | Line 379 | public class NavigableSetCheck {
379              startTime = System.currentTimeMillis();
380          }
381  
394
382          String classify() {
383              if (name.startsWith("Contains"))
384                  return "Contains               ";
# Line 408 | Line 395 | public class NavigableSetCheck {
395          void finish() {
396              long endTime = System.currentTimeMillis();
397              long time = endTime - startTime;
398 <            double timePerOp = ((double)time)/numOps;
398 >            double timePerOp = (double) time / numOps;
399  
400              Object st = accum.get(name);
401              if (st == null)
# Line 447 | Line 434 | public class NavigableSetCheck {
434  
435      static void shuffle(Integer[] keys) {
436          int size = keys.length;
437 <        for (int i=size; i>1; i--) {
437 >        for (int i = size; i > 1; i--) {
438              int r = rng.nextInt(i);
439              Integer t = keys[i-1];
440              keys[i-1] = keys[r];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines