summaryrefslogtreecommitdiff
path: root/src/test/java/bjc/esodata/StackTest.java
blob: 568e6ea3b0342515eab02d24cbac227d03c6dc0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package bjc.esodata;

import org.junit.Test;

import bjc.TestUtils;

import java.util.*;

import static bjc.TestUtils.*;

import static org.junit.Assert.*;

/**
 * Tests of Stack.
 *
 * @author Ben Culkin
 */
public class StackTest {
	@Test
	public void testBasic() {
		Stack<String> st = new SimpleStack<>();

		assertEquals(0, st.size());

		st.push("a");
		assertEquals(1, st.size());

		assertEquals("a", st.top());
		assertEquals(1, st.size());

		assertEquals("a", st.pop());
		assertEquals(0, st.size());
		assertTrue(st.isEmpty());
	}

	@Test
	public void testSpaghetti() {
		Stack<String> st = new SimpleStack<>();

		st.pushAll("a", "b", "c");

		Stack<String> spst1 = st.spaghettify();
		Stack<String> spst2 = st.spaghettify();

		// st should be [a, b, c]
		// spst1 should be [[a, b, c]]
		// spst2 should be [[a, b, c]]

		assertEquals("c", st.top());
		assertEquals("c", spst1.top());
		assertEquals("c", spst2.top());

		assertEquals(3, st.size());
		assertEquals(3, spst1.size());
		assertEquals(3, spst2.size());

		st.push("d");
		spst1.push("e");
		spst2.push("f");

		// st should be [a, b, c, d]
		// spst1 should be [[a, b, c, d], e]
		// spst2 should be [[a, b, c, d], f]

		assertEquals("d", st.top());
		assertEquals("e", spst1.top());
		assertEquals("f", spst2.top());

		assertEquals(4, st.size());
		assertEquals(5, spst1.size());
		assertEquals(5, spst2.size());

		spst1.pop();

		// st should be [a, b, c]
		// spst1 should be [[a, b, c]]
		// spst2 should be [[a, b, c], f]

		assertEquals("d", spst1.pop());
		assertEquals("c", st.top());

		assertEquals(3, st.size());
		assertEquals(3, spst1.size());
		assertEquals(4, spst2.size());
	}

	@Test
	public void testBasicComb() {
		Stack<String> st = new SimpleStack<>();

		st.pushAll("a", "b", "c", "d");

		st.drop();

		// stack should be [a, b, c]

		assertEquals("c", st.top());
		assertEquals(3, st.size());

		st.drop(2);

		// stack should be [a]

		assertEquals("a", st.top());
		assertEquals(1, st.size());

		st.pushAll("b", "c", "d");

		st.nip();
		st.nip();

		// stack should be [a, d]
		assertEquals("d", st.top());
		assertEquals(2, st.size());

		st.pop();

		assertEquals("a", st.top());
		assertEquals(1, st.size());

		st.multidup(1, 1);

		// stack should be [a, a]
		assertEquals("a", st.top());
		assertEquals(2, st.size());

		st.pushAll("b", "c");

		st.multidup(3, 1);

		// stack should be [a, a, b, c, a, b, c]
		assertEquals("c", st.top());
		assertEquals(7, st.size());

		st.pop();
		assertEquals("b", st.pop());
		assertEquals("a", st.pop());
		assertEquals("c", st.top());

		// stack should be [a, a, b, c]

		st.dup();
		assertEquals("c", st.top());
		assertEquals(5, st.size());

		// stack should be [a, a, b, c, c]
		assertEquals("c", st.pop());
		assertEquals(4, st.size());

		// stack should be [a, a, b, c]
	}
}