Skip to content

Commit

Permalink
Add unit tests for Unit method of XY
Browse files Browse the repository at this point in the history
It was untested by any existing unit tests (and is not used internally
anywhere). We don't want to remove it though, since it's part of the
public API. So I added some unit tests instead.
  • Loading branch information
peterstace committed Jul 14, 2021
1 parent 145d718 commit 4b383d3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
7 changes: 7 additions & 0 deletions geom/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package geom_test

import (
"bytes"
"math"
"testing"

. "github.com/peterstace/simplefeatures/geom"
Expand Down Expand Up @@ -86,6 +87,12 @@ func expectXYEq(t *testing.T, got, want XY) {
t.Errorf("\ngot: %v\nwant: %v\n", got, want)
}
}
func expectXYWithinTolerance(t *testing.T, got, want XY, tolerance float64) {
t.Helper()
if delta := math.Abs(got.Sub(want).Length()); delta > tolerance {
t.Errorf("\ngot: %v\nwant: %v\n", got, want)
}
}

func expectCoordinatesTypeEq(t *testing.T, got, want CoordinatesType) {
t.Helper()
Expand Down
5 changes: 0 additions & 5 deletions geom/xy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ func (w XY) Less(o XY) bool {
return w.Y < o.Y
}

func (w XY) distanceTo(o XY) float64 {
delta := o.Sub(w)
return math.Sqrt(delta.Dot(delta))
}

func (w XY) distanceSquaredTo(o XY) float64 {
delta := o.Sub(w)
return delta.Dot(delta)
Expand Down
54 changes: 54 additions & 0 deletions geom/xy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package geom_test

import (
"math"
"testing"

"github.com/peterstace/simplefeatures/geom"
)

func TestXYUnit(t *testing.T) {
sqrt2 := math.Sqrt(2)
sqrt5 := math.Sqrt(5)
for _, tc := range []struct {
description string
input geom.XY
output geom.XY
}{
{
description: "+ve unit in X",
input: geom.XY{X: 1},
output: geom.XY{X: 1},
},
{
description: "+ve unit in Y",
input: geom.XY{Y: 1},
output: geom.XY{Y: 1},
},
{
description: "-ve unit in X",
input: geom.XY{X: -1},
output: geom.XY{X: -1},
},
{
description: "-ve unit in Y",
input: geom.XY{Y: -1},
output: geom.XY{Y: -1},
},
{
description: "non-aligned unit",
input: geom.XY{X: -1 / sqrt2, Y: 1 / sqrt2},
output: geom.XY{X: -1 / sqrt2, Y: 1 / sqrt2},
},
{
description: "non-unit",
input: geom.XY{X: 1, Y: -2},
output: geom.XY{X: 1 / sqrt5, Y: -2 / sqrt5},
},
} {
t.Run(tc.description, func(t *testing.T) {
got := tc.input.Unit()
expectXYWithinTolerance(t, got, tc.output, 0.0000001)
})
}
}

0 comments on commit 4b383d3

Please sign in to comment.