-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRadixSortTest.java
More file actions
417 lines (337 loc) · 10.5 KB
/
Copy pathRadixSortTest.java
File metadata and controls
417 lines (337 loc) · 10.5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* Öffentliche Testfälle von Christian Femers. Siehe
* https://github.com/MaisiKoleni/GAD-Extras
*
* Bitte nicht mit abgeben. (geht sonst nicht)
*
* @version 1.0
*
* @author Christian Femers (IN.TUM)
*
*/
public class RadixSortTest {
private static final String VERSION = "1.0";
private static Counter testNum = new Counter(0);
private static Counter testMethod = new Counter(0);
static {
tryVersionCheck();
System.out.println("CF's TESTS ACTIVE\n");
}
public static void main(String[] args) {
// Test 1
testKey();
// Test 2
testMaxDecimalPlaces();
// Test 3
testConcatenate();
// Test 4
testKSort();
// Test 5
testSort();
}
/**
* Test key method
*/
public static void testKey() {
printTestHeadline("TEST KEY");
// 1
assertEquals(invokeKey(0, 0), 0);
// 2
assertEquals(invokeKey(1, 0), 1);
// 3
assertEquals(invokeKey(9, 0), 9);
// 4
assertEquals(invokeKey(10, 0), 0);
// 5
assertEquals(invokeKey(12, 0), 2);
// 6
assertEquals(invokeKey(12, 1), 1);
// 7
assertEquals(invokeKey(123_456, 5), 1);
// 8
assertEquals(invokeKey(123_456, 0), 6);
// 9
assertEquals(invokeKey(123_456, 3), 3);
// A
assertThrows(() -> invokeKey(-1, 0), IllegalArgumentException.class);
// B
assertEquals(invokeKey(8, 1), 0);
// C
assertEquals(invokeKey(8, 2), 0);
}
/**
* Test getMaxDecimalPlaces method
*/
public static void testMaxDecimalPlaces() {
printTestHeadline("TEST MAX_DECIMAl_PLACES");
// 1
assertEquals(invokeGetMaxDecimalPlaces(0), 1);
// 2
assertEquals(invokeGetMaxDecimalPlaces(1, 0), 1);
// 3
assertEquals(invokeGetMaxDecimalPlaces(1, 5, 8), 1);
// 4
assertEquals(invokeGetMaxDecimalPlaces(10, 0), 2);
// 5
assertEquals(invokeGetMaxDecimalPlaces(2, 10, 12), 2);
// 6
assertEquals(invokeGetMaxDecimalPlaces(12, 123, 1234), 4);
// 7
assertEquals(invokeGetMaxDecimalPlaces(123_456, 123, 5), 6);
// 8
assertEquals(invokeGetMaxDecimalPlaces(123_456), 6);
// 9
assertEquals(invokeGetMaxDecimalPlaces(23_456, 3), 5);
}
/**
* Test concatenate method
*/
public static void testConcatenate() {
printTestHeadline("TEST CONCATENATE");
BucketArray ba;
// 1
ba = BucketArray.of();
checkArray(invokeConcatenate(ba.build(), ints(ba.count())));
// 2
ba = BucketArray.of(0);
checkArray(invokeConcatenate(ba.build(), ints(ba.count())), 0);
// 3
ba = BucketArray.of(0, 1);
checkArray(invokeConcatenate(ba.build(), ints(ba.count())), 0, 1);
// 4
ba = BucketArray.of(0)
.and(1);
checkArray(invokeConcatenate(ba.build(), ints(ba.count())), 0, 1);
// 5
ba = BucketArray.of()
.and();
checkArray(invokeConcatenate(ba.build(), ints(ba.count())));
// 6
ba = BucketArray.of(1)
.and()
.and(2);
checkArray(invokeConcatenate(ba.build(), ints(ba.count())), 1, 2);
// 7
ba = BucketArray.of(1)
.and(8, 4)
.and(2);
checkArray(invokeConcatenate(ba.build(), ints(ba.count())), 1, 8, 4, 2);
// 8
ba = BucketArray.of(0)
.and(1, 4, 5)
.and()
.and(6, 4, 12)
.and(20, 1, 0);
checkArray(invokeConcatenate(ba.build(), ints(ba.count())), 0, 1, 4, 5, 6, 4, 12, 20, 1, 0);
}
/**
* Test kSort method
*/
public static void testKSort() {
printTestHeadline("TEST K_SORT");
// 1 - Einerstelle
checkArray(invokeKSort(0, 1, 3, 2), 1, 2, 3);
// 2 - Einerstelle 2
checkArray(invokeKSort(0, 7, 1, 5, 6, 5, 8, 4, 0, 2, 9, 2), 0, 1, 2, 2, 4, 5, 5, 6, 7, 8, 9);
// 3 - Zehnerstelle
checkArray(invokeKSort(1, 10, 50, 60, 50, 80, 40, 0), 0, 10, 40, 50, 50, 60, 80);
// 4 - Nur Zehnerstelle betrachten, folglich muss 58 vor 53 bestehen bleiben
checkArray(invokeKSort(1, 74, 11, 58, 64, 53, 80, 41, 9), 9, 11, 41, 58, 53, 64, 74, 80);
// 5 - Nur Zehnerstelle betrachten, keine Änderung erlaubt
checkArray(invokeKSort(1, 7, 1, 5, 6, 5, 8, 4, 0, 2, 9, 2), 7, 1, 5, 6, 5, 8, 4, 0, 2, 9, 2);
}
/**
* Test sort method
*/
public static void testSort() {
printTestHeadline("TEST SORT");
// 1
checkArray(invokeSort());
// 2
checkArray(invokeSort(0), 0);
// 3
checkArray(invokeSort(2, 1, 3), 1, 2, 3);
// 4
checkArray(invokeSort(7, 1, 5, 6, 5, 8, 4, 0, 2, 9, 2), 0, 1, 2, 2, 4, 5, 5, 6, 7, 8, 9);
// 5 - Jetzt muss auch 53 vor 58 sein
checkArray(invokeSort(74, 11, 58, 64, 53, 80, 41, 9), 9, 11, 41, 53, 58, 64, 74, 80);
// 6
checkArray(invokeSort(123, 0, 42, 12, 1024, 83, 2, 200), 0, 2, 12, 42, 83, 123, 200, 1024);
Random r = new Random(1);
Integer[] a = r.ints(100, 0, 1_000_000).boxed().toArray(Integer[]::new);
Integer[] expected = a.clone();
RadixSort.sort(a, false);
Arrays.sort(expected);
// 7
checkArray(a, expected);
}
static Integer[] ints(int length) {
return new Integer[length];
}
static int invokeKey(Integer i, int dec) {
try {
Method m = RadixSort.class.getDeclaredMethod("key", Integer.class, Integer.TYPE);
m.setAccessible(true);
return (Integer) m.invoke(null, i, dec);
} catch (InvocationTargetException e) {
throw (RuntimeException) e.getCause();
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("RadixSort key method error", e);
}
}
static Integer[] invokeConcatenate(ArrayList<Integer>[] buckets, Integer[] elements) {
try {
Method m = RadixSort.class.getDeclaredMethod("concatenate", ArrayList[].class, Integer[].class);
m.setAccessible(true);
m.invoke(null, buckets, elements);
return elements;
} catch (InvocationTargetException e) {
throw (RuntimeException) e.getCause();
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("RadixSort concatenate method error", e);
}
}
static int invokeGetMaxDecimalPlaces(Integer... elements) {
try {
Method m = RadixSort.class.getDeclaredMethod("getMaxDecimalPlaces", Integer[].class);
m.setAccessible(true);
return (Integer) m.invoke(null, new Object[] { elements });
} catch (InvocationTargetException e) {
throw (RuntimeException) e.getCause();
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("RadixSort getMaxDecimalPlaces method error", e);
}
}
static Integer[] invokeKSort(int decimal, Integer... vals) {
try {
Method m = RadixSort.class.getDeclaredMethod("kSort", Integer[].class, Integer.TYPE);
m.setAccessible(true);
m.invoke(null, vals, decimal);
return vals;
} catch (InvocationTargetException e) {
throw (RuntimeException) e.getCause();
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("RadixSort kSort method error", e);
}
}
static Integer[] invokeSort(Integer... vals) {
RadixSort.sort(vals, false);
return vals;
}
private static void assertThrows(Runnable r, Class<IllegalArgumentException> class1) {
String testId = getNextTestId();
try {
r.run();
System.err.format(" test %s failed: expected %s but nothing was thrown%n", testId, class1.getName());
} catch (Exception e) {
if (class1.isInstance(e))
System.out.format(" passed %s, result: %s%n", testId, e);
else
System.err.format(" test %s failed: expected %s but was %s%n", testId, class1.getName(), e);
}
}
/**
* Simple assert-method to compare two int.
*/
private static void assertEquals(int actual, int expected) {
String testId = getNextTestId();
if (actual == expected)
System.out.format(" passed %s, result: %d%n", testId, actual);
else
System.err.format(" test %s failed: expected %d but was %d%n", testId, expected, actual);
}
/**
* Compares all of the arrays content with the given sequence
*
* @param array the int array to check
* @param contents the array (varargs) that is expected
*/
private static void checkArray(Integer[] array, Integer... contents) {
String testId = getNextTestId();
if (array.length != contents.length) {
System.err.format(" test %s failed: expected length %d array but was %d%n", testId, contents.length,
array.length);
return;
}
List<Integer> differences = new ArrayList<>();
for (int i = 0; i < contents.length; i++) {
if (contents[i].intValue() != array[i].intValue())
differences.add(i);
}
if (differences.isEmpty())
System.out.format(" passed %s, result: %s%n", testId, Arrays.toString(array));
else {
System.err.format(" test %s failed: array content differs at indices %s%n", testId, differences);
System.err.format(" expected: %s%n", Arrays.toString(contents));
System.err.format(" actual: %s%n", Arrays.toString(array));
}
}
private static String getNextTestId() {
return String.format("%s.%s", testMethod, testNum.incrementAndGet());
}
private static void printTestHeadline(String s) {
System.out.format("Test %s: %s%n", testMethod.incrementAndGet(), s);
testNum.reset();
}
static class BucketArray {
private final ArrayList<ArrayList<Integer>> bucketList;
private BucketArray() {
bucketList = new ArrayList<>();
}
public static BucketArray of(Integer... vals) {
return new BucketArray().and(vals);
}
public BucketArray and(Integer... vals) {
bucketList.add(new ArrayList<>(Arrays.asList(vals)));
return this;
}
public ArrayList<Integer>[] build() {
return bucketList.toArray(new ArrayList[0]);
}
public int count() {
return bucketList.stream().mapToInt(List::size).sum();
}
}
/**
* Counter that can count from 1 to 9 then A to Z (35 digits)
*/
public static class Counter {
private int count;
public Counter(int count) {
this.count = count;
}
public String incrementAndGet() {
count++;
return toString();
}
@Override
public String toString() {
return String.valueOf(toChar());
}
public char toChar() {
return (char) (count + (count < 10 ? 48 : 55));
}
public void reset() {
count = 0;
}
}
private static void tryVersionCheck() {
try {
Class.forName("CFUpdate").getDeclaredMethod("checkForNewVersion", String.class, Class.class, String.class)
.invoke(null, "blatt07", RadixSortTest.class, VERSION);
} catch (@SuppressWarnings("unused") ClassNotFoundException e) {
System.out.println("Automatic update checks are inactive, download the CFUpdate class for that:");
System.out.println("https://raw.githubusercontent.com/MaisiKoleni/GAD-Extras/master/version/CFUpdate.java");
System.out.println();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
System.err.println("Something went wrong invoking checkForNewerVersion:");
e.printStackTrace();
}
}
}