Skip to content

Commit adcecd4

Browse files
committed
added more tests for circular doubly linked list
1 parent 10ed527 commit adcecd4

File tree

1 file changed

+124
-81
lines changed

1 file changed

+124
-81
lines changed
Lines changed: 124 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,124 @@
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.BeforeEach;
7-
import org.junit.jupiter.api.Test;
8-
9-
public class CircularDoublyLinkedListTest {
10-
11-
private CircularDoublyLinkedList<Integer> list;
12-
13-
@BeforeEach
14-
public void setUp() {
15-
list = new CircularDoublyLinkedList<>();
16-
}
17-
18-
@Test
19-
public void testInitialSize() {
20-
assertEquals(0, list.getSize(), "Initial size should be 0.");
21-
}
22-
23-
@Test
24-
public void testAppendAndSize() {
25-
list.append(10);
26-
list.append(20);
27-
list.append(30);
28-
29-
assertEquals(3, list.getSize(), "Size after appends should be 3.");
30-
assertEquals("[ 10, 20, 30 ]", list.toString(), "List content should match appended values.");
31-
}
32-
33-
@Test
34-
public void testRemove() {
35-
list.append(10);
36-
list.append(20);
37-
list.append(30);
38-
39-
int removed = list.remove(1);
40-
assertEquals(20, removed, "Removed element at index 1 should be 20.");
41-
42-
assertEquals("[ 10, 30 ]", list.toString(), "List content should reflect removal.");
43-
assertEquals(2, list.getSize(), "Size after removal should be 2.");
44-
45-
removed = list.remove(0);
46-
assertEquals(10, removed, "Removed element at index 0 should be 10.");
47-
assertEquals("[ 30 ]", list.toString(), "List content should reflect second removal.");
48-
assertEquals(1, list.getSize(), "Size after second removal should be 1.");
49-
}
50-
51-
@Test
52-
public void testRemoveInvalidIndex() {
53-
list.append(10);
54-
list.append(20);
55-
56-
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2), "Removing at invalid index 2 should throw exception.");
57-
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1), "Removing at negative index should throw exception.");
58-
}
59-
60-
@Test
61-
public void testToStringEmpty() {
62-
assertEquals("[]", list.toString(), "Empty list should display as [].");
63-
}
64-
65-
@Test
66-
public void testSingleElement() {
67-
list.append(10);
68-
69-
assertEquals(1, list.getSize(), "Size after adding single element should be 1.");
70-
assertEquals("[ 10 ]", list.toString(), "Single element list string should be formatted correctly.");
71-
int removed = list.remove(0);
72-
assertEquals(10, removed, "Removed element should be the one appended.");
73-
assertEquals("[]", list.toString(), "List should be empty after removing last element.");
74-
assertEquals(0, list.getSize(), "Size after removing last element should be 0.");
75-
}
76-
77-
@Test
78-
public void testNullAppend() {
79-
assertThrows(NullPointerException.class, () -> list.append(null), "Appending null should throw NullPointerException.");
80-
}
81-
}
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.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class CircularDoublyLinkedListTest {
10+
11+
private CircularDoublyLinkedList<Integer> list;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
list = new CircularDoublyLinkedList<>();
16+
}
17+
18+
@Test
19+
public void testInitialSize() {
20+
assertEquals(0, list.getSize(), "Initial size should be 0.");
21+
}
22+
23+
@Test
24+
public void testAppendAndSize() {
25+
list.append(10);
26+
list.append(20);
27+
list.append(30);
28+
29+
assertEquals(3, list.getSize(), "Size after appends should be 3.");
30+
assertEquals("[ 10, 20, 30 ]", list.toString(), "List content should match appended values.");
31+
}
32+
33+
@Test
34+
public void testRemove() {
35+
list.append(10);
36+
list.append(20);
37+
list.append(30);
38+
39+
int removed = list.remove(1);
40+
assertEquals(20, removed, "Removed element at index 1 should be 20.");
41+
42+
assertEquals("[ 10, 30 ]", list.toString(), "List content should reflect removal.");
43+
assertEquals(2, list.getSize(), "Size after removal should be 2.");
44+
45+
removed = list.remove(0);
46+
assertEquals(10, removed, "Removed element at index 0 should be 10.");
47+
assertEquals("[ 30 ]", list.toString(), "List content should reflect second removal.");
48+
assertEquals(1, list.getSize(), "Size after second removal should be 1.");
49+
}
50+
51+
@Test
52+
public void testRemoveInvalidIndex() {
53+
list.append(10);
54+
list.append(20);
55+
56+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2),
57+
"Removing at invalid index 2 should throw exception.");
58+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1),
59+
"Removing at negative index should throw exception.");
60+
}
61+
62+
@Test
63+
public void testToStringEmpty() {
64+
assertEquals("[]", list.toString(), "Empty list should display as [].");
65+
}
66+
67+
@Test
68+
public void testSingleElement() {
69+
list.append(10);
70+
71+
assertEquals(1, list.getSize(), "Size after adding single element should be 1.");
72+
assertEquals("[ 10 ]", list.toString(), "Single element list string should be formatted correctly.");
73+
int removed = list.remove(0);
74+
assertEquals(10, removed, "Removed element should be the one appended.");
75+
assertEquals("[]", list.toString(), "List should be empty after removing last element.");
76+
assertEquals(0, list.getSize(), "Size after removing last element should be 0.");
77+
}
78+
79+
@Test
80+
public void testNullAppend() {
81+
assertThrows(NullPointerException.class, () -> list.append(null),
82+
"Appending null should throw NullPointerException.");
83+
}
84+
85+
@Test
86+
public void testRemoveLastPosition() {
87+
list.append(10);
88+
list.append(20);
89+
list.append(30);
90+
int removed = list.remove(list.getSize() - 1);
91+
assertEquals(30, removed, "Last element removed should be 30.");
92+
assertEquals(2, list.getSize(), "Size should decrease after removing last element.");
93+
}
94+
95+
@Test
96+
public void testRemoveFromEmptyThrows() {
97+
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0), "Remove from empty list should throw.");
98+
}
99+
100+
@Test
101+
public void testRepeatedAppendAndRemove() {
102+
for (int i = 0; i < 100; i++) {
103+
list.append(i);
104+
}
105+
assertEquals(100, list.getSize());
106+
107+
for (int i = 99; i >= 0; i--) {
108+
int removed = list.remove(i);
109+
assertEquals(i, removed, "Removed element should match appended value.");
110+
}
111+
assertEquals(0, list.getSize(), "List should be empty after all removes.");
112+
}
113+
114+
@Test
115+
public void testToStringAfterMultipleRemoves() {
116+
list.append(1);
117+
list.append(2);
118+
list.append(3);
119+
list.remove(2);
120+
list.remove(0);
121+
assertEquals("[ 2 ]", list.toString(), "ToString should correctly represent remaining elements.");
122+
}
123+
124+
}

0 commit comments

Comments
 (0)