|
1 | 1 | package com.thealgorithms.datastructures.stacks; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
3 | 8 | import org.junit.jupiter.api.Test; |
4 | | -import static org.junit.jupiter.api.Assertions.*; |
5 | 9 |
|
6 | 10 | class StackUsingLinkedListTest { |
7 | | - |
| 11 | + |
8 | 12 | @Test |
9 | 13 | void testPushAndPop() { |
10 | 14 | StackUsingLinkedList stack = new StackUsingLinkedList(); |
11 | 15 | stack.push(10); |
12 | 16 | stack.push(20); |
13 | 17 | stack.push(30); |
14 | | - |
| 18 | + |
15 | 19 | assertEquals(30, stack.pop()); |
16 | 20 | assertEquals(20, stack.pop()); |
17 | 21 | assertEquals(10, stack.pop()); |
18 | 22 | } |
19 | | - |
| 23 | + |
20 | 24 | @Test |
21 | 25 | void testPeek() { |
22 | 26 | StackUsingLinkedList stack = new StackUsingLinkedList(); |
23 | 27 | stack.push(100); |
24 | 28 | stack.push(200); |
25 | | - |
| 29 | + |
26 | 30 | assertEquals(200, stack.peek()); |
27 | 31 | assertEquals(200, stack.peek()); |
| 32 | + assertEquals(2, stack.size()); |
28 | 33 | } |
29 | | - |
| 34 | + |
30 | 35 | @Test |
31 | 36 | void testIsEmpty() { |
32 | 37 | StackUsingLinkedList stack = new StackUsingLinkedList(); |
33 | 38 | assertTrue(stack.isEmpty()); |
34 | | - |
| 39 | + |
35 | 40 | stack.push(5); |
36 | 41 | assertFalse(stack.isEmpty()); |
37 | | - |
| 42 | + |
38 | 43 | stack.pop(); |
39 | 44 | assertTrue(stack.isEmpty()); |
40 | 45 | } |
41 | | - |
| 46 | + |
42 | 47 | @Test |
43 | 48 | void testSize() { |
44 | 49 | StackUsingLinkedList stack = new StackUsingLinkedList(); |
45 | 50 | assertEquals(0, stack.size()); |
46 | | - |
| 51 | + |
47 | 52 | stack.push(1); |
48 | 53 | assertEquals(1, stack.size()); |
49 | | - |
| 54 | + |
50 | 55 | stack.push(2); |
51 | 56 | stack.push(3); |
52 | 57 | assertEquals(3, stack.size()); |
53 | | - |
| 58 | + |
54 | 59 | stack.pop(); |
55 | 60 | assertEquals(2, stack.size()); |
56 | 61 | } |
57 | | - |
| 62 | + |
58 | 63 | @Test |
59 | 64 | void testPopEmptyStack() { |
60 | 65 | StackUsingLinkedList stack = new StackUsingLinkedList(); |
61 | | - assertThrows(RuntimeException.class, () -> stack.pop()); |
| 66 | + assertThrows(RuntimeException.class, stack::pop); |
62 | 67 | } |
63 | | - |
| 68 | + |
64 | 69 | @Test |
65 | 70 | void testPeekEmptyStack() { |
66 | 71 | StackUsingLinkedList stack = new StackUsingLinkedList(); |
67 | | - assertThrows(RuntimeException.class, () -> stack.peek()); |
| 72 | + assertThrows(RuntimeException.class, stack::peek); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testMultipleOperations() { |
| 77 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 78 | + |
| 79 | + assertTrue(stack.isEmpty()); |
| 80 | + |
| 81 | + stack.push(1); |
| 82 | + stack.push(2); |
| 83 | + assertEquals(2, stack.pop()); |
| 84 | + |
| 85 | + stack.push(3); |
| 86 | + stack.push(4); |
| 87 | + assertEquals(4, stack.peek()); |
| 88 | + assertEquals(3, stack.size()); |
| 89 | + |
| 90 | + assertFalse(stack.isEmpty()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testSingleElement() { |
| 95 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 96 | + stack.push(42); |
| 97 | + |
| 98 | + assertEquals(42, stack.peek()); |
| 99 | + assertEquals(1, stack.size()); |
| 100 | + assertEquals(42, stack.pop()); |
| 101 | + assertTrue(stack.isEmpty()); |
68 | 102 | } |
69 | 103 | } |
0 commit comments