test: CircleLinkedListTest (#5422)

* test: CircleLinkedListTest

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-08-28 18:31:39 +02:00 committed by GitHub
parent a9bc7c269d
commit 45563ccbde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 92 additions and 25 deletions

View File

@ -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 += " , ";
}
p = p.next;
if (size == 0) {
return "[]";
}
return s + " ]";
StringBuilder sb = new StringBuilder("[ ");
Node<E> current = head.next;
while (current != head) {
sb.append(current.value);
if (current.next != head) {
sb.append(", ");
}
current = current.next;
}
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);
}
}

View File

@ -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));
}
}