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