-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathLinkedStackTest.java
More file actions
60 lines (54 loc) · 1.74 KB
/
LinkedStackTest.java
File metadata and controls
60 lines (54 loc) · 1.74 KB
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
package ssj.algorithm.collections;
import org.junit.Test;
import ssj.algorithm.ArrayUtil;
import ssj.algorithm.Stack;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by shenshijun on 15/2/3.
*/
public class LinkedStackTest {
@Test
public void testStack() {
LinkedStack<Integer> int_stack = new LinkedStack<>();
assertTrue(int_stack.isEmpty());
for (int i = 0; i < 100; i++) {
int_stack.push(i);
}
assertEquals(int_stack.size(), 100);
assertTrue(int_stack.head().equals(99));
for (int i = 99; i >= 0; i--) {
assertTrue(int_stack.pop().equals(i));
}
assertTrue(int_stack.isEmpty());
}
@Test
public void testStacks() {
LinkedStack<Integer> int_stack = new LinkedStack<>();
assertTrue(int_stack.isEmpty());
for (int i = 0; i < 100; i++) {
int_stack.push(i);
}
assertEquals(int_stack.size(), 100);
assertTrue(int_stack.head().equals(99));
for (int i = 99; i >= 0; i--) {
assertTrue(int_stack.pop().equals(i));
}
assertTrue(int_stack.isEmpty());
}
@Test
public void testStackSort() {
LinkedStack<Integer> string_stack = new LinkedStack<>();
Integer[] ints = new Integer[200];
for (int i = 0; i < 200; i++) {
ints[i] = (int) (Math.random() * 1000);
string_stack.push(ints[i]);
}
Stack<Integer> sorted_stack = string_stack.sortStack((a, b) -> a - b);
ArrayUtil.sort(ints);
for (int i = 199; i >= 0; i--) {
assertEquals(ints[i], sorted_stack.pop());
}
assertTrue(sorted_stack.isEmpty());
}
}