1+ package processing .data ;
2+
3+ import org .junit .Test ;
4+ import static org .junit .Assert .*;
5+
6+ /**
7+ * Sort.java is an abstract class implementing quicksort with three abstract methods:
8+ * 1. size() - returns the number of elements
9+ * 2. compare(int a, int b) - compares elements at two indices
10+ * 3. swap(int a, int b) - swaps elements at two indices
11+ */
12+ public class SortTest {
13+
14+ /**
15+ * Concrete implementation of Sort for testing using an int array.
16+ */
17+ private static class IntArraySort extends Sort {
18+ int [] data ;
19+
20+ IntArraySort (int [] data ) {
21+ this .data = data ;
22+ }
23+
24+ @ Override
25+ public int size () {
26+ return data .length ;
27+ }
28+
29+ @ Override
30+ public int compare (int a , int b ) {
31+ return Integer .compare (data [a ], data [b ]);
32+ }
33+
34+ @ Override
35+ public void swap (int a , int b ) {
36+ int temp = data [a ];
37+ data [a ] = data [b ];
38+ data [b ] = temp ;
39+ }
40+ }
41+
42+ @ Test
43+ public void testSortAlreadySorted () {
44+ int [] data = {1 , 2 , 3 , 4 , 5 };
45+ IntArraySort sorter = new IntArraySort (data );
46+ sorter .run ();
47+ assertArrayEquals (new int []{1 , 2 , 3 , 4 , 5 }, data );
48+ }
49+
50+ @ Test
51+ public void testSortReversed () {
52+ int [] data = {5 , 4 , 3 , 2 , 1 };
53+ IntArraySort sorter = new IntArraySort (data );
54+ sorter .run ();
55+ assertArrayEquals (new int []{1 , 2 , 3 , 4 , 5 }, data );
56+ }
57+
58+ @ Test
59+ public void testSortUnsorted () {
60+ int [] data = {3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 };
61+ IntArraySort sorter = new IntArraySort (data );
62+ sorter .run ();
63+ assertArrayEquals (new int []{1 , 1 , 2 , 3 , 4 , 5 , 6 , 9 }, data );
64+ }
65+
66+ @ Test
67+ public void testSortSingleElement () {
68+ int [] data = {42 };
69+ IntArraySort sorter = new IntArraySort (data );
70+ sorter .run ();
71+ assertArrayEquals (new int []{42 }, data );
72+ }
73+
74+ @ Test
75+ public void testSortEmptyArray () {
76+ int [] data = {};
77+ IntArraySort sorter = new IntArraySort (data );
78+ sorter .run ();
79+ assertArrayEquals (new int []{}, data );
80+ }
81+
82+ @ Test
83+ public void testSortTwoElements () {
84+ int [] data = {2 , 1 };
85+ IntArraySort sorter = new IntArraySort (data );
86+ sorter .run ();
87+ assertArrayEquals (new int []{1 , 2 }, data );
88+ }
89+
90+ @ Test
91+ public void testSortTwoElementsAlreadySorted () {
92+ int [] data = {1 , 2 };
93+ IntArraySort sorter = new IntArraySort (data );
94+ sorter .run ();
95+ assertArrayEquals (new int []{1 , 2 }, data );
96+ }
97+
98+ @ Test
99+ public void testSortWithDuplicates () {
100+ int [] data = {3 , 3 , 3 , 3 };
101+ IntArraySort sorter = new IntArraySort (data );
102+ sorter .run ();
103+ assertArrayEquals (new int []{3 , 3 , 3 , 3 }, data );
104+ }
105+
106+ @ Test
107+ public void testSortWithNegativeNumbers () {
108+ int [] data = {0 , -3 , 5 , -1 , 2 };
109+ IntArraySort sorter = new IntArraySort (data );
110+ sorter .run ();
111+ assertArrayEquals (new int []{-3 , -1 , 0 , 2 , 5 }, data );
112+ }
113+
114+ @ Test
115+ public void testSortWithMixedDuplicatesAndNegatives () {
116+ int [] data = {4 , -2 , 4 , 0 , -2 };
117+ IntArraySort sorter = new IntArraySort (data );
118+ sorter .run ();
119+ assertArrayEquals (new int []{-2 , -2 , 0 , 4 , 4 }, data );
120+ }
121+
122+ @ Test
123+ public void testSizeReflectsArrayLength () {
124+ int [] data = {10 , 20 , 30 };
125+ IntArraySort sorter = new IntArraySort (data );
126+ assertEquals (3 , sorter .size ());
127+ }
128+
129+ @ Test
130+ public void testSwapExchangesElements () {
131+ int [] data = {10 , 20 , 30 };
132+ IntArraySort sorter = new IntArraySort (data );
133+ sorter .swap (0 , 2 );
134+ assertArrayEquals (new int []{30 , 20 , 10 }, data );
135+ }
136+
137+ @ Test
138+ public void testCompareReturnsNegativeWhenLess () {
139+ int [] data = {1 , 5 };
140+ IntArraySort sorter = new IntArraySort (data );
141+ assertTrue (sorter .compare (0 , 1 ) < 0 );
142+ }
143+
144+ @ Test
145+ public void testCompareReturnsPositiveWhenGreater () {
146+ int [] data = {5 , 1 };
147+ IntArraySort sorter = new IntArraySort (data );
148+ assertTrue (sorter .compare (0 , 1 ) > 0 );
149+ }
150+
151+ @ Test
152+ public void testCompareReturnsZeroWhenEqual () {
153+ int [] data = {3 , 3 };
154+ IntArraySort sorter = new IntArraySort (data );
155+ assertEquals (0 , sorter .compare (0 , 1 ));
156+ }
157+
158+ @ Test
159+ public void testSortDescendingLargeArray () {
160+ int [] data = {6 , 5 , 4 , 3 , 2 , 1 };
161+ IntArraySort sorter = new IntArraySort (data );
162+ sorter .run ();
163+ assertArrayEquals (new int []{1 , 2 , 3 , 4 , 5 , 6 }, data );
164+ }
165+ }
0 commit comments