-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin_test.go
89 lines (83 loc) · 1.46 KB
/
bin_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
package measure_test
import (
"encoding/json"
"testing"
"github.com/nextmv-io/sdk/measure"
)
func TestBin(t *testing.T) {
points := []measure.Point{{1, 2}, {4, 6}, {6, 6}}
measures := []measure.ByIndex{
measure.Constant(0),
measure.Indexed(measure.EuclideanByPoint(), points),
measure.Constant(10),
measure.Constant(30),
}
m := measure.Bin(
measures,
func(from, to int) int {
if from == 0 && to == 1 { //nolint:gocritic
return 1
} else if from == 1 && to == 2 {
return 2
} else if from == 2 && to == 3 {
return 3
}
return 0
},
)
for i, test := range []struct {
l1 int
l2 int
cost float64
}{
{l1: 0, l2: 1, cost: 5},
{l1: 1, l2: 2, cost: 10},
{l1: 2, l2: 3, cost: 30},
{l1: 3, l2: 2, cost: 0},
{l1: 0, l2: 2, cost: 0},
{l1: 5, l2: 5, cost: 0},
} {
if got := m.Cost(test.l1, test.l2); got != test.cost {
t.Errorf("test %v: got %v; want %v", i, got, test.cost)
}
}
b, err := json.MarshalIndent(m, "", "\t")
if err != nil {
t.Errorf("got %+v; want nil", err)
}
w := `{
"measures": [
{
"count": 3,
"measure": {
"constant": 0,
"type": "constant"
}
},
{
"count": 1,
"measure": {
"type": "euclidean"
}
},
{
"count": 1,
"measure": {
"constant": 10,
"type": "constant"
}
},
{
"count": 1,
"measure": {
"constant": 30,
"type": "constant"
}
}
],
"type": "bin"
}`
if v := string(b); v != w {
t.Errorf("got %s\nwant %s\n", v, w)
}
}