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

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.29 by dl, Tue Feb 22 01:18:59 2011 UTC vs.
Revision 1.35 by jsr166, Sat Jun 25 06:54:53 2011 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   import junit.framework.*;
# Line 12 | Line 12 | import java.util.concurrent.ForkJoinPool
12   import java.util.concurrent.ForkJoinTask;
13   import java.util.concurrent.ForkJoinWorkerThread;
14   import java.util.concurrent.RecursiveAction;
15 + import java.util.concurrent.ThreadLocalRandom;
16   import java.util.concurrent.TimeUnit;
17   import java.util.concurrent.TimeoutException;
18   import static java.util.concurrent.TimeUnit.SECONDS;
19 + import java.util.Arrays;
20   import java.util.HashSet;
21  
22   public class RecursiveActionTest extends JSR166TestCase {
# Line 267 | Line 269 | public class RecursiveActionTest extends
269                  assertEquals(21, f.result);
270                  checkCompletedNormally(f);
271  
272 <                f.reinitialize();
272 >                f = new FibAction(8);
273                  f.cancel(true);
274                  assertSame(f, f.fork());
275                  myself.interrupt();
# Line 280 | Line 282 | public class RecursiveActionTest extends
282                      checkCancelled(f);
283                  }
284  
285 <                f.reinitialize();
285 >                f = new FibAction(8);
286                  f.completeExceptionally(new FJException());
287                  assertSame(f, f.fork());
288                  myself.interrupt();
# Line 294 | Line 296 | public class RecursiveActionTest extends
296                  }
297  
298                  // test quietlyJoin()
299 <                f.reinitialize();
299 >                f = new FibAction(8);
300                  assertSame(f, f.fork());
301                  myself.interrupt();
302                  assertTrue(myself.isInterrupted());
# Line 303 | Line 305 | public class RecursiveActionTest extends
305                  assertEquals(21, f.result);
306                  checkCompletedNormally(f);
307  
308 <                f.reinitialize();
308 >                f = new FibAction(8);
309                  f.cancel(true);
310                  assertSame(f, f.fork());
311                  myself.interrupt();
# Line 312 | Line 314 | public class RecursiveActionTest extends
314                  Thread.interrupted();
315                  checkCancelled(f);
316  
317 <                f.reinitialize();
317 >                f = new FibAction(8);
318                  f.completeExceptionally(new FJException());
319                  assertSame(f, f.fork());
320                  myself.interrupt();
# Line 490 | Line 492 | public class RecursiveActionTest extends
492          testInvokeOnPool(mainPool(), a);
493      }
494  
493
495      /**
496       * helpQuiesce returns when tasks are complete.
497       * getQueuedTaskCount returns 0 when quiescent
# Line 500 | Line 501 | public class RecursiveActionTest extends
501              public void realCompute() {
502                  FibAction f = new FibAction(8);
503                  assertSame(f, f.fork());
504 <                f.helpQuiesce();
504 >                helpQuiesce();
505                  assertEquals(21, f.result);
506                  assertEquals(0, getQueuedTaskCount());
507                  checkCompletedNormally(f);
# Line 508 | Line 509 | public class RecursiveActionTest extends
509          testInvokeOnPool(mainPool(), a);
510      }
511  
511
512      /**
513       * invoke task throws exception when task completes abnormally
514       */
# Line 778 | Line 778 | public class RecursiveActionTest extends
778          testInvokeOnPool(mainPool, a);
779      }
780  
781
781      /**
782       * setRawResult(null) succeeds
783       */
# Line 792 | Line 791 | public class RecursiveActionTest extends
791      }
792  
793      /**
794 <     * A reinitialized task may be re-invoked
794 >     * A reinitialized normally completed task may be re-invoked
795       */
796      public void testReinitialize() {
797          RecursiveAction a = new CheckedRecursiveAction() {
# Line 812 | Line 811 | public class RecursiveActionTest extends
811      }
812  
813      /**
814 +     * A reinitialized abnormally completed task may be re-invoked
815 +     */
816 +    public void testReinitializeAbnormal() {
817 +        RecursiveAction a = new CheckedRecursiveAction() {
818 +            public void realCompute() {
819 +                FailingFibAction f = new FailingFibAction(8);
820 +                checkNotDone(f);
821 +
822 +                for (int i = 0; i < 3; i++) {
823 +                    try {
824 +                        f.invoke();
825 +                        shouldThrow();
826 +                    } catch (FJException success) {
827 +                        checkCompletedAbnormally(f, success);
828 +                    }
829 +                    f.reinitialize();
830 +                    checkNotDone(f);
831 +                }
832 +            }};
833 +        testInvokeOnPool(mainPool(), a);
834 +    }
835 +
836 +    /**
837       * invoke task throws exception after invoking completeExceptionally
838       */
839      public void testCompleteExceptionally() {
# Line 925 | Line 947 | public class RecursiveActionTest extends
947          testInvokeOnPool(mainPool(), a);
948      }
949  
928
950      /**
951       * invokeAll(tasks) with any null task throws NPE
952       */
# Line 1176 | Line 1197 | public class RecursiveActionTest extends
1197          testInvokeOnPool(asyncSingletonPool(), a);
1198      }
1199  
1200 +    static class SortTask extends RecursiveAction {
1201 +        final long[] array; final int lo, hi;
1202 +        SortTask(long[] array, int lo, int hi) {
1203 +            this.array = array; this.lo = lo; this.hi = hi;
1204 +        }
1205 +        final static int THRESHOLD = 100;
1206 +        protected void compute() {
1207 +            if (hi - lo < THRESHOLD)
1208 +                sequentiallySort(array, lo, hi);
1209 +            else {
1210 +                int mid = (lo + hi) >>> 1;
1211 +                invokeAll(new SortTask(array, lo, mid),
1212 +                          new SortTask(array, mid, hi));
1213 +                merge(array, lo, mid, hi);
1214 +            }
1215 +        }
1216 +        static void sequentiallySort(long[] array, int lo, int hi) {
1217 +            Arrays.sort(array, lo, hi);
1218 +        }
1219 +        static void merge(long[] array, int lo, int mid, int hi) {
1220 +            int n = hi - lo;
1221 +            long[] buf = new long[n];
1222 +            int a = lo, b = mid;
1223 +            for (int i = 0; i < n; i++)
1224 +                buf[i] = (b == hi || (a < mid && array[a] < array[b])) ?
1225 +                    array[a++] : array[b++];
1226 +            System.arraycopy(buf, 0, array, lo, n);
1227 +        }
1228 +    }
1229 +
1230 +    /**
1231 +     * SortTask demo works as advertised
1232 +     */
1233 +    public void testSortTaskDemo() {
1234 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
1235 +        long[] array = new long[1007];
1236 +        for (int i = 0; i < array.length; i++)
1237 +            array[i] = rnd.nextLong();
1238 +        long[] arrayClone = array.clone();
1239 +        testInvokeOnPool(mainPool(),
1240 +                         new SortTask(array, 0, array.length));
1241 +        Arrays.sort(arrayClone);
1242 +        assertTrue(Arrays.equals(array, arrayClone));
1243 +    }
1244   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines