1 17 package org.apache.commons.collections.primitives; 18 19 import java.util.EmptyStackException ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 31 public class TestIntStack extends TestCase 32 { 33 IntStack stack = null ; 34 35 36 41 public static void main( String [] args ) 42 { 43 junit.textui.TestRunner.run( TestIntStack.class ) ; 44 } 45 46 public static TestSuite suite() { 47 return new TestSuite(TestIntStack.class); 48 } 49 50 51 54 protected void setUp() throws Exception 55 { 56 super.setUp() ; 57 stack = new IntStack() ; 58 } 59 60 61 65 public TestIntStack( String arg0 ) 66 { 67 super( arg0 ) ; 68 } 69 70 71 public void testEmpty() 72 { 73 assertTrue( "Newly created stacks should be empty", stack.empty() ) ; 74 stack.push( 0 ) ; 75 assertFalse( "Stack with item should not be empty", stack.empty() ) ; 76 stack.pop() ; 77 assertTrue( "Stack last int popped should be empty", stack.empty() ) ; 78 } 79 80 81 public void testPeek() 82 { 83 try 84 { 85 stack.peek() ; 86 fail("Peek should have thrown an EmptyStackException" ) ; 87 } 88 catch( EmptyStackException e ) 89 { 90 assertNotNull( "EmptyStackException should not be null", e ) ; 91 } 92 93 for( int ii = 0; ii < 10; ii++ ) 94 { 95 stack.push( ii ) ; 96 assertEquals( ii, stack.peek() ) ; 97 } 98 } 99 100 101 public void testPop() 102 { 103 try 104 { 105 stack.pop() ; 106 fail("Pop should have thrown an EmptyStackException" ) ; 107 } 108 catch( EmptyStackException e ) 109 { 110 assertNotNull( "EmptyStackException should not be null", e ) ; 111 } 112 113 for( int ii = 0; ii < 10; ii++ ) 114 { 115 stack.push( ii ) ; 116 assertEquals( ii, stack.pop() ) ; 117 } 118 119 for( int ii = 0; ii < 10; ii++ ) 120 { 121 stack.push( ii ) ; 122 } 123 for( int ii = 10; ii < 0; ii-- ) 124 { 125 stack.push( ii ) ; 126 assertEquals( ii, stack.pop() ) ; 127 } 128 } 129 130 131 public void testPush() 132 { 133 stack.push( 0 ) ; 134 stack.push( 0 ) ; 135 assertFalse( stack.empty() ) ; 136 assertEquals( 0, stack.pop() ) ; 137 assertEquals( 0, stack.pop() ) ; 138 } 139 140 141 public void testSearch() 142 { 143 stack.push( 0 ) ; 144 stack.push( 1 ) ; 145 assertEquals( 2, stack.search( 0 ) ) ; 146 stack.push( 0 ) ; 147 assertEquals( 1, stack.search( 0 ) ) ; 148 stack.push( 0 ) ; 149 assertEquals( 3, stack.search( 1 ) ) ; 150 assertEquals( -1, stack.search( 44 ) ) ; 151 } 152 153 public void testArrayConstructor() { 154 int[] array = { 1, 2, 3, 4 }; 155 stack = new IntStack(array); 156 assertEquals(array.length,stack.size()); 157 for(int i=array.length-1;i>=0;i--) { 158 assertEquals(array[i],stack.pop()); 159 } 160 } 161 162 public void testPeekN() { 163 int[] array = { 1, 2, 3, 4 }; 164 stack = new IntStack(array); 165 for(int i=array.length-1;i>=0;i--) { 166 assertEquals(array[i],stack.peek((array.length-1)-i)); 167 } 168 } 169 } 170 | Popular Tags |