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