001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import com.tc.test.TCTestCase;
007:
008: import java.security.SecureRandom;
009: import java.util.Random;
010:
011: public class StackTest extends TCTestCase {
012:
013: public void test() throws Exception {
014: Stack tcStack = new Stack();
015: java.util.Stack javaStack = new java.util.Stack();
016:
017: doRandomTests(tcStack, javaStack);
018: }
019:
020: private void doRandomTests(Stack tcStack, java.util.Stack javaStack) {
021: SecureRandom sr = new SecureRandom();
022: long seed = sr.nextLong();
023: System.err.println("Seed is " + seed);
024: Random r = new Random(seed);
025: int count = 10000;
026: while (count-- > 0) {
027: switch (r.nextInt(6)) {
028: case 0:
029: callPop(tcStack, javaStack);
030: break;
031: case 1:
032: callPush(tcStack, javaStack, r);
033: break;
034: case 2:
035: callPeek(tcStack, javaStack);
036: break;
037: case 3:
038: callEmpty(tcStack, javaStack);
039: break;
040: case 4:
041: callSize(tcStack, javaStack);
042: break;
043: case 5:
044: callSearch(tcStack, javaStack, r);
045: break;
046: default:
047: throw new AssertionError("Should never get here.");
048: }
049: }
050: }
051:
052: private void callSearch(Stack tcStack, java.util.Stack javaStack,
053: Random r) {
054: Integer subject = new Integer(r.nextInt(10000));
055: assertEquals(javaStack.search(subject), tcStack.search(subject));
056: }
057:
058: private void callSize(Stack tcStack, java.util.Stack javaStack) {
059: assertEquals(javaStack.size(), tcStack.size());
060: }
061:
062: private void callEmpty(Stack tcStack, java.util.Stack javaStack) {
063: assertEquals(javaStack.empty(), tcStack.empty());
064: }
065:
066: private void callPush(Stack tcStack, java.util.Stack javaStack,
067: Random r) {
068: Integer subject = new Integer(r.nextInt(10000));
069: assertEquals(javaStack.push(subject), tcStack.push(subject));
070: assertEquals(javaStack.size(), tcStack.size());
071: }
072:
073: private void callPeek(Stack tcStack, java.util.Stack javaStack) {
074: boolean thrownException = false;
075: Object jo = null, to = null;
076: Exception je = null, te = null;
077: try {
078: jo = javaStack.peek();
079: } catch (Exception ex) {
080: je = ex;
081: thrownException = true;
082: }
083: try {
084: to = tcStack.peek();
085: assertFalse(thrownException);
086: } catch (Exception ex) {
087: assertTrue(thrownException);
088: te = ex;
089: }
090: assertEquals(jo, to);
091: if (je != null) {
092: assertEquals(je.toString(), te.toString());
093: }
094: }
095:
096: private void callPop(Stack tcStack, java.util.Stack javaStack) {
097: boolean thrownException = false;
098: Object jo = null, to = null;
099: Exception je = null, te = null;
100: try {
101: jo = javaStack.pop();
102: } catch (Exception ex) {
103: je = ex;
104: thrownException = true;
105: }
106: try {
107: to = tcStack.pop();
108: assertFalse(thrownException);
109: } catch (Exception ex) {
110: assertTrue(thrownException);
111: te = ex;
112: }
113: assertEquals(jo, to);
114: if (je != null) {
115: assertEquals(je.toString(), te.toString());
116: }
117: }
118:
119: }
|