Merge branch 'develop' into features/JAL-845splitPaneMergeDevelop
[jalview.git] / test / jalview / util / QuickSortTest.java
1 package jalview.util;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.util.Arrays;
6
7 import org.junit.Before;
8 import org.junit.Ignore;
9 import org.junit.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   @Before
27   public void setUp()
28   {
29     things = new Object[]
30     { c1, c2, c3, c4 };
31   }
32
33   @Test
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
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
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
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
85   @Ignore
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 }