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