Created
January 31, 2013 04:24
-
-
Save mpdavis/4680177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cs417; | |
import static org.junit.Assert.*; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import cs417.BuggleSort; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class TestBuggleSort { | |
private BuggleSort bs; | |
@Before | |
public void setUp() { | |
bs = new BuggleSort(); | |
} | |
// Test empty array (mode 0) | |
@Test | |
public void testEmpty0() { | |
double[] array = new double[] {}; | |
double[] out_array = bs.buggleSort(array, 0); | |
assertSorted(array, out_array, 0); | |
} | |
// Test single value array (mode 0) | |
@Test | |
public void testSingle0() { | |
double[] array = new double[] {1}; | |
double[] out_array = bs.buggleSort(array, 0); | |
assertSorted(array, out_array, 0); | |
} | |
// Test empty array (mode 1) | |
@Test | |
public void testEmpty1() { | |
double[] array = new double[] {}; | |
double[] out_array = bs.buggleSort(array, 1); | |
assertSorted(array, out_array, 1); | |
} | |
// Test single value array (mode 1) | |
@Test | |
public void testSingle1() { | |
double[] array = new double[] {1}; | |
double[] out_array = bs.buggleSort(array, 1); | |
assertSorted(array, out_array, 1); | |
} | |
// Tests array with negative values (mode 0) | |
@Test | |
public void testNegatives0(){ | |
double[] array = new double[] {1, -2, 4, -5, 10}; | |
double[] out_array = bs.buggleSort(array, 0); | |
assertSorted(array, out_array, 0); | |
} | |
// Tests array with negative values (mode 1) | |
@Test | |
public void testNegatives1(){ | |
double[] array = new double[] {1, -2, 4, -5, 10}; | |
double[] out_array = bs.buggleSort(array, 1); | |
assertSorted(array, out_array, 1); | |
} | |
// Tests that the original array was unaltered by the sort. | |
@Test | |
public void testUnchangedOriginal() { | |
double[] array = new double[] {1, 3, 2}; | |
double[] same_array = new double[] {1, 3, 2}; | |
bs.buggleSort(array, 0); | |
for (int i=0; i<array.length; i++) { | |
assertTrue("The original array was altered", array[i] == same_array[i]); | |
} | |
} | |
// Tests that it can handle duplicate values in mode 0 | |
@Test(timeout=3000) | |
public void testWithDuplicates0() { | |
double[] array = new double[] {3, 2, 2, 1, 4, 7, 5}; | |
double[] same_array = new double[] {3, 2, 2, 1, 4, 7, 5}; | |
double[] other = bs.buggleSort(array, 0); | |
assertSorted(same_array, other, 0); | |
} | |
// Tests that it can handle duplicate values in mode 1 | |
@Test(timeout=3000) | |
public void testWithDuplicates1() { | |
double[] array = new double[] {3, 2, 2, 1, 4, 7, 5}; | |
double[] same_array = new double[] {3, 2, 2, 1, 4, 7, 5}; | |
double[] other = bs.buggleSort(array, 1); | |
assertSorted(same_array, other, 1); | |
} | |
// Tests that the sorter can handle decimal values. | |
@Test | |
public void testDecimals0() { | |
double[] in_array = new double[] {1.1, 1.8, 2.4, 1.6, 0.4}; | |
double[] same_array = new double[] {1.1, 1.8, 2.4, 1.6, 0.4}; | |
double[] out_array = bs.buggleSort(in_array, 0); | |
assertSorted(same_array, out_array, 0); | |
} | |
// Tests that the sorter can handle decimal values. | |
@Test | |
public void testDecimals1() { | |
double[] in_array = new double[] {1.1, 1.8, 2.4, 1.6, 0.4}; | |
double[] same_array = new double[] {1.1, 1.8, 2.4, 1.6, 0.4}; | |
double[] out_array = bs.buggleSort(in_array, 0); | |
assertSorted(same_array, out_array, 0); | |
} | |
// Test large arrays (larger than 100 elements) | |
@Test(timeout=1000) | |
public void testLargeArray() { | |
double[] in_array = new double [101]; | |
for (int i=0; i<101; i++) { | |
in_array[i] = i; | |
} | |
double[] out_array = bs.buggleSort(in_array, 0); | |
} | |
public boolean assertSorted(double[] in_array, double[] out_array, int mode) { | |
// Check that the arrays are the same length | |
if (in_array.length != out_array.length) fail("Arrays have different lengths"); | |
// Check if the array is longer than one value. | |
if (in_array.length < 2) return true; | |
// Check that the array is in order | |
double lastVal, value; | |
for (int i=1; i<in_array.length; i++) { | |
lastVal = out_array[i-1]; | |
value = out_array[i]; | |
if (mode == 0 && lastVal > value) fail("Sorted array not in correct order."); | |
if (mode == 1 && lastVal < value) fail("Sorted array not in correct order."); | |
} | |
// Check that the arrays have the same objects. | |
// This works by constructing an ArrayList out of one of the arrays, and then | |
// checking that each value in the other array is in the ArrayList. If it is | |
// in the ArrayList, we remove it from the ArrayList so that we can account | |
// for duplicates. | |
Double test = new Double(1.5); | |
ArrayList<Double> al = new ArrayList<Double>(); | |
for (int i=0; i<out_array.length; i++) { | |
al.add(new Double(out_array[i])); | |
} | |
for (int i=0; i<in_array.length; i++) { | |
if (!al.remove(in_array[i])) fail("Incorrect value in sorted array."); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment