-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
QueueByTwoStacksTest.java
69 lines (58 loc) · 1.75 KB
/
QueueByTwoStacksTest.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
package com.thealgorithms.datastructures.queues;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class QueueByTwoStacksTest {
private QueueByTwoStacks<Integer> queue;
@BeforeEach
public void setUp() {
queue = new QueueByTwoStacks<>();
}
@Test
public void testEmptyQueue() {
assertEquals(0, queue.size());
}
@Test
public void testEnqueue() {
queue.put(10);
queue.put(20);
assertEquals(2, queue.size());
}
@Test
public void testDequeue() {
queue.put(10);
queue.put(20);
queue.put(30);
assertEquals(10, queue.get()); // First item out
assertEquals(20, queue.get()); // Second item out
assertEquals(30, queue.get()); // Third item out
}
@Test
public void testInterleavedOperations() {
queue.put(10);
queue.put(20);
assertEquals(10, queue.get()); // Dequeue first item
queue.put(30);
assertEquals(20, queue.get()); // Dequeue second item
assertEquals(30, queue.get()); // Dequeue third item
}
@Test
public void testQueueSize() {
assertEquals(0, queue.size());
queue.put(1);
assertEquals(1, queue.size());
queue.put(2);
queue.put(3);
assertEquals(3, queue.size());
queue.get();
assertEquals(2, queue.size());
}
@Test
public void testEmptyQueueException() {
assertThrows(NoSuchElementException.class, () -> {
queue.get(); // Attempting to dequeue from empty queue
});
}
}