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