001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections.primitives; 018 019import java.util.EmptyStackException; 020 021import junit.framework.TestCase; 022import junit.framework.TestSuite; 023 024/** 025 * Tests the LongStack class. 026 * 027 * @author Apache Directory Project 028 * @since Commons Primitives 1.1 029 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $ 030 */ 031public class TestLongStack extends TestCase 032{ 033 LongStack stack = null ; 034 035 036 /** 037 * Runs the test. 038 * 039 * @param args nada 040 */ 041 public static void main( String[] args ) 042 { 043 junit.textui.TestRunner.run( TestLongStack.class ) ; 044 } 045 046 public static TestSuite suite() { 047 return new TestSuite(TestLongStack.class); 048 } 049 050 051 /* (non-Javadoc) 052 * @see junit.framework.TestCase#setUp() 053 */ 054 protected void setUp() throws Exception 055 { 056 super.setUp() ; 057 stack = new LongStack() ; 058 } 059 060 061 /** 062 * Constructor for IntStackTest. 063 * @param arg0 064 */ 065 public TestLongStack( String arg0 ) 066 { 067 super( arg0 ) ; 068 } 069 070 071 public void testEmpty() 072 { 073 assertTrue( "Newly created stacks should be empty", stack.empty() ) ; 074 stack.push( 1234223332233234322l ) ; 075 assertFalse( "Stack with item should not be empty", stack.empty() ) ; 076 stack.pop() ; 077 assertTrue( "Stack last int popped should be empty", stack.empty() ) ; 078 } 079 080 081 public void testPeek() 082 { 083 try 084 { 085 stack.peek() ; 086 fail("Peek should have thrown an EmptyStackException" ) ; 087 } 088 catch( EmptyStackException e ) 089 { 090 assertNotNull( "EmptyStackException should not be null", e ) ; 091 } 092 093 for( int ii = 0; ii < 10; ii++ ) 094 { 095 stack.push( ii ) ; 096 assertTrue( ii == stack.peek() ) ; 097 } 098 } 099 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( long ii = 0; ii < 10; ii++ ) 114 { 115 stack.push( ii ) ; 116 assertTrue( ii == stack.pop() ) ; 117 } 118 119 for( long ii = 0; ii < 10; ii++ ) 120 { 121 stack.push( ii ) ; 122 } 123 for( long ii = 10; ii < 0; ii-- ) 124 { 125 stack.push( ii ) ; 126 assertTrue( ii == stack.pop() ) ; 127 } 128 } 129 130 131 public void testPush() 132 { 133 stack.push( 0L ) ; 134 stack.push( 0L ) ; 135 assertFalse( stack.empty() ) ; 136 assertTrue( 0L == stack.pop() ) ; 137 assertTrue( 0L == stack.pop() ) ; 138 } 139 140 141 public void testSearch() 142 { 143 stack.push( 0L ) ; 144 stack.push( 1L ) ; 145 assertTrue( 2L == stack.search( 0L ) ) ; 146 stack.push( 0L ) ; 147 assertTrue( 1L == stack.search( 0L ) ) ; 148 stack.push( 0L ) ; 149 assertTrue( 3L == stack.search( 1L ) ) ; 150 assertTrue( -1L == stack.search( 44L ) ) ; 151 } 152 153 public void testArrayConstructor() { 154 long[] array = { 1, 2, 3, 4 }; 155 stack = new LongStack(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 long[] array = { 1, 2, 3, 4 }; 164 stack = new LongStack(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