4
4
import static org .junit .jupiter .api .Assertions .assertNull ;
5
5
6
6
import org .junit .jupiter .api .Test ;
7
+
7
8
/**
8
- * Test cases for QuickSortLinkedList
9
+ * Test cases for QuickSortLinkedList.
9
10
* Author: Prabhat-Kumar-42
10
11
* GitHub: https://github.com/Prabhat-Kumar-42
11
12
*/
@@ -16,9 +17,8 @@ public void testSortEmptyList() {
16
17
SinglyLinkedList emptyList = new SinglyLinkedList ();
17
18
QuickSortLinkedList sorter = new QuickSortLinkedList (emptyList );
18
19
19
- // Test case: Sorting an empty list should result in an empty list
20
20
sorter .sortList ();
21
- assertNull (emptyList .getHead ());
21
+ assertNull (emptyList .getHead (), "Sorted empty list should have no elements." );
22
22
}
23
23
24
24
@ Test
@@ -27,10 +27,51 @@ public void testSortSingleNodeList() {
27
27
singleNodeList .insert (5 );
28
28
QuickSortLinkedList sorter = new QuickSortLinkedList (singleNodeList );
29
29
30
- // Test case: Sorting a list with a single node should result in the same list
31
30
sorter .sortList ();
32
- assertEquals (5 , singleNodeList .getHead ().value );
33
- assertNull (singleNodeList .getHead ().next );
31
+ assertEquals (5 , singleNodeList .getHead ().value , "Single node list should remain unchanged after sorting." );
32
+ assertNull (singleNodeList .getHead ().next , "Single node should not have a next node." );
33
+ }
34
+
35
+ @ Test
36
+ public void testSortAlreadySorted () {
37
+ SinglyLinkedList sortedList = new SinglyLinkedList ();
38
+ sortedList .insert (1 );
39
+ sortedList .insert (2 );
40
+ sortedList .insert (3 );
41
+ sortedList .insert (4 );
42
+ sortedList .insert (5 );
43
+ QuickSortLinkedList sorter = new QuickSortLinkedList (sortedList );
44
+
45
+ sorter .sortList ();
46
+ assertEquals ("1->2->3->4->5" , sortedList .toString (), "Already sorted list should remain unchanged." );
47
+ }
48
+
49
+ @ Test
50
+ public void testSortReverseOrderedList () {
51
+ SinglyLinkedList reverseList = new SinglyLinkedList ();
52
+ reverseList .insert (5 );
53
+ reverseList .insert (4 );
54
+ reverseList .insert (3 );
55
+ reverseList .insert (2 );
56
+ reverseList .insert (1 );
57
+ QuickSortLinkedList sorter = new QuickSortLinkedList (reverseList );
58
+
59
+ sorter .sortList ();
60
+ assertEquals ("1->2->3->4->5" , reverseList .toString (), "Reverse ordered list should be sorted in ascending order." );
61
+ }
62
+
63
+ @ Test
64
+ public void testSortWithDuplicates () {
65
+ SinglyLinkedList listWithDuplicates = new SinglyLinkedList ();
66
+ listWithDuplicates .insert (3 );
67
+ listWithDuplicates .insert (1 );
68
+ listWithDuplicates .insert (3 );
69
+ listWithDuplicates .insert (2 );
70
+ listWithDuplicates .insert (2 );
71
+ QuickSortLinkedList sorter = new QuickSortLinkedList (listWithDuplicates );
72
+
73
+ sorter .sortList ();
74
+ assertEquals ("1->2->2->3->3" , listWithDuplicates .toString (), "List with duplicates should be sorted correctly." );
34
75
}
35
76
36
77
@ Test
@@ -48,18 +89,7 @@ public void testSortMultipleElementsList() {
48
89
list .insert (6 );
49
90
QuickSortLinkedList sorter = new QuickSortLinkedList (list );
50
91
51
- // Test case: Sorting a list with multiple elements
52
92
sorter .sortList ();
53
- assertEquals (1 , list .getHead ().value );
54
- assertEquals (2 , list .getHead ().next .value );
55
- assertEquals (3 , list .getHead ().next .next .value );
56
- assertEquals (4 , list .getHead ().next .next .next .value );
57
- assertEquals (5 , list .getHead ().next .next .next .next .value );
58
- assertEquals (6 , list .getHead ().next .next .next .next .next .value );
59
- assertEquals (7 , list .getHead ().next .next .next .next .next .next .value );
60
- assertEquals (8 , list .getHead ().next .next .next .next .next .next .next .value );
61
- assertEquals (9 , list .getHead ().next .next .next .next .next .next .next .next .value );
62
- assertEquals (10 , list .getHead ().next .next .next .next .next .next .next .next .next .value );
63
- assertNull (list .getHead ().next .next .next .next .next .next .next .next .next .next );
93
+ assertEquals ("1->2->3->4->5->6->7->8->9->10" , list .toString (), "List should be sorted in ascending order." );
64
94
}
65
95
}
0 commit comments