3e64249c88d3b761709416ff1081d8acdaf990c5
[jalview.git] / test / jalview / util / QuickSortTest.java
1 package jalview.util;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertTrue;
5
6 import java.util.Arrays;
7
8 import org.testng.annotations.BeforeMethod;
9 import org.testng.annotations.Test;
10
11 public class QuickSortTest
12 {
13   private static final String c1 = "Blue";
14
15   private static final String c2 = "Yellow";
16
17   private static final String c3 = "Orange";
18
19   private static final String c4 = "Green";
20
21   private Object[] things;
22
23   private final Object[] sortedThings = new Object[] { c4, c2, c1, c3 };
24
25   @BeforeMethod(alwaysRun = true)
26   public void setUp()
27   {
28     things = new Object[] { c1, c2, c3, c4 };
29   }
30
31   @Test(groups = { "Functional" })
32   public void testSort_byIntValues()
33   {
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));
38   }
39
40   @Test(groups = { "Functional" })
41   public void testSort_byFloatValues()
42   {
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));
47   }
48
49   @Test(groups = { "Functional" })
50   public void testSort_byDoubleValues()
51   {
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));
56   }
57
58   /**
59    * Sort by String is descending order, case-sensitive
60    */
61   @Test(groups = { "Functional" })
62   public void testSort_byStringValues()
63   {
64     String[] values = new String[] { "JOHN", "henry", "lucy", "ALISON" };
65     QuickSort.sort(values, things);
66     assertTrue(Arrays.equals(new String[] { "lucy", "henry", "JOHN",
67         "ALISON" }, values));
68     assertTrue(Arrays.equals(new Object[] { c3, c2, c1, c4 }, things));
69   }
70
71   /**
72    * Test whether sort is stable i.e. equal values retain their mutual ordering.
73    */
74   @Test(groups = { "Functional" }, enabled = false)
75   public void testSort_withDuplicates()
76   {
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" },
83             things));
84   }
85
86   /**
87    * Test that exercises sort with a mostly zero-valued sortby array. May be of
88    * interest to check the sort algorithm is efficient.
89    */
90   @Test(groups = { "Functional" })
91   public void testSort_MostlyZeroValues()
92   {
93     char[] residues = new char[64];
94     for (int i = 0; i < 64; i++)
95     {
96       residues[i] = (char) i;
97     }
98     float[] counts = new float[64];
99     counts[43] = 16;
100     counts[59] = 7;
101     counts[62] = 2;
102     QuickSort.sort(counts, residues);
103     assertEquals(43, residues[63]);
104     assertEquals(59, residues[62]);
105     assertEquals(62, residues[61]);
106   }
107 }