-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathReverseStackUsingRecursionTest.java
58 lines (42 loc) · 1.49 KB
/
ReverseStackUsingRecursionTest.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
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Stack;
import org.junit.jupiter.api.Test;
public class ReverseStackUsingRecursionTest {
@Test
void testReverseWithMultipleElements() {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < 5; i++) {
stack.push(i);
}
ReverseStackUsingRecursion.reverse(stack);
for (int i = 0; i < 5; i++) {
assertEquals(i, stack.pop());
}
assertTrue(stack.isEmpty());
}
@Test
void testReverseWithSingleElement() {
Stack<Integer> stack = new Stack<>();
stack.push(1);
ReverseStackUsingRecursion.reverse(stack);
assertEquals(1, stack.pop());
assertTrue(stack.isEmpty());
}
@Test
void testReverseWithEmptyStack() {
Stack<Integer> stack = new Stack<>();
ReverseStackUsingRecursion.reverse(stack);
assertTrue(stack.isEmpty());
}
@Test
void testReverseWithNullStack() {
Stack<Integer> stack = null;
Exception exception = assertThrows(IllegalArgumentException.class, () -> ReverseStackUsingRecursion.reverse(stack));
String expectedMessage = "Stack cannot be null";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
}