Skip to content

Commit fe0dcd2

Browse files
committed
geomfn: adjust two tests to work on s390x
`angleFromCoords` function uses `atan` to compute some geometry functions. s390x system has architecture support for that trigonometric function which can produce slightly different result than when computing via math operations, which resulted in a false positive failure in a couple of tests. This commit teaches those tests to use our custom float comparison function that only checks 15 significant decimal digits. Release note: None
1 parent 153bcb4 commit fe0dcd2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pkg/geo/geomfn/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ go_test(
111111
"//pkg/geo/geopb",
112112
"//pkg/geo/geos",
113113
"//pkg/geo/geotest",
114+
"//pkg/testutils/floatcmp",
114115
"@com_github_cockroachdb_errors//:errors",
115116
"@com_github_stretchr_testify//assert",
116117
"@com_github_stretchr_testify//require",

pkg/geo/geomfn/angle_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,32 @@ package geomfn
88
import (
99
"fmt"
1010
"math"
11+
"runtime"
12+
"strconv"
1113
"testing"
1214

1315
"github.com/cockroachdb/cockroach/pkg/geo"
1416
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
17+
"github.com/cockroachdb/cockroach/pkg/testutils/floatcmp"
1518
"github.com/stretchr/testify/require"
1619
)
1720

21+
func assertFloatsMatch(t *testing.T, expected, actual float64) {
22+
if runtime.GOARCH != "s390x" {
23+
require.Equal(t, expected, actual)
24+
return
25+
}
26+
// s390x system has architecture support for 'atan' function (which is used
27+
// in 'angleFromCoords') and produces slightly different result than math
28+
// operations we do to get the expected number. To go around this, we use
29+
// the custom float comparison that checks 15 significant decimal digits.
30+
exp := strconv.FormatFloat(expected, 'f', -1, 64)
31+
act := strconv.FormatFloat(actual, 'f', -1, 64)
32+
matched, err := floatcmp.FloatsMatch(exp, act)
33+
require.NoError(t, err)
34+
require.True(t, matched)
35+
}
36+
1837
func TestAngle(t *testing.T) {
1938
pf := func(f float64) *float64 {
2039
return &f
@@ -58,7 +77,7 @@ func TestAngle(t *testing.T) {
5877
angle, err := Angle(g1, g2, g3, g4)
5978
require.NoError(t, err)
6079
if tc.expected != nil && angle != nil {
61-
require.Equal(t, *tc.expected, *angle)
80+
assertFloatsMatch(t, *tc.expected, *angle)
6281
} else {
6382
require.Equal(t, tc.expected, angle)
6483
}
@@ -134,7 +153,7 @@ func TestAngleLineString(t *testing.T) {
134153
angle, err := AngleLineString(g1, g2)
135154
require.NoError(t, err)
136155
if tc.expected != nil && angle != nil {
137-
require.Equal(t, *tc.expected, *angle)
156+
assertFloatsMatch(t, *tc.expected, *angle)
138157
} else {
139158
require.Equal(t, tc.expected, angle)
140159
}

0 commit comments

Comments
 (0)