Skip to content
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

ppc64le - fix geogfn and geomfn test failures #82250

Merged
merged 1 commit into from
Jun 3, 2022
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
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