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