JAL-1805 test envirionment separation
[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[]
24   { c4, c2, c1, c3 };
25
26   @BeforeMethod
27   public void setUp()
28   {
29     things = new Object[]
30     { c1, c2, c3, c4 };
31   }
32
33   @Test(groups ={ "Functional" })
34   public void testSort_byIntValues()
35   {
36     int[] values = new int[]
37     { 3, 2, 4, 1 };
38     QuickSort.sort(values, things);
39     assertTrue(Arrays.equals(new int[]
40     { 1, 2, 3, 4 }, values));
41     assertTrue(Arrays.equals(sortedThings, things));
42   }
43
44   @Test(groups ={ "Functional" })
45   public void testSort_byFloatValues()
46   {
47     float[] values = new float[]
48     { 3f, 2f, 4f, 1f };
49     QuickSort.sort(values, things);
50     assertTrue(Arrays.equals(new float[]
51     { 1f, 2f, 3f, 4f }, values));
52     assertTrue(Arrays.equals(sortedThings, things));
53   }
54
55   @Test(groups ={ "Functional" })
56   public void testSort_byDoubleValues()
57   {
58     double[] values = new double[]
59     { 3d, 2d, 4d, 1d };
60     QuickSort.sort(values, things);
61     assertTrue(Arrays.equals(new double[]
62     { 1d, 2d, 3d, 4d }, values));
63     assertTrue(Arrays.equals(sortedThings, things));
64   }
65
66   /**
67    * Sort by String is descending order, case-sensitive
68    */
69   @Test(groups ={ "Functional" })
70   public void testSort_byStringValues()
71   {
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));
79   }
80
81   /**
82    * Test whether sort is stable i.e. equal values retain their mutual ordering.
83    */
84   @Test(groups =
85   { "Functional" }, enabled = false)
86   public void testSort_withDuplicates()
87   {
88     int[] values = new int[]
89     { 3, 4, 2, 4, 1 };
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));
97   }
98
99   /**
100    * Test that exercises sort with a mostly zero-valued sortby array. May be of
101    * interest to check the sort algorithm is efficient.
102    */
103   @Test(groups ={ "Functional" })
104   public void testSort_MostlyZeroValues()
105   {
106     char[] residues = new char[64];
107     for (int i = 0; i < 64; i++)
108     {
109       residues[i] = (char) i;
110     }
111     float[] counts = new float[64];
112     counts[43] = 16;
113     counts[59] = 7;
114     counts[62] = 2;
115     QuickSort.sort(counts, residues);
116     assertEquals(43, residues[63]);
117     assertEquals(59, residues[62]);
118     assertEquals(62, residues[61]);
119   }
120 }