Skip to content

Commit

Permalink
Add tests that highlight ASSERT-KTH#67
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
andre15silva committed Jul 21, 2021
1 parent e4e3c34 commit f5706b4
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/install_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mvn clean test -DskipTests -Dmaven.compiler.source=$SRC_VERSION -Dmaven.compiler
mvn clean test -DskipTests -Dmaven.compiler.source=$SRC_VERSION -Dmaven.compiler.target=$SRC_VERSION -B -f examples/exampleFL5JUnit3/FLtest1/
mvn clean test -DskipTests -Dmaven.compiler.source=$SRC_VERSION -Dmaven.compiler.target=$SRC_VERSION -B -f examples/exampleFL6Mixed/FLtest1/
mvn clean test -DskipTests -Dmaven.compiler.source=$SRC_VERSION -Dmaven.compiler.target=$SRC_VERSION -B -f examples/exampleFL7SameNamedMethods/FLtest1/
mvn clean test -DskipTests -Dmaven.compiler.source=$SRC_VERSION -Dmaven.compiler.target=$SRC_VERSION -B -f examples/exampleFL10/FLtest1/

# Copy compiled classes to non-maven mirror projects
rm -r examples/exampleFL8NotMaven/bin/
Expand Down
27 changes: 27 additions & 0 deletions examples/exampleFL10/FLtest1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>fr.spoonlabs</groupId>
<artifactId>FLtest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>FLtest1</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.spoonlabs.FLtest1;

public class Calculator {

public Calculator() {
}

public int calculate(String op, int op1, int op2) {

while(true) {
if (op.equals("+")) {
return op1 + op2;
} else if (op.equals("-")) {
return op1 - op2;
} else if (op.equals("*")) {
return op1 / op2;//buggy
} else if (op.equals("/")) {
return op1 / op2;
} else if (op.equals("%")) {
return op1 % op2;
}
throw new UnsupportedOperationException(op);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.spoonlabs.FLtest1;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class CalculatorTest {

Calculator c = new Calculator();

@Test
public void testSum() {

assertEquals(4, c.calculate("+", 3, 1));

}

@Test
public void testSubs() {

assertEquals(2, c.calculate("-", 3, 1));

}

@Test
public void testMul() {

assertEquals(8, c.calculate("*", 4, 2));

}

@Test
public void testDiv() {

assertEquals(2, c.calculate("/", 12, 6));

}

}
33 changes: 33 additions & 0 deletions src/test/java/fr/spoonlabs/flacoco/api/FlacocoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -913,4 +913,37 @@ public void testExampleFL9SpectrumBasedOchiaiSpoonMode() {
}
}

@Test
public void testExampleFL10SpectrumBasedOchiaiDefaultMode() {
// Setup config
FlacocoConfig config = FlacocoConfig.getInstance();
config.setProjectPath(new File("./examples/exampleFL10/FLtest1").getAbsolutePath());
config.setFamily(FlacocoConfig.FaultLocalizationFamily.SPECTRUM_BASED);
config.setSpectrumFormula(SpectrumFormula.OCHIAI);

// Run Flacoco
Flacoco flacoco = new Flacoco();

// Run default mode
Map<String, Suspiciousness> susp = flacoco.runDefault();

for (String line : susp.keySet()) {
System.out.println("" + line + " " + susp.get(line));
}

assertEquals(5, susp.size());

// Line executed only by the failing
assertEquals(1.0, susp.get("fr/spoonlabs/FLtest1/Calculator@-@16").getScore(), 0);

// Line executed by a mix of failing and passing
assertEquals(0.70, susp.get("fr/spoonlabs/FLtest1/Calculator@-@15").getScore(), 0.01);
assertEquals(0.57, susp.get("fr/spoonlabs/FLtest1/Calculator@-@13").getScore(), 0.01);

// Lines executed by all test
// the first one is the while(true)
assertEquals(0.5, susp.get("fr/spoonlabs/FLtest1/Calculator@-@10").getScore(), 0);
assertEquals(0.5, susp.get("fr/spoonlabs/FLtest1/Calculator@-@11").getScore(), 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -959,4 +959,102 @@ public void testExampleFL9() {
assertNull(modCond);
}

@Test
public void testExampleFL10() {
// Setup config
FlacocoConfig config = FlacocoConfig.getInstance();
config.setProjectPath(new File("./examples/exampleFL10/FLtest1").getAbsolutePath());

CoverageRunner detector = new CoverageRunner();

// Find the tests
TestDetector testDetector = new TestDetector();
List<TestContext> tests = testDetector.getTests();

assertTrue(tests.size() > 0);

CoverageMatrix matrix = detector.getCoverageMatrix(tests);

// verify nr of test
assertEquals(4, matrix.getTests().size());
assertEquals(1, matrix.getFailingTestCases().size());

// 9 executed lines
assertEquals(9, matrix.getResultExecution().keySet().size());

// This line is the first if, so it's covered by all tests
// this is the while(true)
Set<TestMethod> firstLineExecuted = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@10");

assertEquals(4, firstLineExecuted.size());

Set<String> executedTestKeys = firstLineExecuted.stream()
.map(TestMethod::getFullyQualifiedMethodName).collect(Collectors.toSet());

assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSubs"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSum"));

assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testDiv"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testMul"));

// This one is only executed by the sum
Set<TestMethod> returnSum = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@12");

executedTestKeys = returnSum.stream()
.map(TestMethod::getFullyQualifiedMethodName).collect(Collectors.toSet());

assertEquals(1, returnSum.size());

assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSum"));

// This line is the second if, so it's covered by all tests, except the first one
Set<TestMethod> secondIfExecuted = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@13");
assertEquals(3, secondIfExecuted.size());

executedTestKeys = secondIfExecuted.stream()
.map(TestMethod::getFullyQualifiedMethodName).collect(Collectors.toSet());

// The first one returns before
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSum"));

assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSubs"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testDiv"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testMul"));

Set<TestMethod> oneMultCond = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@15");
assertEquals(2, oneMultCond.size());
executedTestKeys = oneMultCond.stream()
.map(TestMethod::getFullyQualifiedMethodName).collect(Collectors.toSet());
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSum"));
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSubs"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testDiv"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testMul"));

// This line is inside one if, so it's executed only one
Set<TestMethod> oneReturnLineExecuted = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@16");
assertEquals(1, oneReturnLineExecuted.size());

executedTestKeys = oneReturnLineExecuted.stream()
.map(TestMethod::getFullyQualifiedMethodName).collect(Collectors.toSet());

assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSum"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testMul"));
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSubs"));
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testDiv"));

Set<TestMethod> divisionCond = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@17");
assertEquals(1, divisionCond.size());
executedTestKeys = divisionCond.stream()
.map(TestMethod::getFullyQualifiedMethodName).collect(Collectors.toSet());

assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSum"));
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testMul"));
assertFalse(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testSubs"));
assertTrue(executedTestKeys.contains("fr.spoonlabs.FLtest1.CalculatorTest#testDiv"));

// Any test executes that
Set<TestMethod> modCond = matrix.getResultExecution().get("fr/spoonlabs/FLtest1/Calculator@-@19");
assertNull(modCond);
}

}

0 comments on commit f5706b4

Please sign in to comment.