StackTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.util;
19
20import java.util.EmptyStackException;
21import java.util.Stack;
22
23public class StackTest extends junit.framework.TestCase {
24
25	Stack s;
26
27	/**
28	 * @tests java.util.Stack#Stack()
29	 */
30	public void test_Constructor() {
31		// Test for method java.util.Stack()
32		assertEquals("Stack creation failed", 0, s.size());
33	}
34
35	/**
36	 * @tests java.util.Stack#empty()
37	 */
38	public void test_empty() {
39		// Test for method boolean java.util.Stack.empty()
40		assertTrue("New stack answers non-empty", s.empty());
41		s.push("blah");
42		assertTrue("Stack should not be empty but answers empty", !s.empty());
43		s.pop();
44		assertTrue("Stack should be empty but answers non-empty", s.empty());
45		s.push(null);
46		assertTrue("Stack with null should not be empty but answers empty", !s
47				.empty());
48	}
49
50	/**
51	 * @tests java.util.Stack#peek()
52	 */
53	public void test_peek() {
54		// Test for method java.lang.Object java.util.Stack.peek()
55		String item1 = "Ichi";
56		String item2 = "Ni";
57		String item3 = "San";
58		s.push(item1);
59		assertTrue("Peek did not return top item when it was the only item", s
60				.peek() == item1);
61		s.push(item2);
62		s.push(item3);
63		assertTrue("Peek did not return top item amoung many other items", s
64				.peek() == item3);
65		s.pop();
66		assertTrue("Peek did not return top item after a pop", s.pop() == item2);
67		s.push(null);
68		assertNull("Peek did not return top item (wanted: null)",
69				s.peek());
70	}
71
72	/**
73	 * @tests java.util.Stack#pop()
74	 */
75	public void test_pop() {
76		// Test for method java.lang.Object java.util.Stack.pop()
77		String item1 = "Ichi";
78		String item2 = "Ni";
79		Object lastPopped;
80		s.push(item1);
81		s.push(item2);
82
83		try {
84			lastPopped = s.pop();
85			assertTrue("a) Pop did not return top item", lastPopped == item2);
86		} catch (EmptyStackException e) {
87			fail(
88					"a) Pop threw EmptyStackException when stack should not have been empty");
89		}
90
91		try {
92			lastPopped = s.pop();
93			assertTrue("b) Pop did not return top item", lastPopped == item1);
94		} catch (EmptyStackException e) {
95			fail(
96					"b) Pop threw EmptyStackException when stack should not have been empty");
97		}
98
99		s.push(null);
100		try {
101			lastPopped = s.pop();
102			assertNull("c) Pop did not return top item", lastPopped);
103		} catch (EmptyStackException e) {
104			fail(
105					"c) Pop threw EmptyStackException when stack should not have been empty");
106		}
107
108		try {
109			lastPopped = s.pop();
110			fail(
111					"d) Pop did not throw EmptyStackException when stack should have been empty");
112		} catch (EmptyStackException e) {
113			return;
114		}
115
116	}
117
118	/**
119	 * @tests java.util.Stack#push(java.lang.Object)
120	 */
121	public void test_pushLjava_lang_Object() {
122		// Test for method java.lang.Object
123		// java.util.Stack.push(java.lang.Object)
124		assertTrue("Used to test", true);
125	}
126
127	/**
128	 * @tests java.util.Stack#search(java.lang.Object)
129	 */
130	public void test_searchLjava_lang_Object() {
131		// Test for method int java.util.Stack.search(java.lang.Object)
132		String item1 = "Ichi";
133		String item2 = "Ni";
134		String item3 = "San";
135		s.push(item1);
136		s.push(item2);
137		s.push(item3);
138		assertEquals("Search returned incorrect value for equivalent object", 3, s
139				.search(item1));
140		assertEquals("Search returned incorrect value for equal object", 3, s
141				.search("Ichi"));
142		s.pop();
143		assertEquals("Search returned incorrect value for equivalent object at top of stack",
144				1, s.search(item2));
145		assertEquals("Search returned incorrect value for equal object at top of stack",
146				1, s.search("Ni"));
147		s.push(null);
148		assertEquals("Search returned incorrect value for search for null at top of stack",
149				1, s.search(null));
150		s.push("Shi");
151		assertEquals("Search returned incorrect value for search for null", 2, s
152				.search(null));
153		s.pop();
154		s.pop();
155		assertEquals("Search returned incorrect value for search for null--wanted -1",
156				-1, s.search(null));
157	}
158
159	static class BugStack<E> extends Stack<E>{
160		/**
161		 *
162		 */
163		private static final long serialVersionUID = -9133762075342926141L;
164
165		/**
166		 *
167		 */
168		public void setLength(int elementCount)
169		{
170			this.elementCount = elementCount;
171		}
172
173		public int getLength()
174		{
175			return elementCount;
176		}
177	}
178
179	//test for wrong exception threw by pop method
180	public void test_pop_modify_elementCount(){
181		BugStack<String> testStack = new BugStack<String>();
182		testStack.push("A");
183		testStack.push("B");
184		testStack.setLength(20);
185		try{
186			testStack.pop();
187			fail("Should throw ArrayIndexOutOfBoundsException here");
188		}
189		catch(ArrayIndexOutOfBoundsException e)
190		{
191			//Expected to throw ArrayIndexOutOfBoundsException here
192		}
193		catch(EmptyStackException e)
194		{
195			fail("Should throw ArrayIndexOutOfBoundsException here");
196		}
197	}
198
199	/**
200	 * Sets up the fixture, for example, open a network connection. This method
201	 * is called before a test is executed.
202	 */
203	protected void setUp() {
204		s = new Stack();
205	}
206
207	/**
208	 * Tears down the fixture, for example, close a network connection. This
209	 * method is called after a test is executed.
210	 */
211	protected void tearDown() {
212	}
213}
214