Commit 659bd58c authored by Christian Wulf's avatar Christian Wulf

refactored tests for the divide and conquer stage

parent 0247b29e
......@@ -32,6 +32,10 @@ public final class QuicksortProblem extends AbstractDivideAndConquerProblem<Quic
private final int high;
private final int[] numbers;
public QuicksortProblem(final int... numbers) {
this(0, numbers.length - 1, numbers);
}
/**
* An implementation of a quicksort problem.
*
......
......@@ -15,60 +15,127 @@
*/
package teetime.stage;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertArrayEquals;
import static teetime.framework.test.StageTester.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import teetime.framework.DivideAndConquerStage;
import teetime.stage.quicksort.QuicksortProblem;
import teetime.stage.quicksort.QuicksortSolution;
import teetime.testutil.ArrayCreator;
import teetime.testutil.AssertHelper;
/**
* @author Robin Mohr
*/
public class DivideAndConquerStageTest {
DivideAndConquerStage<QuicksortProblem, QuicksortSolution> quicksortStage;
List<QuicksortSolution> solutions;
List<QuicksortProblem> problems;
// @Before
private DivideAndConquerStage<QuicksortProblem, QuicksortSolution> quicksortStage;
@Before
public void initialize() {
quicksortStage = new DivideAndConquerStage<QuicksortProblem, QuicksortSolution>();
}
int[] numbers = new int[] { 3, 1, 4, 5, 2 };
QuicksortProblem problemOne = new QuicksortProblem(1, 0, numbers.length - 1, numbers);
@Test
public void shouldSortEmptyArray() {
int[] noInputValues = {};
QuicksortProblem problem = new QuicksortProblem(noInputValues);
List<QuicksortSolution> solutions = new ArrayList<QuicksortSolution>();
problems = new ArrayList<QuicksortProblem>();
problems.add(problemOne);
test(this.quicksortStage).and()
.send(problem).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
solutions = new ArrayList<QuicksortSolution>();
assertThat(solutions.get(0).getNumbers(), is(noInputValues));
assertThat(solutions.size(), is(1));
}
// @Test
// FIXME runs infinitely so far
public void quicksortImplementationShouldSortArray() {
@Test
public void shouldSortSingleArray() {
int[] numbers = { 5, 4, 3, 2, 1 };
QuicksortProblem problem = new QuicksortProblem(numbers);
List<QuicksortSolution> solutions = new ArrayList<QuicksortSolution>();
test(this.quicksortStage).and()
.send(problem).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
int[] sortedNumbers = new int[] { 1, 2, 3, 4, 5 };
assertArrayEquals(solutions.get(0).getNumbers(), sortedNumbers);
assertThat(solutions.size(), is(1));
}
@Test
public void shouldSortMultipleArrays() {
List<QuicksortProblem> problems = new ArrayList<QuicksortProblem>();
int[] firstInts = { 3, 8, 4, 2, 1, 9, 5, 10, 7, 6 };
int[] secondInts = { 2, 4, 3, 1, 5 };
int[] thirdInts = { 4, 3, 3, 4, 2, 4, 1, 2, 3, 4 };
int[] fourthInts = { 4 };
problems.add(new QuicksortProblem(firstInts));
problems.add(new QuicksortProblem(secondInts));
problems.add(new QuicksortProblem(thirdInts));
problems.add(new QuicksortProblem(fourthInts));
List<QuicksortSolution> solutions = new ArrayList<QuicksortSolution>();
test(this.quicksortStage).and()
.send(problems).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
AssertHelper.assertSorted(solutions.get(0).getNumbers());
AssertHelper.assertSorted(solutions.get(1).getNumbers());
AssertHelper.assertSorted(solutions.get(2).getNumbers());
AssertHelper.assertSorted(solutions.get(3).getNumbers());
assertThat(solutions.size(), is(4));
}
@Test
public void tester() {
for (int i = 0; i < 100; i++) {
initialize();
quicksortImplementationShouldSortArray();
evaluate();
}
public void shouldSortHugeArray() {
int[] numbers = new ArrayCreator(0).createFilledArray(10000000);
QuicksortProblem problem = new QuicksortProblem(numbers);
List<QuicksortSolution> solutions = new ArrayList<QuicksortSolution>();
test(this.quicksortStage).and()
.send(problem).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
AssertHelper.assertSorted(solutions.get(0).getNumbers());
assertThat(solutions.size(), is(1));
}
// @After
public void evaluate() {
int[] sortedNumbers = new int[] { 1, 2, 3, 4, 5 };
assertArrayEquals(solutions.get(0).getNumbers(), sortedNumbers);
@Test
public void shouldSortMultipleHugeArrays() {
ArrayCreator arrayCreator = new ArrayCreator(0);
List<QuicksortProblem> problems = new ArrayList<QuicksortProblem>();
problems.add(new QuicksortProblem(arrayCreator.createFilledArray(10000000)));
problems.add(new QuicksortProblem(arrayCreator.createFilledArray(10000000)));
problems.add(new QuicksortProblem(arrayCreator.createFilledArray(10000000)));
List<QuicksortSolution> solutions = new ArrayList<QuicksortSolution>();
test(this.quicksortStage).and()
.send(problems).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
AssertHelper.assertSorted(solutions.get(0).getNumbers());
AssertHelper.assertSorted(solutions.get(1).getNumbers());
AssertHelper.assertSorted(solutions.get(2).getNumbers());
assertThat(solutions.size(), is(problems.size()));
}
}
/**
* Copyright © 2015 Christian Wulf, Nelson Tavares de Sousa (http://teetime-framework.github.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package teetime.stage;
import static org.junit.Assert.assertTrue;
import static teetime.framework.test.StageTester.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import teetime.framework.DivideAndConquerStage;
import teetime.stage.quicksort.QuicksortProblem;
import teetime.stage.quicksort.QuicksortSolution;
/**
* @author Robin Mohr
*/
public class ExtendedDivideAndConquerStageTest {
DivideAndConquerStage<QuicksortProblem, QuicksortSolution> quicksortStage;
List<QuicksortSolution> solutions;
List<QuicksortProblem> problems;
private QuicksortProblem problemOne;
private QuicksortProblem problemTwo;
private QuicksortProblem problemThree;
private QuicksortProblem problemFour;
private QuicksortProblem problemFive;
@Before
public void initialize() {
quicksortStage = new DivideAndConquerStage<QuicksortProblem, QuicksortSolution>();
int[] firstInts = { 3, 8, 4, 2, 1, 9, 5, 10, 7, 6 };
int[] secondInts = { 2, 4, 3, 1, 5 };
int[] thirdInts = { 4, 3, 3, 4, 2, 4, 1, 2, 3, 4 };
int[] fourthInts = { 4 };
int[] fifthInts = {};
problemOne = new QuicksortProblem(0, firstInts.length - 1, firstInts);
problemTwo = new QuicksortProblem(0, secondInts.length - 1, secondInts);
problemThree = new QuicksortProblem(0, thirdInts.length - 1, thirdInts);
problemFour = new QuicksortProblem(0, fourthInts.length - 1, fourthInts);
problemFive = new QuicksortProblem(0, fifthInts.length - 1, fifthInts);
problems = new ArrayList<QuicksortProblem>();
problems.add(problemOne);
problems.add(problemTwo);
problems.add(problemThree);
problems.add(problemFour);
problems.add(problemFive);
solutions = new ArrayList<QuicksortSolution>();
}
@Test
@Ignore
// FIXME runs infinitely so far
public void quicksortImplementationShouldSortArray() {
test(this.quicksortStage).and()
.send(problems).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
}
@After
public void evaluate() {
assertTrue(isSorted(solutions.get(0).getNumbers()));
assertTrue(isSorted(solutions.get(1).getNumbers()));
assertTrue(isSorted(solutions.get(2).getNumbers()));
}
private boolean isSorted(final int... ints) {
for (int i = 1; i < ints.length; i++) {
if (ints[i - 1] > ints[i]) {
return false;
}
}
return true;
}
}
/**
* Copyright © 2015 Christian Wulf, Nelson Tavares de Sousa (http://teetime-framework.github.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package teetime.stage;
import static org.junit.Assert.assertArrayEquals;
import static teetime.framework.test.StageTester.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import teetime.framework.divideandconquer.stages.DivideAndConquerSolveStage;
import teetime.stage.quicksort.QuicksortProblem;
import teetime.stage.quicksort.QuicksortSolution;
/**
* @author Robin Mohr
*/
public class SimpleDivideAndConquerStageTest {
DivideAndConquerSolveStage<QuicksortProblem, QuicksortSolution> quicksortStage;
List<QuicksortSolution> solutions;
List<QuicksortProblem> problems;
@Before
public void initialize() {
quicksortStage = new DivideAndConquerSolveStage<QuicksortProblem, QuicksortSolution>();
int[] numbers = new int[] { 3, 1, 4, 5, 2 };
QuicksortProblem problemOne = new QuicksortProblem(1, 0, numbers.length - 1, numbers);
problems = new ArrayList<QuicksortProblem>();
problems.add(problemOne);
solutions = new ArrayList<QuicksortSolution>();
}
@Test
public void quicksortImplementationShouldSortArray() {
test(this.quicksortStage).and()
.send(problems).to(quicksortStage.getInputPort()).and()
.receive(solutions).from(quicksortStage.getOutputPort())
.start();
}
@After
public void evaluate() {
int[] sortedNumbers = new int[] { 1, 2, 3, 4, 5 };
assertArrayEquals(solutions.get(0).getNumbers(), sortedNumbers);
}
}
package teetime.testutil;
import java.util.Random;
/**
*
* @author Christian Wulf
*
*/
public class ArrayCreator {
private final Random random;
public ArrayCreator(final long seed) {
random = new Random(seed);
}
public int[] createFilledArray(final int numValues) {
int[] randomValues = new int[numValues];
for (int i = 0; i < randomValues.length; i++) {
randomValues[i] = random.nextInt();
}
return randomValues;
}
}
......@@ -30,4 +30,14 @@ public final class AssertHelper {
assertThat(object, is(instanceOf(expectedClazz)));
return (T) object;
}
public static void assertSorted(final int[] values) {
int previousValue = values[0];
for (int i = 0; i < values.length; i++) {
if (previousValue > values[i]) {
String message = String.format("%d at index %d is smaller than its previous value %d", values[i], i, previousValue);
throw new AssertionError(message);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment