3 import static org.junit.Assert.assertTrue;
5 import java.util.Arrays;
7 import org.junit.Before;
8 import org.junit.Ignore;
11 public class QuickSortTest
13 private static final String c1 = "Blue";
15 private static final String c2 = "Yellow";
17 private static final String c3 = "Orange";
19 private static final String c4 = "Green";
21 private Object[] things;
23 private final Object[] sortedThings = new Object[]
34 public void testSort_byIntValues()
36 int[] values = new int[]
38 QuickSort.sort(values, things);
39 assertTrue(Arrays.equals(new int[]
40 { 1, 2, 3, 4 }, values));
41 assertTrue(Arrays.equals(sortedThings, things));
45 public void testSort_byFloatValues()
47 float[] values = new float[]
49 QuickSort.sort(values, things);
50 assertTrue(Arrays.equals(new float[]
51 { 1f, 2f, 3f, 4f }, values));
52 assertTrue(Arrays.equals(sortedThings, things));
56 public void testSort_byDoubleValues()
58 double[] values = new double[]
60 QuickSort.sort(values, things);
61 assertTrue(Arrays.equals(new double[]
62 { 1d, 2d, 3d, 4d }, values));
63 assertTrue(Arrays.equals(sortedThings, things));
67 * Sort by String is descending order, case-sensitive
70 public void testSort_byStringValues()
72 String[] values = new String[]
73 { "JOHN", "henry", "lucy", "ALISON" };
74 QuickSort.sort(values, things);
75 assertTrue(Arrays.equals(new String[]
76 { "lucy", "henry", "JOHN", "ALISON" }, values));
77 assertTrue(Arrays.equals(new Object[]
78 { c3, c2, c1, c4 }, things));
82 * Test whether sort is stable i.e. equal values retain their mutual ordering.
86 public void testSort_withDuplicates()
88 int[] values = new int[]
90 Object [] things = new Object [] {"A", "X", "Y", "B", "Z"};
91 QuickSort.sort(values, things);
92 assertTrue(Arrays.equals(new int[]
93 { 1, 2, 3, 4, 4 }, values));
94 // this fails - do we care?
95 assertTrue(Arrays.equals(new Object[]
96 { "Z", "Y", "A", "X", "B" }, things));