Skip to content

Commit b6c89b8

Browse files
authored
Fixed split implementation in the wrapper for String class and fixed some warnings in tests for String (#1615)
1 parent 1172872 commit b6c89b8

File tree

3 files changed

+79
-16
lines changed

3 files changed

+79
-16
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/examples/strings/StringExamplesTest.kt

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import org.utbot.testcheckers.withSolverTimeoutInMillis
1010
import org.utbot.testcheckers.withoutMinimization
1111
import org.utbot.testing.CodeGeneration
1212
import org.utbot.testing.DoNotCalculate
13+
import org.utbot.testing.FullWithAssumptions
1314
import org.utbot.testing.UtValueTestCaseChecker
1415
import org.utbot.testing.atLeast
1516
import org.utbot.testing.between
1617
import org.utbot.testing.ignoreExecutionsNumber
1718
import org.utbot.testing.isException
1819
import org.utbot.testing.keyMatch
20+
import java.util.Locale
1921

2022
internal class StringExamplesTest : UtValueTestCaseChecker(
2123
testClass = StringExamples::class,
@@ -161,7 +163,7 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
161163
{ v, _ -> v == null },
162164
{ v, r -> v != null && v.startsWith("1234567890") && r!!.startsWith("12a4567890") },
163165
{ v, r -> v != null && v[0] == 'x' && r!![0] == 'x' },
164-
{ v, r -> v != null && v.toLowerCase() == r }
166+
{ v, r -> v != null && v.lowercase(Locale.getDefault()) == r }
165167
)
166168
}
167169

@@ -245,7 +247,7 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
245247
check(
246248
StringExamples::concat,
247249
between(1..2),
248-
{ fst, snd, r -> fst == null || snd == null && r == fst + snd },
250+
{ fst, snd, r -> (fst == null || snd == null) && r == fst + snd },
249251
{ fst, snd, r -> r == fst + snd },
250252
)
251253
}
@@ -327,9 +329,9 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
327329
check(
328330
StringExamples::isValidUuid,
329331
ignoreExecutionsNumber,
330-
{ uuid, r -> uuid == null || uuid.length == 0 && r == false },
331-
{ uuid, r -> uuid.length > 0 && uuid.isBlank() && r == false },
332-
{ uuid, r -> uuid.length > 0 && uuid.isNotBlank() && r == false },
332+
{ uuid, r -> uuid == null || uuid.isEmpty() && r == false },
333+
{ uuid, r -> uuid.isNotEmpty() && uuid.isBlank() && r == false },
334+
{ uuid, r -> uuid.isNotEmpty() && uuid.isNotBlank() && r == false },
333335
{ uuid, r -> uuid.length > 1 && uuid.isNotBlank() && !uuid.matches(pattern) && r == false },
334336
{ uuid, r -> uuid.length > 1 && uuid.isNotBlank() && uuid.matches(pattern) && r == true },
335337
)
@@ -347,15 +349,29 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
347349
)
348350
}
349351

352+
@Test
353+
fun testSplitExample() {
354+
check(
355+
StringExamples::splitExample,
356+
ignoreExecutionsNumber,
357+
{ s, r -> s.all { it.isWhitespace() } && r == 0 },
358+
{ s, r -> s.none { it.isWhitespace() } && r == 1 },
359+
{ s, r -> s[0].isWhitespace() && s.any { !it.isWhitespace() } && r == 2 },
360+
{ s, r -> !s[0].isWhitespace() && s[2].isWhitespace() && r == 1 },
361+
{ s, r -> !s[0].isWhitespace() && s[1].isWhitespace() && !s[2].isWhitespace() && r == 2 },
362+
coverage = FullWithAssumptions(assumeCallsNumber = 2)
363+
)
364+
}
365+
350366
@Test
351367
fun testIsBlank() {
352368
check(
353369
StringExamples::isBlank,
354370
ge(4),
355371
{ cs, r -> cs == null && r == true },
356-
{ cs, r -> cs.length == 0 && r == true },
357-
{ cs, r -> cs.length > 0 && cs.isBlank() && r == true },
358-
{ cs, r -> cs.length > 0 && cs.isNotBlank() && r == false },
372+
{ cs, r -> cs.isEmpty() && r == true },
373+
{ cs, r -> cs.isNotEmpty() && cs.isBlank() && r == true },
374+
{ cs, r -> cs.isNotEmpty() && cs.isNotBlank() && r == false },
359375
summaryNameChecks = listOf(
360376
keyMatch("testIsBlank_StrLenEqualsZero"),
361377
keyMatch("testIsBlank_NotCharacterIsWhitespace"),
@@ -541,9 +557,9 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
541557
check(
542558
StringExamples::endsWith,
543559
between(5..6),
544-
{ s, suffix, r -> suffix == null },
545-
{ s, suffix, r -> suffix != null && suffix.length < 2 },
546-
{ s, suffix, r -> suffix != null && suffix.length >= 2 && s == null },
560+
{ _, suffix, _ -> suffix == null },
561+
{ _, suffix, _ -> suffix != null && suffix.length < 2 },
562+
{ s, suffix, _ -> suffix != null && suffix.length >= 2 && s == null },
547563
{ s, suffix, r -> suffix != null && suffix.length >= 2 && s != null && s.endsWith(suffix) && r == true },
548564
{ s, suffix, r -> suffix != null && suffix.length >= 2 && s != null && !s.endsWith(suffix) && r == false }
549565
)
@@ -609,10 +625,10 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
609625
checkWithException(
610626
StringExamples::compareCodePoints,
611627
between(8..10),
612-
{ s, t, i, r -> s == null && r.isException<NullPointerException>() },
613-
{ s, t, i, r -> s != null && i < 0 || i >= s.length && r.isException<StringIndexOutOfBoundsException>() },
614-
{ s, t, i, r -> s != null && t == null && r.isException<NullPointerException>() },
615-
{ s, t, i, r -> t != null && i < 0 || i >= t.length && r.isException<StringIndexOutOfBoundsException>() },
628+
{ s, _, _, r -> s == null && r.isException<NullPointerException>() },
629+
{ s, _, i, r -> s != null && i < 0 || i >= s.length && r.isException<StringIndexOutOfBoundsException>() },
630+
{ s, t, _, r -> s != null && t == null && r.isException<NullPointerException>() },
631+
{ _, t, i, r -> t != null && i < 0 || i >= t.length && r.isException<StringIndexOutOfBoundsException>() },
616632
{ s, t, i, r -> s != null && t != null && s.codePointAt(i) < t.codePointAt(i) && i == 0 && r.getOrThrow() == 0 },
617633
{ s, t, i, r -> s != null && t != null && s.codePointAt(i) < t.codePointAt(i) && i != 0 && r.getOrThrow() == 1 },
618634
{ s, t, i, r -> s != null && t != null && s.codePointAt(i) >= t.codePointAt(i) && i == 0 && r.getOrThrow() == 2 },
@@ -625,7 +641,7 @@ internal class StringExamplesTest : UtValueTestCaseChecker(
625641
check(
626642
StringExamples::toCharArray,
627643
eq(2),
628-
{ s, r -> s == null },
644+
{ s, _ -> s == null },
629645
{ s, r -> s.toCharArray().contentEquals(r) }
630646
)
631647
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,12 @@ public String[] split(String regex, int limit) {
857857
}
858858

859859
private String[] splitImpl(String regex) {
860+
executeConcretely();
860861
return toString().split(regex);
861862
}
862863

863864
public String[] split(String regex) {
865+
preconditionCheck();
864866
return splitImpl(regex);
865867
}
866868

utbot-sample/src/main/java/org/utbot/examples/strings/StringExamples.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utbot.examples.strings;
22

33
import org.jetbrains.annotations.NotNull;
4+
import org.utbot.api.mock.UtMock;
45

56
import java.util.Arrays;
67

@@ -236,6 +237,50 @@ public boolean isValidUuidShortVersion(String uuid) {
236237
return uuid != null && uuid.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
237238
}
238239

240+
@SuppressWarnings("IfStatementWithIdenticalBranches")
241+
public int splitExample(String s) {
242+
UtMock.assume(s != null);
243+
UtMock.assume(s.length() == 3);
244+
245+
final char firstChar = s.charAt(0);
246+
final char secondChar = s.charAt(1);
247+
final char thirdChar = s.charAt(2);
248+
249+
final boolean isFirstWhitespace = Character.isWhitespace(firstChar);
250+
final boolean isSecondWhitespace = Character.isWhitespace(secondChar);
251+
final boolean isThirdWhitespace = Character.isWhitespace(thirdChar);
252+
253+
if (isFirstWhitespace) {
254+
if (isSecondWhitespace) {
255+
if (isThirdWhitespace) {
256+
return s.split("\\s+").length;
257+
} else {
258+
return s.split("\\s+").length;
259+
}
260+
} else {
261+
if (isThirdWhitespace) {
262+
return s.split("\\s+").length;
263+
} else {
264+
return s.split("\\s+").length;
265+
}
266+
}
267+
} else {
268+
if (isSecondWhitespace) {
269+
if (isThirdWhitespace) {
270+
return s.split("\\s+").length;
271+
} else {
272+
return s.split("\\s+").length;
273+
}
274+
} else {
275+
if (isThirdWhitespace) {
276+
return s.split("\\s+").length;
277+
} else {
278+
return s.split("\\s+").length;
279+
}
280+
}
281+
}
282+
}
283+
239284
public boolean isNotBlank(CharSequence cs) {
240285
return !isBlank(cs);
241286
}

0 commit comments

Comments
 (0)