/* * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9) * Copyright (C) 2015 The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.util; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertTrue; import java.util.Arrays; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class QuickSortTest { private static final String c1 = "Blue"; private static final String c2 = "Yellow"; private static final String c3 = "Orange"; private static final String c4 = "Green"; private Object[] things; private final Object[] sortedThings = new Object[] { c4, c2, c1, c3 }; @BeforeMethod(alwaysRun = true) public void setUp() { things = new Object[] { c1, c2, c3, c4 }; } @Test(groups = { "Functional" }) public void testSort_byIntValues() { int[] values = new int[] { 3, 2, 4, 1 }; QuickSort.sort(values, things); assertTrue(Arrays.equals(new int[] { 1, 2, 3, 4 }, values)); assertTrue(Arrays.equals(sortedThings, things)); } @Test(groups = { "Functional" }) public void testSort_byFloatValues() { float[] values = new float[] { 3f, 2f, 4f, 1f }; QuickSort.sort(values, things); assertTrue(Arrays.equals(new float[] { 1f, 2f, 3f, 4f }, values)); assertTrue(Arrays.equals(sortedThings, things)); } @Test(groups = { "Functional" }) public void testSort_byDoubleValues() { double[] values = new double[] { 3d, 2d, 4d, 1d }; QuickSort.sort(values, things); assertTrue(Arrays.equals(new double[] { 1d, 2d, 3d, 4d }, values)); assertTrue(Arrays.equals(sortedThings, things)); } /** * Sort by String is descending order, case-sensitive */ @Test(groups = { "Functional" }) public void testSort_byStringValues() { String[] values = new String[] { "JOHN", "henry", "lucy", "ALISON" }; QuickSort.sort(values, things); assertTrue(Arrays.equals(new String[] { "lucy", "henry", "JOHN", "ALISON" }, values)); assertTrue(Arrays.equals(new Object[] { c3, c2, c1, c4 }, things)); } /** * Test whether sort is stable i.e. equal values retain their mutual ordering. */ @Test(groups = { "Functional" }, enabled = false) public void testSort_withDuplicates() { int[] values = new int[] { 3, 4, 2, 4, 1 }; Object[] things = new Object[] { "A", "X", "Y", "B", "Z" }; QuickSort.sort(values, things); assertTrue(Arrays.equals(new int[] { 1, 2, 3, 4, 4 }, values)); // this fails - do we care? assertTrue(Arrays.equals(new Object[] { "Z", "Y", "A", "X", "B" }, things)); } /** * Test that exercises sort with a mostly zero-valued sortby array. May be of * interest to check the sort algorithm is efficient. */ @Test(groups = { "Functional" }) public void testSort_MostlyZeroValues() { char[] residues = new char[64]; for (int i = 0; i < 64; i++) { residues[i] = (char) i; } float[] counts = new float[64]; counts[43] = 16; counts[59] = 7; counts[62] = 2; QuickSort.sort(counts, residues); assertEquals(43, residues[63]); assertEquals(59, residues[62]); assertEquals(62, residues[61]); } }