-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtaxicab_test.go
117 lines (109 loc) · 2.53 KB
/
taxicab_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package measure_test
import (
"encoding/json"
"testing"
"github.com/nextmv-io/sdk/measure"
)
func TestTaxicabByPointCost(t *testing.T) {
p1 := measure.Point{1, 2}
p2 := measure.Point{4, 6}
m := measure.TaxicabByPoint()
if v := m.Cost(p1, p2); v != 7 {
t.Errorf("got %v; want 7", v)
}
if v := m.Cost(p2, p1); v != 7 {
t.Errorf("got %v; want 7", v)
}
}
func TestTaxicabByPointMarshalJSON(t *testing.T) {
b, err := json.Marshal(measure.TaxicabByPoint())
if err != nil {
t.Errorf("got %+v; want nil", err)
}
w := `{"type":"taxicab"}`
if v := string(b); v != w {
t.Errorf("got %q; want %q", v, w)
}
}
func TestTaxicabByIndexCost(t *testing.T) {
// All bad configs return zero and don't panic
for i, tc := range []struct {
m measure.ByIndex
f func(m measure.ByIndex) float64
expected float64
}{
// Missing Y coords in points uses 0
{
expected: 0,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{1}, {1}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(0, 1)
},
},
{
expected: 1,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{1}, {2}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(0, 1)
},
},
// Negative values
{
expected: 10,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{-5, 2}, {2, -1}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(1, 0)
},
},
{
expected: 4,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{-5, -2}, {-2, -1}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(1, 0)
},
},
// Mismatched dimensions
{
expected: 4,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{1}, {2, 3}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(0, 1)
},
},
{
expected: 4,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{2, 3}, {1}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(0, 1)
},
},
{
expected: 8,
m: measure.Indexed(
measure.TaxicabByPoint(), []measure.Point{{2, 3, 4}, {1}}),
f: func(m measure.ByIndex) float64 {
return m.Cost(0, 1)
},
},
} {
if v := tc.f(tc.m); v != tc.expected {
t.Errorf("test %d: want: %v got: %v", i, tc.expected, v)
}
}
}
func TestTaxicabByIndexMarshalJSON(t *testing.T) {
b, err := json.Marshal(measure.Indexed(measure.TaxicabByPoint(), nil))
if err != nil {
t.Errorf("got %+v; want nil", err)
}
w := `{"type":"taxicab"}`
if v := string(b); v != w {
t.Errorf("got %q; want %q", v, w)
}
}