Skip to content

Commit 8498b7d

Browse files
Example of a failing test: array swapping
1 parent 980d0c3 commit 8498b7d

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed

src/test/preconditions/InsertionSort.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public boolean insertOrderedPrecondition(int[] array, int n) {
1313
return (n >= 0 && n < array.length && isOrdered(array, n));
1414
}
1515

16+
public boolean insertOrderedPostcondition(int[] array, int n) {
17+
return isOrdered(array, n+1);
18+
}
1619

1720
/**
1821
* Muggl throws a ClassCastException with this function.
@@ -30,11 +33,8 @@ public void sillyFunction(int[] array, Integer position) {
3033

3134
/**
3235
* Sorting by insertion. Iterative algorithm.
33-
*
34-
* But it seems Muggl cannot handle this, because of the error shown
35-
* above (sillyFunction)
3636
*/
37-
public void insertOrdered(int[] array, int n) {
37+
public void insertOrdered(int[] array, Integer n) {
3838
while (n > 0 && array[n] < array[n-1]) {
3939
int tmp = array[n-1];
4040
array[n-1] = array[n];
@@ -49,4 +49,27 @@ public void insertionSort(int[] array) {
4949
}
5050
}
5151

52+
/*
53+
public int insertionSort_check_precondition(int[] array) {
54+
for (int i = 0; i < array.length; i++) {
55+
if (!insertOrderedPrecondition(array, i)) {
56+
return 1;
57+
}
58+
insertOrdered(array, i);
59+
if (!insertOrderedPostcondition(array, i)) {
60+
return -1;
61+
}
62+
}
63+
return 0;
64+
}*/
65+
66+
public int simpleArraySwap(int[] array) {
67+
// Swap the first two elements
68+
int tmp = array[0];
69+
array[0] = array[1];
70+
array[1] = tmp;
71+
72+
if (array[0] > array[1]) return 1;
73+
return 0;
74+
}
5275
}

src/test/unitTests/TestClass4.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package test.unitTests;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* This class has been generated by Muggl for the automated testing of method
9+
* test.preconditions.InsertionSort.simpleArraySwap(int[] array).
10+
* Test cases have been computed using the symbolic execution of Muggl. Muggl
11+
* is a tool for the fully automated generation of test cases by analysing a
12+
* program's byte code. It aims at testing any possible flow through the program's
13+
* code rather than "guessing" required test cases, as a human would do.
14+
* Refer to http://www.wi.uni-muenster.de/pi/personal/majchrzak.php for more
15+
* information or contact the author at [email protected].
16+
*
17+
* Executing the method main(null) will invoke JUnit (if it is in the class path).
18+
* The methods for setting up the test and for running the tests have also been
19+
* annotated.
20+
*
21+
* Important settings for this run:
22+
* Search algorithm: iterative deepening depth first
23+
* Time Limit: 15 seconds
24+
* Maximum loop cycles to take: 200
25+
* Maximum instructions before
26+
* finding a new solution: infinite
27+
*
28+
* The total number of solutions found was 51. After deleting redundancy and
29+
* removing unnecessary solutions, 51 distinct test cases were found.
30+
* By eliminating solutions based on their contribution to the
31+
* def-use chain and control graph edge coverage
32+
* the total number of solutions could be reduced by 47 to the final number of 4 test cases.
33+
*
34+
* Covered def-use chains: 12 of 12
35+
* Covered control graph edges: 26 of 32
36+
*
37+
* This file has been generated on Wednesday, 03 September, 2014 14:06.
38+
*
39+
* @author Muggl 1.00 Alpha (unreleased)
40+
*/
41+
public class TestClass4 {
42+
// Fields for test parameters and expected return values.
43+
private test.preconditions.InsertionSort testedClass;
44+
private int int1;
45+
private int int0;
46+
private int[] array0 = {0,-1};
47+
private int[] array1 = {0,0};
48+
private int[] array2 = null;
49+
private int[] array3 = {0};
50+
51+
/**
52+
* Set up the unit test by initializing the fields to the desired values.
53+
*/
54+
@Before public void setUp() {
55+
this.testedClass = new test.preconditions.InsertionSort();
56+
this.int1 = 1;
57+
this.int0 = 0;
58+
}
59+
60+
/**
61+
* Run the tests on test.preconditions.InsertionSort.simpleArraySwap(int[] array).
62+
*/
63+
@Test public void testIt() {
64+
assertEquals(this.int1, this.testedClass.simpleArraySwap(this.array0));
65+
assertEquals(this.int0, this.testedClass.simpleArraySwap(this.array1));
66+
try {
67+
this.testedClass.simpleArraySwap(this.array2);
68+
fail("Expected a java.lang.NullPointerException to be thrown.");
69+
} catch (java.lang.NullPointerException e) {
70+
// Do nothing - this is what we expect to happen!
71+
}
72+
try {
73+
this.testedClass.simpleArraySwap(this.array3); // In this solution, not all parameters are used (int array[0] is unused).
74+
fail("Expected a java.lang.ArrayIndexOutOfBoundsException to be thrown.");
75+
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
76+
// Do nothing - this is what we expect to happen!
77+
}
78+
}
79+
80+
/**
81+
* Invoke JUnit to run the unit tests.
82+
* @param args Command line arguments, which are ignored here. Just supply null.
83+
*/
84+
public static void main(String args[]) {
85+
org.junit.runner.JUnitCore.main("test.unitTests.TestClass4");
86+
}
87+
88+
}

0 commit comments

Comments
 (0)