Skip to content

Commit 7f6e677

Browse files
authored
testing: improve test coverage SortedLinkedListTest (#6388)
* testing: improve test coverage SortedLinkedListTest * checkstyle: fix comments formatting * checkstyle: fix formatting
1 parent 054002a commit 7f6e677

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,81 @@ public void testIsEmptyAfterDeletion() {
128128
list.delete(10);
129129
assertTrue(list.isEmpty());
130130
}
131+
132+
@Test
133+
public void testInsertNegativeNumbers() {
134+
list.insert(-10);
135+
list.insert(-5);
136+
list.insert(-20);
137+
assertEquals("[-20, -10, -5]", list.toString());
138+
}
139+
140+
@Test
141+
public void testInsertMixedPositiveAndNegativeNumbers() {
142+
list.insert(0);
143+
list.insert(-1);
144+
list.insert(1);
145+
assertEquals("[-1, 0, 1]", list.toString());
146+
}
147+
148+
@Test
149+
public void testMultipleDeletesUntilEmpty() {
150+
list.insert(2);
151+
list.insert(4);
152+
list.insert(6);
153+
assertTrue(list.delete(4));
154+
assertTrue(list.delete(2));
155+
assertTrue(list.delete(6));
156+
assertTrue(list.isEmpty());
157+
assertEquals("[]", list.toString());
158+
}
159+
160+
@Test
161+
public void testDeleteDuplicateValuesOnlyDeletesOneInstance() {
162+
list.insert(5);
163+
list.insert(5);
164+
list.insert(5);
165+
assertTrue(list.delete(5));
166+
assertEquals("[5, 5]", list.toString());
167+
assertTrue(list.delete(5));
168+
assertEquals("[5]", list.toString());
169+
assertTrue(list.delete(5));
170+
assertEquals("[]", list.toString());
171+
}
172+
173+
@Test
174+
public void testSearchOnListWithDuplicates() {
175+
list.insert(7);
176+
list.insert(7);
177+
list.insert(7);
178+
assertTrue(list.search(7));
179+
assertFalse(list.search(10));
180+
}
181+
182+
@Test
183+
public void testToStringOnEmptyList() {
184+
assertEquals("[]", list.toString());
185+
}
186+
187+
@Test
188+
public void testDeleteAllDuplicates() {
189+
list.insert(4);
190+
list.insert(4);
191+
list.insert(4);
192+
assertTrue(list.delete(4));
193+
assertTrue(list.delete(4));
194+
assertTrue(list.delete(4));
195+
assertFalse(list.delete(4)); // nothing left to delete
196+
assertEquals("[]", list.toString());
197+
}
198+
199+
@Test
200+
public void testInsertAfterDeletion() {
201+
list.insert(1);
202+
list.insert(3);
203+
list.insert(5);
204+
assertTrue(list.delete(3));
205+
list.insert(2);
206+
assertEquals("[1, 2, 5]", list.toString());
207+
}
131208
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy