Skip to content

Commit

Permalink
Merge #55566
Browse files Browse the repository at this point in the history
55566: Add missing unit test for LineLocatePoint function r=otan a=C0rWin

This commit returns unit tests for LineLocatePoint which
got accidentally overwritten at #55470 merge.

Signed-off-by: Artem Barger <[email protected]>

Release note: none

Co-authored-by: Artem Barger <[email protected]>
  • Loading branch information
craig[bot] and C0rWin committed Oct 15, 2020
2 parents d8a4d5f + 0373959 commit 38e2c1f
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pkg/geo/geomfn/linestring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package geomfn

import (
"fmt"
"math"
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
Expand Down Expand Up @@ -121,6 +122,72 @@ func TestLineMerge(t *testing.T) {
}
}

func TestLineLocatePoint(t *testing.T) {
testCases := []struct {
lineString *geom.LineString
point *geom.Point
expected float64
}{
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{0, 1, 1, 0}),
point: geom.NewPointFlat(geom.XY, []float64{0, 0}),
expected: 0.5,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{0, 1, 1, 0}),
point: geom.NewPointFlat(geom.XY, []float64{1, 1}),
expected: 0.5,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{-1, -1, -1, 1}),
point: geom.NewPointFlat(geom.XY, []float64{-1, 0}),
expected: 0.5,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{1, -1, -1, 1}),
point: geom.NewPointFlat(geom.XY, []float64{-1, 0}),
expected: 0.75,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{-1, 1, 1, -1}),
point: geom.NewPointFlat(geom.XY, []float64{-1, 0}),
expected: 0.25,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{0, 6, 3, 0}),
point: geom.NewPointFlat(geom.XY, []float64{0, 0}),
expected: 0.8,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{6, 6, 3, 0}),
point: geom.NewPointFlat(geom.XY, []float64{1, 1}),
expected: 1,
},
{
lineString: geom.NewLineStringFlat(geom.XY, []float64{6, 6, 3, 0}),
point: geom.NewPointFlat(geom.XY, []float64{3, 1}),
expected: 0.87,
},
}

for index, tc := range testCases {
t.Run(fmt.Sprintf("%d", index), func(t *testing.T) {
line, err := geo.MakeGeometryFromGeomT(tc.lineString)
require.NoError(t, err)

p, err := geo.MakeGeometryFromGeomT(tc.point)
require.NoError(t, err)

fraction, err := LineLocatePoint(line, p)
require.NoError(t, err)

fraction = math.Round(fraction*100) / 100

require.Equal(t, tc.expected, fraction)
})
}
}

func TestAddPoint(t *testing.T) {
testCases := []struct {
lineString *geom.LineString
Expand Down

0 comments on commit 38e2c1f

Please sign in to comment.