|
6 | 6 | import org.junit.jupiter.api.Test;
|
7 | 7 |
|
8 | 8 | /**
|
9 |
| - * Test cases for RotateSinglyLinkedLists |
| 9 | + * Test cases for RotateSinglyLinkedLists. |
10 | 10 | * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
11 | 11 | */
|
12 | 12 | public class RotateSinglyLinkedListsTest {
|
13 | 13 |
|
| 14 | + private final RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); |
| 15 | + |
| 16 | + // Helper method to create a linked list from an array of values |
| 17 | + private Node createLinkedList(int[] values) { |
| 18 | + if (values.length == 0) { |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + Node head = new Node(values[0]); |
| 23 | + Node current = head; |
| 24 | + for (int i = 1; i < values.length; i++) { |
| 25 | + current.next = new Node(values[i]); |
| 26 | + current = current.next; |
| 27 | + } |
| 28 | + return head; |
| 29 | + } |
| 30 | + |
| 31 | + // Helper method to convert a linked list to a string for easy comparison |
| 32 | + private String linkedListToString(Node head) { |
| 33 | + StringBuilder sb = new StringBuilder(); |
| 34 | + Node current = head; |
| 35 | + while (current != null) { |
| 36 | + sb.append(current.value); |
| 37 | + if (current.next != null) { |
| 38 | + sb.append(" -> "); |
| 39 | + } |
| 40 | + current = current.next; |
| 41 | + } |
| 42 | + return sb.toString(); |
| 43 | + } |
| 44 | + |
14 | 45 | @Test
|
15 | 46 | public void testRotateRightEmptyList() {
|
16 |
| - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); |
17 |
| - |
18 |
| - // Test case: Rotate an empty list |
| 47 | + // Rotate an empty list |
19 | 48 | assertNull(rotator.rotateRight(null, 2));
|
20 | 49 | }
|
21 | 50 |
|
22 | 51 | @Test
|
23 | 52 | public void testRotateRightSingleNodeList() {
|
24 |
| - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); |
25 |
| - |
26 |
| - // Test case: Rotate a list with one element |
| 53 | + // Rotate a list with a single element |
27 | 54 | Node singleNode = new Node(5);
|
28 | 55 | Node rotatedSingleNode = rotator.rotateRight(singleNode, 3);
|
29 |
| - assertEquals(5, rotatedSingleNode.value); |
30 |
| - assertNull(rotatedSingleNode.next); |
| 56 | + assertEquals("5", linkedListToString(rotatedSingleNode)); |
31 | 57 | }
|
32 | 58 |
|
33 | 59 | @Test
|
34 | 60 | public void testRotateRightMultipleElementsList() {
|
35 |
| - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); |
| 61 | + // Rotate a list with multiple elements (rotate by 2) |
| 62 | + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); |
| 63 | + Node rotated = rotator.rotateRight(head, 2); |
| 64 | + assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated)); |
| 65 | + } |
36 | 66 |
|
37 |
| - // Test case: Rotate a list with multiple elements (Rotate by 2) |
38 |
| - Node head = new Node(1); |
39 |
| - head.next = new Node(2); |
40 |
| - head.next.next = new Node(3); |
41 |
| - head.next.next.next = new Node(4); |
42 |
| - head.next.next.next.next = new Node(5); |
| 67 | + @Test |
| 68 | + public void testRotateRightFullRotation() { |
| 69 | + // Rotate by more than the length of the list |
| 70 | + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); |
| 71 | + Node rotated = rotator.rotateRight(head, 7); |
| 72 | + assertEquals("4 -> 5 -> 1 -> 2 -> 3", linkedListToString(rotated)); |
| 73 | + } |
43 | 74 |
|
44 |
| - Node rotated1 = rotator.rotateRight(head, 2); |
45 |
| - assertEquals(4, rotated1.value); |
46 |
| - assertEquals(5, rotated1.next.value); |
47 |
| - assertEquals(1, rotated1.next.next.value); |
48 |
| - assertEquals(2, rotated1.next.next.next.value); |
49 |
| - assertEquals(3, rotated1.next.next.next.next.value); |
50 |
| - assertNull(rotated1.next.next.next.next.next); |
| 75 | + @Test |
| 76 | + public void testRotateRightZeroRotation() { |
| 77 | + // Rotate a list by k = 0 (no rotation) |
| 78 | + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); |
| 79 | + Node rotated = rotator.rotateRight(head, 0); |
| 80 | + assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); |
51 | 81 | }
|
52 | 82 |
|
53 | 83 | @Test
|
54 |
| - public void testRotateRightFullRotation() { |
55 |
| - RotateSinglyLinkedLists rotator = new RotateSinglyLinkedLists(); |
| 84 | + public void testRotateRightByListLength() { |
| 85 | + // Rotate a list by k equal to list length (no change) |
| 86 | + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); |
| 87 | + Node rotated = rotator.rotateRight(head, 5); |
| 88 | + assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); |
| 89 | + } |
56 | 90 |
|
57 |
| - // Test case: Rotate a list with multiple elements (Full rotation) |
58 |
| - Node head = new Node(1); |
59 |
| - head.next = new Node(2); |
60 |
| - head.next.next = new Node(3); |
61 |
| - head.next.next.next = new Node(4); |
62 |
| - head.next.next.next.next = new Node(5); |
| 91 | + @Test |
| 92 | + public void testRotateRightByMultipleOfListLength() { |
| 93 | + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5}); |
| 94 | + Node rotated = rotator.rotateRight(head, 10); // k = 2 * list length |
| 95 | + assertEquals("1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); |
| 96 | + } |
63 | 97 |
|
64 |
| - Node rotated3 = rotator.rotateRight(head, 7); |
65 |
| - assertEquals(4, rotated3.value); |
66 |
| - assertEquals(5, rotated3.next.value); |
67 |
| - assertEquals(1, rotated3.next.next.value); |
68 |
| - assertEquals(2, rotated3.next.next.next.value); |
69 |
| - assertEquals(3, rotated3.next.next.next.next.value); |
70 |
| - assertNull(rotated3.next.next.next.next.next); |
| 98 | + @Test |
| 99 | + public void testRotateRightLongerList() { |
| 100 | + // Rotate a longer list by a smaller k |
| 101 | + Node head = createLinkedList(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}); |
| 102 | + Node rotated = rotator.rotateRight(head, 4); |
| 103 | + assertEquals("6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5", linkedListToString(rotated)); |
71 | 104 | }
|
72 | 105 | }
|
0 commit comments