|
5 | 5 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
6 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
7 | 7 |
|
8 |
| -import java.io.ByteArrayOutputStream; |
9 |
| -import java.io.PrintStream; |
10 |
| -import org.junit.jupiter.api.BeforeEach; |
11 | 8 | import org.junit.jupiter.api.Test;
|
12 | 9 |
|
13 | 10 | class NodeStackTest {
|
14 | 11 |
|
15 |
| - private NodeStack<Integer> stack; |
16 |
| - private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); |
| 12 | + @Test |
| 13 | + void testPush() { |
| 14 | + NodeStack<Integer> stack = new NodeStack<>(); |
| 15 | + stack.push(10); |
| 16 | + stack.push(20); |
| 17 | + assertEquals(20, stack.peek(), "Top element should be 20 after pushing 10 and 20."); |
| 18 | + } |
17 | 19 |
|
18 |
| - @BeforeEach |
19 |
| - void setUp() { |
20 |
| - stack = new NodeStack<>(); |
21 |
| - System.setOut(new PrintStream(outputStreamCaptor)); |
| 20 | + @Test |
| 21 | + void testPop() { |
| 22 | + NodeStack<String> stack = new NodeStack<>(); |
| 23 | + stack.push("First"); |
| 24 | + stack.push("Second"); |
| 25 | + assertEquals("Second", stack.pop(), "Pop should return 'Second', the last pushed element."); |
| 26 | + assertEquals("First", stack.pop(), "Pop should return 'First' after 'Second' is removed."); |
22 | 27 | }
|
23 | 28 |
|
24 | 29 | @Test
|
25 |
| - void testPushAndPeek() { |
26 |
| - stack.push(3); |
27 |
| - assertEquals(3, stack.peek(), "Peek should return the last pushed item"); |
28 |
| - stack.push(4); |
29 |
| - stack.push(5); |
30 |
| - assertEquals(5, stack.peek(), "Peek should return the last pushed item"); |
| 30 | + void testPopOnEmptyStack() { |
| 31 | + NodeStack<Double> stack = new NodeStack<>(); |
| 32 | + assertThrows(IllegalStateException.class, stack::pop, "Popping an empty stack should throw IllegalStateException."); |
31 | 33 | }
|
32 | 34 |
|
33 | 35 | @Test
|
34 |
| - void testPop() { |
| 36 | + void testPeek() { |
| 37 | + NodeStack<Integer> stack = new NodeStack<>(); |
35 | 38 | stack.push(5);
|
36 |
| - stack.push(7); |
37 |
| - assertEquals(7, stack.pop(), "Pop should return the last pushed item"); |
38 |
| - assertEquals(5, stack.pop(), "Pop should return the next item in stack"); |
39 |
| - assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements"); |
| 39 | + stack.push(15); |
| 40 | + assertEquals(15, stack.peek(), "Peek should return 15, the top element."); |
| 41 | + stack.pop(); |
| 42 | + assertEquals(5, stack.peek(), "Peek should return 5 after 15 is popped."); |
40 | 43 | }
|
41 | 44 |
|
42 | 45 | @Test
|
43 |
| - void testIsEmpty() { |
44 |
| - assertTrue(stack.isEmpty(), "New stack should be empty"); |
45 |
| - stack.push(1); |
46 |
| - assertFalse(stack.isEmpty(), "Stack should not be empty after push"); |
| 46 | + void testPeekOnEmptyStack() { |
| 47 | + NodeStack<String> stack = new NodeStack<>(); |
| 48 | + assertThrows(IllegalStateException.class, stack::peek, "Peeking an empty stack should throw IllegalStateException."); |
47 | 49 | }
|
48 | 50 |
|
49 | 51 | @Test
|
50 |
| - void testSize() { |
51 |
| - assertEquals(0, stack.size(), "New stack should have size 0"); |
52 |
| - stack.push(10); |
53 |
| - stack.push(20); |
54 |
| - assertEquals(2, stack.size(), "Stack size should be 2 after two pushes"); |
| 52 | + void testIsEmpty() { |
| 53 | + NodeStack<Character> stack = new NodeStack<>(); |
| 54 | + assertTrue(stack.isEmpty(), "Newly initialized stack should be empty."); |
| 55 | + stack.push('A'); |
| 56 | + assertFalse(stack.isEmpty(), "Stack should not be empty after a push operation."); |
| 57 | + stack.pop(); |
| 58 | + assertTrue(stack.isEmpty(), "Stack should be empty after popping the only element."); |
55 | 59 | }
|
56 | 60 |
|
57 | 61 | @Test
|
58 |
| - void testPopOnEmptyStack() { |
59 |
| - assertThrows(IllegalStateException.class, () -> { stack.pop(); }); |
| 62 | + void testSize() { |
| 63 | + NodeStack<Integer> stack = new NodeStack<>(); |
| 64 | + assertEquals(0, stack.size(), "Size of empty stack should be 0."); |
| 65 | + stack.push(3); |
| 66 | + stack.push(6); |
| 67 | + assertEquals(2, stack.size(), "Size should be 2 after pushing two elements."); |
| 68 | + stack.pop(); |
| 69 | + assertEquals(1, stack.size(), "Size should be 1 after popping one element."); |
| 70 | + stack.pop(); |
| 71 | + assertEquals(0, stack.size(), "Size should be 0 after popping all elements."); |
60 | 72 | }
|
61 | 73 |
|
62 | 74 | @Test
|
63 |
| - void testPeekOnEmptyStack() { |
64 |
| - assertThrows(IllegalStateException.class, () -> { stack.peek(); }); |
| 75 | + void testPrint() { |
| 76 | + NodeStack<Integer> stack = new NodeStack<>(); |
| 77 | + stack.push(1); |
| 78 | + stack.push(2); |
| 79 | + stack.push(3); |
| 80 | + |
| 81 | + // Output verification would ideally be handled through a different means |
| 82 | + // but you can print as a basic check to confirm method runs without errors. |
| 83 | + stack.print(); |
65 | 84 | }
|
66 | 85 | }
|
0 commit comments