Skip to content

Commit f7da114

Browse files
authored
Fixed copyOf, copyOfRange and asList failures (#1690)
1 parent 25036b0 commit f7da114

File tree

11 files changed

+337
-82
lines changed

11 files changed

+337
-82
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.utbot.examples.arrays
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.testing.AtLeast
5+
import org.utbot.testing.FullWithAssumptions
6+
import org.utbot.testing.UtValueTestCaseChecker
7+
import org.utbot.testing.ignoreExecutionsNumber
8+
import org.utbot.testing.isException
9+
10+
class CopyOfExampleTest : UtValueTestCaseChecker(testClass = CopyOfExample::class) {
11+
@Test
12+
fun testCopyOf() {
13+
checkWithException(
14+
CopyOfExample::copyOfExample,
15+
ignoreExecutionsNumber,
16+
{ _, l, r -> l < 0 && r.isException<NegativeArraySizeException>() },
17+
{ arr, l, r -> arr.copyOf(l).contentEquals(r.getOrThrow()) },
18+
coverage = FullWithAssumptions(assumeCallsNumber = 1)
19+
)
20+
}
21+
22+
@Test
23+
fun testCopyOfRange() {
24+
checkWithException(
25+
CopyOfExample::copyOfRangeExample,
26+
ignoreExecutionsNumber,
27+
{ _, from, _, r -> from < 0 && r.isException<ArrayIndexOutOfBoundsException>() },
28+
{ arr, from, _, r -> from > arr.size && r.isException<ArrayIndexOutOfBoundsException>() },
29+
{ _, from, to, r -> from > to && r.isException<IllegalArgumentException>() },
30+
{ arr, from, to, r -> arr.copyOfRange(from, to).contentEquals(r.getOrThrow()) },
31+
coverage = AtLeast(82)
32+
)
33+
}
34+
}

utbot-framework-test/src/test/kotlin/org/utbot/examples/collections/ListsPart3Test.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import org.junit.jupiter.api.Disabled
55
import org.junit.jupiter.api.Test
66
import org.utbot.testcheckers.eq
77
import org.utbot.testcheckers.ge
8+
import org.utbot.testcheckers.withoutConcrete
89
import org.utbot.testing.CodeGeneration
910
import org.utbot.testing.DoNotCalculate
11+
import org.utbot.testing.FullWithAssumptions
1012
import org.utbot.testing.UtValueTestCaseChecker
1113
import org.utbot.testing.between
1214
import org.utbot.testing.isException
@@ -217,6 +219,21 @@ internal class ListsPart3Test : UtValueTestCaseChecker(
217219
)
218220
}
219221

222+
@Test
223+
fun testAsListExample() {
224+
withEnabledTestingCodeGeneration(testCodeGeneration = false) { // TODO Assemble model for java.util.ArrayList is returned, but actual type is java.util.Arrays.ArrayList https://github.com/UnitTestBot/UTBotJava/issues/398
225+
withoutConcrete { // TODO Concrete fail matchers with "Cannot show class" error
226+
check(
227+
Lists::asListExample,
228+
eq(2),
229+
{ arr, r -> arr.isEmpty() && r!!.isEmpty() },
230+
{ arr, r -> arr.isNotEmpty() && arr.contentEquals(r!!.toTypedArray()) },
231+
coverage = FullWithAssumptions(assumeCallsNumber = 1)
232+
)
233+
}
234+
}
235+
}
236+
220237
@Test
221238
@Disabled("TODO: add choosing proper type in list wrapper")
222239
fun testRemoveFromList() {

utbot-framework-test/src/test/kotlin/org/utbot/examples/collections/MapsPart2Test.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.utbot.examples.collections
22

3+
import org.junit.jupiter.api.Disabled
34
import org.utbot.framework.plugin.api.CodegenLanguage
45
import org.junit.jupiter.api.Test
56
import org.utbot.testcheckers.ge
@@ -68,7 +69,7 @@ internal class MapsPart2Test : UtValueTestCaseChecker(
6869
}
6970
}
7071

71-
@Test
72+
@Disabled("Flaky https://github.com/UnitTestBot/UTBotJava/issues/1695")
7273
fun testPutAllEntries() {
7374
withPushingStateFromPathSelectorForConcrete {
7475
check(

utbot-framework/src/main/java/org/utbot/engine/overrides/System.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static void arraycopy(Object src, int srcPos, Object dest, int destPos, i
210210
}
211211

212212
UtArrayMock.arraycopy(srcArray, srcPos, destArray, destPos, length);
213-
} else {
213+
} else if (src instanceof Object[]) {
214214
if (!(dest instanceof Object[])) {
215215
throw new ArrayStoreException();
216216
}
@@ -223,6 +223,9 @@ public static void arraycopy(Object src, int srcPos, Object dest, int destPos, i
223223
}
224224

225225
UtArrayMock.arraycopy(srcArray, srcPos, destArray, destPos, length);
226+
} else {
227+
// From docs: if the src argument refers to an object that is not an array, an ArrayStoreException will be thrown
228+
throw new ArrayStoreException();
226229
}
227230
}
228231
}

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/Arrays.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusi
2121
return new UtStream<>(array, startInclusive, endExclusive);
2222
}
2323

24-
// from docs - array is assumed to be umnodified during use
24+
// from docs - array is assumed to be unmodified during use
2525
public static IntStream stream(int[] array, int startInclusive, int endExclusive) {
2626
int size = array.length;
2727

@@ -37,7 +37,7 @@ public static IntStream stream(int[] array, int startInclusive, int endExclusive
3737
return new UtIntStream(data, startInclusive, endExclusive);
3838
}
3939

40-
// from docs - array is assumed to be umnodified during use
40+
// from docs - array is assumed to be unmodified during use
4141
public static LongStream stream(long[] array, int startInclusive, int endExclusive) {
4242
int size = array.length;
4343

@@ -53,7 +53,7 @@ public static LongStream stream(long[] array, int startInclusive, int endExclusi
5353
return new UtLongStream(data, startInclusive, endExclusive);
5454
}
5555

56-
// from docs - array is assumed to be umnodified during use
56+
// from docs - array is assumed to be unmodified during use
5757
public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
5858
int size = array.length;
5959

@@ -75,6 +75,4 @@ public static <T> List<T> asList(T... a) {
7575
// TODO immutable collection https://github.com/UnitTestBot/UTBotJava/issues/398
7676
return new UtArrayList<>(a);
7777
}
78-
79-
// TODO primitive arrays https://github.com/UnitTestBot/UTBotJava/issues/146
8078
}

utbot-framework/src/main/kotlin/org/utbot/engine/CollectionWrappers.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ val ARRAY_LIST_TYPE: RefType
418418
get() = Scene.v().getSootClass(java.util.ArrayList::class.java.canonicalName).type
419419
val LINKED_LIST_TYPE: RefType
420420
get() = Scene.v().getSootClass(java.util.LinkedList::class.java.canonicalName).type
421+
val ARRAY_DEQUE_TYPE: RefType
422+
get() = Scene.v().getSootClass(java.util.ArrayDeque::class.java.canonicalName).type
421423

422424
val LINKED_HASH_SET_TYPE: RefType
423425
get() = Scene.v().getSootClass(java.util.LinkedHashSet::class.java.canonicalName).type

0 commit comments

Comments
 (0)