Skip to content

UnitTestBot Go. Slices support #1868

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 5 commits into from
Mar 1, 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
2 changes: 1 addition & 1 deletion utbot-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_.

Expand Down
4 changes: 2 additions & 2 deletions utbot-go/go-samples/simple/samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
128 changes: 90 additions & 38 deletions utbot-go/go-samples/simple/samples_go_ut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions utbot-go/go-samples/simple/supported_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Loading