@@ -4,12 +4,11 @@ import org.utbot.framework.plugin.api.ClassId
44import org.utbot.framework.plugin.api.UtPrimitiveModel
55import org.utbot.framework.plugin.api.util.*
66import org.utbot.fuzzer.FuzzedContext
7+ import org.utbot.fuzzer.FuzzedContext.Comparison.*
78import org.utbot.fuzzer.FuzzedType
89import org.utbot.fuzzer.FuzzedValue
910import 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.*
1312import org.utbot.fuzzing.seeds.*
1413import java.util.regex.Pattern
1514import java.util.regex.PatternSyntaxException
@@ -24,27 +23,70 @@ abstract class PrimitiveValueProvider(
2423
2524 protected suspend fun <T : KnownValue > SequenceScope <Seed <FuzzedType , FuzzedValue >>.yieldKnown (
2625 value : T ,
27- description : String = value.toString(),
2826 toValue : T .() -> Any
2927 ) {
3028 yield (Seed .Known (value) { known ->
3129 UtPrimitiveModel (toValue(known)).fuzzed {
3230 summary = buildString {
33- append(" %var% = " )
31+ append(" %var% = ${known.valueToString()} " )
3432 if (known.mutatedFrom != null ) {
35- append(" mutated from " )
33+ append(" ( mutated from ${known.mutatedFrom?.valueToString()} ) " )
3634 }
37- append(description)
3835 }
3936 }
4037 })
4138 }
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+ }
4284}
4385
4486object BooleanValueProvider : PrimitiveValueProvider(booleanClassId, booleanWrapperClassId) {
4587 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() }
4890 }
4991}
5092
@@ -80,6 +122,15 @@ object IntegerValueProvider : PrimitiveValueProvider(
80122
81123 private fun ClassId.cast (value : BitVectorValue ): Any = tryCast(value)!!
82124
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+
83134 override fun generate (
84135 description : FuzzedDescription ,
85136 type : FuzzedType
@@ -90,14 +141,14 @@ object IntegerValueProvider : PrimitiveValueProvider(
90141 val values = listOfNotNull(
91142 value,
92143 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() }
95146 else -> null
96147 }
97148 )
98149 values.forEach {
99150 if (type.classId.tryCast(it) != null ) {
100- yieldKnown(it, description = " $it " ) {
151+ yieldKnown(it) {
101152 type.classId.cast(this )
102153 }
103154 }
@@ -109,7 +160,7 @@ object IntegerValueProvider : PrimitiveValueProvider(
109160 val s = type.classId.typeSize
110161 val value = bound(s)
111162 if (type.classId.tryCast(value) != null ) {
112- yieldKnown(value, description = bound.name.lowercase().replace( " _ " , " " ) ) {
163+ yieldKnown(value) {
113164 type.classId.cast(this )
114165 }
115166 }
@@ -143,12 +194,12 @@ object FloatValueProvider : PrimitiveValueProvider(
143194 ) = sequence {
144195 description.constants.forEach { (t, v, _) ->
145196 if (t in acceptableTypes) {
146- yieldKnown(IEEE754Value .fromValue(v), description = " $v " ) { type.classId.cast(this ) }
197+ yieldKnown(IEEE754Value .fromValue(v)) { type.classId.cast(this ) }
147198 }
148199 }
149200 DefaultFloatBound .values().forEach { bound ->
150201 val (m, e) = type.classId.typeSize
151- yieldKnown(bound(m ,e), description = bound.name.lowercase().replace( " _ " , " " ) ) {
202+ yieldKnown(bound(m ,e)) {
152203 type.classId.cast(this )
153204 }
154205 }
@@ -165,7 +216,7 @@ object StringValueProvider : PrimitiveValueProvider(stringClassId) {
165216 val values = constants
166217 .mapNotNull { it.value as ? String } +
167218 sequenceOf(" " , " abc" , " \n\t\r " )
168- values.forEach { yieldKnown(StringValue (it), description = " predefined string " ) { value } }
219+ values.forEach { yieldKnown(StringValue (it)) { value } }
169220 constants
170221 .filter { it.fuzzedContext.isPatterMatchingContext() }
171222 .map { it.value as String }
@@ -178,7 +229,7 @@ object StringValueProvider : PrimitiveValueProvider(stringClassId) {
178229 false
179230 }
180231 }.forEach {
181- yieldKnown(RegexValue (it, Random (0 )), description = " regex( $it ) " ) { value }
232+ yieldKnown(RegexValue (it, Random (0 ))) { value }
182233 }
183234 }
184235
0 commit comments