Skip to content

Commit c390666

Browse files
committedSep 21, 2022
fix review
1 parent edabce1 commit c390666

File tree

12 files changed

+168
-74
lines changed

12 files changed

+168
-74
lines changed
 

Diff for: ‎utbot-framework-test/src/test/kotlin/org/utbot/examples/casts/InstanceOfExampleTest.kt

+57-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.utbot.examples.casts
22

3-
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
4-
import org.utbot.tests.infrastructure.DoNotCalculate
5-
import org.utbot.tests.infrastructure.ignoreExecutionsNumber
6-
import org.utbot.framework.plugin.api.CodegenLanguage
73
import org.junit.jupiter.api.Disabled
84
import org.junit.jupiter.api.Test
5+
import org.utbot.framework.plugin.api.CodegenLanguage
96
import org.utbot.testcheckers.eq
107
import org.utbot.testcheckers.ge
118
import org.utbot.tests.infrastructure.CodeGeneration
9+
import org.utbot.tests.infrastructure.DoNotCalculate
10+
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
11+
import org.utbot.tests.infrastructure.ignoreExecutionsNumber
1212

1313
// TODO failed Kotlin compilation SAT-1332
1414
internal class InstanceOfExampleTest : UtValueTestCaseChecker(
@@ -405,15 +405,64 @@ internal class InstanceOfExampleTest : UtValueTestCaseChecker(
405405
}
406406

407407
@Test
408-
fun testInstanceOfString() {
408+
fun testStringInstanceOf() {
409409
check(
410-
InstanceOfExample::instanceOfString,
410+
InstanceOfExample::charSequenceInstanceOf,
411411
eq(2),
412-
{ cs, r -> cs == null && r == Unit },
413-
{ cs, r -> cs is String && r == null }
412+
{ _, r -> r == null },
413+
{ _, r -> r!!::class == String::class }
414414
)
415415
}
416416

417+
@Test
418+
fun testListInstanceOf() {
419+
check(
420+
InstanceOfExample::listInstanceOf,
421+
eq(2),
422+
{ _, r -> r == null },
423+
{ _, r -> r!!::class == java.util.ArrayList::class }
424+
)
425+
}
426+
427+
@Test
428+
fun testDequeInstanceOf() {
429+
check(
430+
InstanceOfExample::dequeInstanceOf,
431+
eq(2),
432+
{ _, r -> r == null },
433+
{ _, r -> r!!::class == java.util.LinkedList::class }
434+
)
435+
}
436+
437+
@Test
438+
fun testAbstractSeqListInstanceOf() {
439+
check(
440+
InstanceOfExample::abstractSequentialListInstanceOf,
441+
eq(2),
442+
{ _, r -> r == null },
443+
{ _, r -> r!!::class == java.util.LinkedList::class }
444+
)
445+
}
446+
447+
@Test
448+
fun testSetInstanceOf() {
449+
check(
450+
InstanceOfExample::setInstanceOf,
451+
eq(2),
452+
{ _, r -> r == null },
453+
{ _, r -> r!!::class == java.util.LinkedHashSet::class }
454+
)
455+
}
456+
457+
@Test
458+
fun testMapInstanceOf() {
459+
check(
460+
InstanceOfExample::mapInstanceOf,
461+
eq(2),
462+
{ _, r -> r == null },
463+
{ _, r -> r!!::class == java.util.LinkedHashMap::class }
464+
)
465+
}
417466

418467
private inline fun <reified T : Any> Any?.isInstanceOfArray() =
419468
(this as? Array<*>)?.run { T::class.java.isAssignableFrom(this::class.java.componentType) } == true

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/ArrayObjectWrappers.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,12 @@ class RangeModifiableUnlimitedArrayWrapper : WrapperInterface {
264264
return resultModel
265265
}
266266

267-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
268-
setOf(ARRAY_OBJECT_TYPE)
267+
override fun getPossibleConcreteTypes(type: Type): Set<Type> {
268+
val possibleObjectTypes = Scene.v().classes.map { it.type }
269+
return possibleObjectTypes.mapTo(mutableSetOf()) {
270+
it.arrayType
271+
}
272+
}
269273

270274
companion object {
271275
internal val rangeModifiableArrayClass: SootClass
@@ -280,7 +284,6 @@ class RangeModifiableUnlimitedArrayWrapper : WrapperInterface {
280284
}
281285

282286
val associativeArrayId: ClassId = AssociativeArray::class.id
283-
val associativeArrayType: Type = Scene.v().getSootClass(AssociativeArray::class.java.canonicalName).type
284287

285288
class AssociativeArrayWrapper : WrapperInterface {
286289

@@ -419,8 +422,8 @@ class AssociativeArrayWrapper : WrapperInterface {
419422
return model
420423
}
421424

422-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
423-
setOf(associativeArrayType)
425+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
426+
setOf(associativeArrayClass.type)
424427

425428
private fun Traverser.getStorageArrayField(addr: UtAddrExpression) =
426429
getArrayField(addr, associativeArrayClass, storageField)

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/CollectionWrappers.kt

+2-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import org.utbot.framework.plugin.api.util.methodId
3434
import org.utbot.framework.plugin.api.util.objectClassId
3535
import org.utbot.framework.util.graph
3636
import org.utbot.framework.util.nextModelName
37-
import soot.ArrayType
3837
import soot.IntType
3938
import soot.RefType
4039
import soot.Scene
@@ -132,8 +131,8 @@ abstract class BaseContainerWrapper(containerClassName: String) : BaseOverridden
132131
}
133132
}
134133

135-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
136-
setOf(Scene.v().getSootClass(chooseClassIdWithConstructor(type.classId).canonicalName).type)
134+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
135+
setOf(chooseClassIdWithConstructor(type.classId).sootType)
137136

138137
/**
139138
* Method returns list of parameter models that will be passed to [modificationMethodId]
@@ -403,24 +402,6 @@ private val UT_GENERIC_ASSOCIATIVE_CLASS
403402
private val UT_GENERIC_ASSOCIATIVE_SET_EQUAL_GENERIC_TYPE_SIGNATURE =
404403
UT_GENERIC_ASSOCIATIVE_CLASS.getMethodByName(UtGenericAssociative<*, *>::setEqualGenericType.name).signature
405404

406-
val ARRAY_OBJECT_TYPE: Type
407-
get() = ArrayType.v(OBJECT_TYPE, 1)
408-
409-
val ARRAY_LIST_TYPE: RefType
410-
get() = Scene.v().getSootClass(java.util.ArrayList::class.java.canonicalName).type
411-
val LINKED_LIST_TYPE: RefType
412-
get() = Scene.v().getSootClass(java.util.LinkedList::class.java.canonicalName).type
413-
414-
val LINKED_HASH_SET_TYPE: RefType
415-
get() = Scene.v().getSootClass(java.util.LinkedHashSet::class.java.canonicalName).type
416-
val HASH_SET_TYPE: RefType
417-
get() = Scene.v().getSootClass(java.util.HashSet::class.java.canonicalName).type
418-
419-
val LINKED_HASH_MAP_TYPE: RefType
420-
get() = Scene.v().getSootClass(java.util.LinkedHashMap::class.java.canonicalName).type
421-
val HASH_MAP_TYPE: RefType
422-
get() = Scene.v().getSootClass(java.util.HashMap::class.java.canonicalName).type
423-
424405
val STREAM_TYPE: RefType
425406
get() = Scene.v().getSootClass(java.util.stream.Stream::class.java.canonicalName).type
426407

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/Extensions.kt

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package org.utbot.engine
22

33
import com.google.common.collect.BiMap
4+
import java.lang.reflect.ParameterizedType
5+
import java.util.ArrayDeque
6+
import java.util.Deque
7+
import java.util.LinkedList
8+
import java.util.Queue
9+
import java.util.concurrent.ConcurrentHashMap
10+
import kotlinx.collections.immutable.PersistentMap
11+
import kotlinx.collections.immutable.persistentHashMapOf
412
import org.utbot.api.mock.UtMock
513
import org.utbot.engine.pc.UtAddrExpression
614
import org.utbot.engine.pc.UtArraySort
@@ -33,8 +41,11 @@ import org.utbot.engine.pc.mkString
3341
import org.utbot.engine.pc.toSort
3442
import org.utbot.framework.UtSettings.checkNpeInNestedMethods
3543
import org.utbot.framework.UtSettings.checkNpeInNestedNotPrivateMethods
44+
import org.utbot.framework.plugin.api.ClassId
3645
import org.utbot.framework.plugin.api.FieldId
3746
import org.utbot.framework.plugin.api.id
47+
import org.utbot.framework.plugin.api.util.isArray
48+
import org.utbot.framework.plugin.api.util.isPrimitive
3849
import soot.ArrayType
3950
import soot.PrimType
4051
import soot.RefLikeType
@@ -60,14 +71,6 @@ import soot.jimple.internal.JStaticInvokeExpr
6071
import soot.jimple.internal.JVirtualInvokeExpr
6172
import soot.jimple.internal.JimpleLocal
6273
import soot.tagkit.ArtificialEntityTag
63-
import java.lang.reflect.ParameterizedType
64-
import java.util.ArrayDeque
65-
import java.util.Deque
66-
import java.util.LinkedList
67-
import java.util.Queue
68-
import java.util.concurrent.ConcurrentHashMap
69-
import kotlinx.collections.immutable.PersistentMap
70-
import kotlinx.collections.immutable.persistentHashMapOf
7174

7275
val JIdentityStmt.lines: String
7376
get() = tags.joinToString { "$it" }
@@ -424,3 +427,10 @@ val SootMethod.isUtMockAssumeOrExecuteConcretely
424427
*/
425428
val SootMethod.isPreconditionCheckMethod
426429
get() = declaringClass.isOverridden && name == "preconditionCheck"
430+
431+
val ClassId.sootType: Type
432+
get() = when {
433+
isPrimitive -> Scene.v().getType(name)
434+
isArray -> elementClassId!!.sootType.arrayType
435+
else -> Scene.v().getSootClass(canonicalName).type
436+
}

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/Mocks.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class UtMockWrapper(
307307
override fun value(resolver: Resolver, wrapper: ObjectValue): UtModel =
308308
TODO("value on mock called: $this")
309309

310-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
310+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
311311
setOf(this.type)
312312

313313
override fun toString() = "UtMock(type=$type, target=$mockInfo)"

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/ObjectWrappers.kt

+21-18
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ private val wrappers = mapOf(
152152
},
153153

154154
// list wrappers
155-
wrap(java.util.List::class) { _, addr -> objectValue(ARRAY_LIST_TYPE, addr, ListWrapper(UT_ARRAY_LIST)) },
156-
wrap(java.util.AbstractList::class) { _, addr -> objectValue(ARRAY_LIST_TYPE, addr, ListWrapper(UT_ARRAY_LIST)) },
157-
wrap(java.util.ArrayList::class) { _, addr -> objectValue(ARRAY_LIST_TYPE, addr, ListWrapper(UT_ARRAY_LIST)) },
155+
wrap(java.util.List::class) { type, addr -> objectValue(type, addr, ListWrapper(UT_ARRAY_LIST)) },
156+
wrap(java.util.AbstractList::class) { type, addr -> objectValue(type, addr, ListWrapper(UT_ARRAY_LIST)) },
157+
wrap(java.util.ArrayList::class) { type, addr -> objectValue(type, addr, ListWrapper(UT_ARRAY_LIST)) },
158158

159159

160160
wrap(CopyOnWriteArrayList::class) { type, addr -> objectValue(type, addr, ListWrapper(UT_ARRAY_LIST)) },
161161

162-
wrap(java.util.LinkedList::class) { _, addr -> objectValue(LINKED_LIST_TYPE, addr, ListWrapper(UT_LINKED_LIST)) },
163-
wrap(java.util.AbstractSequentialList::class) { _, addr -> objectValue(LINKED_LIST_TYPE, addr, ListWrapper(UT_LINKED_LIST)) },
162+
wrap(java.util.LinkedList::class) { type, addr -> objectValue(type, addr, ListWrapper(UT_LINKED_LIST)) },
163+
wrap(java.util.AbstractSequentialList::class) { type, addr -> objectValue(type, addr, ListWrapper(UT_LINKED_LIST)) },
164164

165165
// queue, deque wrappers
166166
wrap(java.util.ArrayDeque::class) { type, addr ->
@@ -180,17 +180,17 @@ private val wrappers = mapOf(
180180
},
181181

182182
// set wrappers
183-
wrap(java.util.Set::class) { _, addr -> objectValue(LINKED_HASH_SET_TYPE, addr, SetWrapper()) },
184-
wrap(java.util.AbstractSet::class) { _, addr -> objectValue(LINKED_HASH_SET_TYPE, addr, SetWrapper()) },
185-
wrap(java.util.HashSet::class) { _, addr -> objectValue(HASH_SET_TYPE, addr, SetWrapper()) },
186-
wrap(java.util.LinkedHashSet::class) { _, addr -> objectValue(LINKED_HASH_SET_TYPE, addr, SetWrapper()) },
183+
wrap(java.util.Set::class) { type, addr -> objectValue(type, addr, SetWrapper()) },
184+
wrap(java.util.AbstractSet::class) { type, addr -> objectValue(type, addr, SetWrapper()) },
185+
wrap(java.util.HashSet::class) { type, addr -> objectValue(type, addr, SetWrapper()) },
186+
wrap(java.util.LinkedHashSet::class) { type, addr -> objectValue(type, addr, SetWrapper()) },
187187

188188
// map wrappers
189-
wrap(java.util.Map::class) { _, addr -> objectValue(LINKED_HASH_MAP_TYPE, addr, MapWrapper()) },
190-
wrap(java.util.AbstractMap::class) { _, addr -> objectValue(LINKED_HASH_MAP_TYPE, addr, MapWrapper()) },
191-
wrap(java.util.LinkedHashMap::class) { _, addr -> objectValue(LINKED_HASH_MAP_TYPE, addr, MapWrapper()) },
192-
wrap(java.util.HashMap::class) { _, addr -> objectValue(HASH_MAP_TYPE, addr, MapWrapper()) },
193-
wrap(java.util.concurrent.ConcurrentHashMap::class) { _, addr -> objectValue(HASH_MAP_TYPE, addr, MapWrapper()) },
189+
wrap(java.util.Map::class) { type, addr -> objectValue(type, addr, MapWrapper()) },
190+
wrap(java.util.AbstractMap::class) { type, addr -> objectValue(type, addr, MapWrapper()) },
191+
wrap(java.util.LinkedHashMap::class) { type, addr -> objectValue(type, addr, MapWrapper()) },
192+
wrap(java.util.HashMap::class) { type, addr -> objectValue(type, addr, MapWrapper()) },
193+
wrap(java.util.concurrent.ConcurrentHashMap::class) { type, addr -> objectValue(type, addr, MapWrapper()) },
194194

195195
// stream wrappers
196196
wrap(java.util.stream.BaseStream::class) { _, addr -> objectValue(STREAM_TYPE, addr, CommonStreamWrapper()) },
@@ -225,9 +225,12 @@ interface WrapperInterface {
225225
fun value(resolver: Resolver, wrapper: ObjectValue): UtModel
226226

227227
/**
228-
* Returns list of types that can be produced by [value] method.
228+
* Returns list of possible concrete types that can be produced by [value] method.
229+
* Used for wrapper initialization.
230+
*
231+
* @param type target type to wrap
229232
*/
230-
fun getPotentialPossibleTypes(type: Type): Set<Type>
233+
fun getPossibleConcreteTypes(type: Type): Set<Type>
231234
}
232235

233236
// TODO: perhaps we have to have wrapper around concrete value here
@@ -259,6 +262,6 @@ data class ThrowableWrapper(val throwable: Throwable) : WrapperInterface {
259262
return UtAssembleModel(addr, classId, modelName, instantiationCall)
260263
}
261264

262-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
263-
setOf(Scene.v().getSootClass(throwable.javaClass.canonicalName).type)
265+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
266+
setOf(throwable.javaClass.id.sootType)
264267
}

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/OptionalWrapper.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class OptionalWrapper(private val utOptionalClass: UtOptionalClass) : BaseOverri
9494
return UtAssembleModel(addr, classId, modelName, instantiationCall)
9595
}
9696

97-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
97+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
9898
setOf(type)
9999

100100
private fun Resolver.instantiationFactoryCallModel(classId: ClassId, wrapper: ObjectValue) : UtExecutableCallModel {

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/SecurityManagerWrapper.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SecurityManagerWrapper : BaseOverriddenWrapper(utSecurityManagerClass.name
3838
return UtAssembleModel(addr, classId, modelName, instantiationCall)
3939
}
4040

41-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
41+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
4242
setOf(type)
4343

4444
companion object {

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/StreamWrappers.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ abstract class StreamWrapper(
7777
UtAssembleModel(addr, utStreamClass.overriddenStreamClassId, modelName, instantiationCall)
7878
}
7979

80-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
81-
setOf(STREAM_TYPE)
80+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
81+
setOf(utStreamClass.overriddenStreamClassId.sootType)
8282

8383
override fun chooseClassIdWithConstructor(classId: ClassId): ClassId = error("No constructor for Stream")
8484

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/Strings.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class StringWrapper : BaseOverriddenWrapper(utStringClass.name) {
188188
return UtAssembleModel(addr, classId, modelName, instantiationCall)
189189
}
190190

191-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
191+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
192192
setOf(STRING_TYPE)
193193
}
194194

@@ -286,7 +286,7 @@ class UtNativeStringWrapper : WrapperInterface {
286286

287287
override fun value(resolver: Resolver, wrapper: ObjectValue): UtModel = UtNullModel(STRING_TYPE.classId)
288288

289-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
289+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
290290
setOf(STRING_TYPE)
291291
}
292292

@@ -344,8 +344,8 @@ sealed class UtAbstractStringBuilderWrapper(className: String) : BaseOverriddenW
344344
return UtAssembleModel(addr, wrapper.type.classId, modelName, instantiationCall)
345345
}
346346

347-
override fun getPotentialPossibleTypes(type: Type): Set<Type> =
348-
setOf(STRING_TYPE)
347+
override fun getPossibleConcreteTypes(type: Type): Set<Type> =
348+
setOf(type)
349349

350350
private val SootClass.valueField: SootField
351351
get() = getField("value", CharType.v().arrayType)

Diff for: ‎utbot-framework/src/main/kotlin/org/utbot/engine/SymbolicValue.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fun SymbolicValue.toConcrete(): Any = when (this) {
180180
// TODO: one more constructor?
181181
fun objectValue(type: RefType, addr: UtAddrExpression, implementation: WrapperInterface) =
182182
ObjectValue(
183-
TypeStorage(type, implementation.getPotentialPossibleTypes(type)),
183+
TypeStorage(type, implementation.getPossibleConcreteTypes(type)),
184184
addr, Concrete(implementation)
185185
)
186186

Diff for: ‎utbot-sample/src/main/java/org/utbot/examples/casts/InstanceOfExample.java

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.utbot.examples.casts;
22

3+
import java.util.*;
34
import org.utbot.api.mock.UtMock;
4-
import java.util.HashMap;
5-
import java.util.Map;
65

76
public class InstanceOfExample {
87
public CastClassFirstSucc simpleInstanceOf(CastClass objectExample) {
@@ -323,9 +322,58 @@ public Object[] instanceOfObjectArray(Object[] array) {
323322
return array;
324323
}
325324

326-
public void instanceOfString(CharSequence cs) {
325+
public CharSequence charSequenceInstanceOf(CharSequence cs) {
327326
if (cs instanceof String) {
328-
throw new IllegalStateException("CharSequence must not be a String");
327+
return cs;
329328
}
329+
return cs;
330330
}
331+
332+
public List<Object> listInstanceOf(List<Object> list) {
333+
if (list instanceof AbstractList) {
334+
if (list instanceof ArrayList) {
335+
return list;
336+
}
337+
}
338+
return list;
339+
}
340+
341+
public Deque<Object> dequeInstanceOf(Deque<Object> deque) {
342+
if (deque instanceof LinkedList) {
343+
return deque;
344+
}
345+
return deque;
346+
}
347+
348+
public AbstractSequentialList<Object> abstractSequentialListInstanceOf(AbstractSequentialList<Object> list) {
349+
if (list instanceof LinkedList) {
350+
return list;
351+
}
352+
return list;
353+
}
354+
355+
public Set<Object> setInstanceOf(Set<Object> set) {
356+
if (set instanceof AbstractSet) {
357+
if (set instanceof HashSet) {
358+
if (set instanceof LinkedHashSet) {
359+
return set;
360+
}
361+
// TODO: we have wrapper of HashSet and this branch must be covered, but now it's not truth
362+
}
363+
}
364+
return set;
365+
}
366+
367+
public Map<Object, Object> mapInstanceOf(Map<Object, Object> map) {
368+
if (map instanceof AbstractMap) {
369+
if (map instanceof HashMap) {
370+
if (map instanceof LinkedHashMap) {
371+
return map;
372+
}
373+
// TODO: we have wrapper of HashMap and this branch must be covered, but now it's not truth
374+
}
375+
}
376+
return map;
377+
}
378+
331379
}

0 commit comments

Comments
 (0)
Please sign in to comment.