< prev index next >

test/jdk/java/util/concurrent/tck/RecursiveTaskTest.java

Print this page
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
Reviewed-by: martin, chegar


 208         } catch (Throwable fail) { threadUnexpectedException(fail); }
 209     }
 210 
 211     public static final class FJException extends RuntimeException {
 212         public FJException() { super(); }
 213     }
 214 
 215     /** An invalid return value for Fib. */
 216     static final Integer NoResult = Integer.valueOf(-17);
 217 
 218     /** A simple recursive task for testing. */
 219     final class FibTask extends CheckedRecursiveTask<Integer> {
 220         final int number;
 221         FibTask(int n) { number = n; }
 222         public Integer realCompute() {
 223             int n = number;
 224             if (n <= 1)
 225                 return n;
 226             FibTask f1 = new FibTask(n - 1);
 227             f1.fork();
 228             return (new FibTask(n - 2)).compute() + f1.join();
 229         }
 230 
 231         public void publicSetRawResult(Integer result) {
 232             setRawResult(result);
 233         }
 234     }
 235 
 236     /** A recursive action failing in base case. */
 237     final class FailingFibTask extends RecursiveTask<Integer> {
 238         final int number;
 239         int result;
 240         FailingFibTask(int n) { number = n; }
 241         public Integer compute() {
 242             int n = number;
 243             if (n <= 1)
 244                 throw new FJException();
 245             FailingFibTask f1 = new FailingFibTask(n - 1);
 246             f1.fork();
 247             return (new FibTask(n - 2)).compute() + f1.join();
 248         }
 249     }
 250 
 251     /**
 252      * invoke returns value when task completes normally.
 253      * isCompletedAbnormally and isCancelled return false for normally
 254      * completed tasks. getRawResult of a completed non-null task
 255      * returns value;
 256      */
 257     public void testInvoke() {
 258         RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
 259             public Integer realCompute() {
 260                 FibTask f = new FibTask(8);
 261                 Integer r = f.invoke();
 262                 assertEquals(21, (int) r);
 263                 checkCompletedNormally(f, r);
 264                 return r;
 265             }};
 266         assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
 267     }




 208         } catch (Throwable fail) { threadUnexpectedException(fail); }
 209     }
 210 
 211     public static final class FJException extends RuntimeException {
 212         public FJException() { super(); }
 213     }
 214 
 215     /** An invalid return value for Fib. */
 216     static final Integer NoResult = Integer.valueOf(-17);
 217 
 218     /** A simple recursive task for testing. */
 219     final class FibTask extends CheckedRecursiveTask<Integer> {
 220         final int number;
 221         FibTask(int n) { number = n; }
 222         public Integer realCompute() {
 223             int n = number;
 224             if (n <= 1)
 225                 return n;
 226             FibTask f1 = new FibTask(n - 1);
 227             f1.fork();
 228             return new FibTask(n - 2).compute() + f1.join();
 229         }
 230 
 231         public void publicSetRawResult(Integer result) {
 232             setRawResult(result);
 233         }
 234     }
 235 
 236     /** A recursive action failing in base case. */
 237     final class FailingFibTask extends RecursiveTask<Integer> {
 238         final int number;
 239         int result;
 240         FailingFibTask(int n) { number = n; }
 241         public Integer compute() {
 242             int n = number;
 243             if (n <= 1)
 244                 throw new FJException();
 245             FailingFibTask f1 = new FailingFibTask(n - 1);
 246             f1.fork();
 247             return new FibTask(n - 2).compute() + f1.join();
 248         }
 249     }
 250 
 251     /**
 252      * invoke returns value when task completes normally.
 253      * isCompletedAbnormally and isCancelled return false for normally
 254      * completed tasks. getRawResult of a completed non-null task
 255      * returns value;
 256      */
 257     public void testInvoke() {
 258         RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
 259             public Integer realCompute() {
 260                 FibTask f = new FibTask(8);
 261                 Integer r = f.invoke();
 262                 assertEquals(21, (int) r);
 263                 checkCompletedNormally(f, r);
 264                 return r;
 265             }};
 266         assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
 267     }


< prev index next >