1
+ package array .dutch_national_flag_algorithm ;
2
+
3
+ import org .junit .jupiter .api .BeforeEach ;
4
+ import org .junit .jupiter .api .DisplayName ;
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
8
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
9
+
10
+ /**
11
+ * Test suite for the SortColors class.
12
+ */
13
+ class SortColorsTest {
14
+
15
+ private SortColors sorter ;
16
+
17
+ @ BeforeEach
18
+ void setUp () {
19
+ sorter = new SortColors ();
20
+ }
21
+
22
+ @ Test
23
+ @ DisplayName ("Test with a typical mixed array of 0s, 1s, and 2s" )
24
+ void testSortColors_withMixedValues () {
25
+ int [] nums = {2 , 0 , 2 , 1 , 1 , 0 };
26
+ int [] expected = {0 , 0 , 1 , 1 , 2 , 2 };
27
+ sorter .sortColors (nums );
28
+ assertArrayEquals (expected , nums , "Should sort a mixed array correctly" );
29
+ }
30
+
31
+ @ Test
32
+ @ DisplayName ("Test with an empty array" )
33
+ void testSortColors_withEmptyArray () {
34
+ int [] nums = {};
35
+ int [] expected = {};
36
+ sorter .sortColors (nums );
37
+ assertArrayEquals (expected , nums , "Should handle an empty array without error" );
38
+ }
39
+
40
+ @ Test
41
+ @ DisplayName ("Test with a single-element array" )
42
+ void testSortColors_withSingleElementArray () {
43
+ int [] nums = {1 };
44
+ int [] expected = {1 };
45
+ sorter .sortColors (nums );
46
+ assertArrayEquals (expected , nums , "Should handle a single-element array" );
47
+ }
48
+
49
+ @ Test
50
+ @ DisplayName ("Test with an already sorted array" )
51
+ void testSortColors_withAlreadySortedArray () {
52
+ int [] nums = {0 , 0 , 1 , 1 , 2 , 2 };
53
+ int [] expected = {0 , 0 , 1 , 1 , 2 , 2 };
54
+ sorter .sortColors (nums );
55
+ assertArrayEquals (expected , nums , "Should not change an already sorted array" );
56
+ }
57
+
58
+ @ Test
59
+ @ DisplayName ("Test with a reverse-sorted array" )
60
+ void testSortColors_withReverseSortedArray () {
61
+ int [] nums = {2 , 1 , 0 };
62
+ int [] expected = {0 , 1 , 2 };
63
+ sorter .sortColors (nums );
64
+ assertArrayEquals (expected , nums , "Should correctly sort a reverse-sorted array" );
65
+ }
66
+
67
+ @ Test
68
+ @ DisplayName ("Test with an array containing all the same elements (all 1s)" )
69
+ void testSortColors_withAllSameElements () {
70
+ int [] nums = {1 , 1 , 1 , 1 };
71
+ int [] expected = {1 , 1 , 1 , 1 };
72
+ sorter .sortColors (nums );
73
+ assertArrayEquals (expected , nums , "Should handle an array of all same elements" );
74
+ }
75
+
76
+ @ Test
77
+ @ DisplayName ("Test with an array containing all 0s" )
78
+ void testSortColors_withAllZeros () {
79
+ int [] nums = {0 , 0 , 0 };
80
+ int [] expected = {0 , 0 , 0 };
81
+ sorter .sortColors (nums );
82
+ assertArrayEquals (expected , nums , "Should handle an array of all zeros" );
83
+ }
84
+
85
+ @ Test
86
+ @ DisplayName ("Test with an array containing all 2s" )
87
+ void testSortColors_withAllTwos () {
88
+ int [] nums = {2 , 2 , 2 , 2 };
89
+ int [] expected = {2 , 2 , 2 , 2 };
90
+ sorter .sortColors (nums );
91
+ assertArrayEquals (expected , nums , "Should handle an array of all twos" );
92
+ }
93
+
94
+ @ Test
95
+ @ DisplayName ("Test with an array containing only 0s and 1s" )
96
+ void testSortColors_withOnlyZerosAndOnes () {
97
+ int [] nums = {1 , 0 , 1 , 0 , 1 };
98
+ int [] expected = {0 , 0 , 1 , 1 , 1 };
99
+ sorter .sortColors (nums );
100
+ assertArrayEquals (expected , nums , "Should correctly sort an array with only 0s and 1s" );
101
+ }
102
+
103
+ @ Test
104
+ @ DisplayName ("Test with an array containing only 1s and 2s" )
105
+ void testSortColors_withOnlyOnesAndTwos () {
106
+ int [] nums = {1 , 2 , 1 , 2 , 2 };
107
+ int [] expected = {1 , 1 , 2 , 2 , 2 };
108
+ sorter .sortColors (nums );
109
+ assertArrayEquals (expected , nums , "Should correctly sort an array with only 1s and 2s" );
110
+ }
111
+
112
+ @ Test
113
+ @ DisplayName ("Test with an array containing only 0s and 2s" )
114
+ void testSortColors_withOnlyZerosAndTwos () {
115
+ int [] nums = {2 , 0 , 2 , 0 };
116
+ int [] expected = {0 , 0 , 2 , 2 };
117
+ sorter .sortColors (nums );
118
+ assertArrayEquals (expected , nums , "Should correctly sort an array with only 0s and 2s" );
119
+ }
120
+
121
+ @ Test
122
+ @ DisplayName ("Test with a complex case where a 2 is swapped with a 0" )
123
+ void testSortColors_complexSwapCase () {
124
+ // This case tests the `high--` without `mid++` logic.
125
+ // When nums[mid] (2) is swapped with nums[high] (0), the new nums[mid] is 0,
126
+ // which must be processed in the next iteration.
127
+ int [] nums = {2 , 1 , 0 };
128
+ int [] expected = {0 , 1 , 2 };
129
+ sorter .sortColors (nums );
130
+ assertArrayEquals (expected , nums , "Should handle swapping a 2 with a 0 correctly" );
131
+ }
132
+
133
+ @ Test
134
+ @ DisplayName ("Test with a null array should throw NullPointerException" )
135
+ void testSortColors_withNullArray () {
136
+ assertThrows (NullPointerException .class , () -> {
137
+ sorter .sortColors (null );
138
+ }, "Should throw NullPointerException for a null input array" );
139
+ }
140
+
141
+ @ Test
142
+ @ DisplayName ("Test with an array having other elements should throw IllegalStateException" )
143
+ void testSortColors_withUnexpectedArray () {
144
+ int [] nums = {5 , 1 , 0 };
145
+ assertThrows (IllegalStateException .class , () -> {
146
+ sorter .sortColors (nums );
147
+ }, "Should throw IllegalStateException for an unexpected input array" );
148
+ }
149
+ }
0 commit comments