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

Comparing jsr166/src/test/tck/CountedCompleterTest.java (file contents):
Revision 1.1 by dl, Thu Mar 21 00:27:10 2013 UTC vs.
Revision 1.4 by jsr166, Fri Mar 22 16:29:42 2013 UTC

# Line 33 | Line 33 | public class CountedCompleterTest extend
33          Math.max(2, Runtime.getRuntime().availableProcessors());
34  
35      /**
36 <     * Analog of CheckedRunnable for CountedCompleter
36 >     * Analog of CheckedRunnable for ForkJoinTasks
37       */
38      public abstract class CheckedFJTask extends RecursiveAction {
39          protected abstract void realCompute() throws Throwable;
# Line 217 | Line 217 | public class CountedCompleterTest extend
217          FJException() { super(); }
218      }
219  
220    static abstract class FailingCCF extends CountedCompleter {
221        int number;
222        int rnumber;
223
224        public FailingCCF(CountedCompleter parent, int n) {
225            super(parent, 1);
226            this.number = n;
227        }
228
229        public final void compute() {
230            CountedCompleter p;
231            FailingCCF f = this;
232            int n = number;
233            while (n >= 2) {
234                new RFCCF(f, n - 2).fork();
235                f = new LFCCF(f, --n);
236            }
237            f.number = n;
238            f.onCompletion(f);
239            if ((p = f.getCompleter()) != null)
240                p.tryComplete();
241            else
242                f.quietlyComplete();
243        }
244    }
245
246    static final class LFCCF extends FailingCCF {
247        public LFCCF(CountedCompleter parent, int n) {
248            super(parent, n);
249        }
250        public final void onCompletion(CountedCompleter caller) {
251            FailingCCF p = (FailingCCF)getCompleter();
252            int n = number + rnumber;
253            if (p != null)
254                p.number = n;
255            else
256                number = n;
257        }
258    }
259    static final class RFCCF extends FailingCCF {
260        public RFCCF(CountedCompleter parent, int n) {
261            super(parent, n);
262        }
263        public final void onCompletion(CountedCompleter caller) {
264            completeExceptionally(new FJException());
265        }
266    }
267
268    static abstract class CCF extends CountedCompleter {
269        int number;
270        int rnumber;
271
272        public CCF(CountedCompleter parent, int n) {
273            super(parent, 1);
274            this.number = n;
275        }
276
277        public final void compute() {
278            CountedCompleter p;
279            CCF f = this;
280            int n = number;
281            while (n >= 2) {
282                new RCCF(f, n - 2).fork();
283                f = new LCCF(f, --n);
284            }
285            f.number = n;
286            f.onCompletion(f);
287            if ((p = f.getCompleter()) != null)
288                p.tryComplete();
289            else
290                f.quietlyComplete();
291        }
292    }
293
294    static final class LCCF extends CCF {
295        public LCCF(CountedCompleter parent, int n) {
296            super(parent, n);
297        }
298        public final void onCompletion(CountedCompleter caller) {
299            CCF p = (CCF)getCompleter();
300            int n = number + rnumber;
301            if (p != null)
302                p.number = n;
303            else
304                number = n;
305        }
306    }
307    static final class RCCF extends CCF {
308        public RCCF(CountedCompleter parent, int n) {
309            super(parent, n);
310        }
311        public final void onCompletion(CountedCompleter caller) {
312            CCF p = (CCF)getCompleter();
313            int n = number + rnumber;
314            if (p != null)
315                p.rnumber = n;
316            else
317                number = n;
318        }
319    }
320
220      static final class NoopCountedCompleter extends CountedCompleter {
221          boolean post; // set true if onCompletion called
222          NoopCountedCompleter() { super(); }
# Line 329 | Line 228 | public class CountedCompleterTest extend
228      }
229  
230      /**
231 <     * A newly constructed CountedCompleter is not completed;
231 >     * A newly constructed CountedCompleter is not completed;
232       * complete() causes completion.
233       */
234      public void testComplete() {
# Line 421 | Line 320 | public class CountedCompleterTest extend
320          CountedCompleter b = new NoopCountedCompleter(a);
321          assertEquals(a, b.getCompleter());
322      }
323 <    
323 >
324      /**
325       * getRoot returns self if no parent, else parent's root
326       */
# Line 431 | Line 330 | public class CountedCompleterTest extend
330          assertEquals(a, a.getRoot());
331          assertEquals(a, b.getRoot());
332      }
333 <              
333 >
334      /**
335       * tryComplete causes completion if pending count is zero
336       */
# Line 444 | Line 343 | public class CountedCompleterTest extend
343      }
344  
345      /**
346 <     * propagateCompletion causes completion without invokein
346 >     * propagateCompletion causes completion without invoking
347       * onCompletion if pending count is zero
348       */
349      public void testPropagateCompletion() {
# Line 456 | Line 355 | public class CountedCompleterTest extend
355      }
356  
357      /**
358 <     * tryComplete decrments pending count unless zero
358 >     * tryComplete decrements pending count unless zero
359       */
360      public void testTryComplete2() {
361          NoopCountedCompleter a = new NoopCountedCompleter();
# Line 511 | Line 410 | public class CountedCompleterTest extend
410          assertTrue(a.isDone());
411          assertFalse(b.isDone());
412      }
413 <    
413 >
414 >    // Invocation tests use some interdependent task classes
415 >    // to better test propagation etc
416 >
417 >
418 >    // Version of Fibonacci with different classes for left vs right forks
419 >    abstract static class CCF extends CountedCompleter {
420 >        int number;
421 >        int rnumber;
422 >
423 >        public CCF(CountedCompleter parent, int n) {
424 >            super(parent, 1);
425 >            this.number = n;
426 >        }
427 >
428 >        public final void compute() {
429 >            CountedCompleter p;
430 >            CCF f = this;
431 >            int n = number;
432 >            while (n >= 2) {
433 >                new RCCF(f, n - 2).fork();
434 >                f = new LCCF(f, --n);
435 >            }
436 >            f.number = n;
437 >            f.onCompletion(f);
438 >            if ((p = f.getCompleter()) != null)
439 >                p.tryComplete();
440 >            else
441 >                f.quietlyComplete();
442 >        }
443 >    }
444 >
445 >    static final class LCCF extends CCF {
446 >        public LCCF(CountedCompleter parent, int n) {
447 >            super(parent, n);
448 >        }
449 >        public final void onCompletion(CountedCompleter caller) {
450 >            CCF p = (CCF)getCompleter();
451 >            int n = number + rnumber;
452 >            if (p != null)
453 >                p.number = n;
454 >            else
455 >                number = n;
456 >        }
457 >    }
458 >    static final class RCCF extends CCF {
459 >        public RCCF(CountedCompleter parent, int n) {
460 >            super(parent, n);
461 >        }
462 >        public final void onCompletion(CountedCompleter caller) {
463 >            CCF p = (CCF)getCompleter();
464 >            int n = number + rnumber;
465 >            if (p != null)
466 >                p.rnumber = n;
467 >            else
468 >                number = n;
469 >        }
470 >    }
471 >
472 >    // Version of CCF with forced failure in left completions
473 >    abstract static class FailingCCF extends CountedCompleter {
474 >        int number;
475 >        int rnumber;
476 >
477 >        public FailingCCF(CountedCompleter parent, int n) {
478 >            super(parent, 1);
479 >            this.number = n;
480 >        }
481 >
482 >        public final void compute() {
483 >            CountedCompleter p;
484 >            FailingCCF f = this;
485 >            int n = number;
486 >            while (n >= 2) {
487 >                new RFCCF(f, n - 2).fork();
488 >                f = new LFCCF(f, --n);
489 >            }
490 >            f.number = n;
491 >            f.onCompletion(f);
492 >            if ((p = f.getCompleter()) != null)
493 >                p.tryComplete();
494 >            else
495 >                f.quietlyComplete();
496 >        }
497 >    }
498 >
499 >    static final class LFCCF extends FailingCCF {
500 >        public LFCCF(CountedCompleter parent, int n) {
501 >            super(parent, n);
502 >        }
503 >        public final void onCompletion(CountedCompleter caller) {
504 >            FailingCCF p = (FailingCCF)getCompleter();
505 >            int n = number + rnumber;
506 >            if (p != null)
507 >                p.number = n;
508 >            else
509 >                number = n;
510 >        }
511 >    }
512 >    static final class RFCCF extends FailingCCF {
513 >        public RFCCF(CountedCompleter parent, int n) {
514 >            super(parent, n);
515 >        }
516 >        public final void onCompletion(CountedCompleter caller) {
517 >            completeExceptionally(new FJException());
518 >        }
519 >    }
520 >
521      /**
522       * invoke returns when task completes normally.
523       * isCompletedAbnormally and isCancelled return false for normally

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines