PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/test/kilim/test/TestRing.java

http://github.com/kilim/kilim
Java | 49 lines | 41 code | 6 blank | 2 comment | 8 complexity | 9a7f11ab4e7a91f3ec78f4487b4bcc7e MD5 | raw file
 1package kilim.test;
 2
 3import junit.framework.TestCase;
 4
 5import java.util.Random;
 6
 7import kilim.RingQueue;
 8
 9public class TestRing extends TestCase {
10    public void testInfiniteQueue() {
11        Integer[] a = mkRandom();
12        RingQueue<Integer> rq = new RingQueue<Integer>(10);
13        for (int i = 0; i < a.length; i++) {
14            assertTrue("put returned false", rq.put(a[i]));
15        }
16        assertEquals("Queue size", a.length, rq.size());
17        for (int i = 0; i < a.length; i++) {
18            assertEquals("get[" + i + " ]returned different element", a[i], rq.get());
19        }
20        // No more elements. The next ha    d better be null
21        assertNull("Queue should not have any more elements", rq.get());
22        assertTrue(rq.size() == 0);
23    }
24
25    public void testBoundedQueue() {
26        Integer[] a = mkRandom();
27        RingQueue<Integer>   rq = new RingQueue<Integer>(10, a.length);
28        for (int i = 0; i < a.length; i++) {
29            assertTrue("put returned false", rq.put(a[i]));
30        }
31        assertFalse("put should not accept more than bound", rq.put(100));
32        assertTrue(rq.size() == a.length);
33        for (int i = 0; i < a.length; i++) {
34            assertEquals("get[" + i + " ]returned different element", a[i], rq.get());
35        }
36        // No more elements. The next had better be null
37        assertNull("Queue should not have any more elements", rq.get());
38        assertTrue(rq.size() == 0);
39    }
40
41    private Integer[] mkRandom() {
42        Random r = new Random();
43        Integer[] ret = new Integer[1000];
44        for (int i = 0; i < 1000; i++) {
45            ret[i] = new Integer(r.nextInt(1000));
46        }
47        return ret;
48    }
49}