Skip to content

Commit 5211136

Browse files
committed
add: test for the evaluationof aggregate functions
1 parent 1b2d328 commit 5211136

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package de.vill.model;
2+
3+
import de.vill.main.UVLModelFactory;
4+
import de.vill.model.constraint.Constraint;
5+
import de.vill.model.constraint.ExpressionConstraint;
6+
import de.vill.model.expression.Expression;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.nio.file.Paths;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
class ExpressionTest {
16+
17+
@Test
18+
void testMathExpressionsModel() {
19+
// get the feature model and parse it using the UVLModelFactory
20+
UVLModelFactory uvlModelFactory = new UVLModelFactory();
21+
FeatureModel featureModel = uvlModelFactory.parse(Paths.get("src/test/resources/parsing/expressions.uvl"));
22+
// select features for evaluation
23+
Map<String, Feature> featureMap = featureModel.getFeatureMap();
24+
Set<Feature> selectedFeatures = Set.of(
25+
featureMap.get("B"),
26+
featureMap.get("C")
27+
);
28+
29+
// test the constraints
30+
for (Constraint constraint : featureModel.getOwnConstraints()) {
31+
if (constraint instanceof ExpressionConstraint) {
32+
ExpressionConstraint exprConstraint = (ExpressionConstraint) constraint;
33+
Expression left = exprConstraint.getLeft();
34+
Expression right = exprConstraint.getRight();
35+
double expected = right.evaluate(selectedFeatures);
36+
double actual = left.evaluate(selectedFeatures);
37+
assertEquals(expected, actual, 0.0001, "Constraint '" + exprConstraint );
38+
}
39+
}
40+
}
41+
42+
@Test
43+
void testAggregateFunctions() {
44+
// get the feature model and parse it using the UVLModelFactory
45+
UVLModelFactory uvlModelFactory = new UVLModelFactory();
46+
FeatureModel featureModel = uvlModelFactory.parse(Paths.get("src/test/resources/parsing/aggregateFunctions.uvl"));
47+
// select features for evaluation
48+
Map<String, Feature> featureMap = featureModel.getFeatureMap();
49+
Set<Feature> selectedFeatures = Set.of(
50+
featureMap.get("B"),
51+
featureMap.get("C")
52+
);
53+
54+
// test the constraints
55+
for (Constraint constraint : featureModel.getOwnConstraints()) {
56+
if (constraint instanceof ExpressionConstraint) {
57+
ExpressionConstraint exprConstraint = (ExpressionConstraint) constraint;
58+
Expression left = exprConstraint.getLeft();
59+
Expression right = exprConstraint.getRight();
60+
double expected = right.evaluate(selectedFeatures);
61+
double actual = left.evaluate(selectedFeatures);
62+
assertEquals(expected, actual, 0.0001, "Constraint '" + exprConstraint );
63+
}
64+
}
65+
}
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
features
2+
A {Price 2}
3+
optional
4+
B {Price 2}
5+
C {Price 5}
6+
7+
constraints
8+
sum(Price) == 7
9+
avg(Price) == 3.5
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
features
2+
A
3+
or
4+
B {Price 2, Fun 12}
5+
C {Price 7, Fun 6}
6+
constraints
7+
B.Fun + C.Fun - B.Price == 16
8+
B.Fun + (C.Fun - B.Price) == 16
9+
B.Fun + C.Fun * B.Price == 24
10+
B.Fun + (C.Fun * B.Price) == 24
11+
B.Fun + C.Fun / B.Price == 15
12+
B.Fun + (C.Fun / B.Price) == 15
13+
B.Fun - C.Fun + B.Price == 8
14+
B.Fun - (C.Fun + B.Price) == 4
15+
B.Fun - C.Fun * B.Price == 0
16+
B.Fun - (C.Fun * B.Price) == 0
17+
B.Fun - C.Fun / B.Price == 9
18+
B.Fun - (C.Fun / B.Price) == 9
19+
B.Fun * C.Fun / B.Price == 36
20+
B.Fun * (C.Fun / B.Price) == 36
21+
B.Fun * C.Fun - B.Price == 70
22+
B.Fun * (C.Fun - B.Price) == 48
23+
B.Fun * C.Fun + B.Price == 74
24+
B.Fun * (C.Fun + B.Price) == 96
25+
B.Fun / C.Fun * B.Price == 4
26+
B.Fun / (C.Fun * B.Price) == 1
27+
B.Fun / C.Fun + B.Price == 4
28+
B.Fun / (C.Fun + B.Price) == 1.5
29+
B.Fun / C.Fun - B.Price == 0
30+
B.Fun / (C.Fun - B.Price) == 3

0 commit comments

Comments
 (0)