From 06bcc5b94b876f2fc9477e8593651e4400a7e07e Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Mon, 27 Feb 2023 13:45:41 +0300 Subject: [PATCH 1/5] Add slices support --- .../main/kotlin/org/utbot/go/GoLanguage.kt | 11 +- .../kotlin/org/utbot/go/api/GoTypesApi.kt | 18 ++++ .../kotlin/org/utbot/go/api/GoUtModelsApi.kt | 30 ++++++ .../org/utbot/go/api/util/GoTypesApiUtil.kt | 3 +- .../utbot/go/api/util/GoUtModelsApiUtil.kt | 70 ++++++------ .../fuzzer/providers/GoSliceValueProvider.kt | 35 ++++++ .../go/gocodeanalyzer/AnalysisResults.kt | 17 ++- .../GoUtModelToCodeConverter.kt | 67 ++++++++++++ .../org/utbot/go/worker/GoCodeTemplates.kt | 100 +++++++++++++++++- .../kotlin/org/utbot/go/worker/GoWorker.kt | 1 + .../utbot/go/worker/RawExecutionResults.kt | 30 +++++- .../analysis_results.go | 9 ++ .../go_source_code_analyzer/analyzer_core.go | 16 +++ 13 files changed, 361 insertions(+), 46 deletions(-) create mode 100644 utbot-go/src/main/kotlin/org/utbot/go/fuzzer/providers/GoSliceValueProvider.kt diff --git a/utbot-go/src/main/kotlin/org/utbot/go/GoLanguage.kt b/utbot-go/src/main/kotlin/org/utbot/go/GoLanguage.kt index cce09834af..5d00f199c2 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/GoLanguage.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/GoLanguage.kt @@ -5,14 +5,15 @@ import org.utbot.fuzzing.utils.Trie import org.utbot.go.api.GoUtFunction import org.utbot.go.framework.api.go.GoTypeId import org.utbot.go.framework.api.go.GoUtModel -import org.utbot.go.fuzzer.providers.GoArrayValueProvider -import org.utbot.go.fuzzer.providers.GoConstantValueProvider -import org.utbot.go.fuzzer.providers.GoPrimitivesValueProvider -import org.utbot.go.fuzzer.providers.GoStructValueProvider +import org.utbot.go.fuzzer.providers.* fun goDefaultValueProviders() = listOf( - GoPrimitivesValueProvider, GoArrayValueProvider, GoStructValueProvider, GoConstantValueProvider + GoPrimitivesValueProvider, + GoArrayValueProvider, + GoSliceValueProvider, + GoStructValueProvider, + GoConstantValueProvider ) class GoInstruction( diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt index fc7247bcb0..f898a65301 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt @@ -76,6 +76,24 @@ class GoArrayTypeId( override fun hashCode(): Int = 31 * elementTypeId.hashCode() + length } +class GoSliceTypeId( + name: String, elementTypeId: GoTypeId, +) : GoTypeId(name, elementTypeId = elementTypeId) { + override val canonicalName: String = "[]${elementTypeId.canonicalName}" + + override fun getRelativeName(destinationPackage: GoPackage, aliases: Map): String = + "[]${elementTypeId!!.getRelativeName(destinationPackage, aliases)}" + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is GoArrayTypeId) return false + + return elementTypeId == other.elementTypeId + } + + override fun hashCode(): Int = elementTypeId.hashCode() +} + class GoInterfaceTypeId( name: String, implementsError: Boolean, diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/GoUtModelsApi.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/GoUtModelsApi.kt index 71c3691e62..ae3a17a42b 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/GoUtModelsApi.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/GoUtModelsApi.kt @@ -89,6 +89,36 @@ class GoUtArrayModel( } } +class GoUtSliceModel( + val value: MutableMap, + typeId: GoSliceTypeId, + val length: Int, +) : GoUtModel(typeId) { + override val typeId: GoSliceTypeId + get() = super.typeId as GoSliceTypeId + + override fun getRequiredPackages(destinationPackage: GoPackage): Set { + val elementStructTypeId = typeId.elementTypeId as? GoStructTypeId + val imports = if (elementStructTypeId != null && elementStructTypeId.sourcePackage != destinationPackage) { + mutableSetOf(elementStructTypeId.sourcePackage) + } else { + mutableSetOf() + } + value.values.map { it.getRequiredPackages(destinationPackage) }.forEach { imports += it } + return imports + } + + override fun isComparable(): Boolean = value.values.all { it.isComparable() } + + fun getElements(): List = (0 until length).map { + value[it] ?: typeId.elementTypeId!!.goDefaultValueModel() + } + + override fun toString(): String = getElements().joinToString(prefix = "$typeId{", postfix = "}") { + it.toString() + } +} + class GoUtFloatNaNModel( typeId: GoPrimitiveTypeId ) : GoUtPrimitiveModel( diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt index 82c00102cc..d043d6144a 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt @@ -152,6 +152,7 @@ fun GoTypeId.goDefaultValueModel(): GoUtModel = when (this) { is GoStructTypeId -> GoUtStructModel(listOf(), this) is GoArrayTypeId -> GoUtArrayModel(hashMapOf(), this) + is GoSliceTypeId -> GoUtSliceModel(hashMapOf(), this, 0) else -> GoUtNilModel(this) } @@ -160,7 +161,7 @@ fun GoTypeId.getAllStructTypes(): Set = when (this) { acc + (field.declaringType).getAllStructTypes() } - is GoArrayTypeId -> elementTypeId!!.getAllStructTypes() + is GoArrayTypeId, is GoSliceTypeId -> elementTypeId!!.getAllStructTypes() else -> emptySet() } diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoUtModelsApiUtil.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoUtModelsApiUtil.kt index 556c4edd81..57f0fb7b06 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoUtModelsApiUtil.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/util/GoUtModelsApiUtil.kt @@ -3,10 +3,7 @@ package org.utbot.go.api.util import org.utbot.go.api.* import org.utbot.go.framework.api.go.GoPackage import org.utbot.go.framework.api.go.GoUtModel -import org.utbot.go.worker.ArrayValue -import org.utbot.go.worker.PrimitiveValue -import org.utbot.go.worker.RawValue -import org.utbot.go.worker.StructValue +import org.utbot.go.worker.* fun GoUtModel.isNaNOrInf(): Boolean = this is GoUtFloatNaNModel || this is GoUtFloatInfModel @@ -18,31 +15,40 @@ fun GoUtModel.doesNotContainNaNOrInf(): Boolean { fun GoUtModel.containsNaNOrInf(): Boolean = !this.doesNotContainNaNOrInf() -fun GoUtModel.convertToRawValue(destinationPackage: GoPackage, aliases: Map): RawValue = when (val model = this) { - is GoUtComplexModel -> PrimitiveValue( - model.typeId.getRelativeName(destinationPackage, aliases), - "${model.realValue}@${model.imagValue}" - ) - - is GoUtArrayModel -> ArrayValue( - model.typeId.getRelativeName(destinationPackage, aliases), - model.typeId.elementTypeId!!.getRelativeName(destinationPackage, aliases), - model.length, - model.getElements().map { it.convertToRawValue(destinationPackage, aliases) } - ) - - is GoUtStructModel -> StructValue( - model.typeId.getRelativeName(destinationPackage, aliases), - model.value.map { - StructValue.FieldValue( - it.fieldId.name, - it.model.convertToRawValue(destinationPackage, aliases), - it.fieldId.isExported - ) - } - ) - - is GoUtPrimitiveModel -> PrimitiveValue(model.typeId.name, model.value.toString()) - - else -> error("Converting ${model.javaClass} to RawValue is not supported") -} \ No newline at end of file +fun GoUtModel.convertToRawValue(destinationPackage: GoPackage, aliases: Map): RawValue = + when (val model = this) { + is GoUtComplexModel -> PrimitiveValue( + model.typeId.getRelativeName(destinationPackage, aliases), + "${model.realValue}@${model.imagValue}" + ) + + is GoUtArrayModel -> ArrayValue( + model.typeId.getRelativeName(destinationPackage, aliases), + model.typeId.elementTypeId!!.getRelativeName(destinationPackage, aliases), + model.length, + model.getElements().map { it.convertToRawValue(destinationPackage, aliases) } + ) + + + is GoUtSliceModel -> SliceValue( + model.typeId.getRelativeName(destinationPackage, aliases), + model.typeId.elementTypeId!!.getRelativeName(destinationPackage, aliases), + model.length, + model.getElements().map { it.convertToRawValue(destinationPackage, aliases) } + ) + + is GoUtStructModel -> StructValue( + model.typeId.getRelativeName(destinationPackage, aliases), + model.value.map { + StructValue.FieldValue( + it.fieldId.name, + it.model.convertToRawValue(destinationPackage, aliases), + it.fieldId.isExported + ) + } + ) + + is GoUtPrimitiveModel -> PrimitiveValue(model.typeId.name, model.value.toString()) + + else -> error("Converting ${model.javaClass} to RawValue is not supported") + } \ No newline at end of file diff --git a/utbot-go/src/main/kotlin/org/utbot/go/fuzzer/providers/GoSliceValueProvider.kt b/utbot-go/src/main/kotlin/org/utbot/go/fuzzer/providers/GoSliceValueProvider.kt new file mode 100644 index 0000000000..9178099622 --- /dev/null +++ b/utbot-go/src/main/kotlin/org/utbot/go/fuzzer/providers/GoSliceValueProvider.kt @@ -0,0 +1,35 @@ +package org.utbot.go.fuzzer.providers + +import org.utbot.fuzzing.Routine +import org.utbot.fuzzing.Seed +import org.utbot.fuzzing.ValueProvider +import org.utbot.go.GoDescription +import org.utbot.go.api.GoSliceTypeId +import org.utbot.go.api.GoUtSliceModel +import org.utbot.go.framework.api.go.GoTypeId +import org.utbot.go.framework.api.go.GoUtModel + +object GoSliceValueProvider : ValueProvider { + override fun accept(type: GoTypeId): Boolean = type is GoSliceTypeId + + override fun generate(description: GoDescription, type: GoTypeId): Sequence> = + sequence { + type.let { it as GoSliceTypeId }.also { sliceType -> + yield( + Seed.Collection( + construct = Routine.Collection { + GoUtSliceModel( + value = hashMapOf(), + typeId = sliceType, + length = it, + ) + }, + modify = Routine.ForEach(listOf(sliceType.elementTypeId!!)) { self, i, values -> + val model = self as GoUtSliceModel + model.value[i] = values.first() + } + ) + ) + } + } +} \ No newline at end of file diff --git a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt index d7080f8ca4..14a461f742 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/gocodeanalyzer/AnalysisResults.kt @@ -2,10 +2,7 @@ package org.utbot.go.gocodeanalyzer import com.beust.klaxon.TypeAdapter import com.beust.klaxon.TypeFor -import org.utbot.go.api.GoArrayTypeId -import org.utbot.go.api.GoInterfaceTypeId -import org.utbot.go.api.GoPrimitiveTypeId -import org.utbot.go.api.GoStructTypeId +import org.utbot.go.api.* import org.utbot.go.api.util.goPrimitives import org.utbot.go.framework.api.go.GoFieldId import org.utbot.go.framework.api.go.GoImport @@ -52,6 +49,16 @@ data class AnalyzedArrayType( ) } +data class AnalyzedSliceType( + override val name: String, + val elementType: AnalyzedType, +) : AnalyzedType(name) { + override fun toGoTypeId(): GoTypeId = GoSliceTypeId( + name = name, + elementTypeId = elementType.toGoTypeId(), + ) +} + data class AnalyzedInterfaceType( override val name: String, val implementsError: Boolean, @@ -79,7 +86,7 @@ class AnalyzedTypeAdapter : TypeAdapter { return when { typeName.startsWith("interface ") -> AnalyzedInterfaceType::class typeName.startsWith("map[") -> error("Map type not yet supported") - typeName.startsWith("[]") -> error("Slice type not yet supported") + typeName.startsWith("[]") -> AnalyzedSliceType::class typeName.startsWith("[") -> AnalyzedArrayType::class goPrimitives.map { it.name }.contains(typeName) -> AnalyzedPrimitiveType::class else -> AnalyzedStructType::class diff --git a/utbot-go/src/main/kotlin/org/utbot/go/simplecodegeneration/GoUtModelToCodeConverter.kt b/utbot-go/src/main/kotlin/org/utbot/go/simplecodegeneration/GoUtModelToCodeConverter.kt index 78cc5c07ae..9dc69f8b52 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/simplecodegeneration/GoUtModelToCodeConverter.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/simplecodegeneration/GoUtModelToCodeConverter.kt @@ -25,6 +25,8 @@ class GoUtModelToCodeConverter( is GoUtArrayModel -> arrayModelToGoCode(model) + is GoUtSliceModel -> sliceModelToGoCode(model) + else -> error("Converting a $javaClass to Go code isn't supported") } @@ -64,6 +66,15 @@ class GoUtModelToCodeConverter( arrayModelToGoCodeWithoutTypeName(it as GoUtArrayModel) } + is GoSliceTypeId -> model.getElements().joinToString( + prefix = "[${model.length}]${ + elementType.getRelativeName(destinationPackage, aliases) + }{", + postfix = "}" + ) { + sliceModelToGoCodeWithoutTypeName(it as GoUtSliceModel) + } + else -> model.getElements().joinToString( prefix = "[${model.length}]${elementType.getRelativeName(destinationPackage, aliases)}{", postfix = "}" @@ -80,6 +91,62 @@ class GoUtModelToCodeConverter( arrayModelToGoCodeWithoutTypeName(it as GoUtArrayModel) } + is GoSliceTypeId -> model.getElements().joinToString(prefix = "{", postfix = "}") { + sliceModelToGoCodeWithoutTypeName(it as GoUtSliceModel) + } + + else -> model.getElements().joinToString(prefix = "{", postfix = "}") + } + + private fun sliceModelToGoCode(model: GoUtSliceModel): String = + when (val elementType = model.typeId.elementTypeId!!) { + is GoStructTypeId -> model.getElements().joinToString( + prefix = "[]${ + elementType.getRelativeName(destinationPackage, aliases) + }{", + postfix = "}" + ) { + structModelToGoCodeWithoutStructName(it as GoUtStructModel) + } + + is GoArrayTypeId -> model.getElements().joinToString( + prefix = "[]${ + elementType.getRelativeName(destinationPackage, aliases) + }{", + postfix = "}" + ) { + arrayModelToGoCodeWithoutTypeName(it as GoUtArrayModel) + } + + is GoSliceTypeId -> model.getElements().joinToString( + prefix = "[]${ + elementType.getRelativeName(destinationPackage, aliases) + }{", + postfix = "}" + ) { + sliceModelToGoCodeWithoutTypeName(it as GoUtSliceModel) + } + + else -> model.getElements().joinToString( + prefix = "[]${elementType.getRelativeName(destinationPackage, aliases)}{", + postfix = "}" + ) + } + + private fun sliceModelToGoCodeWithoutTypeName(model: GoUtSliceModel): String = + when (model.typeId.elementTypeId!!) { + is GoStructTypeId -> model.getElements().joinToString(prefix = "{", postfix = "}") { + structModelToGoCodeWithoutStructName(it as GoUtStructModel) + } + + is GoArrayTypeId -> model.getElements().joinToString(prefix = "{", postfix = "}") { + arrayModelToGoCodeWithoutTypeName(it as GoUtArrayModel) + } + + is GoSliceTypeId -> model.getElements().joinToString(prefix = "{", postfix = "}") { + sliceModelToGoCodeWithoutTypeName(it as GoUtSliceModel) + } + else -> model.getElements().joinToString(prefix = "{", postfix = "}") } } diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt index b5f4752b48..ed8da2065c 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt @@ -208,6 +208,38 @@ object GoCodeTemplates { } """.trimIndent() + private val sliceValueStruct = """ + type __SliceValue__ struct { + Type string `json:"type"` + ElementType string `json:"elementType"` + Length int `json:"length"` + Value []__RawValue__ `json:"value"` + } + """.trimIndent() + + private val sliceValueToReflectValueMethod = """ + func (v __SliceValue__) __toReflectValue__() (reflect.Value, error) { + elementType, err := __convertStringToReflectType__(v.ElementType) + __checkErrorAndExit__(err) + + sliceType := reflect.SliceOf(elementType) + slice := reflect.MakeSlice(sliceType, v.Length, v.Length) + slicePtr := reflect.New(slice.Type()) + slicePtr.Elem().Set(slice) + + for i := 0; i < len(v.Value); i++ { + element := slicePtr.Elem().Index(i) + + reflectValue, err := v.Value[i].__toReflectValue__() + __checkErrorAndExit__(err) + + element.Set(reflectValue) + } + + return slicePtr.Elem(), nil + } + """.trimIndent() + private fun convertStringToReflectType( structTypes: Set, destinationPackage: GoPackage, @@ -220,7 +252,17 @@ object GoCodeTemplates { case strings.HasPrefix(typeName, "map["): return nil, fmt.Errorf("map type not supported") case strings.HasPrefix(typeName, "[]"): - return nil, fmt.Errorf("slice type not supported") + index := strings.IndexRune(typeName, ']') + if index == -1 { + return nil, fmt.Errorf("not correct type name '%s'", typeName) + } + + res, err := __convertStringToReflectType__(typeName[index+1:]) + if err != nil { + return nil, err + } + + result = reflect.SliceOf(res) case strings.HasPrefix(typeName, "["): index := strings.IndexRune(typeName, ']') if index == -1 { @@ -423,6 +465,23 @@ object GoCodeTemplates { Length: length, Value: arrayElementValues, }, nil + case reflect.Slice: + elem := valueOfRes.Type().Elem() + elementType := elem.String() + sliceElementValues := []__RawValue__{} + for i := 0; i < valueOfRes.Len(); i++ { + sliceElementValue, err := __convertReflectValueToRawValue__(valueOfRes.Index(i)) + __checkErrorAndExit__(err) + + sliceElementValues = append(sliceElementValues, sliceElementValue) + } + length := len(sliceElementValues) + return __SliceValue__{ + Type: fmt.Sprintf("[]%s", elementType), + ElementType: elementType, + Length: length, + Value: sliceElementValues, + }, nil case reflect.Interface: if valueOfRes.Interface() == nil { return nil, nil @@ -579,7 +638,42 @@ object GoCodeTemplates { case strings.HasPrefix(typeNameStr, "map["): return nil, fmt.Errorf("map type not supported") case strings.HasPrefix(typeNameStr, "[]"): - return nil, fmt.Errorf("slice type not supported") + elementType, ok := rawValue["elementType"] + if !ok { + return nil, fmt.Errorf("sliceValue must contain field 'elementType") + } + elementTypeStr, ok := elementType.(string) + if !ok { + return nil, fmt.Errorf("sliceValue field 'elementType' must be string") + } + + if _, ok := rawValue["length"]; !ok { + return nil, fmt.Errorf("sliceValue must contain field 'length'") + } + length, ok := rawValue["length"].(float64) + if !ok { + return nil, fmt.Errorf("sliceValue field 'length' must be float64") + } + + value, ok := v.([]interface{}) + if !ok || len(value) != int(length) { + return nil, fmt.Errorf("sliceValue field 'value' must be array of length %d", int(length)) + } + + values := []__RawValue__{} + for _, v := range value { + nextValue, err := __convertParsedJsonToRawValue__(v.(map[string]interface{})) + __checkErrorAndExit__(err) + + values = append(values, nextValue) + } + + return __SliceValue__{ + Type: typeNameStr, + ElementType: elementTypeStr, + Length: int(length), + Value: values, + }, nil case strings.HasPrefix(typeNameStr, "["): elementType, ok := rawValue["elementType"] if !ok { @@ -699,6 +793,8 @@ object GoCodeTemplates { structValueToReflectValueMethod, arrayValueStruct, arrayValueToReflectValueMethod, + sliceValueStruct, + sliceValueToReflectValueMethod, convertStringToReflectType(structTypes, destinationPackage, aliases), panicMessageStruct, rawExecutionResultStruct, diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt index 9070c6e61a..9e509ffc93 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt @@ -22,6 +22,7 @@ class GoWorker( fun sendFuzzedParametersValues(parameters: List, aliases: Map) { val rawValues = parameters.map { it.convertToRawValue(function.sourcePackage, aliases) } val json = convertObjectToJsonString(rawValues) + println(json) writer.write(json) writer.flush() } diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt index e105388ee8..7ad066c0f0 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt @@ -81,6 +81,23 @@ data class ArrayValue( } } +data class SliceValue( + override val type: String, + val elementType: String, + val length: Int, + override val value: List +) : RawValue(type, value) { + override fun checkIsEqualTypes(type: GoTypeId): Boolean { + if (type !is GoSliceTypeId) { + return false + } + if (elementType != type.elementTypeId!!.canonicalName) { + return false + } + return value.all { it.checkIsEqualTypes(type.elementTypeId) } + } +} + @TypeFor(field = "type", adapter = RawResultValueAdapter::class) abstract class RawValue(open val type: String, open val value: Any) { abstract fun checkIsEqualTypes(type: GoTypeId): Boolean @@ -91,7 +108,7 @@ class RawResultValueAdapter : TypeAdapter { val typeName = type as String return when { typeName.startsWith("map[") -> error("Map result type not supported") - typeName.startsWith("[]") -> error("Slice result type not supported") + typeName.startsWith("[]") -> SliceValue::class typeName.startsWith("[") -> ArrayValue::class goPrimitives.map { it.name }.contains(typeName) -> PrimitiveValue::class else -> StructValue::class @@ -183,6 +200,8 @@ private fun createGoUtModelFromRawValue( is GoArrayTypeId -> createGoUtArrayModelFromRawValue(rawValue as ArrayValue, typeId, intSize) + is GoSliceTypeId -> createGoUtSliceModelFromRawValue(rawValue as SliceValue, typeId, intSize) + is GoPrimitiveTypeId -> createGoUtPrimitiveModelFromRawValue(rawValue as PrimitiveValue, typeId, intSize) else -> error("Creating a model from raw value of [${typeId.javaClass}] type is not supported") @@ -240,4 +259,13 @@ private fun createGoUtArrayModelFromRawValue( createGoUtModelFromRawValue(resultValue.value[index], resultTypeId.elementTypeId!!, intSize) }.toMutableMap() return GoUtArrayModel(value, resultTypeId) +} + +private fun createGoUtSliceModelFromRawValue( + resultValue: SliceValue, resultTypeId: GoSliceTypeId, intSize: Int +): GoUtSliceModel { + val value = (0 until resultValue.length).associateWith { index -> + createGoUtModelFromRawValue(resultValue.value[index], resultTypeId.elementTypeId!!, intSize) + }.toMutableMap() + return GoUtSliceModel(value, resultTypeId, resultValue.length) } \ No newline at end of file diff --git a/utbot-go/src/main/resources/go_source_code_analyzer/analysis_results.go b/utbot-go/src/main/resources/go_source_code_analyzer/analysis_results.go index 2b433240ae..8e54f34d8e 100644 --- a/utbot-go/src/main/resources/go_source_code_analyzer/analysis_results.go +++ b/utbot-go/src/main/resources/go_source_code_analyzer/analysis_results.go @@ -53,6 +53,15 @@ func (t AnalyzedArrayType) GetName() string { return t.Name } +type AnalyzedSliceType struct { + Name string `json:"name"` + ElementType AnalyzedType `json:"elementType"` +} + +func (t AnalyzedSliceType) GetName() string { + return t.Name +} + type AnalyzedFunctionParameter struct { Name string `json:"name"` Type AnalyzedType `json:"type"` diff --git a/utbot-go/src/main/resources/go_source_code_analyzer/analyzer_core.go b/utbot-go/src/main/resources/go_source_code_analyzer/analyzer_core.go index 96d67e32cc..28a2c3aeb6 100644 --- a/utbot-go/src/main/resources/go_source_code_analyzer/analyzer_core.go +++ b/utbot-go/src/main/resources/go_source_code_analyzer/analyzer_core.go @@ -93,6 +93,19 @@ func toAnalyzedType(typ types.Type) (AnalyzedType, error) { ElementType: arrayElemType, Length: length, } + case *types.Slice: + sliceType := typ.(*types.Slice) + + sliceElemType, err := toAnalyzedType(sliceType.Elem()) + checkError(err) + + elemTypeName := sliceElemType.GetName() + name := fmt.Sprintf("[]%s", elemTypeName) + + result = AnalyzedSliceType{ + Name: name, + ElementType: sliceElemType, + } case *types.Interface: namedType := typ.(*types.Named) name := namedType.Obj().Name() @@ -134,6 +147,9 @@ func checkTypeIsSupported(typ types.Type, isResultType bool) bool { if arrayType, ok := underlyingType.(*types.Array); ok { return checkTypeIsSupported(arrayType.Elem(), isResultType) } + if sliceType, ok := underlyingType.(*types.Slice); ok { + return checkTypeIsSupported(sliceType.Elem(), isResultType) + } if interfaceType, ok := underlyingType.(*types.Interface); ok && isResultType { return interfaceType == errorInterface } From 35d1692fdc696197b679ced6c2c3a4fd5c9e43b1 Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Mon, 27 Feb 2023 14:26:39 +0300 Subject: [PATCH 2/5] Delete print --- utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt index 9e509ffc93..9070c6e61a 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoWorker.kt @@ -22,7 +22,6 @@ class GoWorker( fun sendFuzzedParametersValues(parameters: List, aliases: Map) { val rawValues = parameters.map { it.convertToRawValue(function.sourcePackage, aliases) } val json = convertObjectToJsonString(rawValues) - println(json) writer.write(json) writer.flush() } From fd5d639a104480aa7fca89055137f4acd6bffa64 Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Mon, 27 Feb 2023 18:01:13 +0300 Subject: [PATCH 3/5] Fix nil slice. Fix method equals for byte and rune types --- .../kotlin/org/utbot/go/api/GoTypesApi.kt | 8 +++- .../org/utbot/go/worker/GoCodeTemplates.kt | 22 +++-------- .../utbot/go/worker/RawExecutionResults.kt | 38 +++++++------------ 3 files changed, 25 insertions(+), 43 deletions(-) diff --git a/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt b/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt index f898a65301..f5fa7649e4 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt @@ -8,7 +8,11 @@ import org.utbot.go.framework.api.go.GoTypeId * Represents real Go primitive type. */ class GoPrimitiveTypeId(name: String) : GoTypeId(name) { - override val canonicalName: String = simpleName + override val canonicalName: String = when (name) { + "byte" -> "uint8" + "rune" -> "int32" + else -> simpleName + } override fun getRelativeName(destinationPackage: GoPackage, aliases: Map): String = simpleName @@ -16,7 +20,7 @@ class GoPrimitiveTypeId(name: String) : GoTypeId(name) { if (this === other) return true if (other !is GoPrimitiveTypeId) return false - return name == other.name + return canonicalName == other.canonicalName } override fun hashCode(): Int = name.hashCode() diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt index ed8da2065c..62ce5e69fa 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/GoCodeTemplates.kt @@ -323,23 +323,8 @@ object GoCodeTemplates { result = reflect.TypeOf(uintptr(0)) ${ structTypes.joinToString(separator = "\n") { - "case \"${ - if (it.sourcePackage == destinationPackage || aliases[it.sourcePackage] == ".") { - it.simpleName - } else if (aliases[it.sourcePackage] == null) { - "${it.packageName}.${it.simpleName}" - } else { - "${aliases[it.sourcePackage]}.${it.simpleName}" - } - }\": result = reflect.TypeOf(${ - if (it.sourcePackage == destinationPackage || aliases[it.sourcePackage] == ".") { - it.simpleName - } else if (aliases[it.sourcePackage] == null) { - "${it.packageName}.${it.simpleName}" - } else { - "${aliases[it.sourcePackage]}.${it.simpleName}" - } - }{})" + val relativeName = it.getRelativeName(destinationPackage, aliases) + "case \"${relativeName}\": result = reflect.TypeOf(${relativeName}{})" } } default: @@ -466,6 +451,9 @@ object GoCodeTemplates { Value: arrayElementValues, }, nil case reflect.Slice: + if valueOfRes.IsNil() { + return nil, nil + } elem := valueOfRes.Type().Elem() elementType := elem.String() sliceElementValues := []__RawValue__{} diff --git a/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt b/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt index 7ad066c0f0..cb9e714e4e 100644 --- a/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt +++ b/utbot-go/src/main/kotlin/org/utbot/go/worker/RawExecutionResults.kt @@ -17,15 +17,11 @@ data class PrimitiveValue( if (!type.isPrimitiveGoType && type !is GoInterfaceTypeId && !type.implementsError) { return false } - // byte is an alias for uint8 and rune is an alias for int32 - if (this.type == "uint8" && type == goByteTypeId || this.type == "int32" && type == goRuneTypeId) { - return true - } // for error support if (this.type == "string" && type is GoInterfaceTypeId && type.implementsError) { return true } - return this.type == type.simpleName + return GoPrimitiveTypeId(this.type) == type } } @@ -161,13 +157,7 @@ fun convertRawExecutionResultToExecutionResult( error("Function completed execution must have as many result raw values as result types.") } rawExecutionResult.rawResultValues.zip(functionResultTypes).forEach { (rawResultValue, resultType) -> - if (rawResultValue == null) { - if (resultType !is GoInterfaceTypeId) { - error("Result of function execution must have same type as function result") - } - return@forEach - } - if (!rawResultValue.checkIsEqualTypes(resultType)) { + if (rawResultValue != null && !rawResultValue.checkIsEqualTypes(resultType)) { error("Result of function execution must have same type as function result") } } @@ -188,23 +178,23 @@ fun convertRawExecutionResultToExecutionResult( private fun createGoUtModelFromRawValue( rawValue: RawValue?, typeId: GoTypeId, intSize: Int -): GoUtModel = when (typeId) { - // Only for error interface - is GoInterfaceTypeId -> if (rawValue == null) { - GoUtNilModel(typeId) - } else { - GoUtPrimitiveModel((rawValue as PrimitiveValue).value, goStringTypeId) - } +): GoUtModel = if (rawValue == null) { + GoUtNilModel(typeId) +} else { + when (typeId) { + // Only for error interface + is GoInterfaceTypeId -> GoUtPrimitiveModel((rawValue as PrimitiveValue).value, goStringTypeId) - is GoStructTypeId -> createGoUtStructModelFromRawValue(rawValue as StructValue, typeId, intSize) + is GoStructTypeId -> createGoUtStructModelFromRawValue(rawValue as StructValue, typeId, intSize) - is GoArrayTypeId -> createGoUtArrayModelFromRawValue(rawValue as ArrayValue, typeId, intSize) + is GoArrayTypeId -> createGoUtArrayModelFromRawValue(rawValue as ArrayValue, typeId, intSize) - is GoSliceTypeId -> createGoUtSliceModelFromRawValue(rawValue as SliceValue, typeId, intSize) + is GoSliceTypeId -> createGoUtSliceModelFromRawValue(rawValue as SliceValue, typeId, intSize) - is GoPrimitiveTypeId -> createGoUtPrimitiveModelFromRawValue(rawValue as PrimitiveValue, typeId, intSize) + is GoPrimitiveTypeId -> createGoUtPrimitiveModelFromRawValue(rawValue as PrimitiveValue, typeId, intSize) - else -> error("Creating a model from raw value of [${typeId.javaClass}] type is not supported") + else -> error("Creating a model from raw value of [${typeId.javaClass}] type is not supported") + } } private fun createGoUtPrimitiveModelFromRawValue( From 72cf15fd3e656f7c07c1135513dff0ea41ea754d Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Mon, 27 Feb 2023 18:02:30 +0300 Subject: [PATCH 4/5] Update samples --- utbot-go/go-samples/simple/samples.go | 4 +- .../go-samples/simple/samples_go_ut_test.go | 128 ++++++++++++------ utbot-go/go-samples/simple/supported_types.go | 37 +++++ .../simple/supported_types_go_ut_test.go | 128 +++++++++++++----- 4 files changed, 222 insertions(+), 75 deletions(-) diff --git a/utbot-go/go-samples/simple/samples.go b/utbot-go/go-samples/simple/samples.go index 4c6499c44f..99b57c8292 100644 --- a/utbot-go/go-samples/simple/samples.go +++ b/utbot-go/go-samples/simple/samples.go @@ -52,7 +52,7 @@ func GetCoordinatesOfMiddleBetweenTwoPoints(a, b Point) (float64, float64) { return (a.x + b.x) / 2, (a.y + b.y) / 2 } -func GetCoordinateSumOfPoints(points [10]Point) (float64, float64) { +func GetCoordinateSumOfPoints(points []Point) (float64, float64) { sumX := 0.0 sumY := 0.0 for _, point := range points { @@ -91,7 +91,7 @@ var ErrNotFound = errors.New("target not found in array") // Binary search for target within a sorted array by repeatedly dividing the array in half and comparing the midpoint with the target. // This function uses recursive call to itself. // If a target is found, the index of the target is returned. Else the function return -1 and ErrNotFound. -func Binary(array [10]int, target int, lowIndex int, highIndex int) (int, error) { +func Binary(array []int, target int, lowIndex int, highIndex int) (int, error) { if highIndex < lowIndex { return -1, ErrNotFound } diff --git a/utbot-go/go-samples/simple/samples_go_ut_test.go b/utbot-go/go-samples/simple/samples_go_ut_test.go index e8d4d5a6fd..a6c69385c0 100644 --- a/utbot-go/go-samples/simple/samples_go_ut_test.go +++ b/utbot-go/go-samples/simple/samples_go_ut_test.go @@ -6,37 +6,37 @@ import ( ) func TestDivOrPanicByUtGoFuzzer(t *testing.T) { - actualVal := DivOrPanic(9223372036854775807, -1) + actualVal := DivOrPanic(0, 1) - assert.Equal(t, -9223372036854775807, actualVal) + assert.Equal(t, 0, actualVal) } func TestDivOrPanicPanicsByUtGoFuzzer(t *testing.T) { - assert.PanicsWithValue(t, "div by 0", func() { DivOrPanic(9223372036854775807, 0) }) + assert.PanicsWithValue(t, "div by 0", func() { DivOrPanic(1, 0) }) } func TestExtendedByUtGoFuzzer1(t *testing.T) { - actualVal0, actualVal1, actualVal2 := Extended(9223372036854775807, -1) + actualVal0, actualVal1, actualVal2 := Extended(9223372036854775807, 0) assertMultiple := assert.New(t) - assertMultiple.Equal(int64(-1), actualVal0) - assertMultiple.Equal(int64(0), actualVal1) - assertMultiple.Equal(int64(1), actualVal2) + assertMultiple.Equal(int64(9223372036854775807), actualVal0) + assertMultiple.Equal(int64(1), actualVal1) + assertMultiple.Equal(int64(0), actualVal2) } func TestExtendedByUtGoFuzzer2(t *testing.T) { - actualVal0, actualVal1, actualVal2 := Extended(0, 9223372036854775807) + actualVal0, actualVal1, actualVal2 := Extended(0, -9223372036854775808) assertMultiple := assert.New(t) - assertMultiple.Equal(int64(9223372036854775807), actualVal0) + assertMultiple.Equal(int64(-9223372036854775808), actualVal0) assertMultiple.Equal(int64(0), actualVal1) assertMultiple.Equal(int64(1), actualVal2) } func TestArraySumByUtGoFuzzer(t *testing.T) { - actualVal := ArraySum([5]int{-1, 0, 0, 0, 0}) + actualVal := ArraySum([5]int{1, 0, 0, 0, 0}) - assert.Equal(t, -1, actualVal) + assert.Equal(t, 1, actualVal) } func TestGenerateArrayOfIntegersByUtGoFuzzer(t *testing.T) { @@ -46,31 +46,71 @@ func TestGenerateArrayOfIntegersByUtGoFuzzer(t *testing.T) { } func TestDistanceBetweenTwoPointsByUtGoFuzzer(t *testing.T) { - actualVal := DistanceBetweenTwoPoints(Point{x: 0.730967787376657, y: 0.730967787376657}, Point{x: 0.730967787376657, y: 0.730967787376657}) + actualVal := DistanceBetweenTwoPoints(Point{x: 0.990722785714783, y: 0.990722785714783}, Point{x: 2.0, y: 2.0}) - assert.Equal(t, 0.0, actualVal) + assert.Equal(t, 1.4273335246362906, actualVal) } func TestGetCoordinatesOfMiddleBetweenTwoPointsByUtGoFuzzer(t *testing.T) { - actualVal0, actualVal1 := GetCoordinatesOfMiddleBetweenTwoPoints(Point{x: 0.24053641567148587, y: 0.24053641567148587}, Point{x: 0.24053641567148587, y: 0.24053641567148587}) + actualVal0, actualVal1 := GetCoordinatesOfMiddleBetweenTwoPoints(Point{x: 0.4872328470301428, y: 0.4872328470301428}, Point{x: 2.0, y: 2.0}) + + assertMultiple := assert.New(t) + assertMultiple.Equal(1.2436164235150713, actualVal0) + assertMultiple.Equal(1.2436164235150713, actualVal1) +} + +func TestGetCoordinateSumOfPointsByUtGoFuzzer1(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.7462414053223305, y: 0.7462414053223305}}) + + assertMultiple := assert.New(t) + assertMultiple.Equal(0.7462414053223305, actualVal0) + assertMultiple.Equal(0.7462414053223305, actualVal1) +} + +func TestGetCoordinateSumOfPointsByUtGoFuzzer2(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.7462414053223305, y: 0.0}, {x: 0.7462414053223305, y: 0.0}, {x: 0.7462414053223305, y: 2.8480945388892178e-306}, {x: 0.7462414053223305, y: 0.0}}) + + assertMultiple := assert.New(t) + assertMultiple.Equal(2.984965621289322, actualVal0) + assertMultiple.Equal(2.8480945388892178e-306, actualVal1) +} + +func TestGetCoordinateSumOfPointsByUtGoFuzzer3(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.7462414053223305, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}}) + + assertMultiple := assert.New(t) + assertMultiple.Equal(0.7462414053223305, actualVal0) + assertMultiple.Equal(1.492482810644661, actualVal1) +} + +func TestGetCoordinateSumOfPointsByUtGoFuzzer4(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.0, y: 0.0}, {x: 0.0, y: 0.0}, {x: 0.0, y: 0.0}}) + + assertMultiple := assert.New(t) + assertMultiple.Equal(0.0, actualVal0) + assertMultiple.Equal(0.0, actualVal1) +} + +func TestGetCoordinateSumOfPointsByUtGoFuzzer5(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.0, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}, {x: 4.7783097267364807e-299, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}, {x: 0.0, y: 0.7462414053223305}}) assertMultiple := assert.New(t) - assertMultiple.Equal(0.24053641567148587, actualVal0) - assertMultiple.Equal(0.24053641567148587, actualVal1) + assertMultiple.Equal(4.7783097267364807e-299, actualVal0) + assertMultiple.Equal(3.731207026611653, actualVal1) } -func TestGetCoordinateSumOfPointsByUtGoFuzzer(t *testing.T) { - actualVal0, actualVal1 := GetCoordinateSumOfPoints([10]Point{{x: 0.6374174253501083, y: 0.6374174253501083}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) +func TestGetCoordinateSumOfPointsByUtGoFuzzer6(t *testing.T) { + actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{}) assertMultiple := assert.New(t) - assertMultiple.Equal(0.6374174253501083, actualVal0) - assertMultiple.Equal(0.6374174253501083, actualVal1) + assertMultiple.Equal(0.0, actualVal0) + assertMultiple.Equal(0.0, actualVal1) } func TestGetAreaOfCircleByUtGoFuzzer(t *testing.T) { - actualVal := GetAreaOfCircle(Circle{Center: Point{x: 0.5504370051176339, y: 0.5504370051176339}, Radius: 0.5504370051176339}) + actualVal := GetAreaOfCircle(Circle{Center: Point{x: 0.7331520701949938, y: 0.7331520701949938}, Radius: 2.0}) - assert.Equal(t, 0.9518425589456255, actualVal) + assert.Equal(t, 12.566370614359172, actualVal) } func TestIsIdentityByUtGoFuzzer1(t *testing.T) { @@ -80,7 +120,7 @@ func TestIsIdentityByUtGoFuzzer1(t *testing.T) { } func TestIsIdentityByUtGoFuzzer2(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, -9223372036854775808}, {-9223372036854775808, 1, -9223372036854775808}, {9223372036854775807, -1, 9223372036854775805}}) + actualVal := IsIdentity([3][3]int{{1, 3, 0}, {0, 3, 0}, {0, -9223372036854775808, 2}}) assert.Equal(t, false, actualVal) } @@ -92,25 +132,25 @@ func TestIsIdentityByUtGoFuzzer3(t *testing.T) { } func TestIsIdentityByUtGoFuzzer4(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 288230376151711745, 513}, {1, 9223372036854775807, 9223372036854775807}, {1, 129, 32769}}) + actualVal := IsIdentity([3][3]int{{1, 0, 1}, {1, 0, 1}, {0, 1, 1}}) assert.Equal(t, false, actualVal) } func TestIsIdentityByUtGoFuzzer5(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {0, 0, 0}}) + actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 2048}, {0, 0, 0}}) assert.Equal(t, false, actualVal) } func TestIsIdentityByUtGoFuzzer6(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {1, 0, 0}, {0, 0, 0}}) + actualVal := IsIdentity([3][3]int{{1, 0, 0}, {2199023255552, 1, 2048}, {0, 0, 3}}) assert.Equal(t, false, actualVal) } func TestIsIdentityByUtGoFuzzer7(t *testing.T) { - actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {1, 0, 0}}) + actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {0, 0, 0}}) assert.Equal(t, false, actualVal) } @@ -121,40 +161,52 @@ func TestIsIdentityByUtGoFuzzer8(t *testing.T) { assert.Equal(t, false, actualVal) } +func TestIsIdentityByUtGoFuzzer9(t *testing.T) { + actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {18014398509481984, 1, 0}}) + + assert.Equal(t, false, actualVal) +} + +func TestIsIdentityByUtGoFuzzer10(t *testing.T) { + actualVal := IsIdentity([3][3]int{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}) + + assert.Equal(t, true, actualVal) +} + func TestBinaryWithNonNilErrorByUtGoFuzzer1(t *testing.T) { - actualVal, actualErr := Binary([10]int{-9223372036854775808, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 9223372036854775807, 9223372036854775807, -1) + actualVal, actualErr := Binary([]int{1}, 2, 0, -1) assertMultiple := assert.New(t) assertMultiple.Equal(-1, actualVal) assertMultiple.ErrorContains(actualErr, "target not found in array") } -func TestBinaryWithNonNilErrorByUtGoFuzzer2(t *testing.T) { - actualVal, actualErr := Binary([10]int{9223372036854775807, -9223372036854775808, 0, 0, 0, 0, 0, 0, 0, 0}, -1, 1, 1) +func TestBinaryByUtGoFuzzer2(t *testing.T) { + actualVal, actualErr := Binary([]int{9223372036854775807, -1, -1, -1, -1}, 9223372036854775807, 0, 1) assertMultiple := assert.New(t) - assertMultiple.Equal(-1, actualVal) - assertMultiple.ErrorContains(actualErr, "target not found in array") + assertMultiple.Equal(0, actualVal) + assertMultiple.Nil(actualErr) } func TestBinaryWithNonNilErrorByUtGoFuzzer3(t *testing.T) { - actualVal, actualErr := Binary([10]int{9223372036854775807, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -1, 0, 0) + actualVal, actualErr := Binary([]int{1, 17592186044417, 257, 1125899906842625}, -9223372036854775808, 1, 2) assertMultiple := assert.New(t) assertMultiple.Equal(-1, actualVal) assertMultiple.ErrorContains(actualErr, "target not found in array") } -func TestBinaryByUtGoFuzzer4(t *testing.T) { - actualVal, actualErr := Binary([10]int{9223372036854775807, 1, 9223372036854775807, -1, 0, 0, 0, 0, 0, 0}, 9223372036854775807, 0, 1) +func TestBinaryWithNonNilErrorByUtGoFuzzer4(t *testing.T) { + actualVal, actualErr := Binary([]int{-1, -1, -1, -1, 9223372036854775807}, 9223372036854775807, 0, 1) assertMultiple := assert.New(t) - assertMultiple.Equal(0, actualVal) - assertMultiple.Nil(actualErr) + assertMultiple.Equal(-1, actualVal) + assertMultiple.ErrorContains(actualErr, "target not found in array") } func TestBinaryPanicsByUtGoFuzzer(t *testing.T) { - assert.PanicsWithError(t, "runtime error: index out of range [-9223372036854775808]", func() { Binary([10]int{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -1, -9223372036854775808, 9223372036854775807) }) + assert.PanicsWithError(t, "runtime error: index out of range [-4611686018427387905]", func() { Binary([]int{1}, 2, -9223372036854775808, -1) }) } func TestStringSearchByUtGoFuzzer1(t *testing.T) { diff --git a/utbot-go/go-samples/simple/supported_types.go b/utbot-go/go-samples/simple/supported_types.go index db69c21e7b..3b308da3f1 100644 --- a/utbot-go/go-samples/simple/supported_types.go +++ b/utbot-go/go-samples/simple/supported_types.go @@ -162,6 +162,10 @@ func ArrayOfArrayOfStructs(array [5][5]Structure) [5][5]Structure { return array } +func ArrayOfSliceOfUint(array [5][]uint) [5][]uint { + return array +} + func returnErrorOrNil(n int) error { if n > 0 { return errors.New("error") @@ -177,3 +181,36 @@ func ExternalStruct(match difflib.Match, structure Structure) Structure { func ExternalStructWithAlias(match dif.Match) difflib.Match { return match } + +func SliceOfInt(slice []int) []int { + return slice +} + +func SliceOfUintPtr(slice []uintptr) []uintptr { + return slice +} + +func SliceOfString(slice []string) []string { + return slice +} + +func SliceOfStructs(slice []Structure) []Structure { + return slice +} + +func SliceOfStructsWithNan(slice []Structure) []Structure { + slice[0].float64 = math.NaN() + return slice +} + +func SliceOfSliceOfByte(slice [][]byte) [][]byte { + return slice +} + +func SliceOfSliceOfStructs(slice [][]Structure) [][]Structure { + return slice +} + +func SliceOfArrayOfInt(slice [][5]int) [][5]int { + return slice +} diff --git a/utbot-go/go-samples/simple/supported_types_go_ut_test.go b/utbot-go/go-samples/simple/supported_types_go_ut_test.go index c8068b7579..c3b3f3eaf3 100644 --- a/utbot-go/go-samples/simple/supported_types_go_ut_test.go +++ b/utbot-go/go-samples/simple/supported_types_go_ut_test.go @@ -12,9 +12,9 @@ func TestWithoutParametersAndReturnValuesByUtGoFuzzer(t *testing.T) { } func TestIntByUtGoFuzzer1(t *testing.T) { - actualVal := Int(9223372036854775807) + actualVal := Int(0) - assert.Equal(t, 9223372036854775807, actualVal) + assert.Equal(t, 0, actualVal) } func TestIntByUtGoFuzzer2(t *testing.T) { @@ -24,9 +24,9 @@ func TestIntByUtGoFuzzer2(t *testing.T) { } func TestInt8ByUtGoFuzzer1(t *testing.T) { - actualVal := Int8(127) + actualVal := Int8(0) - assert.Equal(t, int8(127), actualVal) + assert.Equal(t, int8(0), actualVal) } func TestInt8ByUtGoFuzzer2(t *testing.T) { @@ -36,9 +36,9 @@ func TestInt8ByUtGoFuzzer2(t *testing.T) { } func TestInt16ByUtGoFuzzer1(t *testing.T) { - actualVal := Int16(32767) + actualVal := Int16(0) - assert.Equal(t, int16(32767), actualVal) + assert.Equal(t, int16(0), actualVal) } func TestInt16ByUtGoFuzzer2(t *testing.T) { @@ -48,9 +48,9 @@ func TestInt16ByUtGoFuzzer2(t *testing.T) { } func TestInt32ByUtGoFuzzer1(t *testing.T) { - actualVal := Int32(2147483647) + actualVal := Int32(0) - assert.Equal(t, int32(2147483647), actualVal) + assert.Equal(t, int32(0), actualVal) } func TestInt32ByUtGoFuzzer2(t *testing.T) { @@ -60,9 +60,9 @@ func TestInt32ByUtGoFuzzer2(t *testing.T) { } func TestInt64ByUtGoFuzzer1(t *testing.T) { - actualVal := Int64(9223372036854775807) + actualVal := Int64(0) - assert.Equal(t, int64(9223372036854775807), actualVal) + assert.Equal(t, int64(0), actualVal) } func TestInt64ByUtGoFuzzer2(t *testing.T) { @@ -108,39 +108,39 @@ func TestUintPtrByUtGoFuzzer(t *testing.T) { } func TestFloat32ByUtGoFuzzer(t *testing.T) { - actualVal := Float32(0.59754527) + actualVal := Float32(0.6063452) - assert.Equal(t, float32(0.59754527), actualVal) + assert.Equal(t, float32(0.6063452), actualVal) } func TestFloat64ByUtGoFuzzer(t *testing.T) { - actualVal := Float64(0.7815346320453048) + actualVal := Float64(0.6374174253501083) - assert.Equal(t, 0.7815346320453048, actualVal) + assert.Equal(t, 0.6374174253501083, actualVal) } func TestComplex64ByUtGoFuzzer(t *testing.T) { - actualVal := Complex64(complex(0.25277615, 0.25277615)) + actualVal := Complex64(complex(0.550437, 0.550437)) - assert.Equal(t, complex(float32(0.25277615), float32(0.25277615)), actualVal) + assert.Equal(t, complex(float32(0.550437), float32(0.550437)), actualVal) } func TestComplex128ByUtGoFuzzer(t *testing.T) { - actualVal := Complex128(complex(0.3851891847407185, 0.3851891847407185)) + actualVal := Complex128(complex(0.11700660880722513, 0.11700660880722513)) - assert.Equal(t, complex(0.3851891847407185, 0.3851891847407185), actualVal) + assert.Equal(t, complex(0.11700660880722513, 0.11700660880722513), actualVal) } func TestByteByUtGoFuzzer(t *testing.T) { actualVal := Byte(0) - assert.Equal(t, byte(0), actualVal) + assert.Equal(t, uint8(0), actualVal) } func TestRuneByUtGoFuzzer(t *testing.T) { actualVal := Rune(2147483647) - assert.Equal(t, rune(2147483647), actualVal) + assert.Equal(t, int32(2147483647), actualVal) } func TestStringByUtGoFuzzer(t *testing.T) { @@ -156,15 +156,15 @@ func TestBoolByUtGoFuzzer(t *testing.T) { } func TestStructByUtGoFuzzer(t *testing.T) { - actualVal := Struct(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.9848415, float64: 0.9828195255872982, complex64: complex(0.9848415, 0.9848415), complex128: complex(0.9828195255872982, 0.9828195255872982), byte: 0, rune: -1, string: "", bool: false}) + actualVal := Struct(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.7815346, float64: 0.3332183994766498, complex64: complex(0.7815346, 0.7815346), complex128: complex(0.3332183994766498, 0.3332183994766498), byte: 0, rune: -1, string: "", bool: false}) - assert.Equal(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.9848415, float64: 0.9828195255872982, complex64: complex(float32(0.9848415), float32(0.9848415)), complex128: complex(0.9828195255872982, 0.9828195255872982), byte: 0, rune: -1, string: "", bool: false}, actualVal) + assert.Equal(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.7815346, float64: 0.3332183994766498, complex64: complex(float32(0.7815346), float32(0.7815346)), complex128: complex(0.3332183994766498, 0.3332183994766498), byte: 0, rune: -1, string: "", bool: false}, actualVal) } func TestStructWithNanByUtGoFuzzer(t *testing.T) { - actualVal := StructWithNan(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.02308184, float64: 0.9412491794821144, complex64: complex(0.02308184, 0.02308184), complex128: complex(0.9412491794821144, 0.9412491794821144), byte: 0, rune: -1, string: "", bool: false}) + actualVal := StructWithNan(Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.38518918, float64: 0.6130357680446138, complex64: complex(0.38518918, 0.38518918), complex128: complex(0.6130357680446138, 0.6130357680446138), byte: 0, rune: -1, string: "", bool: false}) - assert.NotEqual(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.02308184, float64: math.NaN(), complex64: complex(float32(0.02308184), float32(0.02308184)), complex128: complex(0.9412491794821144, 0.9412491794821144), byte: 0, rune: -1, string: "", bool: false}, actualVal) + assert.NotEqual(t, Structure{int: -1, int8: 1, int16: 32767, int32: -1, int64: -1, uint: 18446744073709551615, uint8: 0, uint16: 1, uint32: 0, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.38518918, float64: math.NaN(), complex64: complex(float32(0.38518918), float32(0.38518918)), complex128: complex(0.6130357680446138, 0.6130357680446138), byte: 0, rune: -1, string: "", bool: false}, actualVal) } func TestArrayOfIntByUtGoFuzzer(t *testing.T) { @@ -186,15 +186,15 @@ func TestArrayOfStringByUtGoFuzzer(t *testing.T) { } func TestArrayOfStructsByUtGoFuzzer(t *testing.T) { - actualVal := ArrayOfStructs([10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.27495396, float64: 0.31293596519376554, complex64: complex(0.27495396, 0.27495396), complex128: complex(0.31293596519376554, 0.31293596519376554), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) + actualVal := ArrayOfStructs([10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.9828195, float64: 0.8791825178724801, complex64: complex(0.9828195, 0.9828195), complex128: complex(0.8791825178724801, 0.8791825178724801), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) - assert.Equal(t, [10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.27495396, float64: 0.31293596519376554, complex64: complex(float32(0.27495396), float32(0.27495396)), complex128: complex(0.31293596519376554, 0.31293596519376554), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) + assert.Equal(t, [10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.9828195, float64: 0.8791825178724801, complex64: complex(float32(0.9828195), float32(0.9828195)), complex128: complex(0.8791825178724801, 0.8791825178724801), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) } func TestArrayOfStructsWithNanByUtGoFuzzer(t *testing.T) { - actualVal := ArrayOfStructsWithNan([10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.3679757, float64: 0.14660165764651822, complex64: complex(0.3679757, 0.3679757), complex128: complex(0.14660165764651822, 0.14660165764651822), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) + actualVal := ArrayOfStructsWithNan([10]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.94124913, float64: 0.17597680203548016, complex64: complex(0.94124913, 0.94124913), complex128: complex(0.17597680203548016, 0.17597680203548016), byte: 255, rune: 2147483647, string: "", bool: false}, {}, {}, {}, {}, {}, {}, {}, {}, {}}) - assert.NotEqual(t, [10]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.3679757, float64: math.NaN(), complex64: complex(float32(0.3679757), float32(0.3679757)), complex128: complex(0.14660165764651822, 0.14660165764651822), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) + assert.NotEqual(t, [10]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.94124913, float64: math.NaN(), complex64: complex(float32(0.94124913), float32(0.94124913)), complex128: complex(0.17597680203548016, 0.17597680203548016), byte: 255, rune: 2147483647, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, actualVal) } func TestArrayOfArrayOfUintByUtGoFuzzer(t *testing.T) { @@ -209,26 +209,84 @@ func TestArrayOfArrayOfStructsByUtGoFuzzer(t *testing.T) { assert.Equal(t, [5][5]Structure{{{int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, {{int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, {{int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, {{int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}, {{int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}, {int: 0, int8: 0, int16: 0, int32: 0, int64: 0, uint: 0, uint8: 0, uint16: 0, uint32: 0, uint64: 0, uintptr: 0, float32: 0.0, float64: 0.0, complex64: complex(float32(0.0), float32(0.0)), complex128: complex(0.0, 0.0), byte: 0, rune: 0, string: "", bool: false}}}, actualVal) } -func TestReturnErrorOrNilWithNonNilErrorByUtGoFuzzer1(t *testing.T) { - actualErr := returnErrorOrNil(9223372036854775807) +func TestArrayOfSliceOfUintByUtGoFuzzer(t *testing.T) { + actualVal := ArrayOfSliceOfUint([5][]uint{{}, {}, {}, {}, {}}) - assert.ErrorContains(t, actualErr, "error") + assert.Equal(t, [5][]uint{{}, {}, {}, {}, {}}, actualVal) } -func TestReturnErrorOrNilByUtGoFuzzer2(t *testing.T) { +func TestReturnErrorOrNilByUtGoFuzzer1(t *testing.T) { actualErr := returnErrorOrNil(0) assert.Nil(t, actualErr) } +func TestReturnErrorOrNilWithNonNilErrorByUtGoFuzzer2(t *testing.T) { + actualErr := returnErrorOrNil(8) + + assert.ErrorContains(t, actualErr, "error") +} + func TestExternalStructByUtGoFuzzer(t *testing.T) { - actualVal := ExternalStruct(difflib.Match{A: 9223372036854775807, B: -1, Size: -9223372036854775808}, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.009224832, float64: 0.9644868606768501, complex64: complex(0.009224832, 0.009224832), complex128: complex(0.9644868606768501, 0.9644868606768501), byte: 1, rune: 0, string: "", bool: false}) + actualVal := ExternalStruct(difflib.Match{A: 9223372036854775807, B: -1, Size: -9223372036854775808}, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.14660162, float64: 0.7051747444754559, complex64: complex(0.14660162, 0.14660162), complex128: complex(0.7051747444754559, 0.7051747444754559), byte: 1, rune: 0, string: "", bool: false}) - assert.Equal(t, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.009224832, float64: 0.9644868606768501, complex64: complex(float32(0.009224832), float32(0.009224832)), complex128: complex(0.9644868606768501, 0.9644868606768501), byte: 1, rune: 0, string: "", bool: false}, actualVal) + assert.Equal(t, Structure{int: -1, int8: 1, int16: -32768, int32: 2147483647, int64: 1, uint: 1, uint8: 1, uint16: 1, uint32: 1, uint64: 18446744073709551615, uintptr: 18446744073709551615, float32: 0.14660162, float64: 0.7051747444754559, complex64: complex(float32(0.14660162), float32(0.14660162)), complex128: complex(0.7051747444754559, 0.7051747444754559), byte: 1, rune: 0, string: "", bool: false}, actualVal) } func TestExternalStructWithAliasByUtGoFuzzer(t *testing.T) { actualVal := ExternalStructWithAlias(difflib.Match{A: 9223372036854775807, B: -1, Size: -9223372036854775808}) assert.Equal(t, difflib.Match{A: 9223372036854775807, B: -1, Size: -9223372036854775808}, actualVal) -} \ No newline at end of file +} + +func TestSliceOfIntByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfInt([]int{-1}) + + assert.Equal(t, []int{-1}, actualVal) +} + +func TestSliceOfUintPtrByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfUintPtr([]uintptr{0}) + + assert.Equal(t, []uintptr{0}, actualVal) +} + +func TestSliceOfStringByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfString([]string{"hello"}) + + assert.Equal(t, []string{"hello"}, actualVal) +} + +func TestSliceOfStructsByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfStructs([]Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.0013866425, float64: 0.5467397571984656, complex64: complex(0.0013866425, 0.0013866425), complex128: complex(0.5467397571984656, 0.5467397571984656), byte: 255, rune: 2147483647, string: "", bool: false}}) + + assert.Equal(t, []Structure{{int: -1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.0013866425, float64: 0.5467397571984656, complex64: complex(float32(0.0013866425), float32(0.0013866425)), complex128: complex(0.5467397571984656, 0.5467397571984656), byte: 255, rune: 2147483647, string: "", bool: false}}, actualVal) +} + +func TestSliceOfStructsWithNanByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfStructsWithNan([]Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.96448684, float64: 0.5629496738983792, complex64: complex(0.96448684, 0.96448684), complex128: complex(0.5629496738983792, 0.5629496738983792), byte: 255, rune: 2147483647, string: "", bool: false}}) + + assert.NotEqual(t, []Structure{{int: 1, int8: 0, int16: -1, int32: -1, int64: -9223372036854775808, uint: 1, uint8: 0, uint16: 65535, uint32: 1, uint64: 18446744073709551615, uintptr: 1, float32: 0.96448684, float64: math.NaN(), complex64: complex(float32(0.96448684), float32(0.96448684)), complex128: complex(0.5629496738983792, 0.5629496738983792), byte: 255, rune: 2147483647, string: "", bool: false}}, actualVal) +} + +func TestSliceOfStructsWithNanPanicsByUtGoFuzzer(t *testing.T) { + assert.PanicsWithError(t, "runtime error: index out of range [0] with length 0", func() { SliceOfStructsWithNan([]Structure{}) }) +} + +func TestSliceOfSliceOfByteByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfSliceOfByte([][]byte{{}}) + + assert.Equal(t, [][]byte{{}}, actualVal) +} + +func TestSliceOfSliceOfStructsByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfSliceOfStructs([][]Structure{{}}) + + assert.Equal(t, [][]Structure{{}}, actualVal) +} + +func TestSliceOfArrayOfIntByUtGoFuzzer(t *testing.T) { + actualVal := SliceOfArrayOfInt([][5]int{{0, 0, 0, 0, 0}}) + + assert.Equal(t, [][5]int{{0, 0, 0, 0, 0}}, actualVal) +} From 791c7a06cfee237a84638248179d3bd725a2ebca Mon Sep 17 00:00:00 2001 From: Egipti Pavel Date: Tue, 28 Feb 2023 12:08:15 +0300 Subject: [PATCH 5/5] Update Go README --- utbot-go/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utbot-go/README.md b/utbot-go/README.md index 3faca20f00..4ce416e740 100644 --- a/utbot-go/README.md +++ b/utbot-go/README.md @@ -15,7 +15,7 @@ inserts these values into the user functions, and executes the resulting test ca ### Supported types for function parameters -At the moment, UnitTestBot Go is able to generate values for _primitive types_, _arrays_ and _structs_. +At the moment, UnitTestBot Go is able to generate values for _primitive types_, _arrays_, _slices_ and _structs_. For _floating point types_, UnitTestBot Go supports working with _infinity_ and _NaN_.