|
1 | 1 | package com.thealgorithms.datastructures.lists;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertSame; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
5 | 6 |
|
| 7 | +import org.junit.jupiter.api.BeforeEach; |
6 | 8 | import org.junit.jupiter.api.Test;
|
7 | 9 |
|
8 | 10 | public class CircleLinkedListTest {
|
9 | 11 |
|
| 12 | + private CircleLinkedList<Integer> list; |
| 13 | + |
| 14 | + @BeforeEach |
| 15 | + public void setUp() { |
| 16 | + list = new CircleLinkedList<>(); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testInitialSize() { |
| 21 | + assertEquals(0, list.getSize(), "Initial size should be 0."); |
| 22 | + } |
| 23 | + |
10 | 24 | @Test
|
11 | 25 | public void testAppendAndSize() {
|
12 |
| - CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
13 | 26 | list.append(1);
|
14 | 27 | list.append(2);
|
15 | 28 | list.append(3);
|
16 | 29 |
|
17 |
| - assertEquals(3, list.getSize()); |
18 |
| - assertEquals("[ 1, 2, 3 ]", list.toString()); |
| 30 | + assertEquals(3, list.getSize(), "Size after three appends should be 3."); |
| 31 | + assertEquals("[ 1, 2, 3 ]", list.toString(), "List content should match appended values."); |
19 | 32 | }
|
20 | 33 |
|
21 | 34 | @Test
|
22 | 35 | public void testRemove() {
|
23 |
| - CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
24 | 36 | list.append(1);
|
25 | 37 | list.append(2);
|
26 | 38 | list.append(3);
|
27 | 39 | list.append(4);
|
28 | 40 |
|
29 |
| - assertEquals(2, list.remove(1)); |
30 |
| - assertEquals(3, list.remove(1)); |
31 |
| - assertEquals("[ 1, 4 ]", list.toString()); |
32 |
| - assertEquals(2, list.getSize()); |
| 41 | + assertEquals(2, list.remove(1), "Removed element at index 1 should be 2."); |
| 42 | + assertEquals(3, list.remove(1), "Removed element at index 1 after update should be 3."); |
| 43 | + assertEquals("[ 1, 4 ]", list.toString(), "List content should reflect removals."); |
| 44 | + assertEquals(2, list.getSize(), "Size after two removals should be 2."); |
33 | 45 | }
|
34 | 46 |
|
35 | 47 | @Test
|
36 | 48 | public void testRemoveInvalidIndex() {
|
37 |
| - CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
38 | 49 | list.append(1);
|
39 | 50 | list.append(2);
|
40 | 51 |
|
41 |
| - assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2)); |
42 |
| - assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1)); |
| 52 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2), "Should throw on out-of-bounds index."); |
| 53 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1), "Should throw on negative index."); |
43 | 54 | }
|
44 | 55 |
|
45 | 56 | @Test
|
46 | 57 | public void testToStringEmpty() {
|
47 |
| - CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
48 |
| - assertEquals("[]", list.toString()); |
| 58 | + assertEquals("[]", list.toString(), "Empty list should be represented by '[]'."); |
49 | 59 | }
|
50 | 60 |
|
51 | 61 | @Test
|
52 | 62 | public void testToStringAfterRemoval() {
|
53 |
| - CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
54 | 63 | list.append(1);
|
55 | 64 | list.append(2);
|
56 | 65 | list.append(3);
|
57 | 66 | list.remove(1);
|
58 | 67 |
|
59 |
| - assertEquals("[ 1, 3 ]", list.toString()); |
| 68 | + assertEquals("[ 1, 3 ]", list.toString(), "List content should match remaining elements after removal."); |
60 | 69 | }
|
61 | 70 |
|
62 | 71 | @Test
|
63 | 72 | public void testSingleElement() {
|
64 |
| - CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
65 | 73 | list.append(1);
|
66 | 74 |
|
67 |
| - assertEquals(1, list.getSize()); |
68 |
| - assertEquals("[ 1 ]", list.toString()); |
69 |
| - assertEquals(1, list.remove(0)); |
70 |
| - assertEquals("[]", list.toString()); |
| 75 | + assertEquals(1, list.getSize(), "Size after single append should be 1."); |
| 76 | + assertEquals("[ 1 ]", list.toString(), "Single element list should display properly."); |
| 77 | + assertEquals(1, list.remove(0), "Single element removed should match appended value."); |
| 78 | + assertEquals("[]", list.toString(), "List should be empty after removing the single element."); |
71 | 79 | }
|
72 | 80 |
|
73 | 81 | @Test
|
74 | 82 | public void testNullElement() {
|
75 |
| - CircleLinkedList<String> list = new CircleLinkedList<>(); |
76 |
| - assertThrows(NullPointerException.class, () -> list.append(null)); |
| 83 | + assertThrows(NullPointerException.class, () -> list.append(null), "Appending null should throw exception."); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testCircularReference() { |
| 88 | + list.append(1); |
| 89 | + list.append(2); |
| 90 | + list.append(3); |
| 91 | + CircleLinkedList.Node<Integer> current = list.head; |
| 92 | + |
| 93 | + // Traverse one full cycle and verify the circular reference |
| 94 | + for (int i = 0; i <= list.getSize(); i++) { |
| 95 | + current = current.next; |
| 96 | + } |
| 97 | + assertEquals(list.head, current, "End of list should point back to the head (circular structure)."); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testClear() { |
| 102 | + list.append(1); |
| 103 | + list.append(2); |
| 104 | + list.append(3); |
| 105 | + |
| 106 | + // Remove all elements to simulate clearing the list |
| 107 | + for (int i = list.getSize() - 1; i >= 0; i--) { |
| 108 | + list.remove(i); |
| 109 | + } |
| 110 | + |
| 111 | + assertEquals(0, list.getSize(), "Size after clearing should be 0."); |
| 112 | + assertEquals("[]", list.toString(), "Empty list should be represented by '[]' after clear."); |
| 113 | + assertSame(list.head.next, list.head, "Head's next should point to itself after clearing."); |
77 | 114 | }
|
78 | 115 | }
|
0 commit comments