Commit 0832528e authored by Ludwig Dickmanns's avatar Ludwig Dickmanns
Browse files

MCDC: Testing

* Test cases for the generate method

Issue-Ref: 3476
Issue-Url: https://af3-developer.fortiss.org/issues/3476

Signed-off-by: Ludwig Dickmanns's avatarLudwig Dickmanns <dickmanns@fortiss.org>
parent a286d037
/*-------------------------------------------------------------------------+
| Copyright 2018 fortiss GmbH |
| |
| 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 test.org.fortiss.af3.testing.mcdc;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createComponent;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createInputPortAndAttach;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createOutputPortAndAttach;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.add;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.and;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.assignment;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.boolConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createNoVal;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.greaterThan;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.lowerThan;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.or;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.sequence;
import static org.fortiss.af3.testing.mcdc.McdcUtils.getAtomsFromGuardMCDC;
import static org.fortiss.af3.testing.utils.TestingModelElementFactory.createFormalRequirementMcdc;
import static org.fortiss.af3.testing.utils.TestingModelElementFactory.createTestCase;
import static org.fortiss.af3.testing.utils.TestingModelElementFactory.createTestInput;
import static org.fortiss.af3.testing.utils.TestingModelElementFactory.createTestOutput;
import static org.fortiss.af3.testing.utils.TestingModelElementFactory.createTestSuite;
import static org.fortiss.af3.testing.utils.TestingModelElementFactory.createTestSuiteSpecification;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.copy;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickFirstInstanceOf;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.model.InputPort;
import org.fortiss.af3.component.model.OutputPort;
import org.fortiss.af3.component.model.Port;
import org.fortiss.af3.expression.model.terms.FunctionCall;
import org.fortiss.af3.expression.model.terms.IExpressionTerm;
import org.fortiss.af3.expression.model.terms.Var;
import org.fortiss.af3.expression.model.terms.imperative.Assignment;
import org.fortiss.af3.expression.model.terms.imperative.StatementSequence;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.fortiss.af3.project.model.typesystem.IType;
import org.fortiss.af3.testing.extension.data.TestSuiteGenerationException;
import org.fortiss.af3.testing.mcdc.generator.McdcTestSuiteGenerator;
import org.fortiss.af3.testing.model.TestCase;
import org.fortiss.af3.testing.model.TestInput;
import org.fortiss.af3.testing.model.TestOutput;
import org.fortiss.af3.testing.model.TestSuite;
import org.fortiss.af3.testing.model.TestSuiteSpecification;
import org.fortiss.af3.testing.model.mcdc.FormalRequirementMcdc;
import org.fortiss.af3.testing.model.mcdc.McdcFactory;
import org.fortiss.af3.testing.model.mcdc.McdcSpecificationPart;
import org.fortiss.af3.testing.model.mcdc.McdcTestStep;
import org.junit.Test;
/**
*
* @author ludwig
*/
public class TestGenerate {
/**
* if (i < 5 && j > 8) {
* o = i + 5;
* }
* else {
* o = j + 5;
* }
*
* Initial test case: i=4 j=9 o=9
*/
// @Test
public void test1() {
Component component = createComponent();
IExpressionTerm guard;
StatementSequence thenBlock, elseBlock;
EList<Var> inputs = new BasicEList<Var>();
EList<Var> outputs = new BasicEList<Var>();
Port iPort = createInputPortAndAttach(component, "i", intType(), createNoVal());
Port jPort = createInputPortAndAttach(component, "j", intType(), createNoVal());
Var i = iPort.getVar();
Var j = jPort.getVar();
inputs.add(i);
inputs.add(j);
Port oPort = createOutputPortAndAttach(component, "o", intType(), createNoVal());
Var o = oPort.getVar();
outputs.add(o);
IExpressionTerm a = lowerThan(i, intConst(5));
IExpressionTerm b = greaterThan(j, intConst(8));
guard = and(a, b);
IExpressionTerm rhs;
rhs = add(i, intConst(5));
Assignment t = assignment(copy(o), rhs);
thenBlock = sequence(t);
rhs = add(j, intConst(5));
Assignment e = assignment(copy(o), rhs);
elseBlock = sequence(e);
FormalRequirementMcdc req =
createFormalRequirementMcdc(inputs, outputs, guard, thenBlock, elseBlock);
McdcSpecificationPart mcdcSpecificationPart =
McdcFactory.eINSTANCE.createMcdcSpecificationPart();
mcdcSpecificationPart.setFormalRequirementMcdc(req);
TestSuiteSpecification specification = createTestSuiteSpecification();
specification.addSpecification(mcdcSpecificationPart);
specification.setSpecificationOf(component);
TestSuite testSuite = createTestSuite("McdcSpecification", specification);
TestCase testCase = createTestCase(testSuite);
testCase.setName(Integer.toString(testCase.hashCode()));
McdcTestStep mcdcTestStep = McdcFactory.eINSTANCE.createMcdcTestStep();
testCase.getTestSteps().add(mcdcTestStep);
createTestInput(mcdcTestStep, intConst(4));
createTestInput(mcdcTestStep, intConst(9));
createTestOutput(mcdcTestStep, intConst(9), null);
mcdcTestStep.getTrueAtoms().addAll(getAtomsFromGuardMCDC(guard));
mcdcTestStep.setTakenBranch(boolConst(true));
// TestStep testStep1 = createTestStep(testCase, null);
// createTestInput(testStep1, intConst(4));
// createTestInput(testStep1, intConst(9));
// createTestOutput(testStep1, null, intConst(9));
//
// TestStep testStep2 = createTestStep(testCase, null);
// createTestInput(testStep2, boolConst(true));
// createTestInput(testStep2, boolConst(true));
// createTestOutput(testStep2, null, boolConst(true));
McdcTestSuiteGenerator generator = new McdcTestSuiteGenerator(testSuite);
try {
generator.generate(specification, null);
} catch(TestSuiteGenerationException | InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(req);
System.out.println();
printResults(testSuite, generator);
System.out.println();
System.out.println();
}
/**
* if (i < 5 && j > 8 && k < 6) {
* o = i + 5 + k;
* }
* else {
* o = j + 5 + k;
* }
*
* Initial test case: i=4 j=9 k=5 o=9
*/
// @Test
public void test2() {
Component component = createComponent();
IExpressionTerm guard;
StatementSequence thenBlock, elseBlock;
EList<Var> inputs = new BasicEList<Var>();
EList<Var> outputs = new BasicEList<Var>();
Port iPort = createInputPortAndAttach(component, "i", intType(), createNoVal());
Port jPort = createInputPortAndAttach(component, "j", intType(), createNoVal());
Port kPort = createInputPortAndAttach(component, "k", intType(), createNoVal());
Var i = iPort.getVar();
Var j = jPort.getVar();
Var k = kPort.getVar();
inputs.add(i);
inputs.add(j);
inputs.add(k);
Port oPort = createOutputPortAndAttach(component, "o", intType(), createNoVal());
Var o = oPort.getVar();
outputs.add(o);
IExpressionTerm a = lowerThan(i, intConst(5));
IExpressionTerm b = greaterThan(j, intConst(8));
IExpressionTerm c = lowerThan(k, intConst(6));
guard = and(and(a, b), c);
IExpressionTerm rhs;
rhs = add(add(i, intConst(5)), k);
Assignment t = assignment(copy(o), rhs);
thenBlock = sequence(t);
rhs = add(add(j, intConst(5)), k);
Assignment e = assignment(copy(o), rhs);
elseBlock = sequence(e);
FormalRequirementMcdc req =
createFormalRequirementMcdc(inputs, outputs, guard, thenBlock, elseBlock);
McdcSpecificationPart mcdcSpecificationPart =
McdcFactory.eINSTANCE.createMcdcSpecificationPart();
mcdcSpecificationPart.setFormalRequirementMcdc(req);
TestSuiteSpecification specification = createTestSuiteSpecification();
specification.addSpecification(mcdcSpecificationPart);
specification.setSpecificationOf(component);
TestSuite testSuite = createTestSuite("McdcSpecification", specification);
TestCase testCase = createTestCase(testSuite);
testCase.setName(Integer.toString(testCase.hashCode()));
McdcTestStep mcdcTestStep = McdcFactory.eINSTANCE.createMcdcTestStep();
testCase.getTestSteps().add(mcdcTestStep);
createTestInput(mcdcTestStep, intConst(4));
createTestInput(mcdcTestStep, intConst(9));
createTestInput(mcdcTestStep, intConst(5));
createTestOutput(mcdcTestStep, intConst(15), null);
mcdcTestStep.getTrueAtoms().addAll(getAtomsFromGuardMCDC(guard));
mcdcTestStep.setTakenBranch(boolConst(true));
McdcTestSuiteGenerator generator = new McdcTestSuiteGenerator(testSuite);
try {
generator.generate(specification, null);
} catch(TestSuiteGenerationException | InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(req);
System.out.println();
printResults(testSuite, generator);
System.out.println();
System.out.println();
}
/**
* if (i < 5 && j > 8 || k < 6 && l > 7) {
* o = (i + 5) + k;
* }
* else {
* o = (j + 5) + k;
* }
*
* Initial test case: i=4 j=5 k=5 l=6 o=15
*/
// @Test
public void test3() {
Component component = createComponent();
IExpressionTerm guard;
StatementSequence thenBlock, elseBlock;
EList<Var> inputs = new BasicEList<Var>();
EList<Var> outputs = new BasicEList<Var>();
Port iPort = createInputPortAndAttach(component, "i", intType(), createNoVal());
Port jPort = createInputPortAndAttach(component, "j", intType(), createNoVal());
Port kPort = createInputPortAndAttach(component, "k", intType(), createNoVal());
Port lPort = createInputPortAndAttach(component, "l", intType(), createNoVal());
Var i = iPort.getVar();
Var j = jPort.getVar();
Var k = kPort.getVar();
Var l = lPort.getVar();
inputs.add(i);
inputs.add(j);
inputs.add(k);
inputs.add(l);
Port oPort = createOutputPortAndAttach(component, "o", intType(), createNoVal());
Var o = oPort.getVar();
outputs.add(o);
IExpressionTerm a = lowerThan(i, intConst(5));
IExpressionTerm b = greaterThan(j, intConst(8));
IExpressionTerm c = lowerThan(k, intConst(6));
IExpressionTerm d = greaterThan(l, intConst(7));
IExpressionTerm leftAnd = and(a, b);
IExpressionTerm rightAnd = and(c, d);
guard = or(leftAnd, rightAnd);
IExpressionTerm rhs;
rhs = add(add(i, intConst(5)), k);
Assignment t = assignment(copy(o), rhs);
thenBlock = sequence(t);
rhs = add(add(j, intConst(5)), k);
Assignment e = assignment(copy(o), rhs);
elseBlock = sequence(e);
FormalRequirementMcdc req =
createFormalRequirementMcdc(inputs, outputs, guard, thenBlock, elseBlock);
McdcSpecificationPart mcdcSpecificationPart =
McdcFactory.eINSTANCE.createMcdcSpecificationPart();
mcdcSpecificationPart.setFormalRequirementMcdc(req);
TestSuiteSpecification specification = createTestSuiteSpecification();
specification.addSpecification(mcdcSpecificationPart);
specification.setSpecificationOf(component);
TestSuite testSuite = createTestSuite("McdcSpecification", specification);
TestCase testCase = createTestCase(testSuite);
testCase.setName(Integer.toString(testCase.hashCode()));
McdcTestStep mcdcTestStep = McdcFactory.eINSTANCE.createMcdcTestStep();
testCase.getTestSteps().add(mcdcTestStep);
createTestInput(mcdcTestStep, intConst(4));
createTestInput(mcdcTestStep, intConst(5));
createTestInput(mcdcTestStep, intConst(5));
createTestInput(mcdcTestStep, intConst(6));
createTestOutput(mcdcTestStep, intConst(15), null);
mcdcTestStep.getTrueAtoms().add(getAtomsFromGuardMCDC(guard).get(0));
mcdcTestStep.getTrueAtoms().add(getAtomsFromGuardMCDC(guard).get(2));
mcdcTestStep.setTakenBranch(boolConst(false));
McdcTestSuiteGenerator generator = new McdcTestSuiteGenerator(testSuite);
try {
generator.generate(specification, null);
} catch(TestSuiteGenerationException | InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(req);
System.out.println();
printResults(testSuite, generator);
System.out.println();
System.out.println();
}
@Test
public void test4() {
Component component = createComponent();
IExpressionTerm guard;
StatementSequence thenBlock, elseBlock;
EList<Var> inputs = new BasicEList<Var>();
EList<Var> outputs = new BasicEList<Var>();
Var i = createVar(component, inputs, "i", intType(), true);
Var j = createVar(component, inputs, "j", intType(), true);
Var k = createVar(component, inputs, "k", intType(), true);
Var l = createVar(component, inputs, "l", intType(), true);
Var m = createVar(component, inputs, "m", intType(), true);
Var n = createVar(component, inputs, "n", intType(), true);
Var o = createVar(component, inputs, "o", intType(), true);
Var p = createVar(component, inputs, "p", intType(), true);
Var out = createVar(component, outputs, "out", intType(), false);
IExpressionTerm a = lowerThan(i, intConst(5));
IExpressionTerm b = greaterThan(j, intConst(8));
IExpressionTerm c = lowerThan(k, intConst(6));
IExpressionTerm d = greaterThan(l, intConst(7));
IExpressionTerm e = lowerThan(m, intConst(5));
IExpressionTerm f = greaterThan(n, intConst(3));
IExpressionTerm g = lowerThan(o, intConst(9));
IExpressionTerm h = greaterThan(p, intConst(0));
List<IExpressionTerm> land = listWithMultipleArgs(a, b, c, d);
List<IExpressionTerm> rand = listWithMultipleArgs(e, f, g, h);
guard = or(and(land), and(rand));
IExpressionTerm rhs;
rhs = add(add(i, intConst(5)), k);
Assignment then = assignment(copy(out), rhs);
thenBlock = sequence(then);
rhs = add(add(j, intConst(5)), k);
Assignment elsE = assignment(copy(out), rhs);
elseBlock = sequence(elsE);
FormalRequirementMcdc req =
createFormalRequirementMcdc(inputs, outputs, guard, thenBlock, elseBlock);
McdcSpecificationPart mcdcSpecificationPart =
McdcFactory.eINSTANCE.createMcdcSpecificationPart();
mcdcSpecificationPart.setFormalRequirementMcdc(req);
TestSuiteSpecification specification = createTestSuiteSpecification();
specification.addSpecification(mcdcSpecificationPart);
specification.setSpecificationOf(component);
TestSuite testSuite = createTestSuite("McdcSpecification", specification);
McdcTestSuiteGenerator generator = new McdcTestSuiteGenerator(testSuite);
try {
generator.generate(specification, null);
} catch(TestSuiteGenerationException | InterruptedException e1) {
e1.printStackTrace();
}
printResults(testSuite, generator);
}
/**
* @param component
* @param vars
* @param name
* @param portType
* @param input
* @return a var
*/
private Var createVar(Component component, EList<Var> vars, String name, IType portType,
boolean input) {
Port p;
if(input) {
p = createInputPortAndAttach(component, name, portType, createNoVal());
} else {
p = createOutputPortAndAttach(component, name, portType, createNoVal());
}
Var v = p.getVar();
vars.add(v);
return v;
}
private <T> List<T> listWithMultipleArgs(T... args) {
List<T> list = new ArrayList<T>();
for(T t : args) {
list.add(t);
}
return list;
}
/**
* @param testSuite
* @param generator
*/
private void printResults(TestSuite testSuite, McdcTestSuiteGenerator generator) {
for(TestCase tc : testSuite.getTestCases()) {
McdcTestStep mcdcTestStep = (McdcTestStep)tc.getTestSteps().get(0);
EList<InputPort> inputPorts = testSuite.getInputPorts();
EList<TestInput> inputs = mcdcTestStep.getInputs();
for(int index = 0; index < inputs.size(); index++) {
InputPort inputPort = inputPorts.get(index);
ITerm evaluatedInput = inputs.get(index).getValue();
System.out.println(inputPort.getName() + " = " + evaluatedInput.toString());
}
EList<OutputPort> outputPorts = testSuite.getOutputPorts();
EList<TestOutput> outputs = mcdcTestStep.getOutputs();
for(int index = 0; index < outputs.size(); index++) {
OutputPort outputPort = outputPorts.get(index);
ITerm evaluatedOutput = outputs.get(index).getExpectedValue();
System.out.println(outputPort.getName() + " = " + evaluatedOutput.toString());
}
McdcSpecificationPart spec = pickFirstInstanceOf(McdcSpecificationPart.class,
testSuite.getGeneratedFrom().getSpecifications());
System.out.println(spec.getFormalRequirementMcdc().getGuard());
List<FunctionCall> atoms = generator.getAtoms();
List<FunctionCall> trueAtoms = mcdcTestStep.getTrueAtoms();
for(FunctionCall atom : atoms) {
boolean value = trueAtoms.contains(atom);
System.out.println(atom.toString() + " => " + value);
}
String takenBranch;
if(mcdcTestStep.getTakenBranch().getValue()) {
takenBranch = "Then branch";
} else {
takenBranch = "Else branch";
}
System.out.println("Taken branch => " + takenBranch);
}
for(Pair<TestCase, TestCase> pair : generator.getIndependencePairs()) {
System.out.println(pair.getKey() + " " + pair.getValue());
}
}
}
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