StackTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.EmptyStackException;
26import java.util.Stack;
27
28@TestTargetClass(Stack.class)
29public class StackTest extends junit.framework.TestCase {
30
31    Stack s;
32
33    /**
34     * @tests java.util.Stack#Stack()
35     */
36    @TestTargetNew(
37        level = TestLevel.COMPLETE,
38        notes = "",
39        method = "Stack",
40        args = {}
41    )
42    public void test_Constructor() {
43        // Test for method java.util.Stack()
44        assertEquals("Stack creation failed", 0, s.size());
45    }
46
47    /**
48     * @tests java.util.Stack#empty()
49     */
50    @TestTargetNew(
51        level = TestLevel.COMPLETE,
52        notes = "",
53        method = "empty",
54        args = {}
55    )
56    public void test_empty() {
57        // Test for method boolean java.util.Stack.empty()
58        assertTrue("New stack answers non-empty", s.empty());
59        s.push("blah");
60        assertTrue("Stack should not be empty but answers empty", !s.empty());
61        s.pop();
62        assertTrue("Stack should be empty but answers non-empty", s.empty());
63        s.push(null);
64        assertTrue("Stack with null should not be empty but answers empty", !s
65                .empty());
66    }
67
68    /**
69     * @tests java.util.Stack#peek()
70     */
71    @TestTargetNew(
72        level = TestLevel.COMPLETE,
73        notes = "",
74        method = "peek",
75        args = {}
76    )
77    public void test_peek() {
78        // Test for method java.lang.Object java.util.Stack.peek()
79        String item1 = "Ichi";
80        String item2 = "Ni";
81        String item3 = "San";
82        s.push(item1);
83        assertTrue("Peek did not return top item when it was the only item", s
84                .peek() == item1);
85        s.push(item2);
86        s.push(item3);
87        assertTrue("Peek did not return top item amoung many other items", s
88                .peek() == item3);
89        s.pop();
90        assertTrue("Peek did not return top item after a pop", s.pop() == item2);
91        s.push(null);
92        assertNull("Peek did not return top item (wanted: null)",
93                s.peek());
94        s.pop();
95        s.pop();
96        try {
97            s.pop();
98            fail("EmptyStackException expected");
99        } catch (EmptyStackException e) {
100            //expected
101        }
102    }
103
104    /**
105     * @tests java.util.Stack#pop()
106     */
107    @TestTargetNew(
108        level = TestLevel.COMPLETE,
109        notes = "",
110        method = "pop",
111        args = {}
112    )
113    public void test_pop() {
114        // Test for method java.lang.Object java.util.Stack.pop()
115        String item1 = "Ichi";
116        String item2 = "Ni";
117        Object lastPopped;
118        s.push(item1);
119        s.push(item2);
120
121        try {
122            lastPopped = s.pop();
123            assertTrue("a) Pop did not return top item", lastPopped == item2);
124        } catch (EmptyStackException e) {
125            fail(
126                    "a) Pop threw EmptyStackException when stack should not have been empty");
127        }
128
129        try {
130            lastPopped = s.pop();
131            assertTrue("b) Pop did not return top item", lastPopped == item1);
132        } catch (EmptyStackException e) {
133            fail(
134                    "b) Pop threw EmptyStackException when stack should not have been empty");
135        }
136
137        s.push(null);
138        try {
139            lastPopped = s.pop();
140            assertNull("c) Pop did not return top item", lastPopped);
141        } catch (EmptyStackException e) {
142            fail(
143                    "c) Pop threw EmptyStackException when stack should not have been empty");
144        }
145
146        try {
147            lastPopped = s.pop();
148            fail(
149                    "d) Pop did not throw EmptyStackException when stack should have been empty");
150        } catch (EmptyStackException e) {
151            return;
152        }
153
154    }
155
156    /**
157     * @tests java.util.Stack#push(java.lang.Object)
158     */
159    @TestTargetNew(
160        level = TestLevel.COMPLETE,
161        notes = "",
162        method = "push",
163        args = {java.lang.Object.class}
164    )
165    public void test_pushLjava_lang_Object() {
166        Object [] array = {new Integer(0), new Object(),
167                           new Float(0), new String()};
168
169        Stack<Object> stack = new Stack<Object>();
170        for(int i = 0; i < array.length; i++) {
171            stack.push(array[i]);
172        }
173        for(int i = 0; i < array.length; i++) {
174            assertEquals(array.length - i, stack.search(array[i]));
175        }
176    }
177
178    /**
179     * @tests java.util.Stack#search(java.lang.Object)
180     */
181    @TestTargetNew(
182        level = TestLevel.COMPLETE,
183        notes = "",
184        method = "search",
185        args = {java.lang.Object.class}
186    )
187    public void test_searchLjava_lang_Object() {
188        // Test for method int java.util.Stack.search(java.lang.Object)
189        String item1 = "Ichi";
190        String item2 = "Ni";
191        String item3 = "San";
192        s.push(item1);
193        s.push(item2);
194        s.push(item3);
195        assertEquals("Search returned incorrect value for equivalent object", 3, s
196                .search(item1));
197        assertEquals("Search returned incorrect value for equal object", 3, s
198                .search("Ichi"));
199        s.pop();
200        assertEquals("Search returned incorrect value for equivalent object at top of stack",
201                1, s.search(item2));
202        assertEquals("Search returned incorrect value for equal object at top of stack",
203                1, s.search("Ni"));
204        s.push(null);
205        assertEquals("Search returned incorrect value for search for null at top of stack",
206                1, s.search(null));
207        s.push("Shi");
208        assertEquals("Search returned incorrect value for search for null", 2, s
209                .search(null));
210        s.pop();
211        s.pop();
212        assertEquals("Search returned incorrect value for search for null--wanted -1",
213                -1, s.search(null));
214    }
215
216    /**
217     * Sets up the fixture, for example, open a network connection. This method
218     * is called before a test is executed.
219     */
220    protected void setUp() {
221        s = new Stack();
222    }
223
224    /**
225     * Tears down the fixture, for example, close a network connection. This
226     * method is called after a test is executed.
227     */
228    protected void tearDown() {
229    }
230}
231