1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package net.sourceforge.combean.test.graph.containers; |
23 | |
|
24 | |
import java.util.EmptyStackException; |
25 | |
import java.util.Stack; |
26 | |
|
27 | |
import junit.framework.TestCase; |
28 | |
import net.sourceforge.combean.graph.NumberNode; |
29 | |
import net.sourceforge.combean.graph.containers.StackAsNodeStack; |
30 | |
import net.sourceforge.combean.interfaces.graph.Node; |
31 | |
import net.sourceforge.combean.interfaces.graph.containers.NodeStack; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class TestStackAsNodeStack extends TestCase { |
38 | |
|
39 | 6 | private NodeStack S = null; |
40 | |
private static final int STACKSIZE = 4; |
41 | |
|
42 | |
public static void main(String[] args) { |
43 | 0 | junit.textui.TestRunner.run(TestStackAsNodeStack.class); |
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
protected void setUp() throws Exception { |
50 | 6 | super.setUp(); |
51 | 6 | Stack<Node> genericStack = new Stack<Node>(); |
52 | 6 | this.S = new StackAsNodeStack(genericStack); |
53 | 6 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
protected void tearDown() throws Exception { |
59 | 6 | super.tearDown(); |
60 | 6 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public TestStackAsNodeStack(String name) { |
67 | 6 | super(name); |
68 | 6 | } |
69 | |
|
70 | |
public void testWorkWithStack() { |
71 | 15 | for (int i = 0; i < STACKSIZE; i++) { |
72 | 12 | this.S.push(new NumberNode(i)); |
73 | |
} |
74 | 3 | assertFalse("stack is non-empty", this.S.isEmpty()); |
75 | 15 | for (int i = STACKSIZE-1; i >= 0; i--) { |
76 | 12 | NumberNode v = (NumberNode) this.S.peek(); |
77 | 12 | assertEquals(i, v.getNodeNum()); |
78 | 12 | v = (NumberNode) this.S.pop(); |
79 | 12 | assertEquals(i, v.getNodeNum()); |
80 | |
} |
81 | 3 | assertTrue("stack is empty after removing all elements", |
82 | |
this.S.isEmpty()); |
83 | 3 | } |
84 | |
|
85 | |
public void testEmptyStack() { |
86 | 3 | assertTrue("initially the stack is empty", this.S.isEmpty()); |
87 | |
try { |
88 | 3 | this.S.peek(); |
89 | 0 | fail("cannot peek on empty stack"); |
90 | |
} |
91 | 3 | catch (EmptyStackException e) { |
92 | |
|
93 | 0 | } |
94 | |
try { |
95 | 3 | this.S.pop(); |
96 | 0 | fail("cannot peek on empty stack"); |
97 | |
} |
98 | 3 | catch (EmptyStackException e) { |
99 | |
|
100 | 0 | } |
101 | 3 | } |
102 | |
|
103 | |
} |