Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for branch coverage instrumentation and branch trace instru…
…mentation
  • Loading branch information
egiptipavel committed Aug 11, 2023
commit fc6e5ad551ff7dbd3694665961c93268a327fc4a
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.utbot.examples

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.utbot.examples.samples.ExampleClass
import org.utbot.examples.samples.et.ClassSimpleCatch
import org.utbot.examples.statics.substitution.StaticSubstitution
import org.utbot.examples.statics.substitution.StaticSubstitutionExamples
import org.utbot.framework.plugin.api.util.fieldId
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.execute
import org.utbot.instrumentation.instrumentation.coverage.BranchCoverageInstrumentation
import org.utbot.instrumentation.instrumentation.coverage.collectCoverage
import org.utbot.instrumentation.util.StaticEnvironment
import org.utbot.instrumentation.withInstrumentation

class TestBranchCoverageInstrumentation {
lateinit var utContext: AutoCloseable

@Test
fun testIfBranches() {
withInstrumentation(
BranchCoverageInstrumentation.Factory,
ExampleClass::class.java.protectionDomain.codeSource.location.path
) { executor ->
val testObject = ExampleClass()

executor.execute(ExampleClass::bar, arrayOf(testObject, 2))
val coverageInfo1 = executor.collectCoverage(ExampleClass::class.java)

assertEquals(2, coverageInfo1.visitedInstrs.size)
assertEquals(1..3, coverageInfo1.methodToInstrRange[ExampleClass::bar.signature])

executor.execute(ExampleClass::bar, arrayOf(testObject, 0))
val coverageInfo2 = executor.collectCoverage(ExampleClass::class.java)

assertEquals(2, coverageInfo2.visitedInstrs.size)
assertEquals(1..3, coverageInfo2.methodToInstrRange[ExampleClass::bar.signature])
}
}

@Test
fun testTryCatch() {
withInstrumentation(
BranchCoverageInstrumentation.Factory,
ClassSimpleCatch::class.java.protectionDomain.codeSource.location.path
) { executor ->
executor.execute(ClassSimpleCatch::A_catches, emptyArray())
val coverageInfo1 = executor.collectCoverage(ClassSimpleCatch::class.java)

assertEquals(2, coverageInfo1.visitedInstrs.size)
assertEquals(3..5, coverageInfo1.methodToInstrRange[ClassSimpleCatch::A_catches.signature])

executor.execute(ClassSimpleCatch::A_catchesWrongException, emptyArray())
val coverageInfo2 = executor.collectCoverage(ClassSimpleCatch::class.java)

assertEquals(0, coverageInfo2.visitedInstrs.size)
assertEquals(7..9, coverageInfo2.methodToInstrRange[ClassSimpleCatch::A_catchesWrongException.signature])
}
}

@Test
fun testTernaryOperator() {
withInstrumentation(
BranchCoverageInstrumentation.Factory,
StaticSubstitutionExamples::class.java.protectionDomain.codeSource.location.path
) { executor ->
val testObject = StaticSubstitutionExamples()

val emptyStaticEnvironment = StaticEnvironment()

val res1 = executor.execute(
StaticSubstitutionExamples::lessThanZero,
arrayOf(testObject),
parameters = emptyStaticEnvironment
)

val staticEnvironment = StaticEnvironment(
StaticSubstitution::mutableValue.fieldId to -1
)
val res2 = executor.execute(
StaticSubstitutionExamples::lessThanZero,
arrayOf(testObject),
parameters = staticEnvironment
)
val coverageInfo = executor.collectCoverage(StaticSubstitutionExamples::class.java)

assertEquals(res1.getOrNull(), 5)
assertEquals(res2.getOrNull(), 0)
assertEquals(coverageInfo.visitedInstrs, (1..3).toList())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package org.utbot.examples.et

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.utbot.examples.samples.et.ClassSimple
import org.utbot.examples.samples.et.ClassSimpleCatch
import org.utbot.instrumentation.instrumentation.et.*
import org.utbot.instrumentation.util.Isolated
import org.utbot.instrumentation.withInstrumentation

class TestExecutionBranchTraceInstrumentation {
lateinit var utContext: AutoCloseable

val CLASSPATH = ClassSimple::class.java.protectionDomain.codeSource.location.path

@Test
fun testClassSimple() {
withInstrumentation(
ExecutionBranchTraceInstrumentation.Factory,
CLASSPATH
) { executor ->
val doesNotThrow = Isolated(ClassSimple::doesNotThrow, executor)
val alwaysThrows = Isolated(ClassSimple::alwaysThrows, executor)
val maybeThrows = Isolated(ClassSimple::maybeThrows, executor)

val et1 = doesNotThrow()
Assertions.assertEquals(
function(doesNotThrow.signature) {
pass()
ret()
},
convert(et1)
)

val et2 = alwaysThrows()
Assertions.assertEquals(
function(alwaysThrows.signature) {
pass()
explThr()
},
convert(et2)
)

val et3 = maybeThrows(-1)
Assertions.assertEquals(
function(maybeThrows.signature) {
pass()
explThr()
},
convert(et3)
)

val et4 = maybeThrows(0)
Assertions.assertEquals(
function(maybeThrows.signature) {
pass()
ret()
},
convert(et4)
)
}
}

@Test
fun testClasSimpleCatch() {
withInstrumentation(
ExecutionBranchTraceInstrumentation.Factory,
CLASSPATH
) { executor ->
val A = Isolated(ClassSimpleCatch::A, executor)
val A_catches = Isolated(ClassSimpleCatch::A_catches, executor)
val A_catchesWrongException = Isolated(ClassSimpleCatch::A_catchesWrongException, executor)
val A_doesNotCatch = Isolated(ClassSimpleCatch::A_doesNotCatch, executor)

val B = Isolated(ClassSimpleCatch::B, executor)
val B_throws = Isolated(ClassSimpleCatch::B_throws, executor)

val et1 = A()
Assertions.assertEquals(
function(A.signature) {
pass()
invoke(B.signature) {
pass()
ret()
}
ret()
},
convert(et1)
)

val et2 = A_catches()
Assertions.assertEquals(
function(A_catches.signature) {
pass()
invoke(B_throws.signature) {
implThr()
}
pass()
ret()
},
convert(et2)
)

val et3 = A_catchesWrongException()
Assertions.assertEquals(
function(A_catchesWrongException.signature) {
pass()
invoke(B_throws.signature) {
implThr()
}
},
convert(et3)
)

val et4 = A_doesNotCatch()
Assertions.assertEquals(
function(A_doesNotCatch.signature) {
pass()
invoke(B_throws.signature) {
implThr()
}
},
convert(et4)
)
}
}
}