Skip to content

Commit f4b7991

Browse files
test: add proper unit tests for ThreadSafeQueue
1 parent b2199c9 commit f4b7991

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package com.thealgorithms.datastructures.queues;
22

3-
public class ThreadSafeQueueTest {
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
45

5-
public static void main(String[] args) {
6+
class ThreadSafeQueueTest {
67

8+
@Test
9+
void testQueueOperations() {
710
ThreadSafeQueue<Integer> queue = new ThreadSafeQueue<>();
811

9-
Thread producer = new Thread(() -> {
10-
for (int i = 1; i <= 5; i++) {
11-
queue.enqueue(i);
12-
System.out.println("Enqueued: " + i);
13-
}
14-
});
15-
16-
Thread consumer = new Thread(() -> {
17-
for (int i = 1; i <= 5; i++) {
18-
Integer val = queue.dequeue();
19-
System.out.println("Dequeued: " + val);
20-
}
21-
});
22-
23-
producer.start();
24-
consumer.start();
12+
assertTrue(queue.isEmpty());
13+
14+
queue.enqueue(1);
15+
queue.enqueue(2);
16+
17+
assertFalse(queue.isEmpty());
18+
19+
assertEquals(1, queue.dequeue());
20+
assertEquals(2, queue.dequeue());
21+
22+
assertTrue(queue.isEmpty());
23+
}
24+
25+
@Test
26+
void testDequeueEmpty() {
27+
ThreadSafeQueue<Integer> queue = new ThreadSafeQueue<>();
28+
29+
assertNull(queue.dequeue());
2530
}
2631
}

0 commit comments

Comments
 (0)