001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.util;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019:
020: import java.util.EmptyStackException;
021: import java.util.Stack;
022:
023: /**
024: * TODO: document me.
025: */
026: public class StackTest extends GWTTestCase {
027:
028: public static final int TEST_SEARCH_SIZE = 10;
029: public static final int TEST_SIZE = 10;
030: public static Stack testStack = new Stack();
031:
032: /** Checks for emptiness of stack by peeking and popping */
033: public void checkEmptiness(Stack s) {
034: assertTrue(s.empty());
035: try {
036: s.pop();
037: fail("Did not throw exception on pop of checkEmptiness");
038: } catch (EmptyStackException es) {
039: try {
040: s.peek();
041: fail("Did not throw exception on peek of checkEmptiness");
042: } catch (EmptyStackException es2) {
043: // we wanted to get here
044: }
045: }
046: }
047:
048: /** Sets module name so that javascript compiler can operate */
049: public String getModuleName() {
050: return "com.google.gwt.emultest.EmulSuite";
051: }
052:
053: /** Tests clone on Stacks */
054: public void testClone() {
055: Stack large = createLargeStack();
056: Stack cloned = (Stack) large.clone();
057: checkLargeStack(cloned, 0);
058: assertEquals(large.size(), TEST_SIZE);
059: }
060:
061: /**
062: * Tests pushing many elements into a stack, and seeing if they come out in
063: * order. Also verifies that we get the correct exception when we run out of
064: * elements, and tests peek
065: */
066: public void testCountAndOrderWithPeek() {
067: Stack large = new Stack();
068: for (int i = 0; i < TEST_SIZE; i++) {
069: Integer theInt = new Integer(i);
070: large.push(theInt);
071: assertTrue(large.peek() == theInt);
072: assertTrue(large.pop() == theInt);
073: Integer theFinalInt = new Integer(theInt.intValue()
074: + TEST_SIZE);
075: large.push(theFinalInt);
076: assertTrue(large.peek() == theFinalInt);
077: }
078: checkLargeStack(large, TEST_SIZE);
079: }
080:
081: /** tests empty and tries to get emptyStackException as desired */
082: public void testEmptyAndEmptyStackException() {
083: Stack s = new Stack();
084: String item = "empty1";
085: s.push(item);
086: assertEquals(1, s.size());
087: assertFalse(s.empty());
088: assertEquals(s.pop(), item);
089: checkEmptiness(s);
090: }
091:
092: /** Tests pop and peek */
093: public void testPopAndPeek() {
094: int x = testStack.size();
095: Object item = testStack.peek();
096: assertTrue(testStack.pop() == item);
097: assertEquals(x - 1, testStack.size());
098: }
099:
100: /** Tests push and peek */
101: public void testPushAndPeek() {
102: int x = testStack.size();
103: Object item = "4";
104: testStack.push(item);
105: assertEquals(x + 1, testStack.size());
106: assertTrue(testStack.peek() == item);
107: }
108:
109: /**
110: * Tests all combinations of search for a stack that attains a max size of
111: * TEST_SEARCH_SIZE
112: */
113: public void testSearch() {
114: Stack searchStack = new Stack();
115: checkEmptiness(searchStack);
116: for (int stackSizeIncreasing = 0; stackSizeIncreasing < TEST_SEARCH_SIZE; stackSizeIncreasing++) {
117: for (int theInt = 0; theInt < stackSizeIncreasing; theInt++) {
118: assertEquals("Increasing search found", searchStack
119: .search(new Integer(theInt)), searchStack
120: .size()
121: - theInt);
122: }
123: for (int theInt = stackSizeIncreasing; theInt < TEST_SEARCH_SIZE; theInt++) {
124: assertEquals("Increasing not found", searchStack
125: .search(new Integer(theInt)), -1);
126: }
127: searchStack.push(new Integer(stackSizeIncreasing));
128: }
129: for (int stackSizeDecreasing = TEST_SEARCH_SIZE - 1; stackSizeDecreasing >= 0; stackSizeDecreasing--) {
130: for (int theInt = TEST_SEARCH_SIZE - 1; theInt > stackSizeDecreasing; theInt--) {
131: assertEquals("Search decreasing not found", searchStack
132: .search(new Integer(theInt)), -1);
133: }
134: for (int theInt = stackSizeDecreasing; theInt >= 0; theInt--) {
135: assertEquals("Search decreasing found", searchStack
136: .search(new Integer(theInt)), searchStack
137: .size()
138: - theInt);
139: }
140: searchStack.pop();
141: }
142: checkEmptiness(searchStack);
143: }
144:
145: private void checkLargeStack(Stack cloned, int offset) {
146: for (int i = TEST_SIZE - 1; i >= 0; i--) {
147: Integer theObtainedInt = (Integer) cloned.pop();
148: assertEquals(i + offset, theObtainedInt.intValue());
149: }
150: checkEmptiness(cloned);
151: }
152:
153: private Stack createLargeStack() {
154: Stack large = new Stack();
155: for (int i = 0; i < TEST_SIZE; i++) {
156: Integer theFinalInt = new Integer(i);
157: large.push(theFinalInt);
158: }
159: return large;
160: }
161:
162: static {
163: testStack.push("1");
164: testStack.push("2");
165: testStack.push("3");
166: }
167:
168: }
|