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.13 by jsr166, Mon Aug 10 03:13:33 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 <
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();
69 >            NavigableSet m = (NavigableSet) cl.newInstance();
70              return m;
71 <        } catch(Exception e) {
71 >        } catch (Exception e) {
72              throw new RuntimeException("Can't instantiate " + cl + ": " + e);
73          }
74      }
75  
80
76      static void runTest(NavigableSet s, Integer[] key) {
77          shuffle(key);
78          int size = key.length;
# Line 95 | Line 90 | public class NavigableSetCheck {
90                  if (s.contains(key[i])) ++sum;
91              }
92          }
93 <        timer.finish();
94 <        reallyAssert (sum == expect * iters);
93 >        timer.finish();
94 >        reallyAssert(sum == expect * iters);
95      }
96  
97      static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 105 | Line 100 | public class NavigableSetCheck {
100          for (int i = 0; i < n; i++) {
101              if (s.remove(key[i])) ++sum;
102          }
103 <        timer.finish();
104 <        reallyAssert (sum == expect);
103 >        timer.finish();
104 >        reallyAssert(sum == expect);
105      }
106  
107      static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 115 | Line 110 | public class NavigableSetCheck {
110          for (int i = 0; i < n; i++) {
111              if (s.add(key[i])) ++sum;
112          }
113 <        timer.finish();
114 <        reallyAssert (sum == expect);
113 >        timer.finish();
114 >        reallyAssert(sum == expect);
115      }
116  
117      static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 125 | Line 120 | public class NavigableSetCheck {
120          for (int i = 0; i < n; i++) {
121              if (s.contains(key[i])) ++sum;
122          }
123 <        timer.finish();
124 <        reallyAssert (sum == expect);
123 >        timer.finish();
124 >        reallyAssert(sum == expect);
125      }
126  
127      static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 135 | Line 130 | public class NavigableSetCheck {
130          for (int i = n-2; i >= 0; i-=2) {
131              if (s.remove(key[i])) ++sum;
132          }
133 <        timer.finish();
134 <        reallyAssert (sum == expect);
133 >        timer.finish();
134 >        reallyAssert(sum == expect);
135      }
136  
137      static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
# Line 146 | Line 141 | public class NavigableSetCheck {
141              if (s.contains(k1[i])) ++sum;
142              if (s.contains(k2[i & absentMask])) ++sum;
143          }
144 <        timer.finish();
145 <        reallyAssert (sum == n);
144 >        timer.finish();
145 >        reallyAssert(sum == n);
146      }
147  
148      static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) {
# Line 157 | Line 152 | public class NavigableSetCheck {
152              if (s.contains(k1[i])) ++sum;
153              if (s.contains(k2[i & absentMask])) ++sum;
154          }
155 <        timer.finish();
156 <        reallyAssert (sum == n);
155 >        timer.finish();
156 >        reallyAssert(sum == n);
157      }
158  
159      static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) {
# Line 167 | Line 162 | public class NavigableSetCheck {
162          for (int i = 0; i < n; i++) {
163              if (s.contains(key[i])) ++sum;
164          }
165 <        timer.finish();
166 <        reallyAssert (sum == expect);
165 >        timer.finish();
166 >        reallyAssert(sum == expect);
167      }
168  
174
169      static void higherTest(NavigableSet s) {
170          int sum = 0;
171          int iters = s.size();
# Line 181 | Line 175 | public class NavigableSetCheck {
175              ++sum;
176              e = s.higher(e);
177          }
178 <        timer.finish();
179 <        reallyAssert (sum == iters);
178 >        timer.finish();
179 >        reallyAssert(sum == iters);
180      }
181  
182      static void lowerTest(NavigableSet s) {
# Line 194 | Line 188 | public class NavigableSetCheck {
188              ++sum;
189              e = s.higher(e);
190          }
191 <        timer.finish();
192 <        reallyAssert (sum == iters);
191 >        timer.finish();
192 >        reallyAssert(sum == iters);
193      }
194  
195      static void ceilingTest(NavigableSet s) {
# Line 208 | Line 202 | public class NavigableSetCheck {
202              if (e != null)
203                  ++sum;
204          }
205 <        timer.finish();
206 <        reallyAssert (sum == iters);
205 >        timer.finish();
206 >        reallyAssert(sum == iters);
207      }
208  
209      static void floorTest(NavigableSet s) {
# Line 222 | Line 216 | public class NavigableSetCheck {
216              if (e != null)
217                  ++sum;
218          }
219 <        timer.finish();
220 <        reallyAssert (sum == iters-1);
219 >        timer.finish();
220 >        reallyAssert(sum == iters-1);
221      }
222  
229
223      static void ktest(NavigableSet s, int size, Integer[] key) {
224          timer.start("Contains               ", size);
225          int sum = 0;
226          for (int i = 0; i < size; i++) {
227              if (s.contains(key[i])) ++sum;
228          }
229 <        timer.finish();
230 <        reallyAssert (sum == size);
229 >        timer.finish();
230 >        reallyAssert(sum == size);
231      }
232  
240
233      static void ittest1(NavigableSet s, int size) {
234          int sum = 0;
235          timer.start("Iter Key               ", size);
236          for (Iterator it = s.iterator(); it.hasNext(); ) {
237 <            if(it.next() != MISSING)
237 >            if (it.next() != MISSING)
238                  ++sum;
239          }
240 <        timer.finish();
241 <        reallyAssert (sum == size);
240 >        timer.finish();
241 >        reallyAssert(sum == size);
242      }
243  
244      static void ittest(NavigableSet s, int size) {
# Line 257 | Line 249 | public class NavigableSetCheck {
249          int sum = 0;
250          timer.start("Desc Iter Key          ", size);
251          for (Iterator it = s.descendingIterator(); it.hasNext(); ) {
252 <            if(it.next() != MISSING)
252 >            if (it.next() != MISSING)
253                  ++sum;
254          }
255 <        timer.finish();
256 <        reallyAssert (sum == size);
255 >        timer.finish();
256 >        reallyAssert(sum == size);
257      }
258  
259      static void rittest(NavigableSet s, int size) {
260          rittest1(s, size);
261      }
262  
271
263      static void rtest(NavigableSet s, int size) {
264          timer.start("Remove (iterator)      ", size);
265          for (Iterator it = s.iterator(); it.hasNext(); ) {
266              it.next();
267              it.remove();
268          }
269 <        timer.finish();
269 >        timer.finish();
270      }
271  
281
282
272      static void dtest(NavigableSet s, int size, Integer[] key) {
273          timer.start("Add (addAll)           ", size * 2);
274          NavigableSet s2 = null;
# Line 288 | Line 277 | public class NavigableSetCheck {
277              s2.addAll(s);
278          }
279          catch (Exception e) { e.printStackTrace(); return; }
280 <        timer.finish();
281 <    
280 >        timer.finish();
281 >
282          timer.start("Iter Equals            ", size * 2);
283          boolean eqt = s2.equals(s) && s.equals(s2);
284 <        reallyAssert (eqt);
285 <        timer.finish();
284 >        reallyAssert(eqt);
285 >        timer.finish();
286  
287          timer.start("Iter HashCode          ", size * 2);
288          int shc = s.hashCode();
289          int s2hc = s2.hashCode();
290 <        reallyAssert (shc == s2hc);
291 <        timer.finish();
290 >        reallyAssert(shc == s2hc);
291 >        timer.finish();
292  
293          timer.start("Add (present)          ", size);
294          s2.addAll(s);
295 <        timer.finish();
295 >        timer.finish();
296  
297          t6("Contains               ", size, s2, key, absent);
298  
# Line 312 | Line 301 | public class NavigableSetCheck {
301          timer.start("Iter Equals            ", size * 2);
302          eqt = s2.equals(s) && s.equals(s2);
303          if (as2)
304 <            reallyAssert (!eqt);
305 <        timer.finish();
304 >            reallyAssert(!eqt);
305 >        timer.finish();
306  
307          timer.start("Iter HashCode          ", size * 2);
308          int s1h = s.hashCode();
309          int s2h = s2.hashCode();
310          if (as2)
311 <            reallyAssert (s1h != s2h);
312 <        timer.finish();
311 >            reallyAssert(s1h != s2h);
312 >        timer.finish();
313  
314          timer.start("Clear                  ", size);
315          s.clear();
316          s2.clear();
317 <        timer.finish();
318 <        reallyAssert (s2.isEmpty() && s.isEmpty());
317 >        timer.finish();
318 >        reallyAssert(s2.isEmpty() && s.isEmpty());
319      }
320  
332
333    
321      static void test(NavigableSet s, Integer[] key) {
322          int size = key.length;
323  
# Line 368 | Line 355 | public class NavigableSetCheck {
355          private String cname;
356  
357          static final java.util.TreeMap accum = new java.util.TreeMap();
358 <    
358 >
359          static void printStats() {
360              for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
361 <                Map.Entry e = (Map.Entry)(it.next());
362 <                Stats stats = ((Stats)(e.getValue()));
361 >                Map.Entry e = (Map.Entry) it.next();
362 >                Stats stats = (Stats) e.getValue();
363                  int n = stats.number;
364                  double t;
365 <                if (n > 0)
365 >                if (n > 0)
366                      t = stats.sum / n;
367                  else
368                      t = stats.least;
# Line 383 | Line 370 | public class NavigableSetCheck {
370                  System.out.println(e.getKey() + ": " + nano);
371              }
372          }
373 <    
373 >
374          void start(String name, long numOps) {
375              this.name = name;
376              this.cname = classify();
377              this.numOps = numOps;
378              startTime = System.currentTimeMillis();
379          }
393    
380  
381          String classify() {
382              if (name.startsWith("Contains"))
# Line 401 | Line 387 | public class NavigableSetCheck {
387                  return "Remove                 ";
388              else if (name.startsWith("Iter"))
389                  return "Iter                   ";
390 <            else
390 >            else
391                  return null;
392          }
393  
394          void finish() {
395              long endTime = System.currentTimeMillis();
396              long time = endTime - startTime;
397 <            double timePerOp = ((double)time)/numOps;
397 >            double timePerOp = (double) time / numOps;
398  
399              Object st = accum.get(name);
400              if (st == null)
# Line 447 | Line 433 | public class NavigableSetCheck {
433  
434      static void shuffle(Integer[] keys) {
435          int size = keys.length;
436 <        for (int i=size; i>1; i--) {
436 >        for (int i = size; i > 1; i--) {
437              int r = rng.nextInt(i);
438              Integer t = keys[i-1];
439              keys[i-1] = keys[r];
# Line 456 | Line 442 | public class NavigableSetCheck {
442      }
443  
444   }
459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines