Skip to content

Commit 8e8c0a4

Browse files
committed
Refactoring
1 parent e86fa2a commit 8e8c0a4

File tree

4 files changed

+66
-90
lines changed

4 files changed

+66
-90
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ sealed class UtReferenceModel(
282282
) : UtModel(classId)
283283

284284
/**
285-
* Checks if [UtModel] is a null.
285+
* Checks if [UtModel] is a [UtNullModel].
286286
*/
287287
fun UtModel.isNull() = this is UtNullModel
288288

289289
/**
290-
* Checks if [UtModel] is not a null.
290+
* Checks if [UtModel] is not a [UtNullModel].
291291
*/
292292
fun UtModel.isNotNull() = !isNull()
293293

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
167167

168168
private lateinit var methodType: CgTestMethodType
169169

170-
private val cachedFieldsFromAllExecutions = mutableMapOf<Pair<FieldId, Int>, MutableList<UtModel>>()
170+
private val fieldsOfExecutionResults = mutableMapOf<Pair<FieldId, Int>, MutableList<UtModel>>()
171171

172172
private fun setupInstrumentation() {
173173
if (currentExecution is UtSymbolicExecution) {
@@ -831,27 +831,24 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
831831
depth: Int,
832832
visitedModels: MutableSet<UtModel>
833833
) {
834+
// if field is static, it is represents itself in "before" and
835+
// "after" state: no need to assert its equality to itself.
836+
if (fieldId.isStatic) {
837+
return
838+
}
839+
840+
// if model is already processed, so we don't want to add new statements
841+
if (fieldModel in visitedModels) {
842+
return
843+
}
844+
834845
when (parametrizedTestSource) {
835846
ParametrizedTestSource.DO_NOT_PARAMETRIZE -> {
836-
traverseField(
837-
fieldId,
838-
fieldModel,
839-
expected,
840-
actual,
841-
depth,
842-
visitedModels
843-
)
847+
traverseField(fieldId, fieldModel, expected, actual, depth, visitedModels)
844848
}
845849

846850
ParametrizedTestSource.PARAMETRIZE -> {
847-
traverseFieldForParametrizedTest(
848-
fieldId,
849-
fieldModel,
850-
expected,
851-
actual,
852-
depth,
853-
visitedModels
854-
)
851+
traverseFieldForParametrizedTest(fieldId, fieldModel, expected, actual, depth, visitedModels)
855852
}
856853
}
857854
}
@@ -864,17 +861,6 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
864861
depth: Int,
865862
visitedModels: MutableSet<UtModel>
866863
) {
867-
// if field is static, it is represents itself in "before" and
868-
// "after" state: no need to assert its equality to itself.
869-
if (fieldId.isStatic) {
870-
return
871-
}
872-
873-
// if model is already processed, so we don't want to add new statements
874-
if (fieldModel in visitedModels) {
875-
return
876-
}
877-
878864
// fieldModel is not visited and will be marked in assertDeepEquals call
879865
val fieldName = fieldId.name
880866
var expectedVariable: CgVariable? = null
@@ -907,31 +893,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
907893
depth: Int,
908894
visitedModels: MutableSet<UtModel>
909895
) {
910-
// if field is static, it is represents itself in "before" and
911-
// "after" state: no need to assert its equality to itself.
912-
if (fieldId.isStatic) {
913-
return
914-
}
896+
val fieldResultModels = fieldsOfExecutionResults[fieldId to depth]
897+
val nullResultModelInExecutions = fieldResultModels?.find { it.isNull() }
898+
val notNullResultModelInExecutions = fieldResultModels?.find { it.isNotNull() }
915899

916-
val hasNullModelFieldInOtherExecutions = cachedFieldsFromAllExecutions[fieldId to depth]?.any { it.isNull() } ?: false
917-
val notNullModelFieldFromOtherExecution = cachedFieldsFromAllExecutions[fieldId to depth]?.find { it.isNotNull() }
900+
val hasNullResultModel = nullResultModelInExecutions != null
901+
val hasNotNullResultModel = notNullResultModelInExecutions != null
918902

919-
val needToSubstituteFieldModel = notNullModelFieldFromOtherExecution != null && fieldModel is UtNullModel
903+
val needToSubstituteFieldModel = fieldModel is UtNullModel && hasNotNullResultModel
920904

921-
val fieldModel = if (needToSubstituteFieldModel) notNullModelFieldFromOtherExecution!! else fieldModel
922-
923-
val needIfStatement = needToSubstituteFieldModel || hasNullModelFieldInOtherExecutions
924-
925-
// if model is already processed, so we don't want to add new statements
926-
if (fieldModel in visitedModels) {
927-
return
928-
}
905+
val fieldModelForAssert = if (needToSubstituteFieldModel) notNullResultModelInExecutions!! else fieldModel
929906

930907
// fieldModel is not visited and will be marked in assertDeepEquals call
931908
val fieldName = fieldId.name
932909
var expectedVariable: CgVariable? = null
933910

934-
if (needExpectedDeclaration(fieldModel)) {
911+
val needExpectedDeclaration = needExpectedDeclaration(fieldModelForAssert)
912+
if (needExpectedDeclaration) {
935913
val expectedFieldDeclaration = createDeclarationForFieldFromVariable(fieldId, expected, fieldName)
936914

937915
currentBlock += expectedFieldDeclaration
@@ -941,13 +919,13 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
941919
val actualFieldDeclaration = createDeclarationForFieldFromVariable(fieldId, actual, fieldName)
942920
currentBlock += actualFieldDeclaration
943921

944-
if (needExpectedDeclaration(fieldModel) && needIfStatement) {
922+
if (needExpectedDeclaration && hasNullResultModel) {
945923
ifStatement(
946924
CgEqualTo(expectedVariable!!, nullLiteral()),
947925
trueBranch = { +testFrameworkManager.assertions[testFramework.assertNull](actualFieldDeclaration.variable).toStatement() },
948926
falseBranch = {
949927
assertDeepEquals(
950-
fieldModel,
928+
fieldModelForAssert,
951929
expectedVariable,
952930
actualFieldDeclaration.variable,
953931
depth + 1,
@@ -957,7 +935,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
957935
)
958936
} else {
959937
assertDeepEquals(
960-
fieldModel,
938+
fieldModelForAssert,
961939
expectedVariable,
962940
actualFieldDeclaration.variable,
963941
depth + 1,
@@ -967,7 +945,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
967945
emptyLineIfNeeded()
968946
}
969947

970-
private fun traverseExecutionsFieldsForParametrizedTest() {
948+
private fun collectExecutionsResultFields() {
971949
val successfulExecutionsModels = allExecutions
972950
.filter {
973951
it.result is UtExecutionSuccess
@@ -979,22 +957,31 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
979957
when (model) {
980958
is UtCompositeModel -> {
981959
for ((fieldId, fieldModel) in model.fields) {
982-
traverseExecutionsFieldsForParametrizedTestRecursively(fieldId, fieldModel, 0)
960+
collectExecutionsResultFieldsRecursively(fieldId, fieldModel, 0)
983961
}
984962
}
985963

986964
is UtAssembleModel -> {
987-
for ((fieldId, fieldModel) in model.origin!!.fields) {
988-
traverseExecutionsFieldsForParametrizedTestRecursively(fieldId, fieldModel, 0)
965+
model.origin?.let {
966+
for ((fieldId, fieldModel) in it.fields) {
967+
collectExecutionsResultFieldsRecursively(fieldId, fieldModel, 0)
968+
}
989969
}
990970
}
991971

992-
else -> {} // TODO: check this specific case
972+
is UtNullModel,
973+
is UtPrimitiveModel,
974+
is UtArrayModel,
975+
is UtClassRefModel,
976+
is UtEnumConstantModel,
977+
is UtVoidModel -> {
978+
// only [UtCompositeModel] and [UtAssembleModel] have fields to traverse
979+
}
993980
}
994981
}
995982
}
996983

997-
private fun traverseExecutionsFieldsForParametrizedTestRecursively(
984+
private fun collectExecutionsResultFieldsRecursively(
998985
fieldId: FieldId,
999986
fieldModel: UtModel,
1000987
depth: Int,
@@ -1003,43 +990,32 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
1003990
return
1004991
}
1005992

993+
val fieldKey = fieldId to depth
994+
fieldsOfExecutionResults.getOrPut(fieldKey) { mutableListOf() } += fieldModel
995+
1006996
when (fieldModel) {
1007997
is UtCompositeModel -> {
1008998
for ((id, model) in fieldModel.fields) {
1009-
if (id.isInnerClassEnclosingClassReference) continue
1010-
1011-
if (id.isStatic) {
1012-
return
1013-
}
1014-
1015-
if (cachedFieldsFromAllExecutions[id to depth] != null) {
1016-
cachedFieldsFromAllExecutions[id to depth]!!.add(model)
1017-
} else {
1018-
cachedFieldsFromAllExecutions[id to depth] = listOf(model).toMutableList()
1019-
}
1020-
1021-
traverseExecutionsFieldsForParametrizedTestRecursively(
1022-
id,
1023-
model,
1024-
depth + 1,
1025-
)
999+
collectExecutionsResultFieldsRecursively(id, model, depth + 1)
10261000
}
10271001
}
10281002

10291003
is UtAssembleModel -> {
10301004
fieldModel.origin?.let {
1031-
traverseExecutionsFieldsForParametrizedTestRecursively(fieldId, it, depth)
1005+
for ((id, model) in it.fields) {
1006+
collectExecutionsResultFieldsRecursively(id, model, depth + 1)
1007+
}
10321008
}
1033-
return
10341009
}
10351010

1036-
else -> {} // TODO: check this specific case
1037-
}
1038-
1039-
if (cachedFieldsFromAllExecutions[fieldId to depth] != null) {
1040-
cachedFieldsFromAllExecutions[fieldId to depth]!!.add(fieldModel)
1041-
} else {
1042-
cachedFieldsFromAllExecutions[fieldId to depth] = listOf(fieldModel).toMutableList()
1011+
is UtNullModel,
1012+
is UtPrimitiveModel,
1013+
is UtArrayModel,
1014+
is UtClassRefModel,
1015+
is UtEnumConstantModel,
1016+
is UtVoidModel -> {
1017+
// only [UtCompositeModel] and [UtAssembleModel] have fields to traverse
1018+
}
10431019
}
10441020
}
10451021

@@ -1234,7 +1210,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12341210
when (parametrizedTestSource) {
12351211
ParametrizedTestSource.DO_NOT_PARAMETRIZE -> generateDeepEqualsAssertion(expected, actual)
12361212
ParametrizedTestSource.PARAMETRIZE -> {
1237-
traverseExecutionsFieldsForParametrizedTest()
1213+
collectExecutionsResultFields()
12381214

12391215
when {
12401216
actual.type.isPrimitive -> generateDeepEqualsAssertion(expected, actual)

utbot-framework/src/test/kotlin/org/utbot/examples/codegen/deepequals/ClassWithNullableFieldTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ClassWithNullableFieldTest : UtValueTestCaseChecker(
2727
@Test
2828
fun testClassWithNullableField1() {
2929
check(
30-
ClassWithNullableField::returnCompoundWithNullableField1,
30+
ClassWithNullableField::returnGreatCompoundWithNullableField,
3131
eq(3),
3232
coverage = DoNotCalculate
3333
)

utbot-sample/src/main/java/org/utbot/examples/codegen/deepequals/ClassWithNullableField.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class Compound {
1212
}
1313
}
1414

15-
class BiggerCompound {
15+
class GreatCompound {
1616
Compound compound;
1717

18-
BiggerCompound(Compound compound) {
18+
GreatCompound(Compound compound) {
1919
this.compound = compound;
2020
}
2121
}
@@ -26,9 +26,9 @@ public Compound returnCompoundWithNullableField(int value) {
2626
else return new Compound(new Component());
2727
}
2828

29-
public BiggerCompound returnCompoundWithNullableField1(int value) {
30-
if (value > 0) return new BiggerCompound(null);
31-
else if (value == 0) return new BiggerCompound(new Compound(new Component()));
32-
else return new BiggerCompound(new Compound(null));
29+
public GreatCompound returnGreatCompoundWithNullableField(int value) {
30+
if (value > 0) return new GreatCompound(null);
31+
else if (value == 0) return new GreatCompound(new Compound(new Component()));
32+
else return new GreatCompound(new Compound(null));
3333
}
3434
}

0 commit comments

Comments
 (0)