@@ -4,12 +4,11 @@ import org.utbot.framework.plugin.api.ClassId
4
4
import org.utbot.framework.plugin.api.UtPrimitiveModel
5
5
import org.utbot.framework.plugin.api.util.*
6
6
import org.utbot.fuzzer.FuzzedContext
7
+ import org.utbot.fuzzer.FuzzedContext.Comparison.*
7
8
import org.utbot.fuzzer.FuzzedType
8
9
import org.utbot.fuzzer.FuzzedValue
9
10
import org.utbot.fuzzer.providers.ConstantsModelProvider.fuzzed
10
- import org.utbot.fuzzing.FuzzedDescription
11
- import org.utbot.fuzzing.Seed
12
- import org.utbot.fuzzing.ValueProvider
11
+ import org.utbot.fuzzing.*
13
12
import org.utbot.fuzzing.seeds.*
14
13
import java.util.regex.Pattern
15
14
import java.util.regex.PatternSyntaxException
@@ -24,27 +23,70 @@ abstract class PrimitiveValueProvider(
24
23
25
24
protected suspend fun <T : KnownValue > SequenceScope <Seed <FuzzedType , FuzzedValue >>.yieldKnown (
26
25
value : T ,
27
- description : String = value.toString(),
28
26
toValue : T .() -> Any
29
27
) {
30
28
yield (Seed .Known (value) { known ->
31
29
UtPrimitiveModel (toValue(known)).fuzzed {
32
30
summary = buildString {
33
- append(" %var% = " )
31
+ append(" %var% = ${known.valueToString()} " )
34
32
if (known.mutatedFrom != null ) {
35
- append(" mutated from " )
33
+ append(" ( mutated from ${known.mutatedFrom?.valueToString()} ) " )
36
34
}
37
- append(description)
38
35
}
39
36
}
40
37
})
41
38
}
39
+
40
+ private fun <T : KnownValue > T.valueToString (): String {
41
+ when (this ) {
42
+ is BitVectorValue -> {
43
+ for (defaultBound in Signed .values()) {
44
+ if (defaultBound.test(this )) {
45
+ return defaultBound.name.lowercase()
46
+ }
47
+ }
48
+ return when (size) {
49
+ 8 -> toByte().toString()
50
+ 16 -> toShort().toString()
51
+ 32 -> toInt().toString()
52
+ 64 -> toLong().toString()
53
+ else -> toString(10 )
54
+ }
55
+ }
56
+ is IEEE754Value -> {
57
+ for (defaultBound in DefaultFloatBound .values()) {
58
+ if (defaultBound.test(this )) {
59
+ return defaultBound.name.lowercase().replace(" _" , " " )
60
+ }
61
+ }
62
+ return when {
63
+ is32Float() -> toFloat().toString()
64
+ is64Float() -> toDouble().toString()
65
+ else -> toString()
66
+ }
67
+ }
68
+ is StringValue -> {
69
+ return " '${value.substringToLength(10 , " ..." )} '"
70
+ }
71
+ is RegexValue -> {
72
+ return " '${value.substringToLength(10 , " ..." )} ' from $pattern "
73
+ }
74
+ else -> return toString()
75
+ }
76
+ }
77
+
78
+ private fun String.substringToLength (size : Int , postfix : String ): String {
79
+ return when {
80
+ length <= size -> this
81
+ else -> substring(0 , size) + postfix
82
+ }
83
+ }
42
84
}
43
85
44
86
object BooleanValueProvider : PrimitiveValueProvider(booleanClassId, booleanWrapperClassId) {
45
87
override fun generate (description : FuzzedDescription , type : FuzzedType ) = sequence {
46
- yieldKnown(Bool .TRUE (), description = " true " ) { toBoolean() }
47
- yieldKnown(Bool .FALSE (), description = " false " ) { toBoolean() }
88
+ yieldKnown(Bool .TRUE ()) { toBoolean() }
89
+ yieldKnown(Bool .FALSE ()) { toBoolean() }
48
90
}
49
91
}
50
92
@@ -80,6 +122,15 @@ object IntegerValueProvider : PrimitiveValueProvider(
80
122
81
123
private fun ClassId.cast (value : BitVectorValue ): Any = tryCast(value)!!
82
124
125
+ private val randomStubWithNoUsage = Random (0 )
126
+ private val configurationStubWithNoUsage = Configuration ()
127
+
128
+ private fun BitVectorValue.change (func : BitVectorValue .() -> Unit ): BitVectorValue {
129
+ return Mutation <KnownValue > { _, _, _ ->
130
+ BitVectorValue (this ).apply { func() }
131
+ }.mutate(this , randomStubWithNoUsage, configurationStubWithNoUsage) as BitVectorValue
132
+ }
133
+
83
134
override fun generate (
84
135
description : FuzzedDescription ,
85
136
type : FuzzedType
@@ -90,14 +141,14 @@ object IntegerValueProvider : PrimitiveValueProvider(
90
141
val values = listOfNotNull(
91
142
value,
92
143
when (c) {
93
- FuzzedContext . Comparison . EQ , FuzzedContext . Comparison . NE , FuzzedContext . Comparison . LE , FuzzedContext . Comparison . GT -> BitVectorValue ( value). apply { inc() }
94
- FuzzedContext . Comparison . LT , FuzzedContext . Comparison . GE -> BitVectorValue ( value). apply { dec() }
144
+ EQ , NE , LE , GT -> value.change { inc() }
145
+ LT , GE -> value.change { dec() }
95
146
else -> null
96
147
}
97
148
)
98
149
values.forEach {
99
150
if (type.classId.tryCast(it) != null ) {
100
- yieldKnown(it, description = " $it " ) {
151
+ yieldKnown(it) {
101
152
type.classId.cast(this )
102
153
}
103
154
}
@@ -109,7 +160,7 @@ object IntegerValueProvider : PrimitiveValueProvider(
109
160
val s = type.classId.typeSize
110
161
val value = bound(s)
111
162
if (type.classId.tryCast(value) != null ) {
112
- yieldKnown(value, description = bound.name.lowercase().replace( " _ " , " " ) ) {
163
+ yieldKnown(value) {
113
164
type.classId.cast(this )
114
165
}
115
166
}
@@ -143,12 +194,12 @@ object FloatValueProvider : PrimitiveValueProvider(
143
194
) = sequence {
144
195
description.constants.forEach { (t, v, _) ->
145
196
if (t in acceptableTypes) {
146
- yieldKnown(IEEE754Value .fromValue(v), description = " $v " ) { type.classId.cast(this ) }
197
+ yieldKnown(IEEE754Value .fromValue(v)) { type.classId.cast(this ) }
147
198
}
148
199
}
149
200
DefaultFloatBound .values().forEach { bound ->
150
201
val (m, e) = type.classId.typeSize
151
- yieldKnown(bound(m ,e), description = bound.name.lowercase().replace( " _ " , " " ) ) {
202
+ yieldKnown(bound(m ,e)) {
152
203
type.classId.cast(this )
153
204
}
154
205
}
@@ -165,7 +216,7 @@ object StringValueProvider : PrimitiveValueProvider(stringClassId) {
165
216
val values = constants
166
217
.mapNotNull { it.value as ? String } +
167
218
sequenceOf(" " , " abc" , " \n\t\r " )
168
- values.forEach { yieldKnown(StringValue (it), description = " predefined string " ) { value } }
219
+ values.forEach { yieldKnown(StringValue (it)) { value } }
169
220
constants
170
221
.filter { it.fuzzedContext.isPatterMatchingContext() }
171
222
.map { it.value as String }
@@ -178,7 +229,7 @@ object StringValueProvider : PrimitiveValueProvider(stringClassId) {
178
229
false
179
230
}
180
231
}.forEach {
181
- yieldKnown(RegexValue (it, Random (0 )), description = " regex( $it ) " ) { value }
232
+ yieldKnown(RegexValue (it, Random (0 ))) { value }
182
233
}
183
234
}
184
235
0 commit comments