001/*
002 * $Id$
003 */
004
005//package edu.unima.ky.parallel;
006package edu.jas.util;
007
008import java.util.Iterator;
009
010import junit.framework.Test;
011import junit.framework.TestCase;
012import junit.framework.TestSuite;
013
014
015
016/**
017 * DistributedList tests with JUnit.
018 * @author Heinz Kredel
019 */
020public class DistributedListTest extends TestCase {
021
022    /**
023     * main
024     */
025    public static void main (String[] args) {
026        junit.textui.TestRunner.run( suite() );
027    }
028
029    /**
030     * Constructs a <CODE>DistributedListTest</CODE> object.
031     * @param name String.
032     */
033    public DistributedListTest(String name) {
034        super(name);
035    }
036
037    /**
038     */ 
039    public static Test suite() {
040        TestSuite suite= new TestSuite(DistributedListTest.class);
041        return suite;
042    }
043
044    private static final String host = "localhost";
045
046    private DistributedList l1;
047    private DistributedList l2;
048    private DistributedList l3;
049
050    private DistributedListServer dls;
051
052    int rl = 7; 
053    int kl = 10;
054    int ll = 10;
055    int el = 5;
056    float q = 0.5f;
057
058    protected void setUp() {
059        dls = new DistributedListServer();
060        dls.init();
061    }
062
063    protected void tearDown() {
064        dls.terminate();
065        dls = null;
066        if ( l1 != null ) l1.terminate();
067        if ( l2 != null ) l2.terminate();
068        if ( l3 != null ) l3.terminate();
069        l1 = l2 = l3 = null;
070    }
071
072
073    /**
074     * Tests if the created DistributedList has #n objects as content.
075     */
076    public void testDistributedList1() {
077        l1 = new DistributedList(host);
078        l1.init();
079        assertTrue("l1==empty",l1.isEmpty());
080        l1.add( Integer.valueOf(1) );
081        assertFalse("l1!=empty",l1.isEmpty());
082        assertTrue("#l1==1", l1.size() == 1 );
083        l1.add( Integer.valueOf(2) );
084        assertTrue("#l1==2", l1.size() == 2 );
085        l1.add( Integer.valueOf(3) );
086        assertTrue("#l1==3", l1.size() == 3 );
087
088        Iterator it = null;
089        it = l1.iterator();
090        int i = 0;
091        while ( it.hasNext() ) {
092            Object o = it.next();
093            Integer x = Integer.valueOf( ++i );
094            assertEquals("l1(i)==(i)", x, o );
095        }
096
097        l1.clear();
098        assertTrue("#l1==0", l1.size() == 0 );
099    }
100
101
102    /**
103     * Tests if the two created DistributedLists have #n objects as content.
104     */
105    public void testDistributedList2() {
106        l2 = new DistributedList(host);
107        l2.init();
108        assertTrue("l2==empty",l2.isEmpty());
109        l1 = new DistributedList(host);
110        l1.init();
111        assertTrue("l1==empty",l1.isEmpty());
112
113        int i = 0, loops = 10;
114        while ( i < loops ) {
115            Integer x = Integer.valueOf( ++i );
116            l1.add( x );
117            assertTrue("#l1==i", l1.size() == i );
118        }
119        assertTrue("#l1==10", l1.size() == loops );
120
121        while ( l2.size() < loops ) {
122            try {
123                System.out.print("2");
124                Thread.sleep(100);
125            } catch (InterruptedException e) {
126            }
127        }
128        Iterator it = null;
129        it = l2.iterator();
130        i = 0;
131        while ( it.hasNext() ) {
132            Object o = it.next();
133            Integer x = Integer.valueOf( ++i );
134            //System.out.println("o = " + o + " x = "+ x);
135            assertEquals("l2(i)==(i)", x, o );
136        }
137        assertTrue("#l2==10", l2.size() == loops );
138    }
139
140
141    /**
142     * Tests if the three created DistributedLists have #n objects as content.
143     */
144    public void testDistributedList3() {
145        l1 = new DistributedList(host);
146        l1.init();
147        assertTrue("l1==empty",l1.isEmpty());
148        l2 = new DistributedList(host);
149        l2.init();
150        assertTrue("l2==empty",l2.isEmpty());
151        l3 = new DistributedList(host);
152        l3.init();
153        assertTrue("l3==empty",l3.isEmpty());
154
155        int i = 0, loops = 10;
156        while ( i < loops ) {
157            Integer x = Integer.valueOf( ++i );
158            l3.add( x );
159            assertTrue("#l3==i", l3.size() == i );
160        }
161        assertTrue("#l3==10", l3.size() == loops );
162
163        while ( l2.size() < loops || l1.size() < loops-1 ) {
164            try {
165                System.out.print("3");
166                Thread.sleep(100);
167            } catch (InterruptedException e) {
168            }
169        }
170        assertTrue("#l2==10", l2.size() == loops );
171        assertTrue("#l1==10", l1.size() == loops );
172        Iterator it = null;
173        it = l2.iterator();
174        Iterator it3 = null;
175        it3 = l1.iterator();
176        i = 0;
177        /* sequence of elements is not correct at moment
178         */
179        while ( it.hasNext() && it3.hasNext() ) {
180            Object o = it.next();
181            Object p = it3.next();
182            Integer x = Integer.valueOf( ++i );
183            //System.out.println("o = " + o + " x = "+ x);
184            assertEquals("l2(i)==(i)", x, o );
185            assertEquals("l1(i)==(i)", x, p );
186        }
187    }
188
189
190/**
191 * Tests if the two created DistributedLists have #n objects as content 
192 * when one is created later.
193 */
194    public void testDistributedList6() {
195        l1 = new DistributedList(host);
196        l1.init();
197        assertTrue("l1==empty",l1.isEmpty());
198
199        int i = 0, loops = 10;
200        while ( i < loops ) {
201            Integer x = Integer.valueOf( ++i );
202            l1.add( x );
203            assertTrue("#l1==i", l1.size() == i );
204        }
205        assertTrue("#l1==10", l1.size() == loops );
206
207        l2 = new DistributedList(host);
208        l2.init();
209        // assertTrue("l2==empty",l2.isEmpty());
210        while ( l2.size() < loops ) {
211            try {
212                //System.out.print("2");
213                Thread.sleep(100);
214            } catch (InterruptedException e) {
215            }
216        }
217        Iterator it = null;
218        it = l2.iterator();
219        i = 0;
220        while ( it.hasNext() ) {
221            Object o = it.next();
222            Integer x = Integer.valueOf( ++i );
223            //System.out.println("o = " + o + " x = "+ x);
224            assertEquals("l2(i)==(i)", x, o );
225        }
226        assertTrue("#l2==10", l2.size() == loops );
227    }
228
229}