Enhance docs, add tests in RotateSinglyLinkedLists (#6011)

This commit is contained in:
Hardik Pawar
2024-10-26 13:56:21 +05:30
committed by GitHub
parent e1a0155966
commit 9dbdaf6afb
2 changed files with 107 additions and 42 deletions

View File

@@ -6,67 +6,100 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
/**
* Test cases for RotateSinglyLinkedLists
* Test cases for RotateSinglyLinkedLists.
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
*/
public class RotateSinglyLinkedListsTest {
private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
// Helper method to create a linked list from an array of values
private Node createLinkedList(int[] values) {
if (values.length == 0) {
return null;
}
Node head = new Node(values[0]);
Node current = head;
for (int i = 1; i < values.length; i++) {
current.next = new Node(values[i]);
current = current.next;
}
return head;
}
// Helper method to convert a linked list to a string for easy comparison
private String linkedListToString(Node head) {
StringBuilder sb = new StringBuilder();
Node current = head;
while (current != null) {
sb.append(current.value);
if (current.next != null) {
sb.append(" -> ");
}
current = current.next;
}
return sb.toString();
}
@Test
public void testRotateRightEmptyList() {
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
// Test case: Rotate an empty list
// Rotate an empty list
assertNull(rotator.rotateRight(null, 2));
}
@Test
public void testRotateRightSingleNodeList() {
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
// Test case: Rotate a list with one element
// Rotate a list with a single element
Node singleNode = new Node(5);
Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
assertEquals(5, rotatedSingleNode.value);
assertNull(rotatedSingleNode.next);
assertEquals("5", linkedListToString(rotatedSingleNode));
}
@Test
public void testRotateRightMultipleElementsList() {
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
// Test case: Rotate a list with multiple elements (Rotate by 2)
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
Node rotated1 = rotator.rotateRight(head, 2);
assertEquals(4, rotated1.value);
assertEquals(5, rotated1.next.value);
assertEquals(1, rotated1.next.next.value);
assertEquals(2, rotated1.next.next.next.value);
assertEquals(3, rotated1.next.next.next.next.value);
assertNull(rotated1.next.next.next.next.next);
// Rotate a list with multiple elements (rotate by 2)
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
Node rotated = rotator.rotateRight(head, 2);
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
}
@Test
public void testRotateRightFullRotation() {
RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists();
// Rotate by more than the length of the list
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
Node rotated = rotator.rotateRight(head, 7);
assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated));
}
// Test case: Rotate a list with multiple elements (Full rotation)
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
@Test
public void testRotateRightZeroRotation() {
// Rotate a list by k = 0 (no rotation)
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
Node rotated = rotator.rotateRight(head, 0);
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
Node rotated3 = rotator.rotateRight(head, 7);
assertEquals(4, rotated3.value);
assertEquals(5, rotated3.next.value);
assertEquals(1, rotated3.next.next.value);
assertEquals(2, rotated3.next.next.next.value);
assertEquals(3, rotated3.next.next.next.next.value);
assertNull(rotated3.next.next.next.next.next);
@Test
public void testRotateRightByListLength() {
// Rotate a list by k equal to list length (no change)
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
Node rotated = rotator.rotateRight(head, 5);
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
@Test
public void testRotateRightByMultipleOfListLength() {
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5});
Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length
assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
@Test
public void testRotateRightLongerList() {
// Rotate a longer list by a smaller k
Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
Node rotated = rotator.rotateRight(head, 4);
assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated));
}
}