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 TestBooleanStack extends TestCase 32 { 33 BooleanStack stack = null ; 34 35 36 41 public static void main( String [] args ) 42 { 43 junit.textui.TestRunner.run( TestBooleanStack.class ) ; 44 } 45 46 public static TestSuite suite() { 47 return new TestSuite(TestBooleanStack.class); 48 } 49 50 51 54 protected void setUp() throws Exception 55 { 56 super.setUp() ; 57 stack = new BooleanStack() ; 58 } 59 60 61 65 public TestBooleanStack( 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( true ) ; 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 if ( ii % 2 == 0 ) 96 { 97 stack.push( false ) ; 98 assertFalse( stack.peek() ) ; 99 } 100 else 101 { 102 stack.push( true ) ; 103 assertTrue( stack.peek() ) ; 104 } 105 } 106 } 107 108 109 public void testPop() 110 { 111 try 112 { 113 stack.pop() ; 114 fail("Pop should have thrown an EmptyStackException" ) ; 115 } 116 catch( EmptyStackException e ) 117 { 118 assertNotNull( "EmptyStackException should not be null", e ) ; 119 } 120 121 for( int ii = 0; ii < 10; ii++ ) 122 { 123 if ( ii % 2 == 0 ) 124 { 125 stack.push( false ) ; 126 assertFalse( stack.pop() ) ; 127 } 128 else 129 { 130 stack.push( true ) ; 131 assertTrue( stack.pop() ) ; 132 } 133 } 134 135 for( int ii = 0; ii < 10; ii++ ) 136 { 137 if ( ii % 2 == 0 ) 138 { 139 stack.push( false ) ; 140 } 141 else 142 { 143 stack.push( true ) ; 144 } 145 } 146 147 for( short ii = 10; ii < 0; ii-- ) 148 { 149 if ( ii % 2 == 0 ) 150 { 151 stack.push( false ) ; 152 assertFalse( stack.pop() ) ; 153 } 154 else 155 { 156 stack.push( true ) ; 157 assertTrue( stack.pop() ) ; 158 } 159 } 160 } 161 162 163 public void testPush() 164 { 165 stack.push( false ) ; 166 stack.push( false ) ; 167 stack.push( true ) ; 168 assertFalse( stack.empty() ) ; 169 assertTrue( stack.pop() ) ; 170 assertFalse( stack.pop() ) ; 171 assertFalse( stack.pop() ) ; 172 } 173 174 175 public void testSearch() 176 { 177 stack.push( false ) ; 178 assertTrue( -1 == stack.search( true ) ) ; 179 stack.push( true ) ; 180 assertTrue( 2 == stack.search( false ) ) ; 181 stack.push( false ) ; 182 assertTrue( 1 == stack.search( false ) ) ; 183 stack.push( false ) ; 184 assertTrue( 3 == stack.search( true ) ) ; 185 } 186 187 public void testArrayConstructor() { 188 boolean[] array = { true, false, true, true }; 189 stack = new BooleanStack(array); 190 assertEquals(array.length,stack.size()); 191 for(int i=array.length-1;i>=0;i--) { 192 assertEquals(array[i],stack.pop()); 193 } 194 } 195 196 public void testPeekN() { 197 boolean[] array = { true, false, true, true }; 198 stack = new BooleanStack(array); 199 for(int i=array.length-1;i>=0;i--) { 200 assertEquals(array[i],stack.peek((array.length-1)-i)); 201 } 202 } 203 204 } 205 | Popular Tags |