mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Enhance docs, add tests in SortedLinkedList (#6014)
This commit is contained in:
@@ -4,13 +4,26 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SortedLinkedListTest {
|
||||
|
||||
private SortedLinkedList list;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
list = new SortedLinkedList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
public void testInsertIntoEmptyList() {
|
||||
list.insert(5);
|
||||
assertEquals("[5]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertInSortedOrder() {
|
||||
list.insert(5);
|
||||
list.insert(3);
|
||||
list.insert(7);
|
||||
@@ -18,48 +31,99 @@ public class SortedLinkedListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
public void testInsertDuplicateValues() {
|
||||
list.insert(5);
|
||||
list.insert(3);
|
||||
list.insert(7);
|
||||
assertTrue(list.delete(5));
|
||||
assertEquals("[3, 7]", list.toString());
|
||||
assertFalse(list.delete(10));
|
||||
list.insert(5);
|
||||
list.insert(5);
|
||||
assertEquals("[5, 5, 5]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearch() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(5);
|
||||
public void testDeleteHeadElement() {
|
||||
list.insert(1);
|
||||
list.insert(2);
|
||||
list.insert(3);
|
||||
list.insert(7);
|
||||
assertTrue(list.search(5));
|
||||
assertFalse(list.search(10));
|
||||
assertTrue(list.delete(1));
|
||||
assertEquals("[2, 3]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyList() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
public void testDeleteTailElement() {
|
||||
list.insert(1);
|
||||
list.insert(2);
|
||||
list.insert(3);
|
||||
assertTrue(list.delete(3));
|
||||
assertEquals("[1, 2]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteMiddleElement() {
|
||||
list.insert(1);
|
||||
list.insert(2);
|
||||
list.insert(3);
|
||||
assertTrue(list.delete(2));
|
||||
assertEquals("[1, 3]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNonexistentElement() {
|
||||
list.insert(1);
|
||||
list.insert(2);
|
||||
assertFalse(list.delete(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFromSingleElementList() {
|
||||
list.insert(5);
|
||||
assertTrue(list.delete(5));
|
||||
assertEquals("[]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteFromEmptyList() {
|
||||
assertFalse(list.delete(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchInEmptyList() {
|
||||
assertFalse(list.search(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchForExistingElement() {
|
||||
list.insert(3);
|
||||
list.insert(1);
|
||||
list.insert(5);
|
||||
assertTrue(list.search(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchForNonexistentElement() {
|
||||
list.insert(3);
|
||||
list.insert(1);
|
||||
list.insert(5);
|
||||
assertFalse(list.search(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmptyOnEmptyList() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmptyOnNonEmptyList() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(10);
|
||||
assertFalse(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmptyAfterInsertion() {
|
||||
list.insert(10);
|
||||
assertFalse(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmptyAfterDeletion() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(10);
|
||||
list.delete(10);
|
||||
assertTrue(list.isEmpty());
|
||||
|
||||
Reference in New Issue
Block a user