Skip to content

Commit e1a0155

Browse files
authored
Enhance docs, add tests in GenericArrayListQueue (#6016)
1 parent ab9a552 commit e1a0155

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
import java.util.List;
55

66
/**
7-
* This class implements a GenericArrayListQueue.
7+
* This class implements a GenericArrayListQueue, a queue data structure that
8+
* holds elements of any type specified at runtime, allowing flexibility in the type
9+
* of elements it stores.
810
*
9-
* A GenericArrayListQueue data structure functions the same as any
10-
* specific-typed queue. The GenericArrayListQueue holds elements of types
11-
* to-be-specified at runtime. The elements that are added first are the first
12-
* to be removed (FIFO). New elements are added to the back/rear of the queue.
11+
* <p>The GenericArrayListQueue operates on a First-In-First-Out (FIFO) basis, where
12+
* elements added first are the first to be removed. New elements are added to the back
13+
* (or rear) of the queue, while removal of elements occurs from the front.
14+
*
15+
* @param <T> The type of elements held in this queue.
1316
*/
1417
public class GenericArrayListQueue<T> {
1518

1619
/**
17-
* The generic List for the queue. T is the generic element type.
20+
* A list that stores the queue's elements in insertion order.
1821
*/
1922
private final List<T> elementList = new ArrayList<>();
2023

2124
/**
2225
* Checks if the queue is empty.
2326
*
24-
* @return True if the queue is empty, false otherwise.
27+
* @return {@code true} if the queue has no elements; {@code false} otherwise.
2528
*/
26-
private boolean isEmpty() {
29+
public boolean isEmpty() {
2730
return elementList.isEmpty();
2831
}
2932

3033
/**
31-
* Returns the element at the front of the queue without removing it.
34+
* Retrieves, but does not remove, the element at the front of the queue.
3235
*
33-
* @return The element at the front of the queue, or null if the queue is empty.
36+
* @return The element at the front of the queue, or {@code null} if the queue is empty.
3437
*/
3538
public T peek() {
3639
return isEmpty() ? null : elementList.getFirst();
3740
}
3841

3942
/**
40-
* Inserts an element of type T to the back of the queue.
43+
* Inserts an element at the back of the queue.
4144
*
42-
* @param element the element to be added to the queue.
43-
* @return True if the element was added successfully.
45+
* @param element The element to be added to the queue.
46+
* @return {@code true} if the element was successfully added.
4447
*/
4548
public boolean add(T element) {
4649
return elementList.add(element);
@@ -49,7 +52,7 @@ public boolean add(T element) {
4952
/**
5053
* Retrieves and removes the element at the front of the queue.
5154
*
52-
* @return The element removed from the front of the queue, or null if the queue is empty.
55+
* @return The element removed from the front of the queue, or {@code null} if the queue is empty.
5356
*/
5457
public T poll() {
5558
return isEmpty() ? null : elementList.removeFirst();
Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.queues;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertNull;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
67

@@ -13,43 +14,87 @@ void testAdd() {
1314
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
1415
assertTrue(queue.add(10));
1516
assertTrue(queue.add(20));
17+
assertEquals(10, queue.peek()); // Ensure the first added element is at the front
1618
}
1719

1820
@Test
1921
void testPeek() {
2022
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
21-
assertNull(queue.peek());
23+
assertNull(queue.peek(), "Peek should return null for an empty queue");
2224

2325
queue.add(10);
2426
queue.add(20);
2527

26-
assertEquals(10, queue.peek());
28+
assertEquals(10, queue.peek(), "Peek should return the first element (10)");
2729
queue.poll();
28-
assertEquals(20, queue.peek());
30+
assertEquals(20, queue.peek(), "Peek should return the next element (20) after poll");
2931
}
3032

3133
@Test
3234
void testPoll() {
3335
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
34-
assertNull(queue.poll());
36+
assertNull(queue.poll(), "Poll should return null for an empty queue");
3537

3638
queue.add(10);
3739
queue.add(20);
3840

39-
assertEquals(10, queue.poll());
40-
assertEquals(20, queue.poll());
41-
assertNull(queue.poll());
41+
assertEquals(10, queue.poll(), "Poll should return and remove the first element (10)");
42+
assertEquals(20, queue.poll(), "Poll should return and remove the next element (20)");
43+
assertNull(queue.poll(), "Poll should return null when queue is empty after removals");
4244
}
4345

4446
@Test
4547
void testIsEmpty() {
4648
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
47-
assertNull(queue.peek());
48-
assertNull(queue.poll());
49+
assertTrue(queue.isEmpty(), "Queue should initially be empty");
4950

5051
queue.add(30);
51-
assertEquals(30, queue.peek());
52-
assertEquals(30, queue.poll());
53-
assertNull(queue.peek());
52+
assertFalse(queue.isEmpty(), "Queue should not be empty after adding an element");
53+
queue.poll();
54+
assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
55+
}
56+
57+
@Test
58+
void testClearQueueAndReuse() {
59+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
60+
queue.add(5);
61+
queue.add(10);
62+
queue.poll();
63+
queue.poll(); // Remove all elements
64+
65+
assertTrue(queue.isEmpty(), "Queue should be empty after all elements are removed");
66+
assertNull(queue.peek(), "Peek should return null on an empty queue after clear");
67+
assertTrue(queue.add(15), "Queue should be reusable after being emptied");
68+
assertEquals(15, queue.peek(), "Newly added element should be accessible in the empty queue");
69+
}
70+
71+
@Test
72+
void testOrderMaintained() {
73+
GenericArrayListQueue<String> queue = new GenericArrayListQueue<>();
74+
queue.add("First");
75+
queue.add("Second");
76+
queue.add("Third");
77+
78+
assertEquals("First", queue.poll(), "Order should be maintained; expected 'First'");
79+
assertEquals("Second", queue.poll(), "Order should be maintained; expected 'Second'");
80+
assertEquals("Third", queue.poll(), "Order should be maintained; expected 'Third'");
81+
}
82+
83+
@Test
84+
void testVariousDataTypes() {
85+
GenericArrayListQueue<Double> queue = new GenericArrayListQueue<>();
86+
queue.add(1.1);
87+
queue.add(2.2);
88+
89+
assertEquals(1.1, queue.peek(), "Queue should handle Double data type correctly");
90+
assertEquals(1.1, queue.poll(), "Poll should return correct Double value");
91+
assertEquals(2.2, queue.peek(), "Peek should show next Double value in the queue");
92+
}
93+
94+
@Test
95+
void testEmptyPollAndPeekBehavior() {
96+
GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<>();
97+
assertNull(queue.peek(), "Peek on an empty queue should return null");
98+
assertNull(queue.poll(), "Poll on an empty queue should return null");
5499
}
55100
}

0 commit comments

Comments
 (0)