Home | History | Annotate | Download | only in util
      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 
     18 package org.apache.harmony.tests.java.util;
     19 
     20 import java.util.EmptyStackException;
     21 import java.util.Stack;
     22 
     23 public class StackTest extends junit.framework.TestCase {
     24 
     25     Stack s;
     26 
     27     /**
     28      * 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      * 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      * 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         s.pop();
     71         s.pop();
     72         try {
     73             s.pop();
     74             fail("EmptyStackException expected");
     75         } catch (EmptyStackException e) {
     76             //expected
     77         }
     78     }
     79 
     80     /**
     81      * java.util.Stack#pop()
     82      */
     83     public void test_pop() {
     84         // Test for method java.lang.Object java.util.Stack.pop()
     85         String item1 = "Ichi";
     86         String item2 = "Ni";
     87         Object lastPopped;
     88         s.push(item1);
     89         s.push(item2);
     90 
     91         try {
     92             lastPopped = s.pop();
     93             assertTrue("a) Pop did not return top item", lastPopped == item2);
     94         } catch (EmptyStackException e) {
     95             fail(
     96                     "a) Pop threw EmptyStackException when stack should not have been empty");
     97         }
     98 
     99         try {
    100             lastPopped = s.pop();
    101             assertTrue("b) Pop did not return top item", lastPopped == item1);
    102         } catch (EmptyStackException e) {
    103             fail(
    104                     "b) Pop threw EmptyStackException when stack should not have been empty");
    105         }
    106 
    107         s.push(null);
    108         try {
    109             lastPopped = s.pop();
    110             assertNull("c) Pop did not return top item", lastPopped);
    111         } catch (EmptyStackException e) {
    112             fail(
    113                     "c) Pop threw EmptyStackException when stack should not have been empty");
    114         }
    115 
    116         try {
    117             lastPopped = s.pop();
    118             fail(
    119                     "d) Pop did not throw EmptyStackException when stack should have been empty");
    120         } catch (EmptyStackException e) {
    121             return;
    122         }
    123 
    124     }
    125 
    126     /**
    127      * java.util.Stack#push(java.lang.Object)
    128      */
    129     public void test_pushLjava_lang_Object() {
    130         Object [] array = {new Integer(0), new Object(),
    131                            new Float(0), new String()};
    132 
    133         Stack<Object> stack = new Stack<Object>();
    134         for(int i = 0; i < array.length; i++) {
    135             stack.push(array[i]);
    136         }
    137         for(int i = 0; i < array.length; i++) {
    138             assertEquals(array.length - i, stack.search(array[i]));
    139         }
    140     }
    141 
    142     /**
    143      * java.util.Stack#search(java.lang.Object)
    144      */
    145     public void test_searchLjava_lang_Object() {
    146         // Test for method int java.util.Stack.search(java.lang.Object)
    147         String item1 = "Ichi";
    148         String item2 = "Ni";
    149         String item3 = "San";
    150         s.push(item1);
    151         s.push(item2);
    152         s.push(item3);
    153         assertEquals("Search returned incorrect value for equivalent object", 3, s
    154                 .search(item1));
    155         assertEquals("Search returned incorrect value for equal object", 3, s
    156                 .search("Ichi"));
    157         s.pop();
    158         assertEquals("Search returned incorrect value for equivalent object at top of stack",
    159                 1, s.search(item2));
    160         assertEquals("Search returned incorrect value for equal object at top of stack",
    161                 1, s.search("Ni"));
    162         s.push(null);
    163         assertEquals("Search returned incorrect value for search for null at top of stack",
    164                 1, s.search(null));
    165         s.push("Shi");
    166         assertEquals("Search returned incorrect value for search for null", 2, s
    167                 .search(null));
    168         s.pop();
    169         s.pop();
    170         assertEquals("Search returned incorrect value for search for null--wanted -1",
    171                 -1, s.search(null));
    172     }
    173 
    174     static class BugStack<E> extends Stack<E> {
    175         public void setLength(int elementCount) {
    176             this.elementCount = elementCount;
    177         }
    178 
    179         public int getLength() {
    180             return elementCount;
    181         }
    182     }
    183 
    184     //test for wrong exception threw by pop method
    185     public void test_pop_modify_elementCount() {
    186         BugStack<String> testStack = new BugStack<String>();
    187         testStack.push("A");
    188         testStack.push("B");
    189         testStack.setLength(20);
    190         try {
    191             testStack.pop();
    192             fail("Should throw ArrayIndexOutOfBoundsException here");
    193         } catch (ArrayIndexOutOfBoundsException e) {
    194             //Expected to throw ArrayIndexOutOfBoundsException here
    195         } catch (EmptyStackException e) {
    196             fail("Should throw ArrayIndexOutOfBoundsException here");
    197         }
    198     }
    199 
    200     /**
    201      * Sets up the fixture, for example, open a network connection. This method
    202      * is called before a test is executed.
    203      */
    204     protected void setUp() {
    205         s = new Stack();
    206     }
    207 
    208     /**
    209      * Tears down the fixture, for example, close a network connection. This
    210      * method is called after a test is executed.
    211      */
    212     protected void tearDown() {
    213     }
    214 }
    215