-
Notifications
You must be signed in to change notification settings - Fork 19.8k
/
Copy pathStackArrayTest.java
129 lines (105 loc) · 3.34 KB
/
StackArrayTest.java
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
package com.thealgorithms.datastructures.stacks;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class StackArrayTest {
private Stack<Integer> stack;
@BeforeEach
void setUp() {
stack = new StackArray<>(5); // Initialize a stack with capacity of 5
}
@Test
void testPushAndPop() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
Assertions.assertEquals(5, stack.pop());
Assertions.assertEquals(4, stack.pop());
Assertions.assertEquals(3, stack.pop());
Assertions.assertEquals(2, stack.pop());
Assertions.assertEquals(1, stack.pop());
}
@Test
void testPeek() {
stack.push(10);
stack.push(20);
stack.push(30);
Assertions.assertEquals(30, stack.peek());
Assertions.assertEquals(3, stack.size());
stack.pop();
Assertions.assertEquals(20, stack.peek());
}
@Test
void testIsEmpty() {
Assertions.assertTrue(stack.isEmpty());
stack.push(42);
Assertions.assertFalse(stack.isEmpty());
stack.pop();
Assertions.assertTrue(stack.isEmpty());
}
@Test
void testResizeOnPush() {
StackArray<Integer> smallStack = new StackArray<>(2);
smallStack.push(1);
smallStack.push(2);
Assertions.assertTrue(smallStack.isFull());
smallStack.push(3);
Assertions.assertFalse(smallStack.isFull());
Assertions.assertEquals(3, smallStack.size());
Assertions.assertEquals(3, smallStack.pop());
Assertions.assertEquals(2, smallStack.pop());
Assertions.assertEquals(1, smallStack.pop());
}
@Test
void testResizeOnPop() {
StackArray<Integer> stack = new StackArray<>(4);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.pop();
stack.pop();
stack.pop();
Assertions.assertEquals(1, stack.size());
stack.pop();
Assertions.assertTrue(stack.isEmpty());
}
@Test
void testMakeEmpty() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.makeEmpty();
Assertions.assertTrue(stack.isEmpty());
Assertions.assertThrows(IllegalStateException.class, stack::pop);
}
@Test
void testPopEmptyStackThrowsException() {
Assertions.assertThrows(IllegalStateException.class, stack::pop);
}
@Test
void testPeekEmptyStackThrowsException() {
Assertions.assertThrows(IllegalStateException.class, stack::peek);
}
@Test
void testConstructorWithInvalidSizeThrowsException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new StackArray<>(0));
Assertions.assertThrows(IllegalArgumentException.class, () -> new StackArray<>(-5));
}
@Test
void testDefaultConstructor() {
StackArray<Integer> defaultStack = new StackArray<>();
Assertions.assertEquals(0, defaultStack.size());
defaultStack.push(1);
Assertions.assertEquals(1, defaultStack.size());
}
@Test
void testToString() {
stack.push(1);
stack.push(2);
stack.push(3);
Assertions.assertEquals("StackArray [1, 2, 3]", stack.toString());
}
}