JUnit tests

Krzysztof Dębski

package pfor.sample;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import pfor.CompletionCallback;
import pfor.Merger;
import pfor.PFor;
import pfor.ParallelContext;
import pfor.ProgressCallback;
import pfor.annotations.Merge.MergeType;
import pfor.annotations.Speed.SpeedType;
import pfor.closures.index.IndexAction;
import pfor.closures.index.IndexActionWithArgument;
import pfor.closures.index.IndexTask;

/*
* @author Krzysztof Dębski
*
/
public class PForTests {

ParallelContext context;
PFor pfor;

private void doTestEach(final SpeedType speedType, int start, int end)
        throws InterruptedException, ExecutionException {
    final AtomicInteger atomicInteger = new AtomicInteger(0);
    pfor.each(new IndexAction(speedType) {

        public void perform(int index) throws InterruptedException {
            atomicInteger.addAndGet(index);
        }
    }, start, end).execute();

    Assert.assertEquals(end <= start ? 0 : end * (end - 1) / 2 - start * (start - 1) / 2,
            atomicInteger.intValue());
}

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    context = ParallelContext.newInstance(1);
    pfor = context.getPFor();
}

/**
 * @throws java.lang.Exception
 */
@After
public void tearDown() throws Exception {
    if (context != null) {
        context.shutdown();
        context = null;
        pfor = null;
    }
}

private void testEach(int start, int end) throws InterruptedException, ExecutionException {
    for (final SpeedType speedType : SpeedType.values()) {
        doTestEach(speedType, start, end);
    }
}

@Test
public void testEach10_10() throws InterruptedException, ExecutionException {
    testEach(10, 10);
}

@Test
public void testEach10_100() throws InterruptedException, ExecutionException {

    testEach(10, 100);

}

@Test
public void testEach10_11() throws InterruptedException, ExecutionException {
    testEach(10, 11);
}

@Test
public void testEach11_10() throws InterruptedException, ExecutionException {
    testEach(11, 10);
}

@Test
public void testEachArg() throws InterruptedException, ExecutionException {
    final int endValue = 10;
    final AtomicInteger atomicInteger = new AtomicInteger(0);

    pfor.each(new IndexActionWithArgument<AtomicInteger>() {

        public void perform(int index, AtomicInteger atomicInt) throws InterruptedException {
            atomicInt.addAndGet(index);

        }
    }, atomicInteger, 0, endValue).execute();

    Assert.assertEquals(endValue * (endValue - 1) / 2, atomicInteger.intValue());
}

private void testEachRet(int start, int end) throws InterruptedException, ExecutionException {
    final Integer result = pfor.each(new IndexTask<Integer>() {

        @Override
        public Integer perform(int index) throws InterruptedException {
            return index;

        }
    }, new Merger<Integer>(MergeType.ORDERED) {

        public Integer merge(Integer i1, Integer i2) {
            return i1 + i2;
        }
    }, start, end).execute();

    if (end <= start) {
        Assert.assertNull(result);
    } else {
        Assert.assertEquals(end * (end - 1) / 2 - start * (start - 1) / 2, (int) result);
    }
}

@Test
public void testEachRet0_10() throws InterruptedException, ExecutionException {
    testEachRet(0, 10);
}

@Test
public void testEachRet10_10() throws InterruptedException, ExecutionException {
    testEachRet(10, 10);
}

@Test
public void testEachRet10_11() throws InterruptedException, ExecutionException {
    testEachRet(10, 11);
}

@Test
public void testEachRet11_10() throws InterruptedException, ExecutionException {
    testEachRet(11, 10);
}

private void testEachRetAssyn(int start, int end) throws InterruptedException,
        ExecutionException {
    final Integer result = pfor.each(new IndexTask<Integer>() {

        @Override
        public Integer perform(int index) throws InterruptedException {
            return index;

        }
    }, new Merger<Integer>(MergeType.ORDERED) {

        public Integer merge(Integer i1, Integer i2) {
            return i1 + i2;
        }
    }, start, end).executeAsynchronously(false).get();

    if (end <= start) {
        Assert.assertNull(result);
    } else {
        Assert.assertEquals(end * (end - 1) / 2 - start * (start - 1) / 2, (int) result);
    }
}

@Test
public void testEachRetAssyn0_10() throws InterruptedException, ExecutionException {
    testEachRetAssyn(0, 10);
}

@Test
public void testEachRetAssyn10_10() throws InterruptedException, ExecutionException {
    testEachRetAssyn(10, 10);
}

@Test
public void testEachRetAssyn10_11() throws InterruptedException, ExecutionException {
    testEachRetAssyn(10, 11);
}

@Test
public void testEachRetAssyn11_10() throws InterruptedException, ExecutionException {
    testEachRetAssyn(11, 10);
}

@Test(expected = ExecutionException.class)
public void testException() throws InterruptedException, ExecutionException {
    context.getPFor().each(new IndexAction() {
        public void perform(int index) throws InterruptedException {
            throw new NullPointerException();
        }
    }, 0, 100).executeAsynchronously(false).get();
}

@Test(expected = ExecutionException.class)
public void testExceptionMerge() throws InterruptedException, ExecutionException {
    context.getPFor().each(new IndexTask<Object>() {

        @Override
        public Object perform(int index) throws InterruptedException {
            return new Object();
        }
    }, new Merger<Object>() {

        @Override
        public Object merge(Object arg1, Object arg2) {
            throw new NullPointerException();
        }
    }, 0, 100).execute();
}

@Test(expected = ExecutionException.class)
public void testExceptionRet() throws InterruptedException, ExecutionException {
    context.getPFor().each(new IndexTask<Object>() {

        @Override
        public Object perform(int index) throws InterruptedException {
            throw new NullPointerException();
        }
    }, new Merger<Object>() {

        @Override
        public Object merge(Object arg1, Object arg2) {
            return arg1;
        }
    }, 0, 100).executeAsynchronously(false).get();
}

@Test
public void testMergeWrappers() throws InterruptedException, ExecutionException {
    final AtomicInteger i = new AtomicInteger(0);
    final AtomicInteger j = new AtomicInteger(0);
    final AtomicInteger k = new AtomicInteger(0);
    final int steps = 100;

    final List<Integer> list = pfor.each(new IndexTask<Integer>() {
        public Integer perform(int index) throws Exception {
            i.incrementAndGet();
            return -index;
        }
    }, 0, steps).withCompletionCallback(new CompletionCallback<List<Integer>>() {
        public void onComplete(Future<List<Integer>> future) {
            System.out.println("onComplete");
            Assert.assertNotNull(future);
            try {
                final List<Integer> list = future.get();
                int s = 0;
                for (final int i : list) {
                    s += i;
                }
                k.addAndGet(s);
            } catch (final Exception e) {
                Assert.fail();
            }
        }
    }).withProgressCallback(new ProgressCallback() {
        public void onProgress(int totalStepsCompleted) {
            System.out.println("onProgress");
            j.addAndGet(totalStepsCompleted);
        }
    }).executeAsynchronously(false).get();
    int s = 0;
    for (final int x : list) {
        s += x;
    }
    Thread.sleep(10);
    Assert.assertEquals(i.get(), steps);
    Assert.assertEquals(k.get(), s);
    Assert.assertEquals(-(steps * (steps - 1)) / 2, s);
    Assert.assertTrue(j.get() >= steps);
}

@Test
public void testWrappers() throws InterruptedException, ExecutionException {
    final AtomicInteger i = new AtomicInteger(0);
    final AtomicInteger j = new AtomicInteger(0);
    final AtomicInteger k = new AtomicInteger(0);
    final int steps = 100;

    pfor.each(new IndexAction() {
        public void perform(int index) throws Exception {
            i.incrementAndGet();
        }
    }, 0, steps).withCompletionCallback(new CompletionCallback<Void>() {
        public void onComplete(Future<Void> future) {
            System.out.println("onComplete");
            Assert.assertNotNull(future);
            k.addAndGet(123);
        }
    }).withProgressCallback(new ProgressCallback() {
        public void onProgress(int totalStepsCompleted) {
            System.out.println("onProgress");
            j.addAndGet(totalStepsCompleted);
        }
    }).execute();
    Thread.sleep(10);

    Assert.assertEquals(i.get(), steps);
    Assert.assertEquals(k.get(), 123);
    Assert.assertTrue(j.get() >= steps);
}

}


Related

Wiki: Home

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks