|
| 1 | +package com.thealgorithms.datastructures.stacks; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
| 5 | + |
| 6 | +class StackUsingLinkedListTest { |
| 7 | + |
| 8 | + @Test |
| 9 | + void testPushAndPop() { |
| 10 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 11 | + stack.push(10); |
| 12 | + stack.push(20); |
| 13 | + stack.push(30); |
| 14 | + |
| 15 | + assertEquals(30, stack.pop()); |
| 16 | + assertEquals(20, stack.pop()); |
| 17 | + assertEquals(10, stack.pop()); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void testPeek() { |
| 22 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 23 | + stack.push(100); |
| 24 | + stack.push(200); |
| 25 | + |
| 26 | + assertEquals(200, stack.peek()); |
| 27 | + assertEquals(200, stack.peek()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void testIsEmpty() { |
| 32 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 33 | + assertTrue(stack.isEmpty()); |
| 34 | + |
| 35 | + stack.push(5); |
| 36 | + assertFalse(stack.isEmpty()); |
| 37 | + |
| 38 | + stack.pop(); |
| 39 | + assertTrue(stack.isEmpty()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testSize() { |
| 44 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 45 | + assertEquals(0, stack.size()); |
| 46 | + |
| 47 | + stack.push(1); |
| 48 | + assertEquals(1, stack.size()); |
| 49 | + |
| 50 | + stack.push(2); |
| 51 | + stack.push(3); |
| 52 | + assertEquals(3, stack.size()); |
| 53 | + |
| 54 | + stack.pop(); |
| 55 | + assertEquals(2, stack.size()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testPopEmptyStack() { |
| 60 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 61 | + assertThrows(RuntimeException.class, () -> stack.pop()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testPeekEmptyStack() { |
| 66 | + StackUsingLinkedList stack = new StackUsingLinkedList(); |
| 67 | + assertThrows(RuntimeException.class, () -> stack.peek()); |
| 68 | + } |
| 69 | +} |
0 commit comments