Created
October 16, 2014 20:22
-
-
Save chbaranowski/86fdbceb3d6be0832f7f to your computer and use it in GitHub Desktop.
Quicksort Test Example
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
import static org.fest.assertions.Assertions.*; | |
import java.util.Arrays; | |
import org.junit.Test; | |
public class QuicksortTest { | |
Quicksort<String> textSorter = Quicksort.createTextSorter(); | |
Quicksort<Integer> intSorter = Quicksort.createIntSorter(); | |
@Test | |
public void sortIntArray() { | |
Integer[] values = new Integer[] { 5, 6, 3 }; | |
intSorter.sort(values); | |
assertArrayIsSorted(values); | |
} | |
@Test | |
public void sortIntArrayFast() { | |
Integer[] values = new Integer[] { 5, 4, 6, 2, 1 }; | |
intSorter.slowIntegerSort = false; | |
intSorter.sort(values); | |
assertArrayIsSorted(values); | |
} | |
@Test | |
public void sortTextArray() { | |
String[] values = new String[] { "xy", "aa", "bb" }; | |
textSorter.sort(values); | |
assertArrayIsSorted(values); | |
} | |
static <T extends Comparable<T>> void assertArrayIsSorted(T[] array) { | |
assertThat(array) | |
.describedAs("A null instance of array could not be verified as sorted.") | |
.isNotNull(); | |
assertThat(array.length) | |
.describedAs("A array with one element could not be tested as sorted.") | |
.isGreaterThan(1); | |
for (int i = 1; i < array.length; i++) { | |
T before = array[i - 1]; | |
T actual = array[i]; | |
boolean valueBeforeIsSmallerOrEqualsThanActual = before.compareTo(actual) <= 0; | |
assertThat(valueBeforeIsSmallerOrEqualsThanActual) | |
.describedAs( Arrays.toString(array) + " array is not sorted.") | |
.isTrue(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment