PrimitiveArraySorter { public static void sort(short[] array, ShortComparator comparator) { sort(array, 0, array.length, comparator); } private  PrimitiveArraySorter(); static void sort(short[] array, ShortComparator comparator); static void sort(short[] array, int fromIndex, int toIndex, ShortComparator comparator); static boolean sortAndMerge(short[] array, short[] mergeArray, int pivotIndex, int toIndex, ShortComparator comparator); static void merge(short[] array, short[] mergeArray, int pivotIndex, ShortComparator comparator); static void merge(short[] array, short[] mergeArray, int pivotIndex, int toIndex, ShortComparator comparator);  }
@Test public void sorting_empty_array_should_not_throw_exception() { short[] array = {}; PrimitiveArraySorter.sort(array, Short::compare); }
@Test public void test_sorting_single_item_array() { short[] array = {42}; PrimitiveArraySorter.sort(array, Short::compare); assertEquals(42, array[0]); }
@Test public void test_sorting_custom_comparator() { short[] array = {4, 2, 5}; PrimitiveArraySorter.sort(array, (a, b) -> Short.compare(b, a)); short[] expected = {5, 4, 2}; assertArrayEquals(expected, array); }
@Test public void test_complicated_array() { short[] array = {20381, -28785, -19398, 17307, -12612, 11459, -30164, -16597, -4267, 30838, 8918, 9014, -26444, -1232, -14620, 12636, -12389, -4931, 32108, 19854, -12681, 14933, 319, 27348, -4907, 19196, 14209, -32694, 2579, 9771, -1157, -13717, 28506, -8016, 21423, 23697, 23755, 29650, 25644, -14660, -18952, 25272, -19933, -11375, -32363, -11766, -29509, -23898, 12398, -2600, -20703, -23812, -8292, -1605, 28642, 12748, 2547, -14535, 4476, -7802}; short[] expected = Arrays.copyOf(array, array.length); Arrays.sort(expected); PrimitiveArraySorter.sort(array, Short::compare); assertArrayEquals(expected, array); }
@Test public void sorting_random_arrays_should_produce_identical_result_as_java_sort() { Random r = new Random(4234); for (int i = 0; i < 10000; i++) { short[] original = makeRandomArray(r); short[] javaSorted = Arrays.copyOf(original, original.length); short[] customSorted = Arrays.copyOf(original, original.length); PrimitiveArraySorter.sort(customSorted, Short::compare); Arrays.sort(javaSorted); String errorMsg = String.format("%s != %s (before sorting: %s)", Arrays.toString(customSorted), Arrays.toString(javaSorted), Arrays.toString(original)); assertArrayEquals(errorMsg, customSorted, javaSorted); } }