Skip to content

Go. Support for all user-defined types #2056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions utbot-go/go-samples/simple/samples_go_ut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ func TestGenerateArrayOfIntegersByUtGoFuzzer(t *testing.T) {
}

func TestDistanceBetweenTwoPointsByUtGoFuzzer(t *testing.T) {
actualVal := DistanceBetweenTwoPoints(Point{x: 0.730967787376657, y: 0.730967787376657}, Point{x: 2.0, y: 2.0})
actualVal := DistanceBetweenTwoPoints(Point{}, Point{})

assert.Equal(t, 1.794682566180269, actualVal)
assert.Equal(t, 0.0, actualVal)
}

func TestGetCoordinatesOfMiddleBetweenTwoPointsByUtGoFuzzer(t *testing.T) {
actualVal0, actualVal1 := GetCoordinatesOfMiddleBetweenTwoPoints(Point{x: 0.24053641567148587, y: 0.24053641567148587}, Point{x: 2.0, y: 2.0})
actualVal0, actualVal1 := GetCoordinatesOfMiddleBetweenTwoPoints(Point{}, Point{})

assertMultiple := assert.New(t)
assertMultiple.Equal(1.1202682078357429, actualVal0)
assertMultiple.Equal(1.1202682078357429, actualVal1)
assertMultiple.Equal(0.0, actualVal0)
assertMultiple.Equal(0.0, actualVal1)
}

func TestGetCoordinateSumOfPointsByUtGoFuzzer(t *testing.T) {
actualVal0, actualVal1 := GetCoordinateSumOfPoints([]Point{{x: 0.6374174253501083, y: 0.6374174253501083}})
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: 2.0})
actualVal := GetAreaOfCircle(Circle{Center: Point{}, Radius: 2.0})

assert.Equal(t, 12.566370614359172, actualVal)
}
Expand Down Expand Up @@ -155,4 +155,4 @@ func TestStringSearchByUtGoFuzzer5(t *testing.T) {
actualVal := StringSearch("ABC")

assert.Equal(t, true, actualVal)
}
}
43 changes: 43 additions & 0 deletions utbot-go/go-samples/simple/supported_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,46 @@ func SliceOfArrayOfInt(slice [][5]int) [][5]int {
func ExportedStructWithEmbeddedUnexportedStruct(exportedStruct nested.ExportedStruct) nested.ExportedStruct {
return exportedStruct
}

type Type byte

func NamedType(n Type) Type {
return n
}

func ArrayOfNamedType(array [5]Type) [5]Type {
return array
}

type T [5][5]Type

func ArrayOfArrayOfNamedType(array [5][5]Type) T {
return array
}

func SliceOfNamedType(slice []Type) []Type {
return slice
}

type NA [5]uintptr

func NamedArray(array NA) NA {
return array
}

type NS []int

func NamedSlice(slice NS) NS {
return slice
}

type S struct {
t Type
T
n NA
NS
}

func StructWithFieldsOfNamedTypes(s S) S {
return s
}
93 changes: 67 additions & 26 deletions utbot-go/go-samples/simple/supported_types_go_ut_test.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion utbot-go/src/main/kotlin/org/utbot/go/GoLanguage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ fun goDefaultValueProviders() = listOf(
GoArrayValueProvider,
GoSliceValueProvider,
GoStructValueProvider,
GoConstantValueProvider
GoConstantValueProvider,
GoNamedValueProvider,
GoNilValueProvider
)

class GoInstruction(
Expand Down
65 changes: 33 additions & 32 deletions utbot-go/src/main/kotlin/org/utbot/go/api/GoTypesApi.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.utbot.go.api

import org.utbot.go.framework.api.go.GoFieldId
import org.utbot.go.framework.api.go.GoNamedTypeId
import org.utbot.go.framework.api.go.GoPackage
import org.utbot.go.framework.api.go.GoTypeId

Expand Down Expand Up @@ -29,37 +28,21 @@ class GoPrimitiveTypeId(name: String) : GoTypeId(name) {

class GoStructTypeId(
name: String,
implementsError: Boolean,
override val sourcePackage: GoPackage,
val fields: List<GoFieldId>,
) : GoNamedTypeId(name, implementsError = implementsError) {
val packageName: String = sourcePackage.packageName
val packagePath: String = sourcePackage.packagePath
override val canonicalName: String = "${sourcePackage.packageName}.$name"
) : GoTypeId(name) {
override val canonicalName: String = name

override fun getRelativeName(destinationPackage: GoPackage, aliases: Map<GoPackage, String?>): String {
val alias = aliases[sourcePackage]
return if (sourcePackage == destinationPackage || alias == ".") {
simpleName
} else if (alias == null) {
"${packageName}.${simpleName}"
} else {
"${alias}.${simpleName}"
}
}
override fun getRelativeName(destinationPackage: GoPackage, aliases: Map<GoPackage, String?>): String = simpleName

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GoStructTypeId) return false

return packagePath == other.packagePath && packageName == other.packageName && name == other.name
return fields == other.fields
}

override fun hashCode(): Int {
var result = packagePath.hashCode()
result = 31 * result + packageName.hashCode()
result = 31 * result + name.hashCode()
return result
return fields.hashCode()
}
}

Expand Down Expand Up @@ -91,30 +74,48 @@ class GoSliceTypeId(

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GoArrayTypeId) return false
if (other !is GoSliceTypeId) return false

return elementTypeId == other.elementTypeId
}

override fun hashCode(): Int = elementTypeId.hashCode()
}

class GoInterfaceTypeId(
class GoInterfaceTypeId(name: String) : GoTypeId(name) {
override val canonicalName: String = name

override fun getRelativeName(destinationPackage: GoPackage, aliases: Map<GoPackage, String?>): String = simpleName

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GoInterfaceTypeId) return false

return name == other.name
}

override fun hashCode(): Int = name.hashCode()
}

class GoNamedTypeId(
name: String,
implementsError: Boolean,
override val sourcePackage: GoPackage,
) : GoNamedTypeId(name, implementsError = implementsError) {
implementsError: Boolean,
val underlyingTypeId: GoTypeId
) : GoTypeId(name, implementsError = implementsError) {
val packageName: String = sourcePackage.packageName
val packagePath: String = sourcePackage.packagePath
override val canonicalName: String = if (packageName != "") {
"$packageName.$name"
override val canonicalName: String = if (sourcePackage.isBuiltin) {
name
} else {
simpleName
"${sourcePackage.packageName}.$name"
}

fun exported(): Boolean = name.first().isUpperCase()

override fun getRelativeName(destinationPackage: GoPackage, aliases: Map<GoPackage, String?>): String {
val alias = aliases[sourcePackage]
return if (sourcePackage == destinationPackage || alias == ".") {
return if (sourcePackage.isBuiltin || sourcePackage == destinationPackage || alias == ".") {
simpleName
} else if (alias == null) {
"${packageName}.${simpleName}"
Expand All @@ -125,9 +126,9 @@ class GoInterfaceTypeId(

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GoInterfaceTypeId) return false
if (other !is GoNamedTypeId) return false

return packagePath == other.packagePath && packageName == other.packageName && name == other.name
return sourcePackage == other.sourcePackage && name == other.name
}

override fun hashCode(): Int {
Expand Down
59 changes: 44 additions & 15 deletions utbot-go/src/main/kotlin/org/utbot/go/api/GoUtModelsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GoUtStructModel(
value.filter { typeId.sourcePackage == destinationPackage || it.fieldId.isExported }

override fun getRequiredPackages(destinationPackage: GoPackage): Set<GoPackage> =
getVisibleFields(destinationPackage).fold(setOf(typeId.sourcePackage)) { acc, fieldModel ->
getVisibleFields(destinationPackage).fold(emptySet()) { acc, fieldModel ->
acc + fieldModel.getRequiredPackages(destinationPackage)
}

Expand All @@ -68,14 +68,19 @@ class GoUtArrayModel(
get() = super.typeId as GoArrayTypeId

override fun getRequiredPackages(destinationPackage: GoPackage): Set<GoPackage> {
val elementStructTypeId = typeId.elementTypeId as? GoStructTypeId
val imports = if (elementStructTypeId != null && elementStructTypeId.sourcePackage != destinationPackage) {
mutableSetOf(elementStructTypeId.sourcePackage)
} else {
mutableSetOf()
val elementNamedTypeId = typeId.elementTypeId as? GoNamedTypeId
val imports =
if (elementNamedTypeId != null &&
!elementNamedTypeId.sourcePackage.isBuiltin &&
elementNamedTypeId.sourcePackage != destinationPackage
) {
setOf(elementNamedTypeId.sourcePackage)
} else {
emptySet()
}
return value.values.fold(imports) { acc, model ->
acc + model.getRequiredPackages(destinationPackage)
}
value.values.map { it.getRequiredPackages(destinationPackage) }.forEach { imports += it }
return imports
}

override fun isComparable(): Boolean = value.values.all { it.isComparable() }
Expand All @@ -98,14 +103,19 @@ class GoUtSliceModel(
get() = super.typeId as GoSliceTypeId

override fun getRequiredPackages(destinationPackage: GoPackage): Set<GoPackage> {
val elementStructTypeId = typeId.elementTypeId as? GoStructTypeId
val imports = if (elementStructTypeId != null && elementStructTypeId.sourcePackage != destinationPackage) {
mutableSetOf(elementStructTypeId.sourcePackage)
} else {
mutableSetOf()
val elementNamedTypeId = typeId.elementTypeId as? GoNamedTypeId
val imports =
if (elementNamedTypeId != null &&
!elementNamedTypeId.sourcePackage.isBuiltin &&
elementNamedTypeId.sourcePackage != destinationPackage
) {
setOf(elementNamedTypeId.sourcePackage)
} else {
emptySet()
}
return value.values.fold(imports) { acc, model ->
acc + model.getRequiredPackages(destinationPackage)
}
value.values.map { it.getRequiredPackages(destinationPackage) }.forEach { imports += it }
return imports
}

override fun isComparable(): Boolean = value.values.all { it.isComparable() }
Expand Down Expand Up @@ -167,4 +177,23 @@ class GoUtNilModel(
) : GoUtModel(typeId) {
override fun isComparable(): Boolean = true
override fun toString() = "nil"
}

class GoUtNamedModel(
var value: GoUtModel,
typeId: GoNamedTypeId,
) : GoUtModel(typeId) {
override val typeId: GoNamedTypeId
get() = super.typeId as GoNamedTypeId

override fun getRequiredPackages(destinationPackage: GoPackage): Set<GoPackage> {
val import = if (!typeId.sourcePackage.isBuiltin && typeId.sourcePackage != destinationPackage) {
setOf(typeId.sourcePackage)
} else {
emptySet()
}
return import + value.getRequiredPackages(destinationPackage)
}

override fun isComparable(): Boolean = value.isComparable()
}
25 changes: 14 additions & 11 deletions utbot-go/src/main/kotlin/org/utbot/go/api/util/GoTypesApiUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,32 @@ fun GoTypeId.goDefaultValueModel(): GoUtModel = when (this) {
goStringTypeId -> GoUtPrimitiveModel("", this)
goUintPtrTypeId -> GoUtPrimitiveModel(0, this)

else -> error("Go primitive ${this.javaClass} is not supported")
else -> error("Generating Go default value model for ${this.javaClass} is not supported")
}

is GoStructTypeId -> GoUtStructModel(listOf(), this)
is GoArrayTypeId -> GoUtArrayModel(hashMapOf(), this)
is GoSliceTypeId -> GoUtSliceModel(hashMapOf(), this, 0)
else -> GoUtNilModel(this)
is GoSliceTypeId -> GoUtNilModel(this)
is GoNamedTypeId -> GoUtNamedModel(this.underlyingTypeId.goDefaultValueModel(), this)
else -> error("Generating Go default value model for ${this.javaClass} is not supported")
}

fun GoTypeId.getAllVisibleStructTypes(goPackage: GoPackage): Set<GoStructTypeId> = when (this) {
is GoStructTypeId -> if (this.sourcePackage == goPackage || this.exported()) {
fields.fold(setOf(this)) { acc: Set<GoStructTypeId>, field ->
acc + (field.declaringType).getAllVisibleStructTypes(goPackage)
}
fun GoTypeId.getAllVisibleNamedTypes(goPackage: GoPackage): Set<GoNamedTypeId> = when (this) {
is GoStructTypeId -> fields.fold(emptySet()) { acc: Set<GoNamedTypeId>, field ->
acc + (field.declaringType).getAllVisibleNamedTypes(goPackage)
}

is GoArrayTypeId, is GoSliceTypeId -> elementTypeId!!.getAllVisibleNamedTypes(goPackage)
is GoNamedTypeId -> if (this.sourcePackage == goPackage || this.exported()) {
setOf(this) + underlyingTypeId.getAllVisibleNamedTypes(goPackage)
} else {
emptySet()
}

is GoArrayTypeId, is GoSliceTypeId -> elementTypeId!!.getAllVisibleStructTypes(goPackage)
else -> emptySet()
}

fun List<GoTypeId>.getAllVisibleStructTypes(goPackage: GoPackage): Set<GoStructTypeId> =
fun List<GoTypeId>.getAllVisibleNamedTypes(goPackage: GoPackage): Set<GoNamedTypeId> =
this.fold(emptySet()) { acc, type ->
acc + type.getAllVisibleStructTypes(goPackage)
acc + type.getAllVisibleNamedTypes(goPackage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ fun GoUtModel.convertToRawValue(destinationPackage: GoPackage, aliases: Map<GoPa
"${model.realValue}@${model.imagValue}"
)

is GoUtNamedModel -> NamedValue(
model.typeId.getRelativeName(destinationPackage, aliases),
model.value.convertToRawValue(destinationPackage, aliases)
)

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),
Expand All @@ -49,6 +53,6 @@ fun GoUtModel.convertToRawValue(destinationPackage: GoPackage, aliases: Map<GoPa
)

is GoUtPrimitiveModel -> PrimitiveValue(model.typeId.name, model.value.toString())

is GoUtNilModel -> NilValue(model.typeId.getRelativeName(destinationPackage, aliases))
else -> error("Converting ${model.javaClass} to RawValue is not supported")
}
Loading