Skip to content

Commit

Permalink
Add missing unit test for LineLocatePoint function
Browse files Browse the repository at this point in the history
This commit returns unit tests for LineLocatePoint which
got accidentally overwritten at #55470 merge.

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

Release note: none
  • Loading branch information
C0rWin committed Oct 14, 2020
1 parent b6efb2b commit 0373959
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 0373959

Please sign in to comment.