-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTestConstructors.kt
More file actions
141 lines (123 loc) · 5.71 KB
/
TestConstructors.kt
File metadata and controls
141 lines (123 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.utbot.examples
import org.utbot.examples.samples.ClassWithMultipleConstructors
import org.utbot.examples.samples.ClassWithSameMethodNames
import org.utbot.framework.plugin.api.util.signature
import org.utbot.instrumentation.ConcreteExecutor
import org.utbot.instrumentation.execute
import org.utbot.instrumentation.instrumentation.InvokeInstrumentation
import org.utbot.instrumentation.instrumentation.coverage.InstructionCoverageInstrumentation
import org.utbot.instrumentation.instrumentation.coverage.collectCoverage
import org.utbot.instrumentation.instrumentation.et.ExecutionTraceInstrumentation
import org.utbot.instrumentation.instrumentation.et.convert
import org.utbot.instrumentation.instrumentation.et.function
import org.utbot.instrumentation.instrumentation.et.invoke
import org.utbot.instrumentation.instrumentation.et.pass
import org.utbot.instrumentation.instrumentation.et.ret
import org.utbot.instrumentation.withInstrumentation
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
class TestConstructors {
lateinit var utContext: AutoCloseable
private val CLASSPATH = ClassWithSameMethodNames::class.java.protectionDomain.codeSource.location.path
@Test
fun testDefaultConstructor() {
ConcreteExecutor(
InvokeInstrumentation.Factory,
CLASSPATH
).use { executor ->
val constructors = ClassWithMultipleConstructors::class.constructors
val constr = constructors.first { it.parameters.isEmpty() }
val res = executor.execute(constr, arrayOf())
val checkClass = ClassWithMultipleConstructors()
assertEquals(checkClass, res.getOrNull())
assertFalse(checkClass === res.getOrNull())
}
}
@Test
fun testIntConstructors() {
ConcreteExecutor(
InvokeInstrumentation.Factory,
CLASSPATH
).use { executor ->
val constructors = ClassWithMultipleConstructors::class.constructors
val constrI = constructors.first { it.signature == "<init>(I)V" }
val resI = executor.execute(constrI, arrayOf(1))
assertEquals(ClassWithMultipleConstructors(1), resI.getOrNull())
val constrII = constructors.first { it.signature == "<init>(II)V" }
val resII = executor.execute(constrII, arrayOf(1, 2))
assertEquals(ClassWithMultipleConstructors(3), resII.getOrNull())
val constrIII = constructors.first { it.signature == "<init>(III)V" }
val resIII = executor.execute(constrIII, arrayOf(1, 2, 3))
assertEquals(ClassWithMultipleConstructors(6), resIII.getOrNull())
}
}
@Test
fun testStringConstructors() {
withInstrumentation(
InvokeInstrumentation.Factory,
CLASSPATH
) { executor ->
val constructors = ClassWithMultipleConstructors::class.constructors
val constrSS = constructors.first { it.parameters.size == 2 && it.signature != "<init>(II)V" }
val resSS = executor.execute(constrSS, arrayOf("100", "23"))
assertEquals(ClassWithMultipleConstructors(123), resSS.getOrNull())
val constrS = constructors.first { it.parameters.size == 1 && it.signature != "<init>(I)V" }
val resS1 = executor.execute(constrS, arrayOf("one"))
assertEquals(ClassWithMultipleConstructors(1), resS1.getOrNull())
val resS2 = executor.execute(constrS, arrayOf("kek"))
assertEquals(ClassWithMultipleConstructors(-1), resS2.getOrNull())
}
}
@Test
fun testCoverageConstructor() {
withInstrumentation(
InstructionCoverageInstrumentation.Factory,
CLASSPATH
) { executor ->
val constructors = ClassWithMultipleConstructors::class.constructors
val constrIII = constructors.first { it.signature == "<init>(III)V" }
executor.execute(constrIII, arrayOf(1, 2, 3))
val coverage = executor.collectCoverage(ClassWithMultipleConstructors::class.java)
val method2instr = coverage.methodToInstrRange
assertTrue(method2instr["<init>()V"]!!.minus(coverage.visitedInstrs).isEmpty())
assertTrue(method2instr["<init>(I)V"]!!.minus(coverage.visitedInstrs).isEmpty())
assertTrue(method2instr["<init>(II)V"]!!.minus(coverage.visitedInstrs).isEmpty())
assertTrue(method2instr["<init>(III)V"]!!.minus(coverage.visitedInstrs).toList() == (36..40).toList())
}
}
@Test
fun testExecutionTraceConstructor() {
withInstrumentation(
ExecutionTraceInstrumentation.Factory,
CLASSPATH
) { executor ->
val constructors = ClassWithMultipleConstructors::class.constructors
val constrIII = constructors.first { it.signature == "<init>(III)V" }
val trace = executor.execute(constrIII, arrayOf(1, 2, 3))
assertEquals(
function("<init>(III)V") {
pass()
invoke("<init>(II)V") {
pass()
invoke("<init>(I)V") {
pass()
invoke("<init>()V") {
pass()
ret()
}
pass()
ret()
}
pass()
ret()
}
pass()
ret()
},
convert(trace)
)
}
}
}