Skip to content

Commit 86b3a51

Browse files
Atos1337Victor Samoilov
andauthored
Fix bug in FeatureProcessor (#224)
* Fix bug in FeatureProcessor * Set testCodeGeneration to false * Add comments Co-authored-by: Victor Samoilov <samoilov.victor@huawei.com>
1 parent 1948fa4 commit 86b3a51

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

utbot-analytics/src/main/kotlin/org/utbot/features/FeatureProcessorWithStatesRepetition.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class FeatureProcessorWithStatesRepetition(
5252
val states = mutableListOf<Pair<Int, Long>>()
5353
var newCoverage = 0
5454

55-
generateSequence(executionState) { currentState ->
55+
generateSequence(executionState) { it.parent }.forEach { currentState ->
5656
val stateHashCode = currentState.hashCode()
5757

5858
if (currentState.features.isEmpty()) {
@@ -68,8 +68,6 @@ class FeatureProcessorWithStatesRepetition(
6868
newCoverage++
6969
}
7070
}
71-
72-
currentState.parent
7371
}
7472

7573
generatedTestCases++
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.utbot.features;
2+
3+
/**
4+
* Class that have one execution path
5+
*/
6+
public class OnePath {
7+
int onePath() {
8+
return 1;
9+
}
10+
}

utbot-analytics/src/test/kotlin/org/utbot/features/FeatureProcessorWithRepetitionTest.kt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
package org.utbot.features
22

3+
import org.junit.jupiter.api.AfterAll
34
import org.junit.jupiter.api.Assertions
5+
import org.junit.jupiter.api.BeforeAll
46
import org.junit.jupiter.api.Test
7+
import org.utbot.analytics.EngineAnalyticsContext
8+
import org.utbot.examples.AbstractTestCaseGeneratorTest
9+
import org.utbot.examples.eq
10+
import org.utbot.examples.withFeaturePath
11+
import java.io.File
12+
import java.io.FileInputStream
513

6-
class FeatureProcessorWithRepetitionTest {
14+
class FeatureProcessorWithRepetitionTest : AbstractTestCaseGeneratorTest(OnePath::class, false) {
715
companion object {
16+
const val featureDir = "src/test/resources/features"
817
fun reward(coverage: Double, time: Double) = RewardEstimator.reward(coverage, time)
18+
19+
@JvmStatic
20+
@BeforeAll
21+
fun beforeAll() {
22+
File(featureDir).mkdir()
23+
EngineAnalyticsContext.featureProcessorFactory = FeatureProcessorWithStatesRepetitionFactory()
24+
EngineAnalyticsContext.featureExtractorFactory = FeatureExtractorFactoryImpl()
25+
}
26+
27+
@JvmStatic
28+
@AfterAll
29+
fun afterAll() {
30+
File(featureDir).deleteRecursively()
31+
}
932
}
1033

1134
@Test
12-
fun testDumpFeatures() {
35+
fun testCalculateRewards() {
1336
val statesToInt = mapOf(
1437
"a0" to 1,
1538
"c0" to 2,
@@ -70,4 +93,19 @@ class FeatureProcessorWithRepetitionTest {
7093
Assertions.assertEquals(it.value, rewards[statesToInt[it.key]])
7194
}
7295
}
96+
97+
/**
98+
* Test, that we correctly add test cases and dump them into file
99+
*/
100+
@Test
101+
fun addTestCaseTest() {
102+
withFeaturePath(featureDir) {
103+
check(
104+
OnePath::onePath,
105+
eq(1)
106+
)
107+
108+
Assertions.assertTrue(FileInputStream("$featureDir/0.csv").bufferedReader().readLines().size > 1)
109+
}
110+
}
73111
}

utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,3 +2811,18 @@ inline fun <reified T> withPathSelectorType(pathSelectorType: PathSelectorType,
28112811
UtSettings.pathSelectorType = prev
28122812
}
28132813
}
2814+
2815+
inline fun <reified T> withFeaturePath(featurePath: String, block: () -> T): T {
2816+
val prevFeaturePath = UtSettings.featurePath
2817+
val prevEnableFeatureProcess = UtSettings.enableFeatureProcess
2818+
2819+
UtSettings.featurePath = featurePath
2820+
UtSettings.enableFeatureProcess = true
2821+
2822+
try {
2823+
return block()
2824+
} finally {
2825+
UtSettings.featurePath = prevFeaturePath
2826+
UtSettings.enableFeatureProcess = prevEnableFeatureProcess
2827+
}
2828+
}

0 commit comments

Comments
 (0)