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 TestCharStack extends TestCase 32 { 33 CharStack stack = null ; 34 35 36 41 public static void main( String [] args ) 42 { 43 junit.textui.TestRunner.run( TestCharStack.class ) ; 44 } 45 46 public static TestSuite suite() { 47 return new TestSuite(TestCharStack.class); 48 } 49 50 51 54 protected void setUp() throws Exception 55 { 56 super.setUp() ; 57 stack = new CharStack() ; 58 } 59 60 61 65 public TestCharStack( 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( 'A' ) ; 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( char ii = 0; ii < 10; ii++ ) 94 { 95 stack.push( ii ) ; 96 assertTrue( 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( char ii = 0; ii < 10; ii++ ) 114 { 115 stack.push( ii ) ; 116 assertTrue( ii == stack.pop() ) ; 117 } 118 119 for( char ii = 0; ii < 10; ii++ ) 120 { 121 stack.push( ii ) ; 122 } 123 for( char ii = 10; ii < 0; ii-- ) 124 { 125 stack.push( ( char ) ii ) ; 126 assertTrue( ii == stack.pop() ) ; 127 } 128 } 129 130 131 public void testPush() 132 { 133 stack.push( ( char ) 0 ) ; 134 stack.push( ( char ) 0 ) ; 135 assertFalse( stack.empty() ) ; 136 assertTrue( 0 == stack.pop() ) ; 137 assertTrue( 0 == stack.pop() ) ; 138 } 139 140 141 public void testSearch() 142 { 143 stack.push( ( char ) 0 ) ; 144 stack.push( ( char ) 1 ) ; 145 assertTrue( 2 == stack.search( ( char ) 0 ) ) ; 146 stack.push( ( char ) 0 ) ; 147 assertTrue( 1 == stack.search( ( char ) 0 ) ) ; 148 stack.push( ( char ) 0 ) ; 149 assertTrue( 3 == stack.search( ( char ) 1 ) ) ; 150 assertTrue( -1 == stack.search( ( char ) 44 ) ) ; 151 } 152 153 public void testArrayConstructor() { 154 char[] array = { 1, 2, 3, 4 }; 155 stack = new CharStack(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 char[] array = { 1, 2, 3, 4 }; 164 stack = new CharStack(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 |