Skip to content

Commit 45563cc

Browse files
alxkmalxkm
and
alxkm
authored
test: CircleLinkedListTest (#5422)
* test: CircleLinkedListTest * checkstyle: fix formatting --------- Co-authored-by: alxkm <[email protected]>
1 parent a9bc7c2 commit 45563cc

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java

+13-24
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,25 @@ public void append(E value) {
5151
size++;
5252
}
5353

54-
// utility function for traversing the list
5554
public String toString() {
56-
Node<E> p = head.next;
57-
String s = "[ ";
58-
while (p != head) {
59-
s += p.value;
60-
if (p != tail) {
61-
s += " , ";
55+
if (size == 0) {
56+
return "[]";
57+
}
58+
StringBuilder sb = new StringBuilder("[ ");
59+
Node<E> current = head.next;
60+
while (current != head) {
61+
sb.append(current.value);
62+
if (current.next != head) {
63+
sb.append(", ");
6264
}
63-
p = p.next;
65+
current = current.next;
6466
}
65-
return s + " ]";
67+
sb.append(" ]");
68+
return sb.toString();
6669
}
6770

6871
public E remove(int pos) {
69-
if (pos > size || pos < 0) {
72+
if (pos >= size || pos < 0) {
7073
// catching errors
7174
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
7275
}
@@ -89,18 +92,4 @@ public E remove(int pos) {
8992
size--;
9093
return saved;
9194
}
92-
93-
public static void main(String[] args) {
94-
CircleLinkedList<Integer> cl = new CircleLinkedList<>();
95-
cl.append(12);
96-
System.out.println(cl);
97-
cl.append(23);
98-
System.out.println(cl);
99-
cl.append(34);
100-
System.out.println(cl);
101-
cl.append(56);
102-
System.out.println(cl);
103-
cl.remove(3);
104-
System.out.println(cl);
105-
}
10695
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class CircleLinkedListTest {
9+
10+
@Test
11+
public void testAppendAndSize() {
12+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
13+
list.append(1);
14+
list.append(2);
15+
list.append(3);
16+
17+
assertEquals(3, list.getSize());
18+
assertEquals("[ 1, 2, 3 ]", list.toString());
19+
}
20+
21+
@Test
22+
public void testRemove() {
23+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
24+
list.append(1);
25+
list.append(2);
26+
list.append(3);
27+
list.append(4);
28+
29+
assertEquals(2, list.remove(1));
30+
assertEquals(3, list.remove(1));
31+
assertEquals("[ 1, 4 ]", list.toString());
32+
assertEquals(2, list.getSize());
33+
}
34+
35+
@Test
36+
public void testRemoveInvalidIndex() {
37+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
38+
list.append(1);
39+
list.append(2);
40+
41+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2));
42+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1));
43+
}
44+
45+
@Test
46+
public void testToStringEmpty() {
47+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
48+
assertEquals("[]", list.toString());
49+
}
50+
51+
@Test
52+
public void testToStringAfterRemoval() {
53+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
54+
list.append(1);
55+
list.append(2);
56+
list.append(3);
57+
list.remove(1);
58+
59+
assertEquals("[ 1, 3 ]", list.toString());
60+
}
61+
62+
@Test
63+
public void testSingleElement() {
64+
CircleLinkedList<Integer> list = new CircleLinkedList<>();
65+
list.append(1);
66+
67+
assertEquals(1, list.getSize());
68+
assertEquals("[ 1 ]", list.toString());
69+
assertEquals(1, list.remove(0));
70+
assertEquals("[]", list.toString());
71+
}
72+
73+
@Test
74+
public void testNullElement() {
75+
CircleLinkedList<String> list = new CircleLinkedList<>();
76+
assertThrows(NullPointerException.class, () -> list.append(null));
77+
}
78+
}

0 commit comments

Comments
 (0)