forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStackTest.java
More file actions
77 lines (64 loc) · 2 KB
/
SortStackTest.java
File metadata and controls
77 lines (64 loc) · 2 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Stack;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class SortStackTest {
private Stack<Integer> stack;
@BeforeEach
public void setUp() {
stack = new Stack<>();
}
@Test
public void testSortEmptyStack() {
SortStack.sortStack(stack);
assertTrue(stack.isEmpty()); // An empty stack should remain empty
}
@Test
public void testSortSingleElementStack() {
stack.push(10);
SortStack.sortStack(stack);
assertEquals(1, stack.size());
assertEquals(10, (int) stack.peek()); // Single element should remain unchanged
}
@Test
public void testSortAlreadySortedStack() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
SortStack.sortStack(stack);
assertEquals(4, stack.size());
assertEquals(4, (int) stack.pop());
assertEquals(3, (int) stack.pop());
assertEquals(2, (int) stack.pop());
assertEquals(1, (int) stack.pop());
}
@Test
public void testSortUnsortedStack() {
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(2);
SortStack.sortStack(stack);
assertEquals(4, stack.size());
assertEquals(4, (int) stack.pop());
assertEquals(3, (int) stack.pop());
assertEquals(2, (int) stack.pop());
assertEquals(1, (int) stack.pop());
}
@Test
public void testSortWithDuplicateElements() {
stack.push(3);
stack.push(1);
stack.push(3);
stack.push(2);
SortStack.sortStack(stack);
assertEquals(4, stack.size());
assertEquals(3, (int) stack.pop());
assertEquals(3, (int) stack.pop());
assertEquals(2, (int) stack.pop());
assertEquals(1, (int) stack.pop());
}
}