Repository: TheAlgorithms/Java
Pull request #5422 (2024-08-27 21:51:14)

test: `CircleLinkedListTest`
Added tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5422?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 49.66%. Comparing base [(`a9bc7c2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a9bc7c269d3146aa2d67d69f2922f1bb2b07b4c5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`14a204d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/14a204d3e0442911f6cfa1dc179f8ab0944e6ff4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5422      +/-   ##
============================================
+ Coverage     49.34%   49.66%   +0.31%     
- Complexity     3067     3080      +13     
============================================
  Files           524      524              
  Lines         15204    15195       -9     
  Branches       2890     2890              
============================================
+ Hits           7503     7547      +44     
+ Misses         7389     7336      -53     
  Partials        312      312              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5422?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java
index 6eb9d75f..2b50f731 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java
@@ -51,22 +51,25 @@ public class CircleLinkedList<E> {
         size++;
     }
 
-    // utility function for traversing the list
     public String toString() {
-        Node<E> p = head.next;
-        String s = "[ ";
-        while (p != head) {
-            s += p.value;
-            if (p != tail) {
-                s += " , ";
+        if (size == 0) {
+            return "[]";
+        }
+        StringBuilder sb = new StringBuilder("[ ");
+        Node<E> current = head.next;
+        while (current != head) {
+            sb.append(current.value);
+            if (current.next != head) {
+                sb.append(", ");
             }
-            p = p.next;
+            current = current.next;
         }
-        return s + " ]";
+        sb.append(" ]");
+        return sb.toString();
     }
 
     public E remove(int pos) {
-        if (pos > size || pos < 0) {
+        if (pos >= size || pos < 0) {
             // catching errors
             throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
         }
@@ -89,18 +92,4 @@ public class CircleLinkedList<E> {
         size--;
         return saved;
     }
-
-    public static void main(String[] args) {
-        CircleLinkedList<Integer> cl = new CircleLinkedList<>();
-        cl.append(12);
-        System.out.println(cl);
-        cl.append(23);
-        System.out.println(cl);
-        cl.append(34);
-        System.out.println(cl);
-        cl.append(56);
-        System.out.println(cl);
-        cl.remove(3);
-        System.out.println(cl);
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java
new file mode 100644
index 00000000..b7a05ef2
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java
@@ -0,0 +1,78 @@
+package com.thealgorithms.datastructures.lists;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+public class CircleLinkedListTest {
+
+    @Test
+    public void testAppendAndSize() {
+        CircleLinkedList<Integer> list = new CircleLinkedList<>();
+        list.append(1);
+        list.append(2);
+        list.append(3);
+
+        assertEquals(3, list.getSize());
+        assertEquals("[ 1, 2, 3 ]", list.toString());
+    }
+
+    @Test
+    public void testRemove() {
+        CircleLinkedList<Integer> list = new CircleLinkedList<>();
+        list.append(1);
+        list.append(2);
+        list.append(3);
+        list.append(4);
+
+        assertEquals(2, list.remove(1));
+        assertEquals(3, list.remove(1));
+        assertEquals("[ 1, 4 ]", list.toString());
+        assertEquals(2, list.getSize());
+    }
+
+    @Test
+    public void testRemoveInvalidIndex() {
+        CircleLinkedList<Integer> list = new CircleLinkedList<>();
+        list.append(1);
+        list.append(2);
+
+        assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2));
+        assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1));
+    }
+
+    @Test
+    public void testToStringEmpty() {
+        CircleLinkedList<Integer> list = new CircleLinkedList<>();
+        assertEquals("[]", list.toString());
+    }
+
+    @Test
+    public void testToStringAfterRemoval() {
+        CircleLinkedList<Integer> list = new CircleLinkedList<>();
+        list.append(1);
+        list.append(2);
+        list.append(3);
+        list.remove(1);
+
+        assertEquals("[ 1, 3 ]", list.toString());
+    }
+
+    @Test
+    public void testSingleElement() {
+        CircleLinkedList<Integer> list = new CircleLinkedList<>();
+        list.append(1);
+
+        assertEquals(1, list.getSize());
+        assertEquals("[ 1 ]", list.toString());
+        assertEquals(1, list.remove(0));
+        assertEquals("[]", list.toString());
+    }
+
+    @Test
+    public void testNullElement() {
+        CircleLinkedList<String> list = new CircleLinkedList<>();
+        assertThrows(NullPointerException.class, () -> list.append(null));
+    }
+}