Skip to content

Commit d290903

Browse files
Add: dynamic invoke processing for string concatenation
1 parent dcb65f3 commit d290903

File tree

10 files changed

+471
-24
lines changed

10 files changed

+471
-24
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ allprojects {
4141
withType<KotlinCompile> {
4242
kotlinOptions {
4343
jvmTarget = "1.8"
44-
freeCompilerArgs = freeCompilerArgs + listOf("-Xallow-result-return-type", "-Xsam-conversions=class")
44+
freeCompilerArgs = freeCompilerArgs + listOf("-Xallow-result-return-type", "-Xsam-conversions=class", "-Xcontext-receivers")
4545
allWarningsAsErrors = false
4646
}
4747
}
4848
compileTestKotlin {
4949
kotlinOptions {
5050
jvmTarget = "1.8"
51-
freeCompilerArgs = freeCompilerArgs + listOf("-Xallow-result-return-type", "-Xsam-conversions=class")
51+
freeCompilerArgs = freeCompilerArgs + listOf("-Xallow-result-return-type", "-Xsam-conversions=class", "-Xcontext-receivers")
5252
allWarningsAsErrors = false
5353
}
5454
}

utbot-framework-test/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
66
}
77

88
tasks.withType(JavaCompile) {
9-
sourceCompatibility = JavaVersion.VERSION_1_8
9+
sourceCompatibility = JavaVersion.VERSION_11
1010
targetCompatibility = JavaVersion.VERSION_11
1111
}
1212

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.utbot.examples.strings;
2+
3+
import org.utbot.api.mock.UtMock;
4+
5+
class Test {
6+
int x;
7+
8+
@Override
9+
public String toString() {
10+
if (x == 42) {
11+
throw new IllegalArgumentException();
12+
}
13+
return "x = " + x;
14+
}
15+
}
16+
17+
public class StringConcat {
18+
String str;
19+
public String concatArguments(String a, String b, String c) {
20+
return a + b + c;
21+
}
22+
23+
public int concatWithConstants(String a) {
24+
String res = '<' + a + '>';
25+
26+
if (res.equals("<head>")) {
27+
return 1;
28+
}
29+
30+
if (res.equals("<body>")) {
31+
return 2;
32+
}
33+
34+
if (a == null) {
35+
return 3;
36+
}
37+
38+
return 4;
39+
}
40+
41+
public String concatWithPrimitives(String a) {
42+
return a + '#' + 42 + 53.0;
43+
}
44+
45+
public String exceptionInToString(Test t) {
46+
return "Test: " + t + "!";
47+
}
48+
49+
public String concatWithField(String a) {
50+
return a + str + '#';
51+
}
52+
53+
public int concatWithPrimitiveWrappers(Integer b, char c) {
54+
String res = "" + b + c;
55+
56+
if (res.endsWith("42")) {
57+
return 1;
58+
}
59+
return 2;
60+
}
61+
62+
public int sameConcat(String a, String b) {
63+
UtMock.assume(a != null && b != null);
64+
65+
String res1 = '!' + a + '#';
66+
String res2 = '!' + b + '#';
67+
68+
if (res1.equals(res2)) {
69+
return 0;
70+
} else {
71+
return 1;
72+
}
73+
}
74+
75+
public String concatStrangeSymbols() {
76+
return "\u0000" + '#' + '\u0001' + "!\u0002" + "@\u0012\t";
77+
}
78+
}
79+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package org.utbot.examples.strings
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.framework.plugin.api.CodegenLanguage
5+
import org.utbot.testcheckers.eq
6+
import org.utbot.testcheckers.withoutConcrete
7+
import org.utbot.tests.infrastructure.*
8+
9+
class StringConcatTest : UtValueTestCaseChecker(
10+
testClass = StringConcat::class,
11+
testCodeGeneration = true,
12+
pipelines = listOf(
13+
TestLastStage(CodegenLanguage.JAVA),
14+
TestLastStage(CodegenLanguage.KOTLIN, CodeGeneration)
15+
)
16+
) {
17+
@Test
18+
fun testConcatArguments() {
19+
withoutConcrete {
20+
check(
21+
StringConcat::concatArguments,
22+
eq(1),
23+
{ a, b, c, r -> "$a$b$c" == r }
24+
)
25+
}
26+
}
27+
28+
@Test
29+
fun testConcatWithConstants() {
30+
withoutConcrete {
31+
check(
32+
StringConcat::concatWithConstants,
33+
eq(4),
34+
{ a, r -> a == "head" && r == 1 },
35+
{ a, r -> a == "body" && r == 2 },
36+
{ a, r -> a == null && r == 3 },
37+
{ a, r -> a != "head" && a != "body" && a != null && r == 4 },
38+
)
39+
}
40+
}
41+
42+
@Test
43+
fun testConcatWithPrimitives() {
44+
withoutConcrete {
45+
check(
46+
StringConcat::concatWithPrimitives,
47+
eq(1),
48+
{ a, r -> "$a#4253.0" == r}
49+
)
50+
}
51+
}
52+
53+
@Test
54+
fun testExceptionInToString() {
55+
withoutConcrete {
56+
checkWithException(
57+
StringConcat::exceptionInToString,
58+
ignoreExecutionsNumber,
59+
{ t, r -> t.x == 42 && r.isException<IllegalArgumentException>() },
60+
{ t, r -> t.x != 42 && r.getOrThrow() == "Test: x = ${t.x}!" },
61+
coverage = DoNotCalculate
62+
)
63+
}
64+
}
65+
66+
@Test
67+
fun testConcatWithField() {
68+
withoutConcrete {
69+
checkWithThis(
70+
StringConcat::concatWithField,
71+
eq(1),
72+
{ o, a, r -> "$a${o.str}#" == r }
73+
)
74+
}
75+
}
76+
77+
@Test
78+
fun testConcatWithPrimitiveWrappers() {
79+
withoutConcrete {
80+
check(
81+
StringConcat::concatWithPrimitiveWrappers,
82+
ignoreExecutionsNumber,
83+
{ b, c, r -> b.toString().endsWith("4") && c == '2' && r == 1 },
84+
{ _, c, r -> !c.toString().endsWith("42") && r == 2 },
85+
coverage = DoNotCalculate
86+
)
87+
}
88+
}
89+
90+
@Test
91+
fun testSameConcat() {
92+
withoutConcrete {
93+
check(
94+
StringConcat::sameConcat,
95+
ignoreExecutionsNumber,
96+
{ a, b, r -> a == b && r == 0 },
97+
{ a, b, r -> a != b && r == 1 },
98+
coverage = DoNotCalculate
99+
)
100+
}
101+
}
102+
103+
@Test
104+
fun testConcatStrangeSymbols() {
105+
withoutConcrete {
106+
check(
107+
StringConcat::concatStrangeSymbols,
108+
eq(1),
109+
{ r -> r == "\u0000#\u0001!\u0002@\u0012\t" }
110+
)
111+
}
112+
}
113+
114+
}

utbot-framework/src/main/java/org/utbot/engine/overrides/strings/UtStringBuffer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ public StringBuffer insert(int dstOffset, CharSequence s, int start, int end) {
450450
}
451451

452452
public StringBuffer insert(int offset, boolean b) {
453-
return insert(offset, String.valueOf(b));
453+
return insert(offset, Boolean.toString(b));
454454
}
455455

456456
public StringBuffer insert(int offset, char c) {
@@ -463,19 +463,19 @@ public StringBuffer insert(int offset, char c) {
463463
}
464464

465465
public StringBuffer insert(int offset, int i) {
466-
return insert(offset, String.valueOf(i));
466+
return insert(offset, Integer.toString(i));
467467
}
468468

469469
public StringBuffer insert(int offset, long l) {
470-
return insert(offset, String.valueOf(l));
470+
return insert(offset, Long.toString(l));
471471
}
472472

473473
public StringBuffer insert(int offset, float f) {
474-
return insert(offset, String.valueOf(f));
474+
return insert(offset, Float.toString(f));
475475
}
476476

477477
public StringBuffer insert(int offset, double d) {
478-
return insert(offset, String.valueOf(d));
478+
return insert(offset, Double.toString(d));
479479
}
480480

481481
public int indexOf(String str) {

utbot-framework/src/main/java/org/utbot/engine/overrides/strings/UtStringBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public StringBuilder insert(int dstOffset, CharSequence s, int start, int end) {
451451
}
452452

453453
public StringBuilder insert(int offset, boolean b) {
454-
return insert(offset, String.valueOf(b));
454+
return insert(offset, Boolean.toString(b));
455455
}
456456

457457
public StringBuilder insert(int offset, char c) {
@@ -464,19 +464,19 @@ public StringBuilder insert(int offset, char c) {
464464
}
465465

466466
public StringBuilder insert(int offset, int i) {
467-
return insert(offset, String.valueOf(i));
467+
return insert(offset, Integer.toString(i));
468468
}
469469

470470
public StringBuilder insert(int offset, long l) {
471-
return insert(offset, String.valueOf(l));
471+
return insert(offset, Long.toString(l));
472472
}
473473

474474
public StringBuilder insert(int offset, float f) {
475-
return insert(offset, String.valueOf(f));
475+
return insert(offset, Float.toString(f));
476476
}
477477

478478
public StringBuilder insert(int offset, double d) {
479-
return insert(offset, String.valueOf(d));
479+
return insert(offset, Double.toString(d));
480480
}
481481

482482
public int indexOf(String str) {

0 commit comments

Comments
 (0)