|
| 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 | +class MergeSortedSinglyLinkedListTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + void testMergeTwoSortedLists() { |
| 12 | + SinglyLinkedList listA = new SinglyLinkedList(); |
| 13 | + SinglyLinkedList listB = new SinglyLinkedList(); |
| 14 | + |
| 15 | + for (int i = 2; i <= 10; i += 2) { |
| 16 | + listA.insert(i); |
| 17 | + listB.insert(i - 1); |
| 18 | + } |
| 19 | + |
| 20 | + SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB); |
| 21 | + assertEquals("1->2->3->4->5->6->7->8->9->10", mergedList.toString(), "Merged list should contain all elements in sorted order."); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void testMergeWithEmptyListA() { |
| 26 | + SinglyLinkedList listA = new SinglyLinkedList(); // Empty listA |
| 27 | + SinglyLinkedList listB = new SinglyLinkedList(); |
| 28 | + listB.insert(1); |
| 29 | + listB.insert(3); |
| 30 | + listB.insert(5); |
| 31 | + |
| 32 | + SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB); |
| 33 | + assertEquals("1->3->5", mergedList.toString(), "Merged list should match listB when listA is empty."); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testMergeWithEmptyListB() { |
| 38 | + SinglyLinkedList listA = new SinglyLinkedList(); |
| 39 | + SinglyLinkedList listB = new SinglyLinkedList(); // Empty listB |
| 40 | + listA.insert(2); |
| 41 | + listA.insert(4); |
| 42 | + listA.insert(6); |
| 43 | + |
| 44 | + SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB); |
| 45 | + assertEquals("2->4->6", mergedList.toString(), "Merged list should match listA when listB is empty."); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testMergeWithBothEmptyLists() { |
| 50 | + SinglyLinkedList listA = new SinglyLinkedList(); // Empty listA |
| 51 | + SinglyLinkedList listB = new SinglyLinkedList(); // Empty listB |
| 52 | + |
| 53 | + SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB); |
| 54 | + assertEquals("", mergedList.toString(), "Merged list should be empty when both input lists are empty."); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testMergeWithDuplicateValues() { |
| 59 | + SinglyLinkedList listA = new SinglyLinkedList(); |
| 60 | + SinglyLinkedList listB = new SinglyLinkedList(); |
| 61 | + |
| 62 | + listA.insert(1); |
| 63 | + listA.insert(3); |
| 64 | + listA.insert(5); |
| 65 | + listB.insert(1); |
| 66 | + listB.insert(4); |
| 67 | + listB.insert(5); |
| 68 | + |
| 69 | + SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB); |
| 70 | + assertEquals("1->1->3->4->5->5", mergedList.toString(), "Merged list should include duplicate values in sorted order."); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testMergeThrowsExceptionOnNullInput() { |
| 75 | + SinglyLinkedList listA = null; |
| 76 | + SinglyLinkedList listB = new SinglyLinkedList(); |
| 77 | + listB.insert(1); |
| 78 | + listB.insert(2); |
| 79 | + |
| 80 | + SinglyLinkedList finalListA = listA; |
| 81 | + SinglyLinkedList finalListB = listB; |
| 82 | + assertThrows(NullPointerException.class, () -> MergeSortedSinglyLinkedList.merge(finalListA, finalListB), "Should throw NullPointerException if listA is null."); |
| 83 | + |
| 84 | + listA = new SinglyLinkedList(); |
| 85 | + listB = null; |
| 86 | + SinglyLinkedList finalListA1 = listA; |
| 87 | + SinglyLinkedList finalListB1 = listB; |
| 88 | + assertThrows(NullPointerException.class, () -> MergeSortedSinglyLinkedList.merge(finalListA1, finalListB1), "Should throw NullPointerException if listB is null."); |
| 89 | + } |
| 90 | +} |
0 commit comments