|
| 1 | +package com.thealgorithms.others; |
| 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.assertTrue; |
| 6 | + |
| 7 | +import java.util.EmptyStackException; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class QueueUsingTwoStacksTest { |
| 12 | + |
| 13 | + private QueueUsingTwoStacks queue; |
| 14 | + |
| 15 | + @BeforeEach |
| 16 | + void setUp() { |
| 17 | + queue = new QueueUsingTwoStacks(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void testIsEmptyInitially() { |
| 22 | + assertTrue(queue.isEmpty(), "Queue should be empty initially"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void testInsertSingleElement() { |
| 27 | + queue.insert(1); |
| 28 | + assertFalse(queue.isEmpty(), "Queue should not be empty after inserting an element"); |
| 29 | + assertEquals(1, queue.peekFront(), "The front element should be the inserted element"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testRemoveSingleElement() { |
| 34 | + queue.insert(1); |
| 35 | + assertEquals(1, queue.remove(), "Removing should return the first inserted element"); |
| 36 | + assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testRemoveMultipleElements() { |
| 41 | + queue.insert(1); |
| 42 | + queue.insert(2); |
| 43 | + queue.insert(3); |
| 44 | + assertEquals(1, queue.remove(), "First removed element should be the first inserted element"); |
| 45 | + assertEquals(2, queue.remove(), "Second removed element should be the second inserted element"); |
| 46 | + assertEquals(3, queue.remove(), "Third removed element should be the third inserted element"); |
| 47 | + assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testPeekFrontWithMultipleElements() { |
| 52 | + queue.insert(1); |
| 53 | + queue.insert(2); |
| 54 | + queue.insert(3); |
| 55 | + assertEquals(1, queue.peekFront(), "The front element should be the first inserted element"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testPeekBackWithMultipleElements() { |
| 60 | + queue.insert(1); |
| 61 | + queue.insert(2); |
| 62 | + queue.insert(3); |
| 63 | + assertEquals(3, queue.peekBack(), "The back element should be the last inserted element"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testPeekFrontAfterRemovals() { |
| 68 | + queue.insert(1); |
| 69 | + queue.insert(2); |
| 70 | + queue.insert(3); |
| 71 | + queue.remove(); |
| 72 | + assertEquals(2, queue.peekFront(), "After removing one element, the front should be the second element"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testIsEmptyAfterRemovals() { |
| 77 | + queue.insert(1); |
| 78 | + queue.insert(2); |
| 79 | + queue.remove(); |
| 80 | + queue.remove(); |
| 81 | + assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testRemoveFromEmptyQueue() { |
| 86 | + org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::remove, "Removing from an empty queue should throw an exception"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testPeekFrontFromEmptyQueue() { |
| 91 | + org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekFront, "Peeking front from an empty queue should throw an exception"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testPeekBackFromEmptyQueue() { |
| 96 | + org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekBack, "Peeking back from an empty queue should throw an exception"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testIsInStackEmptyInitially() { |
| 101 | + assertTrue(queue.isInStackEmpty(), "inStack should be empty initially"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testIsOutStackEmptyInitially() { |
| 106 | + assertTrue(queue.isOutStackEmpty(), "outStack should be empty initially"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testIsInStackEmptyAfterInsertion() { |
| 111 | + queue.insert(1); |
| 112 | + assertFalse(queue.isInStackEmpty(), "inStack should not be empty after an insertion"); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testIsOutStackEmptyAfterInsertion() { |
| 117 | + queue.insert(1); |
| 118 | + assertTrue(queue.isOutStackEmpty(), "outStack should still be empty after an insertion"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testIsOutStackEmptyAfterRemoval() { |
| 123 | + queue.insert(1); |
| 124 | + queue.remove(); |
| 125 | + assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing the only element"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void testIsInStackEmptyAfterMultipleRemovals() { |
| 130 | + queue.insert(1); |
| 131 | + queue.insert(2); |
| 132 | + queue.remove(); |
| 133 | + queue.remove(); |
| 134 | + assertTrue(queue.isInStackEmpty(), "inStack should be empty after removing all elements"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testIsOutStackEmptyAfterMultipleRemovals() { |
| 139 | + queue.insert(1); |
| 140 | + queue.insert(2); |
| 141 | + queue.remove(); |
| 142 | + queue.remove(); |
| 143 | + assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing all elements"); |
| 144 | + } |
| 145 | +} |
0 commit comments