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;
015
016import edu.jas.kern.MPJEngine;
017
018
019/**
020 * DistHashTableMPJ test with JUnit.
021 * @author Heinz Kredel
022 */
023public class DistHashTableMPJTest extends TestCase {
024
025
026    protected static Comm engine;
027
028
029    /**
030     * main.
031     */
032    public static void main(String[] args) throws IOException {
033        //long t = System.currentTimeMillis();
034        engine = MPJEngine.getCommunicator(args);
035        junit.textui.TestRunner.run(suite());
036        engine.Barrier();
037        MPJEngine.terminate();
038        //t = System.currentTimeMillis() - t;
039        //System.out.println("MPJ runtime = " + t + " milli seconds");
040    }
041
042
043    /**
044     * Constructs a <CODE>DistHashTableMPJTest</CODE> object.
045     * @param name String.
046     */
047    public DistHashTableMPJTest(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(DistHashTableMPJTest.class);
058        return suite;
059    }
060
061
062    private DistHashTableMPJ<Integer, Integer> l1;
063
064
065    private DistHashTableMPJ<Integer, Integer> l2;
066
067
068    private DistHashTableMPJ<Integer, Integer> l3;
069
070
071    @Override
072    protected void setUp() {
073        engine.Barrier();
074    }
075
076
077    @Override
078    protected void tearDown() {
079        engine.Barrier();
080        if (l1 != null)
081            l1.terminate();
082        if (l2 != null)
083            l2.terminate();
084        if (l3 != null)
085            l3.terminate();
086        l1 = l2 = l3 = null;
087        try {
088            //Thread.currentThread();
089            //System.out.println("tearDown: sleep = 1");
090            Thread.sleep(1);
091        } catch (InterruptedException e) {
092        }
093        engine.Barrier();
094    }
095
096
097    /**
098     * Tests create and terminate DistHashTableMPJ.
099     */
100    public void xtestDistHashTable1() throws IOException {
101        l1 = new DistHashTableMPJ<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 IOException {
111        int me = engine.Rank();
112        l1 = new DistHashTableMPJ<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 IOException {
158        int me = engine.Rank();
159        l2 = new DistHashTableMPJ<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}