1
+ package org.utbot.examples
2
+
3
+ import org.utbot.examples.samples.ExampleClass
4
+ import org.utbot.examples.statics.substitution.StaticSubstitution
5
+ import org.utbot.examples.statics.substitution.StaticSubstitutionExamples
6
+ import org.utbot.framework.plugin.api.util.fieldId
7
+ import org.utbot.framework.plugin.api.util.signature
8
+ import org.utbot.instrumentation.ConcreteExecutor
9
+ import org.utbot.instrumentation.execute
10
+ import org.utbot.instrumentation.instrumentation.coverage.CoverageInstrumentation
11
+ import org.utbot.instrumentation.instrumentation.coverage.collectCoverage
12
+ import org.utbot.instrumentation.util.ChildProcessError
13
+ import org.utbot.instrumentation.util.StaticEnvironment
14
+ import org.junit.jupiter.api.Assertions.assertEquals
15
+ import org.junit.jupiter.api.Assertions.assertInstanceOf
16
+ import org.junit.jupiter.api.Assertions.assertTrue
17
+ import org.junit.jupiter.api.Test
18
+ import org.junit.jupiter.api.assertThrows
19
+
20
+ class TestCoverageInstrumentation {
21
+ lateinit var utContext: AutoCloseable
22
+
23
+ @Test
24
+ fun testCatchTargetException () {
25
+ ConcreteExecutor (
26
+ CoverageInstrumentation ,
27
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
28
+ ).use {
29
+ val testObject = ExampleClass ()
30
+
31
+ val res = it.execute(ExampleClass ::kek2, arrayOf(testObject, 123 ))
32
+ val coverageInfo = it.collectCoverage(ExampleClass ::class .java)
33
+
34
+ assertEquals(5 , coverageInfo.visitedInstrs.size)
35
+ assertEquals(50 .. 55 , coverageInfo.methodToInstrRange[ExampleClass ::kek2.signature])
36
+ assertTrue(res.exceptionOrNull() is ArrayIndexOutOfBoundsException )
37
+ }
38
+ }
39
+
40
+ @Test
41
+ fun testIfBranches () {
42
+ ConcreteExecutor (
43
+ CoverageInstrumentation ,
44
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
45
+ ).use {
46
+ val testObject = ExampleClass ()
47
+
48
+ it.execute(ExampleClass ::bar, arrayOf(testObject, 2 ))
49
+ val coverageInfo1 = it.collectCoverage(ExampleClass ::class .java)
50
+
51
+ assertEquals(21 , coverageInfo1.visitedInstrs.size)
52
+ assertEquals(13 .. 49 , coverageInfo1.methodToInstrRange[ExampleClass ::bar.signature])
53
+
54
+ it.execute(ExampleClass ::bar, arrayOf(testObject, 0 ))
55
+ val coverageInfo2 = it.collectCoverage(ExampleClass ::class .java)
56
+
57
+ assertEquals(20 , coverageInfo2.visitedInstrs.size)
58
+ assertEquals(13 .. 49 , coverageInfo2.methodToInstrRange[ExampleClass ::bar.signature])
59
+ }
60
+ }
61
+
62
+ @Test
63
+ fun testWrongArgumentsException () {
64
+ ConcreteExecutor (
65
+ CoverageInstrumentation ,
66
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
67
+ ).use {
68
+ val testObject = ExampleClass ()
69
+ val exc = assertThrows<ChildProcessError > {
70
+ it.execute(
71
+ ExampleClass ::bar,
72
+ arrayOf(testObject, 1 , 2 , 3 )
73
+ )
74
+ }
75
+
76
+ assertInstanceOf(
77
+ IllegalArgumentException ::class .java,
78
+ exc.cause!!
79
+ )
80
+ }
81
+ }
82
+
83
+
84
+ @Test
85
+ fun testMultipleRunsInsideCoverage () {
86
+ ConcreteExecutor (
87
+ CoverageInstrumentation ,
88
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
89
+ ).use {
90
+ val testObject = ExampleClass ()
91
+ val exc = assertThrows<ChildProcessError > {
92
+ it.execute(
93
+ ExampleClass ::bar,
94
+ arrayOf(testObject, 1 , 2 , 3 )
95
+ )
96
+ }
97
+
98
+ assertInstanceOf(
99
+ IllegalArgumentException ::class .java,
100
+ exc.cause!!
101
+ )
102
+
103
+ it.execute(ExampleClass ::bar, arrayOf(testObject, 2 ))
104
+ val coverageInfo1 = it.collectCoverage(ExampleClass ::class .java)
105
+
106
+ assertEquals(21 , coverageInfo1.visitedInstrs.size)
107
+ assertEquals(13 .. 49 , coverageInfo1.methodToInstrRange[ExampleClass ::bar.signature])
108
+
109
+ it.execute(ExampleClass ::bar, arrayOf(testObject, 0 ))
110
+ val coverageInfo2 = it.collectCoverage(ExampleClass ::class .java)
111
+
112
+ assertEquals(20 , coverageInfo2.visitedInstrs.size)
113
+ assertEquals(13 .. 49 , coverageInfo2.methodToInstrRange[ExampleClass ::bar.signature])
114
+ }
115
+ }
116
+
117
+
118
+ @Test
119
+ fun testSameResult () {
120
+ ConcreteExecutor (
121
+ CoverageInstrumentation ,
122
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
123
+ ).use {
124
+ val testObject = ExampleClass ()
125
+
126
+ it.execute(ExampleClass ::dependsOnField, arrayOf(testObject))
127
+ val coverageInfo1 = it.collectCoverage(ExampleClass ::class .java)
128
+
129
+ assertEquals(19 , coverageInfo1.visitedInstrs.size)
130
+ assertEquals(99 .. 124 , coverageInfo1.methodToInstrRange[ExampleClass ::dependsOnField.signature])
131
+
132
+ it.execute(ExampleClass ::dependsOnField, arrayOf(testObject))
133
+ val coverageInfo2 = it.collectCoverage(ExampleClass ::class .java)
134
+
135
+ assertEquals(19 , coverageInfo2.visitedInstrs.size)
136
+ assertEquals(99 .. 124 , coverageInfo2.methodToInstrRange[ExampleClass ::dependsOnField.signature])
137
+ }
138
+ }
139
+
140
+ @Test
141
+ fun testResult () {
142
+ ConcreteExecutor (
143
+ CoverageInstrumentation ,
144
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
145
+ ).use {
146
+ val testObject = ExampleClass ()
147
+
148
+ val res = it.execute(ExampleClass ::foo, arrayOf(testObject, 3 ))
149
+ val coverageInfo = it.collectCoverage(ExampleClass ::class .java)
150
+
151
+ assertEquals(1 , res.getOrNull())
152
+ assertEquals(35 , coverageInfo.visitedInstrs.size)
153
+ assertEquals(56 .. 98 , coverageInfo.methodToInstrRange[ExampleClass ::foo.signature])
154
+ }
155
+ }
156
+
157
+ @Test
158
+ fun testEmptyMethod () {
159
+ ConcreteExecutor (
160
+ CoverageInstrumentation ,
161
+ ExampleClass ::class .java.protectionDomain.codeSource.location.path
162
+ ).use {
163
+ val testObject = ExampleClass ()
164
+
165
+ val res = it.execute(ExampleClass ::emptyMethod, arrayOf(testObject))
166
+ val coverageInfo = it.collectCoverage(ExampleClass ::class .java)
167
+
168
+ assertEquals(Unit ::class , res.getOrNull()!! ::class )
169
+ assertEquals(1 , coverageInfo.visitedInstrs.size)
170
+ }
171
+ }
172
+
173
+ @Test
174
+ fun testTernaryOperator () {
175
+ ConcreteExecutor (
176
+ CoverageInstrumentation ,
177
+ StaticSubstitutionExamples ::class .java.protectionDomain.codeSource.location.path
178
+ ).use {
179
+ val testObject = StaticSubstitutionExamples ()
180
+
181
+ val emptyStaticEnvironment = StaticEnvironment ()
182
+
183
+ val res1 = it.execute(StaticSubstitutionExamples ::lessThanZero, arrayOf(testObject), parameters = emptyStaticEnvironment)
184
+
185
+ val staticEnvironment = StaticEnvironment (
186
+ StaticSubstitution ::mutableValue.fieldId to - 1
187
+ )
188
+ val res2 = it.execute(StaticSubstitutionExamples ::lessThanZero, arrayOf(testObject), parameters = staticEnvironment)
189
+ val coverageInfo = it.collectCoverage(StaticSubstitutionExamples ::class .java)
190
+
191
+ assertEquals(res1.getOrNull(), 5 )
192
+ assertEquals(res2.getOrNull(), 0 )
193
+ assertEquals(coverageInfo.visitedInstrs, (3 .. 10 ).toList())
194
+ }
195
+ }
196
+ }
0 commit comments