import static org.junit.Assert.*; import org.junit.Test; import java.util.Arrays; import java.util.Random; public class SortTester { // The test function uses Java's sort to validate our // sort. If we did not have something to compare our // sort against, we would need a way to check if an // array is sorted and that the same values that were // in the original array are the exact same ones in // the sorted version. @Test public void testRandomArray() { int[] nums = createRandomArray(9); int[] copy = Arrays.copyOf(nums, nums.length); System.out.println(Arrays.toString(nums)); Sorter.quickSort(nums); Arrays.sort(copy); // use Java's sort assertArrayEquals(nums, copy); } private static int[] createRandomArray(int size) { int[] array = new int[size]; Random rand = new Random(); // fill it with random data in [0, size] for (int i = 0; i < size; i++) { // pick random numbers (subtract a bit so that some // are negative) array[i] = rand.nextInt(size * 3) - size / 4; } return array; } }