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