3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertTrue;
6 import java.util.Arrays;
8 import org.testng.annotations.BeforeMethod;
9 import org.testng.annotations.Test;
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[]
26 @BeforeMethod(alwaysRun = true)
33 @Test(groups ={ "Functional" })
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));
44 @Test(groups ={ "Functional" })
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));
55 @Test(groups ={ "Functional" })
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
69 @Test(groups ={ "Functional" })
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.
85 { "Functional" }, enabled = false)
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));
100 * Test that exercises sort with a mostly zero-valued sortby array. May be of
101 * interest to check the sort algorithm is efficient.
103 @Test(groups ={ "Functional" })
104 public void testSort_MostlyZeroValues()
106 char[] residues = new char[64];
107 for (int i = 0; i < 64; i++)
109 residues[i] = (char) i;
111 float[] counts = new float[64];
115 QuickSort.sort(counts, residues);
116 assertEquals(43, residues[63]);
117 assertEquals(59, residues[62]);
118 assertEquals(62, residues[61]);