-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathQueueUsingTwoStacksTest.java
145 lines (122 loc) · 4.62 KB
/
QueueUsingTwoStacksTest.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.EmptyStackException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class QueueUsingTwoStacksTest {
private QueueUsingTwoStacks queue;
@BeforeEach
void setUp() {
queue = new QueueUsingTwoStacks();
}
@Test
void testIsEmptyInitially() {
assertTrue(queue.isEmpty(), "Queue should be empty initially");
}
@Test
void testInsertSingleElement() {
queue.insert(1);
assertFalse(queue.isEmpty(), "Queue should not be empty after inserting an element");
assertEquals(1, queue.peekFront(), "The front element should be the inserted element");
}
@Test
void testRemoveSingleElement() {
queue.insert(1);
assertEquals(1, queue.remove(), "Removing should return the first inserted element");
assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
}
@Test
void testRemoveMultipleElements() {
queue.insert(1);
queue.insert(2);
queue.insert(3);
assertEquals(1, queue.remove(), "First removed element should be the first inserted element");
assertEquals(2, queue.remove(), "Second removed element should be the second inserted element");
assertEquals(3, queue.remove(), "Third removed element should be the third inserted element");
assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements");
}
@Test
void testPeekFrontWithMultipleElements() {
queue.insert(1);
queue.insert(2);
queue.insert(3);
assertEquals(1, queue.peekFront(), "The front element should be the first inserted element");
}
@Test
void testPeekBackWithMultipleElements() {
queue.insert(1);
queue.insert(2);
queue.insert(3);
assertEquals(3, queue.peekBack(), "The back element should be the last inserted element");
}
@Test
void testPeekFrontAfterRemovals() {
queue.insert(1);
queue.insert(2);
queue.insert(3);
queue.remove();
assertEquals(2, queue.peekFront(), "After removing one element, the front should be the second element");
}
@Test
void testIsEmptyAfterRemovals() {
queue.insert(1);
queue.insert(2);
queue.remove();
queue.remove();
assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements");
}
@Test
void testRemoveFromEmptyQueue() {
org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::remove, "Removing from an empty queue should throw an exception");
}
@Test
void testPeekFrontFromEmptyQueue() {
org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekFront, "Peeking front from an empty queue should throw an exception");
}
@Test
void testPeekBackFromEmptyQueue() {
org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekBack, "Peeking back from an empty queue should throw an exception");
}
@Test
void testIsInStackEmptyInitially() {
assertTrue(queue.isInStackEmpty(), "inStack should be empty initially");
}
@Test
void testIsOutStackEmptyInitially() {
assertTrue(queue.isOutStackEmpty(), "outStack should be empty initially");
}
@Test
void testIsInStackEmptyAfterInsertion() {
queue.insert(1);
assertFalse(queue.isInStackEmpty(), "inStack should not be empty after an insertion");
}
@Test
void testIsOutStackEmptyAfterInsertion() {
queue.insert(1);
assertTrue(queue.isOutStackEmpty(), "outStack should still be empty after an insertion");
}
@Test
void testIsOutStackEmptyAfterRemoval() {
queue.insert(1);
queue.remove();
assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing the only element");
}
@Test
void testIsInStackEmptyAfterMultipleRemovals() {
queue.insert(1);
queue.insert(2);
queue.remove();
queue.remove();
assertTrue(queue.isInStackEmpty(), "inStack should be empty after removing all elements");
}
@Test
void testIsOutStackEmptyAfterMultipleRemovals() {
queue.insert(1);
queue.insert(2);
queue.remove();
queue.remove();
assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing all elements");
}
}