Skip to content

Instantly share code, notes, and snippets.

@chbaranowski
Created October 16, 2014 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chbaranowski/86fdbceb3d6be0832f7f to your computer and use it in GitHub Desktop.
Save chbaranowski/86fdbceb3d6be0832f7f to your computer and use it in GitHub Desktop.
Quicksort Test Example
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