Skip to content

Commit 97d8336

Browse files
committed
Clarify some ObjectValueProvider entities
1 parent 4dc214e commit 97d8336

File tree

3 files changed

+29
-60
lines changed

3 files changed

+29
-60
lines changed

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzer/providers/ObjectModelProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import org.utbot.fuzzer.IdentityPreservingIdGenerator
2424
import org.utbot.fuzzer.objects.FuzzerMockableMethodId
2525
import org.utbot.fuzzer.objects.assembleModel
2626
import org.utbot.fuzzing.providers.FieldDescription
27-
import org.utbot.fuzzing.providers.findSuitableFields
27+
import org.utbot.fuzzing.providers.findAccessibleModifableFields
2828
import org.utbot.fuzzing.providers.isAccessible
2929

3030
/**
@@ -64,7 +64,7 @@ class ObjectModelProvider(
6464
// and mutate some fields. Only if there's no option next block
6565
// with empty constructor should be used.
6666
if (constructorId.parameters.isEmpty()) {
67-
val fields = findSuitableFields(constructorId.classId, description.packageName)
67+
val fields = findAccessibleModifableFields(constructorId.classId, description.packageName)
6868
if (fields.isNotEmpty()) {
6969
yield(
7070
ModelConstructor(fields.map { FuzzedType(it.classId) }) {

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,67 +23,22 @@ class ObjectValueProvider(
2323
NumberValueProvider.classId
2424
)
2525

26-
override fun accept(type: FuzzedType) = !shouldPass(type.classId)
26+
override fun accept(type: FuzzedType) = !isIgnored(type.classId)
2727

2828
override fun generate(
2929
description: FuzzedDescription,
3030
type: FuzzedType
3131
) = sequence {
3232
val classId = type.classId
33-
// this adds mock support, but we should discuss first if mock is really needed
34-
// if (description.description.shouldMock(classId)) {
35-
// yield(createMock(classId, description))
36-
// } else {
37-
// prefer constructors without recursion call, but use them if no other options
38-
val constructors = findTypesOfNonRecursiveConstructor(type, description.description.packageName)
39-
.takeIf { it.isNotEmpty() }
40-
?.asSequence()
41-
?: classId.allConstructors.filter {
42-
isAccessible(it.constructor, description.description.packageName)
43-
}
44-
constructors.forEach { constructorId ->
45-
yield(createValue(classId, constructorId, description))
46-
}
47-
// }
48-
}
49-
50-
@Suppress("unused")
51-
private fun createMock(classId: ClassId, description: FuzzedDescription): Seed.Recursive<FuzzedType, FuzzedValue> {
52-
return Seed.Recursive(
53-
construct = Routine.Create(emptyList()) {
54-
UtCompositeModel(
55-
id = idGenerator.createId(),
56-
classId = classId,
57-
isMock = true,
58-
).fuzzed {
59-
summary = "%var% = mock"
60-
}
61-
},
62-
modify = sequence {
63-
findSuitableFields(classId, description.description.packageName).forEach { fd ->
64-
when {
65-
fd.canBeSetDirectly -> {
66-
yield(Routine.Call(listOf(FuzzedType(fd.classId))) { self, values ->
67-
val model = self.model as UtCompositeModel
68-
model.fields[FieldId(classId, fd.name)] = values.first().model
69-
})
70-
}
71-
72-
fd.setter != null && fd.getter != null -> {
73-
yield(Routine.Call(listOf(FuzzedType(fd.classId))) { self, values ->
74-
val model = self.model as UtCompositeModel
75-
model.mocks[fd.getter.executableId] = values.map { it.model }
76-
})
77-
}
78-
}
79-
}
80-
},
81-
empty = Routine.Empty {
82-
UtNullModel(classId).fuzzed {
83-
summary = "%var% = null"
84-
}
33+
val constructors = findTypesOfNonRecursiveConstructor(type, description.description.packageName)
34+
.takeIf { it.isNotEmpty() }
35+
?.asSequence()
36+
?: classId.allConstructors.filter {
37+
isAccessible(it.constructor, description.description.packageName)
8538
}
86-
)
39+
constructors.forEach { constructorId ->
40+
yield(createValue(classId, constructorId, description))
41+
}
8742
}
8843

8944
private fun createValue(classId: ClassId, constructorId: ConstructorId, description: FuzzedDescription): Seed.Recursive<FuzzedType, FuzzedValue> {
@@ -104,7 +59,7 @@ class ObjectValueProvider(
10459
}
10560
},
10661
modify = sequence {
107-
findSuitableFields(classId, description.description.packageName).forEach { fd ->
62+
findAccessibleModifableFields(classId, description.description.packageName).forEach { fd ->
10863
when {
10964
fd.canBeSetDirectly -> {
11065
yield(Routine.Call(listOf(FuzzedType(fd.classId))) { self, values ->
@@ -137,8 +92,9 @@ class ObjectValueProvider(
13792
)
13893
}
13994

140-
private fun shouldPass(type: ClassId): Boolean {
95+
private fun isIgnored(type: ClassId): Boolean {
14196
return unwantedConstructorsClasses.contains(type)
97+
|| type.isIterableOrMap
14298
|| type.isPrimitiveWrapper
14399
|| type.isEnum
144100
|| type.isAbstract
@@ -193,7 +149,7 @@ internal class FieldDescription(
193149
val getter: Method?
194150
)
195151

196-
internal fun findSuitableFields(classId: ClassId, packageName: String?): List<FieldDescription> {
152+
internal fun findAccessibleModifableFields(classId: ClassId, packageName: String?): List<FieldDescription> {
197153
val jClass = classId.jClass
198154
return jClass.declaredFields.map { field ->
199155
val setterAndGetter = jClass.findPublicSetterGetterIfHasPublicGetter(field, packageName)

utbot-fuzzing/src/test/kotlin/org/utbot/fuzzing/samples/Collections.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,20 @@ public boolean testTreeSetWithComparable(NavigableSet<Integer> set) {
128128
return false;
129129
}
130130

131-
public static class ConcreteList<T> extends LinkedList<T> {}
131+
public static class ConcreteList<T extends Number> extends LinkedList<T> {
132+
public boolean equals(Collection<T> collection) {
133+
if (collection.size() != size()) {
134+
return false;
135+
}
136+
int i = 0;
137+
for (T t : collection) {
138+
if (!java.util.Objects.equals(get(i), t)) {
139+
return false;
140+
}
141+
}
142+
return true;
143+
}
144+
}
132145

133146
/**
134147
* Should create concrete class

0 commit comments

Comments
 (0)