Skip to content

test: CircleLinkedListTest #5422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,25 @@ public void append(E value) {
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");
}
Expand All @@ -89,18 +92,4 @@ public E remove(int pos) {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}