3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
6 import java.util.Arrays;
8 import org.junit.Before;
9 import org.junit.Ignore;
10 import org.junit.Test;
12 public class QuickSortTest
14 private static final String c1 = "Blue";
16 private static final String c2 = "Yellow";
18 private static final String c3 = "Orange";
20 private static final String c4 = "Green";
22 private Object[] things;
24 private final Object[] sortedThings = new Object[]
35 public void testSort_byIntValues()
37 int[] values = new int[]
39 QuickSort.sort(values, things);
40 assertTrue(Arrays.equals(new int[]
41 { 1, 2, 3, 4 }, values));
42 assertTrue(Arrays.equals(sortedThings, things));
46 public void testSort_byFloatValues()
48 float[] values = new float[]
50 QuickSort.sort(values, things);
51 assertTrue(Arrays.equals(new float[]
52 { 1f, 2f, 3f, 4f }, values));
53 assertTrue(Arrays.equals(sortedThings, things));
57 public void testSort_byDoubleValues()
59 double[] values = new double[]
61 QuickSort.sort(values, things);
62 assertTrue(Arrays.equals(new double[]
63 { 1d, 2d, 3d, 4d }, values));
64 assertTrue(Arrays.equals(sortedThings, things));
68 * Sort by String is descending order, case-sensitive
71 public void testSort_byStringValues()
73 String[] values = new String[]
74 { "JOHN", "henry", "lucy", "ALISON" };
75 QuickSort.sort(values, things);
76 assertTrue(Arrays.equals(new String[]
77 { "lucy", "henry", "JOHN", "ALISON" }, values));
78 assertTrue(Arrays.equals(new Object[]
79 { c3, c2, c1, c4 }, things));
83 * Test whether sort is stable i.e. equal values retain their mutual ordering.
87 public void testSort_withDuplicates()
89 int[] values = new int[]
91 Object [] things = new Object [] {"A", "X", "Y", "B", "Z"};
92 QuickSort.sort(values, things);
93 assertTrue(Arrays.equals(new int[]
94 { 1, 2, 3, 4, 4 }, values));
95 // this fails - do we care?
96 assertTrue(Arrays.equals(new Object[]
97 { "Z", "Y", "A", "X", "B" }, things));
101 * Test that exercises sort with a mostly zero-valued sortby array. May be of
102 * interest to check the sort algorithm is efficient.
105 public void testSort_MostlyZeroValues()
107 char[] residues = new char[64];
108 for (int i = 0; i < 64; i++)
110 residues[i] = (char) i;
112 float[] counts = new float[64];
116 QuickSort.sort(counts, residues);
117 assertEquals(43, residues[63]);
118 assertEquals(59, residues[62]);
119 assertEquals(62, residues[61]);