-
Notifications
You must be signed in to change notification settings - Fork 22
/
tree_test.go
99 lines (91 loc) · 1.81 KB
/
tree_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
package goscore_test
import (
"encoding/xml"
"errors"
"github.com/asafschers/goscore"
"io/ioutil"
"testing"
)
var TreeTests = []struct {
features map[string]interface{}
score float64
err error
}{
{map[string]interface{}{},
4.3463944950723456E-4,
nil,
},
{
map[string]interface{}{"f2": "f2v1"},
-1.8361380219689046E-4,
nil,
},
{
map[string]interface{}{"f2": "f2v1", "f1": "f1v3"},
-6.237581139073701E-4,
nil,
},
{
map[string]interface{}{"f2": "f2v1", "f1": "f1v3", "f4": 0.08},
0.0021968294712358194,
nil,
},
{
map[string]interface{}{"f2": "f2v1", "f1": "f1v3", "f4": 0.09},
-9.198573460887271E-4,
nil,
},
{
map[string]interface{}{"f2": "f2v1", "f1": "f1v3", "f4": 0.09, "f3": "f3v2"},
-0.0021187239505556523,
nil,
},
{
map[string]interface{}{"f2": "f2v1", "f1": "f1v3", "f4": 0.09, "f3": "f3v4"},
-3.3516227414227926E-4,
nil,
},
{
map[string]interface{}{"f2": "f2v1", "f1": "f1v4"},
0.0011015286521365208,
nil,
},
{
map[string]interface{}{"f2": "f2v4"},
0.0022726641744997256,
nil,
},
{
map[string]interface{}{"f1": "f1v3", "f2": "f2v1", "f3": "f3v7", "f4": 0.09},
-1,
errors.New("Terminal node without score, Node id: 5"),
},
}
// TODO: test score distribution
func TestTree(t *testing.T) {
treeXml, err := ioutil.ReadFile("fixtures/tree.pmml")
if err != nil {
panic(err)
}
tree := []byte(treeXml)
var n goscore.Node
xml.Unmarshal(tree, &n)
for _, tt := range TreeTests {
actual, err := n.TraverseTree(tt.features)
if err != nil {
if tt.err == nil {
t.Errorf("expected no error, actual: %s",
err)
} else if tt.err.Error() != err.Error() {
t.Errorf("expected error %s, actual: %s",
tt.err.Error(),
err)
}
}
if actual != tt.score {
t.Errorf("expected %f, actual %f",
tt.score,
actual)
}
}
}