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[] { c4, c2, c1, c3 };
25 @BeforeMethod(alwaysRun = true)
28 things = new Object[] { c1, c2, c3, c4 };
31 @Test(groups = { "Functional" })
32 public void testSort_byIntValues()
34 int[] values = new int[] { 3, 2, 4, 1 };
35 QuickSort.sort(values, things);
36 assertTrue(Arrays.equals(new int[] { 1, 2, 3, 4 }, values));
37 assertTrue(Arrays.equals(sortedThings, things));
40 @Test(groups = { "Functional" })
41 public void testSort_byFloatValues()
43 float[] values = new float[] { 3f, 2f, 4f, 1f };
44 QuickSort.sort(values, things);
45 assertTrue(Arrays.equals(new float[] { 1f, 2f, 3f, 4f }, values));
46 assertTrue(Arrays.equals(sortedThings, things));
49 @Test(groups = { "Functional" })
50 public void testSort_byDoubleValues()
52 double[] values = new double[] { 3d, 2d, 4d, 1d };
53 QuickSort.sort(values, things);
54 assertTrue(Arrays.equals(new double[] { 1d, 2d, 3d, 4d }, values));
55 assertTrue(Arrays.equals(sortedThings, things));
59 * Sort by String is descending order, case-sensitive
61 @Test(groups = { "Functional" })
62 public void testSort_byStringValues()
64 String[] values = new String[] { "JOHN", "henry", "lucy", "ALISON" };
65 QuickSort.sort(values, things);
66 assertTrue(Arrays.equals(new String[] { "lucy", "henry", "JOHN",
68 assertTrue(Arrays.equals(new Object[] { c3, c2, c1, c4 }, things));
72 * Test whether sort is stable i.e. equal values retain their mutual ordering.
74 @Test(groups = { "Functional" }, enabled = false)
75 public void testSort_withDuplicates()
77 int[] values = new int[] { 3, 4, 2, 4, 1 };
78 Object[] things = new Object[] { "A", "X", "Y", "B", "Z" };
79 QuickSort.sort(values, things);
80 assertTrue(Arrays.equals(new int[] { 1, 2, 3, 4, 4 }, values));
81 // this fails - do we care?
82 assertTrue(Arrays.equals(new Object[] { "Z", "Y", "A", "X", "B" },
87 * Test that exercises sort with a mostly zero-valued sortby array. May be of
88 * interest to check the sort algorithm is efficient.
90 @Test(groups = { "Functional" })
91 public void testSort_MostlyZeroValues()
93 char[] residues = new char[64];
94 for (int i = 0; i < 64; i++)
96 residues[i] = (char) i;
98 float[] counts = new float[64];
102 QuickSort.sort(counts, residues);
103 assertEquals(43, residues[63]);
104 assertEquals(59, residues[62]);
105 assertEquals(62, residues[61]);