Skip to content

Commit

Permalink
ppc64le - fix geogfn and geomfn test failures
Browse files Browse the repository at this point in the history
geogfn
    - TestAzimuth/north_east
    - TestSegmentize/LINESTRING(1.0_1.0,_2.0_2.0,_3.0_3.0),_maximum_segment_length:_100000.000000
    - TestSegmentize/POLYGON((0.0_0.0,_1.0_0.0,_1.0_1.0,_0.0_0.0)),_maximum_segment_length:_100000.000000
    - TestSegmentize/POLYGON((0.0_0.0,_1.0_0.0,_1.0_1.0,_0.0_0.0),_(0.1_0.1,_0.2_0.1,_0.2_0.2,_0.1_0.1)),_maximum_segment_length:_50000.000000
    - TestSegmentize/MULTILINESTRING((1.0_1.0,_2.0_2.0,_3.0_3.0),_(6.0_6.0,_7.0_6.0)),_maximum_segment_length:_100000.000000
    - TestSegmentize/MULTILINESTRING_(EMPTY,_(1.0_1.0,_2.0_2.0,_3.0_3.0),_(6.0_6.0,_7.0_6.0)),_maximum_segment_length:_100000.000000
    - TestSegmentize/MULTIPOLYGON(((3.0_3.0,_4.0_3.0,_4.0_4.0,_3.0_3.0)),_((0.0_0.0,_1.0_0.0,_1.0_1.0,_0.0_0.0),_(0.1_0.1,_0.2_0.1,_0.2_0.2,_0.1_0.1))),_maximum_segment_length:_100000.000000
    - TestSegmentize/GEOMETRYCOLLECTION_(POINT_(40_10),LINESTRING_(10_10,_20_20,_10_40),POLYGON_((40_40,_20_45,_45_30,_40_40))),_maximum_segment_length:_1000000.000000
    - TestSegmentize/LINESTRING(0_0_25,_0_1_0,_2_5_100),_maximum_segment_length:_150000.000000
    - TestSegmentizeCoords/Coordinate(85,_85)_to_Coordinate(0,_0),_0.29502092024628396
    - TestSegmentizeCoords/Coordinate(0,_16,_23,_10)_to_Coordinate(1,_0,_-5,_0),_0.07848050723825097
    - TestProject/SRID

geomfn - TestGenerateRandomPoints/Polygon_with_a_hole

Cherry picks #66621

Release note: None

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
prashantkhoje and otan committed Jun 2, 2022
1 parent 6c63bda commit 02c0eac
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 70 deletions.
1 change: 1 addition & 0 deletions pkg/geo/geogfn/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
deps = [
"//pkg/geo",
"//pkg/geo/geoprojbase",
"//pkg/geo/geotest",
"@com_github_golang_geo//s1",
"@com_github_golang_geo//s2",
"@com_github_stretchr_testify//require",
Expand Down
7 changes: 6 additions & 1 deletion pkg/geo/geogfn/azimuth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -67,7 +68,11 @@ func TestAzimuth(t *testing.T) {
r, err := Azimuth(a, b)
require.NoError(t, err)
require.NotNil(t, r)
require.Equal(t, tc.expected, *r)
if tc.expected == 0 {
require.Equal(t, tc.expected, *r)
} else {
require.InEpsilon(t, tc.expected, *r, geotest.Epsilon)
}
})
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/geo/geogfn/segmentize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
)
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestSegmentize(t *testing.T) {
require.NoError(t, err)
expectedGeog, err := geo.ParseGeography(test.expectedWKT)
require.NoError(t, err)
require.Equal(t, expectedGeog, modifiedGeog)
geotest.RequireGeographyInEpsilon(t, expectedGeog, modifiedGeog, geotest.Epsilon)
})
}
// Test for segment maximum length as negative.
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestSegmentizeCoords(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
convertedPoints, err := segmentizeCoords(test.a, test.b, test.segmentMaxAngle)
require.NoError(t, err)
require.Equal(t, test.resultantCoordinates, convertedPoints)
geotest.FlatCoordsInEpsilon(t, test.resultantCoordinates, convertedPoints, geotest.Epsilon)
})
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/geo/geogfn/unary_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/golang/geo/s1"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
Expand Down Expand Up @@ -270,13 +271,11 @@ func TestProject(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
projected, err := Project(tc.point, tc.distance, s1.Angle(tc.azimuth))
require.NoError(t, err)
require.Equalf(
geotest.RequireGeographyInEpsilon(
t,
tc.projected,
projected,
"expected %f, found %f",
&tc.projected,
projected,
geotest.Epsilon,
)
})
}
Expand Down
1 change: 1 addition & 0 deletions pkg/geo/geomfn/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ go_test(
"//pkg/geo",
"//pkg/geo/geopb",
"//pkg/geo/geos",
"//pkg/geo/geotest",
"@com_github_cockroachdb_errors//:errors",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
Expand Down
11 changes: 6 additions & 5 deletions pkg/geo/geomfn/affine_transforms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
)
Expand Down Expand Up @@ -497,7 +498,7 @@ func TestRotate(t *testing.T) {
actual, err := Rotate(geometry, tc.rotRadians)
require.NoError(t, err)

requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
})
}
}
Expand Down Expand Up @@ -597,7 +598,7 @@ func TestRotateWithPointOrigin(t *testing.T) {
require.EqualError(t, err, tt.wantErrStr)
} else {
require.NoError(t, err)
requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tt.wantGeom), got, 1e-5)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tt.wantGeom), got, 1e-5)
}
})
}
Expand Down Expand Up @@ -743,7 +744,7 @@ func TestRotateX(t *testing.T) {
actual, err := RotateX(geometry, tc.rotRadians)
require.NoError(t, err)

requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
})
}
}
Expand Down Expand Up @@ -818,7 +819,7 @@ func TestRotateY(t *testing.T) {

actual, err := RotateY(geometry, tc.rotRadians)
require.NoError(t, err)
requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
})
}
}
Expand Down Expand Up @@ -893,7 +894,7 @@ func TestRotateZ(t *testing.T) {

actual, err := RotateZ(geometry, tc.rotRadians)
require.NoError(t, err)
requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
})
}
}
3 changes: 2 additions & 1 deletion pkg/geo/geomfn/distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geos"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -1001,7 +1002,7 @@ func TestClosestPoint(t *testing.T) {
ret, err := ClosestPoint(gA, gB)
require.NoError(t, err)

requireGeometryWithinEpsilon(t, expected, ret, 2e-10)
geotest.RequireGeometryInEpsilon(t, expected, ret, 2e-10)
})
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/geo/geomfn/generate_points_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -70,7 +71,7 @@ func TestGenerateRandomPoints(t *testing.T) {
rng := rand.New(rand.NewSource(tt.args.seed))
got, err := GenerateRandomPoints(tt.args.g, tt.args.nPoints, rng)
require.NoError(t, err)
require.Equal(t, tt.want, got)
geotest.RequireGeometryInEpsilon(t, tt.want, got, 1e-5)
})
}

Expand Down
51 changes: 0 additions & 51 deletions pkg/geo/geomfn/geomfn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
package geomfn

import (
"math"
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
)
Expand Down Expand Up @@ -136,52 +134,3 @@ func requireGeometryFromGeomT(t *testing.T, g geom.T) geo.Geometry {
require.NoError(t, err)
return ret
}

// flatCoordsInEpsilon ensures the flat coords are within the expected epsilon.
func flatCoordsInEpsilon(t *testing.T, expected []float64, actual []float64, epsilon float64) {
require.Equal(t, len(expected), len(actual), "expected %#v, got %#v", expected, actual)
for i := range expected {
require.True(t, math.Abs(expected[i]-actual[i]) < epsilon, "expected %#v, got %#v (mismatching at position %d)", expected, actual, i)
}
}

// requireGeometryWithinEpsilon and ensures the geometry shape and SRID are equal,
// and that each coordinate is within the provided epsilon.
func requireGeometryWithinEpsilon(t *testing.T, expected, got geo.Geometry, epsilon float64) {
expectedT, err := expected.AsGeomT()
require.NoError(t, err)
gotT, err := got.AsGeomT()
require.NoError(t, err)
requireGeomTWithinEpsilon(t, expectedT, gotT, epsilon)
}

func requireGeomTWithinEpsilon(t *testing.T, expectedT, gotT geom.T, epsilon float64) {
require.Equal(t, expectedT.SRID(), gotT.SRID())
require.Equal(t, expectedT.Layout(), gotT.Layout())
require.IsType(t, expectedT, gotT)
switch lhs := expectedT.(type) {
case *geom.Point, *geom.LineString:
flatCoordsInEpsilon(t, expectedT.FlatCoords(), gotT.FlatCoords(), epsilon)
case *geom.MultiPoint, *geom.Polygon, *geom.MultiLineString:
require.Equal(t, expectedT.Ends(), gotT.Ends())
flatCoordsInEpsilon(t, expectedT.FlatCoords(), gotT.FlatCoords(), epsilon)
case *geom.MultiPolygon:
require.Equal(t, expectedT.Ends(), gotT.Ends())
require.Equal(t, expectedT.Endss(), gotT.Endss())
flatCoordsInEpsilon(t, expectedT.FlatCoords(), gotT.FlatCoords(), epsilon)
case *geom.GeometryCollection:
rhs, ok := gotT.(*geom.GeometryCollection)
require.True(t, ok)
require.Len(t, rhs.Geoms(), len(lhs.Geoms()))
for i := range lhs.Geoms() {
requireGeomTWithinEpsilon(
t,
lhs.Geom(i),
rhs.Geom(i),
epsilon,
)
}
default:
panic(errors.AssertionFailedf("unknown geometry type: %T", expectedT))
}
}
3 changes: 2 additions & 1 deletion pkg/geo/geomfn/linestring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
)
Expand Down Expand Up @@ -604,7 +605,7 @@ func TestLineSubstring(t *testing.T) {
require.Equal(t, tt.wantErrString, err.Error())
return
}
requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tt.wantGeomT), got, 1e-4)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tt.wantGeomT), got, 1e-4)
})
}
}
3 changes: 2 additions & 1 deletion pkg/geo/geomfn/segmentize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
)
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestSegmentize(t *testing.T) {
require.NoError(t, err)
expectedGeom, err := geo.ParseGeometry(test.expectedWKT)
require.NoError(t, err)
requireGeometryWithinEpsilon(t, expectedGeom, modifiedGeom, 1e-6)
geotest.RequireGeometryInEpsilon(t, expectedGeom, modifiedGeom, geotest.Epsilon)
})
}
// Test for segment maximum length as negative.
Expand Down
3 changes: 2 additions & 1 deletion pkg/geo/geomfn/snap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestSnap(t *testing.T) {
actual, err := Snap(input, target, tc.tolerance)
require.NoError(t, err)

requireGeometryWithinEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
geotest.RequireGeometryInEpsilon(t, requireGeometryFromGeomT(t, tc.expected), actual, 1e-5)
})
}
}
5 changes: 3 additions & 2 deletions pkg/geo/geomfn/topology_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geotest"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-geom"
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestCentroid(t *testing.T) {
require.NoError(t, err)
expected, err := geo.ParseGeometry(tc.expected)
require.NoError(t, err)
requireGeometryWithinEpsilon(t, expected, ret, 2e-10)
geotest.RequireGeometryInEpsilon(t, expected, ret, 2e-10)
})
}
}
Expand Down Expand Up @@ -300,7 +301,7 @@ func TestPointOnSurface(t *testing.T) {
require.NoError(t, err)
expected, err := geo.ParseGeometry(tc.expected)
require.NoError(t, err)
requireGeometryWithinEpsilon(t, expected, ret, 2e-10)
geotest.RequireGeometryInEpsilon(t, expected, ret, 2e-10)
})
}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/geo/geotest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "geotest",
srcs = ["geotest.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/geo/geotest",
visibility = ["//visibility:public"],
deps = [
"//pkg/geo",
"@com_github_cockroachdb_errors//:errors",
"@com_github_stretchr_testify//require",
"@com_github_twpayne_go_geom//:go-geom",
],
)
Loading

0 comments on commit 02c0eac

Please sign in to comment.