001/*
002 * $Id: DistHashTableMPITest.java 4604 2013-08-25 16:59:02Z kredel $
003 */
004
005package edu.jas.util;
006
007
008import java.io.IOException;
009import java.util.Iterator;
010
011import junit.framework.Test;
012import junit.framework.TestCase;
013import junit.framework.TestSuite;
014import mpi.Comm;
015import mpi.MPIException;
016
017import org.apache.log4j.BasicConfigurator;
018
019import edu.jas.kern.MPIEngine;
020
021
022/**
023 * DistHashTableMPI test with JUnit.
024 * @author Heinz Kredel
025 */
026public class DistHashTableMPITest extends TestCase {
027
028
029    protected static Comm engine;
030
031
032    /**
033     * main.
034     */
035    public static void main(String[] args) throws IOException, MPIException {
036        //long t = System.currentTimeMillis();
037        BasicConfigurator.configure();
038        engine = MPIEngine.getCommunicator(args);
039        junit.textui.TestRunner.run(suite());
040        MPIEngine.terminate();
041        //t = System.currentTimeMillis() - t;
042        //System.out.println("MPI runtime = " + t + " milli seconds");
043    }
044
045
046    /**
047     * Constructs a <CODE>DistHashTableMPITest</CODE> object.
048     * @param name String.
049     */
050    public DistHashTableMPITest(String name) {
051        super(name);
052    }
053
054
055    /**
056     * suite.
057     * @return a test suite.
058     */
059    public static Test suite() {
060        TestSuite suite = new TestSuite(DistHashTableMPITest.class);
061        return suite;
062    }
063
064
065    private DistHashTableMPI<Integer, Integer> l1;
066
067
068    private DistHashTableMPI<Integer, Integer> l2;
069
070
071    private DistHashTableMPI<Integer, Integer> l3;
072
073
074    @Override
075    protected void setUp() {
076    }
077
078
079    @Override
080    protected void tearDown() {
081        if (l1 != null)
082            l1.terminate();
083        if (l2 != null)
084            l2.terminate();
085        if (l3 != null)
086            l3.terminate();
087        l1 = l2 = l3 = null;
088        try {
089            //Thread.currentThread();
090            //System.out.println("tearDown: sleep = 1");
091            Thread.sleep(1);
092        } catch (InterruptedException e) {
093        }
094    }
095
096
097    /**
098     * Tests create and terminate DistHashTableMPI.
099     */
100    public void xtestDistHashTable1() throws MPIException, IOException {
101        l1 = new DistHashTableMPI<Integer, Integer>(engine);
102        l1.init();
103        assertTrue("l1==empty", l1.isEmpty());
104    }
105
106
107    /**
108     * Tests if the created DistHashTable has #n objects as content.
109     */
110    public void xtestDistHashTable2() throws MPIException, IOException {
111        int me = engine.Rank();
112        l1 = new DistHashTableMPI<Integer, Integer>(engine);
113        l1.init();
114        assertTrue("l1==empty", l1.isEmpty());
115        Integer s = 0;
116        if (me == 0) {
117            l1.putWait(Integer.valueOf(1), Integer.valueOf(1));
118        } else {
119            s = l1.getWait(Integer.valueOf(1));
120        }
121        assertFalse("l1!=empty: ", l1.isEmpty());
122        assertTrue("#l1==1: " + l1.getList(), l1.size() >= 1);
123        assertEquals("s == 1: ", s, Integer.valueOf(1));
124        if (me == 0) {
125            l1.putWait(Integer.valueOf(2), Integer.valueOf(2));
126        } else {
127            s = l1.getWait(Integer.valueOf(2));
128        }
129        assertTrue("#l1==2: " + l1.getList(), l1.size() >= 2);
130        assertEquals("s == 2: ", s, Integer.valueOf(2));
131        if (me == 0) {
132            l1.putWait(Integer.valueOf(3), Integer.valueOf(3));
133        } else {
134            s = l1.getWait(Integer.valueOf(3));
135        }
136        assertTrue("#l1==3: " + l1.getList(), l1.size() >= 3);
137        assertEquals("s == 3: ", s, Integer.valueOf(3));
138
139        Iterator it = null;
140        it = l1.iterator();
141        int i = 0;
142        while (it.hasNext()) {
143            Object k = it.next();
144            Object o = l1.get(k);
145            Integer x = Integer.valueOf(++i);
146            assertEquals("l1(i)==v(i)", x, o);
147            assertEquals("l1(i)==k(i)", x, k);
148        }
149        l1.clear();
150        assertTrue("#l1==0", l1.size() == 0);
151    }
152
153
154    /**
155     * Tests if the two created DistHashTables have #n objects as content.
156     */
157    public void testDistHashTable3() throws MPIException, IOException {
158        int me = engine.Rank();
159        l2 = new DistHashTableMPI<Integer, Integer>(engine);
160        l2.init();
161        //System.out.println("test3: me = " + me + ", l2 = "+ l2);
162        assertTrue("l2==empty", l2.isEmpty());
163
164        int i = 0, loops = 10;
165        while (i < loops) {
166            Integer x = Integer.valueOf(++i);
167            //System.out.println("me = " + me + ", x = "+ x);
168            if (me == 0) {
169                l2.putWait(x, x);
170            } else {
171                Integer s = l2.getWait(x);
172                assertEquals("s = x: " + s + ", " + x, s, x);
173            }
174            assertTrue("#l1==i: " + i + ", #l1 = " + l2.size(), l2.size() >= i);
175        }
176        assertTrue("#l2==" + loops, l2.size() == loops);
177
178        Iterator it = l2.iterator();
179        i = 0;
180        while (it.hasNext()) {
181            Object k = it.next();
182            Object o = l2.get(k);
183            Integer x = Integer.valueOf(++i);
184            //System.out.println("me = " + me + ", o = " + o + ", x = "+ x);
185            assertEquals("l2(i)==k(i)", x, k);
186            assertEquals("l2(i)==v(i)", x, o);
187        }
188    }
189
190}