Skip to content

Commit

Permalink
bindings/go: Add tests for new Range / Position APIs
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph Hegemann <[email protected]>
  • Loading branch information
varungandhi-src and kritzcreek committed Jun 11, 2024
1 parent 81b222f commit c89a34f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
156 changes: 156 additions & 0 deletions bindings/go/scip/position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package scip

import (
"testing"

"github.com/stretchr/testify/require"
"pgregory.net/rapid"
)

// TODO: Replace with cmp.Compare go 1.21 or newer.
func compare(i1 int, i2 int) int {
if i1 < i2 {
return -1
}
if i1 > i2 {
return 1
}
return 0
}

func TestUnitComparePosition(t *testing.T) {
positions := []Position{
{0, 0},
{0, 1},
{1, 0},
{1, 1},
{1, 2},
{2, 1},
{2, 2},
}

type testCase struct {
p1 Position
p2 Position
expected int
}

testCases := make([]testCase, len(positions)*len(positions))

// There is a total ordering on these positions, so we just test all possible combinations
for i1, pos1 := range positions {
for i2, pos2 := range positions {
testCases = append(testCases, testCase{pos1, pos2, compare(i1, i2)})
}
}

for _, testCase := range testCases {
if actual := testCase.p1.Compare(testCase.p2); actual != testCase.expected {
t.Errorf("unexpected result. %+v.Compare(%+v) want=%d have=%d", testCase.p1, testCase.p2, testCase.expected, actual)
}
}
}

func TestUnitIntersect(t *testing.T) {
r1 := Range{Position{0, 0}, Position{0, 10}}
r2 := Range{Position{0, 5}, Position{1, 5}}
r3 := Range{Position{1, 0}, Position{1, 10}}

require.Truef(t, r1.Intersects(r2), "%+v.Intersects(%+v)", r1, r2)
require.Truef(t, r2.Intersects(r3), "%+v.Intersects(%+v)", r2, r3)
require.Falsef(t, r1.Intersects(r3), "%+v.Intersects(%+v)", r1, r3)
}

func genPosition() *rapid.Generator[Position] {
return rapid.Custom(func(t *rapid.T) Position {
return Position{
Line: rapid.Int32Range(0, 10).Draw(t, "Line"),
Character: rapid.Int32Range(0, 10).Draw(t, "Character"),
}
})
}

func genRange() *rapid.Generator[Range] {
return rapid.Custom(func(t *rapid.T) Range {
posGen := genPosition()
start := posGen.Draw(t, "start")
end := posGen.Draw(t, "end")
// A well formed range is always start <= end.
if start.Compare(end) > 0 {
start, end = end, start
}
return Range{
Start: start,
End: end,
}
})
}

func TestComparePositionTransitive(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
p1 := genPosition().Draw(t, "p1")
p2 := genPosition().Draw(t, "p2")
p3 := genPosition().Draw(t, "p3")

if p1.Compare(p2) < 0 && p2.Compare(p3) < 0 {
if !(p1.Compare(p3) < 0) {
t.Errorf("%+v < %+v < %+v but !(%+v < %+v)", p1, p2, p3, p1, p3)
}
}
})
}

func TestIntersects(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
r1 := genRange().Draw(t, "r1")
r2 := genRange().Draw(t, "r2")

if r1.Intersects(r2) {
if !(r1.Contains(r2.Start) || r2.Contains(r1.Start)) {
t.Errorf("%+v overlaps with %+v but neither contains the others start position", r1, r2)
}
}
})
}

func TestLessCompareConsistent(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
r1 := genRange().Draw(t, "r1")
r2 := genRange().Draw(t, "r2")
if r1.Compare(r2) < 0 {
if !(r1.Less(r2)) {
t.Errorf("%+v.Compare(%+v) < 0 but !(%+v < %+v)", r1, r2, r1, r2)
}
} else {
if r1.Less(r2) {
t.Errorf("%+v.Compare(%+v) >= 0 but (%+v < %+v)", r1, r2, r1, r2)
}
}
})

rapid.Check(t, func(t *rapid.T) {
r1 := genRange().Draw(t, "r1")
r2 := genRange().Draw(t, "r2")
if r1.CompareStrict(r2) < 0 {
if !(r1.LessStrict(r2)) {
t.Errorf("%+v.CompareStrict(%+v) < 0 but !(%+v < %+v)", r1, r2, r1, r2)
}
} else {
if r1.LessStrict(r2) {
t.Errorf("%+v.CompareStrict(%+v) >= 0 but (%+v < %+v)", r1, r2, r1, r2)
}
}
})
}

func TestCompareReverse(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
r1 := genRange().Draw(t, "r1")
r2 := genRange().Draw(t, "r2")
cmp1 := r1.Compare(r2)
cmp2 := r2.Compare(r1)
if cmp1 != -cmp2 {
t.Errorf("%+v.Compare(%+v) = %d but %+v.Compare(%+v) = %d", r1, r2, cmp1, r2, r1, cmp2)
}
})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/urfave/cli/v2 v2.25.7
golang.org/x/tools v0.12.0
google.golang.org/protobuf v1.31.0
pgregory.net/rapid v1.1.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,5 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
mvdan.cc/gofumpt v0.4.0/go.mod h1:PljLOHDeZqgS8opHRKLzp2It2VBuSdteAgqUfzMTxlQ=
mvdan.cc/gofumpt v0.5.0 h1:0EQ+Z56k8tXjj/6TQD25BFNKQXpCvT0rnansIc7Ug5E=
mvdan.cc/gofumpt v0.5.0/go.mod h1:HBeVDtMKRZpXyxFciAirzdKklDlGu8aAy1wEbH5Y9js=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=

0 comments on commit c89a34f

Please sign in to comment.