Skip to content

Commit 76a4ec0

Browse files
committed
Add tests on lambdas
1 parent c08820b commit 76a4ec0

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.utbot.examples.lambda
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.examples.UtValueTestCaseChecker
5+
import org.utbot.examples.eq
6+
7+
class CustomPredicateExampleTest : UtValueTestCaseChecker(testClass = CustomPredicateExample::class) {
8+
@Test
9+
fun testNoCapturedValuesPredicateCheck() {
10+
check(
11+
CustomPredicateExample::noCapturedValuesPredicateCheck,
12+
eq(2),
13+
{ predicate, x, r -> !predicate.test(x) && !r!! },
14+
{ predicate, x, r -> predicate.test(x) && r!! },
15+
)
16+
}
17+
18+
@Test
19+
fun testCapturedLocalVariablePredicateCheck() {
20+
check(
21+
CustomPredicateExample::capturedLocalVariablePredicateCheck,
22+
eq(2),
23+
{ predicate, x, r -> !predicate.test(x) && !r!! },
24+
{ predicate, x, r -> predicate.test(x) && r!! },
25+
)
26+
}
27+
28+
@Test
29+
fun testCapturedParameterPredicateCheck() {
30+
check(
31+
CustomPredicateExample::capturedParameterPredicateCheck,
32+
eq(2),
33+
{ predicate, x, r -> !predicate.test(x) && !r!! },
34+
{ predicate, x, r -> predicate.test(x) && r!! },
35+
)
36+
}
37+
38+
@Test
39+
fun testCapturedStaticFieldPredicateCheck() {
40+
check(
41+
CustomPredicateExample::capturedStaticFieldPredicateCheck,
42+
eq(2),
43+
{ predicate, x, r -> !predicate.test(x) && !r!! },
44+
{ predicate, x, r -> predicate.test(x) && r!! },
45+
)
46+
}
47+
48+
@Test
49+
fun testCapturedNonStaticFieldPredicateCheck() {
50+
check(
51+
CustomPredicateExample::capturedNonStaticFieldPredicateCheck,
52+
eq(2),
53+
{ predicate, x, r -> !predicate.test(x) && !r!! },
54+
{ predicate, x, r -> predicate.test(x) && r!! },
55+
)
56+
}
57+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.utbot.examples.lambda;
2+
3+
public class CustomPredicateExample {
4+
static int someStaticField = 5;
5+
int someNonStaticField = 10;
6+
7+
public boolean noCapturedValuesPredicateCheck(PredicateNoCapturedValues<Integer> predicate, int x) {
8+
//noinspection RedundantIfStatement
9+
if (predicate.test(x)) {
10+
return true;
11+
} else {
12+
return false;
13+
}
14+
}
15+
16+
public boolean capturedLocalVariablePredicateCheck(PredicateCapturedLocalVariable<Integer> predicate, int x) {
17+
//noinspection RedundantIfStatement
18+
if (predicate.test(x)) {
19+
return true;
20+
} else {
21+
return false;
22+
}
23+
}
24+
25+
public boolean capturedParameterPredicateCheck(PredicateCapturedParameter<Integer> predicate, int x) {
26+
//noinspection RedundantIfStatement
27+
if (predicate.test(x)) {
28+
return true;
29+
} else {
30+
return false;
31+
}
32+
}
33+
34+
public boolean capturedStaticFieldPredicateCheck(PredicateCapturedStaticField<Integer> predicate, int x) {
35+
//noinspection RedundantIfStatement
36+
if (predicate.test(x)) {
37+
return true;
38+
} else {
39+
return false;
40+
}
41+
}
42+
43+
public boolean capturedNonStaticFieldPredicateCheck(PredicateCapturedNonStaticField<Integer> predicate, int x) {
44+
//noinspection RedundantIfStatement
45+
if (predicate.test(x)) {
46+
return true;
47+
} else {
48+
return false;
49+
}
50+
}
51+
52+
// this method contains implementation of functional interface 'CustomPredicate'
53+
void someLambdas(int someParameter) {
54+
PredicateNoCapturedValues<Integer> predicate1 = (x) -> x == 5;
55+
56+
int localVariable = 10;
57+
PredicateCapturedLocalVariable<Integer> predicate2 = (x) -> x + localVariable == 5;
58+
59+
PredicateCapturedParameter<Integer> predicate3 = (x) -> x + someParameter == 5;
60+
61+
PredicateCapturedStaticField<Integer> predicate4 = (x) -> x + someStaticField == 5;
62+
63+
PredicateCapturedNonStaticField<Integer> predicate5 = (x) -> x + someNonStaticField == 5;
64+
}
65+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.utbot.examples.lambda;
2+
3+
/**
4+
* This functional interface is implemented via one lambda in {@link CustomPredicateExample#someLambdas}.
5+
*
6+
* DO NOT implement it anymore, because test on {@link CustomPredicateExample#capturedLocalVariablePredicateCheck}
7+
* relies on the fact that there is only one implementation and that implementation is lambda.
8+
* In addition, in this case we want the implementing lambda to capture some local variable.
9+
*
10+
* It is important because we want to test how we generate tests when the only available implementation is lambda,
11+
* and we want to check different cases: with or without captured values. Note that lambdas may capture
12+
* local variables, method parameters, static and non-static fields. That is why we have multiple functional interfaces
13+
* in this package: one for each case.
14+
*/
15+
@FunctionalInterface
16+
public interface PredicateCapturedLocalVariable<T> {
17+
boolean test(T value);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.utbot.examples.lambda;
2+
3+
/**
4+
* This functional interface is implemented via one lambda in {@link CustomPredicateExample#someLambdas}.
5+
*
6+
* DO NOT implement it anymore, because test on {@link CustomPredicateExample#capturedNonStaticFieldPredicateCheck}
7+
* relies on the fact that there is only one implementation and that implementation is lambda.
8+
* In addition, in this case we want the implementing lambda to capture some non-static field of a class.
9+
*
10+
* It is important because we want to test how we generate tests when the only available implementation is lambda,
11+
* and we want to check different cases: with or without captured values. Note that lambdas may capture
12+
* local variables, method parameters, static and non-static fields. That is why we have multiple functional interfaces
13+
* in this package: one for each case.
14+
*/
15+
@FunctionalInterface
16+
public interface PredicateCapturedNonStaticField<T> {
17+
boolean test(T value);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.utbot.examples.lambda;
2+
3+
/**
4+
* This functional interface is implemented via one lambda in {@link CustomPredicateExample#someLambdas}.
5+
*
6+
* DO NOT implement it anymore, because test on {@link CustomPredicateExample#capturedParameterPredicateCheck}
7+
* relies on the fact that there is only one implementation and that implementation is lambda.
8+
* In addition, in this case we want the implementing lambda to capture some method parameter.
9+
*
10+
* It is important because we want to test how we generate tests when the only available implementation is lambda,
11+
* and we want to check different cases: with or without captured values. Note that lambdas may capture
12+
* local variables, method parameters, static and non-static fields. That is why we have multiple functional interfaces
13+
* in this package: one for each case.
14+
*/
15+
@FunctionalInterface
16+
public interface PredicateCapturedParameter<T> {
17+
boolean test(T value);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.utbot.examples.lambda;
2+
3+
/**
4+
* This functional interface is implemented via one lambda in {@link CustomPredicateExample#someLambdas}.
5+
*
6+
* DO NOT implement it anymore, because test on {@link CustomPredicateExample#capturedStaticFieldPredicateCheck}
7+
* relies on the fact that there is only one implementation and that implementation is lambda.
8+
* In addition, in this case we want the implementing lambda to capture some static field of some class.
9+
*
10+
* It is important because we want to test how we generate tests when the only available implementation is lambda,
11+
* and we want to check different cases: with or without captured values. Note that lambdas may capture
12+
* local variables, method parameters, static and non-static fields. That is why we have multiple functional interfaces
13+
* in this package: one for each case.
14+
*/
15+
@FunctionalInterface
16+
public interface PredicateCapturedStaticField<T> {
17+
boolean test(T value);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.utbot.examples.lambda;
2+
3+
/**
4+
* This functional interface is implemented via one lambda in {@link CustomPredicateExample#someLambdas}.
5+
*
6+
* DO NOT implement it anymore, because test on {@link CustomPredicateExample#noCapturedValuesPredicateCheck}
7+
* relies on the fact that there is only one implementation and that implementation is lambda.
8+
* In addition, in this case we want the implementing lambda to not capture any values.
9+
*
10+
* It is important because we want to test how we generate tests when the only available implementation is lambda,
11+
* and we want to check different cases: with or without captured values. Note that lambdas may capture
12+
* local variables, method parameters, static and non-static fields. That is why we have multiple functional interfaces
13+
* in this package: one for each case.
14+
*/
15+
@FunctionalInterface
16+
public interface PredicateNoCapturedValues<T> {
17+
boolean test(T value);
18+
}

0 commit comments

Comments
 (0)