1 4 package com.tc.stats; 5 6 import junit.framework.TestCase; 7 8 public class LossyStackTest extends TestCase { 9 10 public void testException() { 11 try { 12 new LossyStack(0); 13 fail(); 14 } catch (IllegalArgumentException iae) { 15 } 17 18 try { 19 new LossyStack(-4); 20 fail(); 21 } catch (IllegalArgumentException iae) { 22 } 24 25 } 26 27 public void test() { 28 LossyStack stack = new LossyStack(5); 29 assertEquals(0, stack.depth()); 30 assertTrue(stack.isEmtpy()); 31 assertNull(stack.peek()); 32 33 try { 34 stack.pop(); 35 fail(); 36 } catch (IllegalStateException ise) { 37 } 39 40 stack.push(new Integer (1)); 41 assertFalse(stack.isEmtpy()); 42 assertEquals(1, stack.depth()); 43 stack.push(new Integer (2)); 44 assertFalse(stack.isEmtpy()); 45 assertEquals(2, stack.depth()); 46 47 assertEquals(new Integer (2), stack.pop()); 48 assertFalse(stack.isEmtpy()); 49 assertEquals(new Integer (1), stack.pop()); 50 assertEquals(0, stack.depth()); 51 assertTrue(stack.isEmtpy()); 52 assertNull(stack.peek()); 53 54 stack.push(new Integer (1)); 55 stack.push(new Integer (2)); 56 stack.push(new Integer (3)); 57 stack.push(new Integer (4)); 58 stack.push(new Integer (5)); 59 assertEquals(5, stack.depth()); 60 stack.push(new Integer (6)); 61 assertEquals(5, stack.depth()); 62 stack.push(new Integer (7)); 63 assertEquals(5, stack.depth()); 64 65 Integer [] data = (Integer []) stack.toArray(new Integer [stack.depth()]); 66 assertEquals(5, data.length); 67 for (int i = 0; i < data.length; i++) { 68 assertEquals(new Integer (7 - i), data[i]); 69 } 70 71 } 72 73 } 74 | Popular Tags |