-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscale_test.go
95 lines (88 loc) · 2.17 KB
/
scale_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
package measure_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/nextmv-io/sdk/measure"
)
func TestScale(t *testing.T) {
m := measure.Scale(measure.Constant(50), 0.1)
if v := m.Cost(100, 42); v != 5 {
t.Errorf("got %v; want 5", v)
}
b, err := json.MarshalIndent(m, "", "\t")
if err != nil {
t.Errorf("got %+v; want nil", err)
}
w := `{
"measure": {
"constant": 50,
"type": "constant"
},
"scale": 0.1,
"type": "scale"
}`
if v := string(b); v != w {
t.Errorf("got %s; want %s", v, w)
}
}
func TestScaleByPoint(t *testing.T) {
for i, tc := range []struct {
inner measure.ByPoint // measure to scale
from measure.Point // distance from this point
to measure.Point // to this point
scale float64 // scale constant to apply
expected float64 // expected distance
}{
// Taxicab
{
inner: measure.TaxicabByPoint(),
from: measure.Point{0, 0},
to: measure.Point{100, 100},
scale: 0.5,
expected: 100,
},
// Euclidean
{
inner: measure.EuclideanByPoint(),
from: measure.Point{0, 0},
to: measure.Point{100, 100},
scale: 1.5,
expected: 212.13203435596427,
},
// Haversine
{
inner: measure.HaversineByPoint(),
from: measure.Point{8.7536, 51.7173}, // Paderborn
to: measure.Point{-75.1647, 39.9525}, // Philadelphia
scale: 0.1,
expected: 626380.7847000001,
},
// Double scale (cancel each other)
{
inner: measure.ScaleByPoint(measure.TaxicabByPoint(), 2),
from: measure.Point{0, 0},
to: measure.Point{100, 100},
scale: 0.5,
expected: 200,
},
} {
m := measure.ScaleByPoint(tc.inner, tc.scale)
if v := m.Cost(tc.from, tc.to); v != tc.expected {
t.Errorf("test %d: got %v; want %v", i, v, tc.expected)
}
b, err := json.Marshal(m)
if err != nil {
t.Errorf("test %d: got %+v; want nil", i, err)
}
innerB, err := json.Marshal(tc.inner)
if err != nil {
t.Errorf("test %d: got %+v; want nil", i, err)
}
w := fmt.Sprintf(`{"measure":%s,"scale":%v,"type":"scale"}`,
string(innerB), tc.scale)
if v := string(b); v != w {
t.Errorf("test %d: got %s; want %s", i, v, w)
}
}
}