001/*
002 * $Id$
003 */
004
005package edu.jas.kern;
006
007
008import java.io.IOException;
009
010import junit.framework.Test;
011import junit.framework.TestCase;
012import junit.framework.TestSuite;
013import mpi.MPI;
014import mpi.Status;
015
016
017/**
018 * MPJEngine tests with JUnit.
019 * @author Heinz Kredel
020 */
021
022public class MPJEngineTest extends TestCase {
023
024
025    /**
026     * main
027     */
028    public static void main(String[] args) {
029        cmdline = args;
030        junit.textui.TestRunner.run(suite());
031        MPJEngine.terminate();
032    }
033
034
035    static String[] cmdline;
036
037
038    static mpi.Comm engine;
039
040
041    /**
042     * Constructs a <CODE>MPJEngineTest</CODE> object.
043     * @param name String.
044     */
045    public MPJEngineTest(String name) {
046        super(name);
047    }
048
049
050    /**
051     * suite.
052     */
053    public static Test suite() {
054        TestSuite suite = new TestSuite(MPJEngineTest.class);
055        return suite;
056    }
057
058
059    @Override
060    protected void setUp() throws IOException {
061        if (engine == null) {
062            engine = MPJEngine.getCommunicator(cmdline);
063        }
064    }
065
066
067    @Override
068    protected void tearDown() {
069        if (engine == null) {
070            return;
071        }
072        engine = null;
073    }
074
075
076    /**
077     * Test MPJEngine.
078     */
079    public void testMPJEngine() {
080        int me = engine.Rank();
081        int size = engine.Size();
082        assertTrue("size > 0", size > 0);
083        assertTrue("0 <= me < size", 0 <= me && me < size);
084        //System.out.println("testMPJEngine(): Hello World from " + me + " of " + size);
085    }
086
087
088    /**
089     * Test communication.
090     */
091    public void testCommunication() {
092        int me = engine.Rank();
093        int size = engine.Size();
094        int tag = 13;
095        int[] data = new int[5];
096        if (me == 0) {
097            //System.out.println("testCommunication(): from " + me + " of " + size);
098            for (int i = 1; i < size; i++) {
099                data[0] = i;
100                engine.Send(data, 0, data.length, MPI.INT, i, tag);
101            }
102        } else {
103            Status stat = engine.Recv(data, 0, data.length, MPI.INT, 0, tag);
104            int cnt = stat.Get_count(MPI.INT);
105            int elem = stat.Get_elements(MPI.INT);
106            //System.out.println("testCommunication(): status " + me + ", " + cnt + ", " + elem);
107            //System.out.println("testCommunication(): received " + Arrays.toString(data));
108            assertTrue("length == count", data.length == cnt);
109            assertTrue("recv == me", data[0] == me);
110            assertTrue("elem >= 0: " + elem, elem >= 0);
111        }
112        //System.out.println("testCommunication(): done");
113    }
114
115}