-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
pointz_test.go
96 lines (89 loc) · 2.03 KB
/
pointz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package geom_test
import (
"fmt"
"math"
"reflect"
"strconv"
"testing"
"github.com/go-spatial/geom"
)
func TestPointZSetter(t *testing.T) {
type tcase struct {
point [3]float64
setter geom.PointZSetter
expected geom.PointZSetter
err error
}
fn := func(tc tcase) func(*testing.T) {
return func(t *testing.T) {
err := tc.setter.SetXYZ(tc.point)
if tc.err == nil && err != nil {
t.Errorf("error, expected nil got %v", err)
return
}
if tc.err != nil {
if tc.err.Error() != err.Error() {
t.Errorf("error, expected %v got %v", tc.err, err)
}
return
}
// compare the results
if !reflect.DeepEqual(tc.expected, tc.setter) {
t.Errorf("setter, expected %v got %v", tc.expected, tc.setter)
return
}
xyz := tc.setter.XYZ()
if !reflect.DeepEqual(tc.point, xyz) {
t.Errorf("XYZ, expected %v, got %v", tc.point, xyz)
}
}
}
tests := []tcase{
{
point: [3]float64{10, 20, 30},
setter: &geom.PointZ{15, 20, 30},
expected: &geom.PointZ{10, 20, 30},
},
{
setter: (*geom.PointZ)(nil),
err: geom.ErrNilPointZ,
},
}
for i, tc := range tests {
t.Run(strconv.FormatInt(int64(i), 10), fn(tc))
}
}
func TestPointZ(t *testing.T) {
fn := func(pt geom.PointZ) (string, func(*testing.T)) {
return fmt.Sprintf("%v", pt),
func(t *testing.T) {
t.Run("xy", func(t *testing.T) {
xy := pt.XY()
exp_xy := geom.Point{pt[0], pt[1]}
if xy != exp_xy {
t.Errorf("xy, expected %v got %v", exp_xy, xy)
}
})
t.Run("xyz", func(t *testing.T) {
xyz := pt.XYZ()
exp_xyz := pt
if xyz != exp_xyz {
t.Errorf("xyz, expected %v got %v", exp_xyz, xyz)
}
})
t.Run("magnitude", func(t *testing.T) {
m := pt.Magnitude()
exp_m := math.Sqrt((pt[0] * pt[0]) + (pt[1] * pt[1]) + (pt[2] * pt[2]))
if m != exp_m {
t.Errorf("magnitude, expected %v got %v", exp_m, m)
}
})
}
}
tests := []geom.PointZ{
{0, 1, 2}, {2, 2, 3}, {1, 2, 3},
}
for _, pt := range tests {
t.Run(fn(pt))
}
}