This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
problem_test.go
78 lines (61 loc) · 1.98 KB
/
problem_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
// Copyright 2015 The golinear Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
package golinear
import "testing"
func TestFromDenseVector(t *testing.T) {
fromDense := FromDenseVector([]float64{0.2, 0.1, 0.3, 0.6})
check := FeatureVector{{1, 0.2}, {2, 0.1}, {3, 0.3}, {4, 0.6}}
compareVectors(t, fromDense, check, "fromDense")
}
func TestSortedFeatureVector(t *testing.T) {
unsorted := FeatureVector{{2, 1}, {1, 0.5}, {3, 1}}
sorted := sortedFeatureVector(unsorted)
check := FeatureVector{{1, 0.5}, {2, 1}, {3, 1}}
compareVectors(t, sorted, check, "sorted")
}
func TestInvalidIndex(t *testing.T) {
p := NewProblem()
erronous := FeatureVector{{1, 1}, {2, 0.5}, {0, 1}}
if err := p.Add(TrainingInstance{0, erronous}); err == nil {
t.Error("Erronous feature index should be rejected")
}
}
func TestProblemIterate(t *testing.T) {
problem := simpleProblem(t)
instances := simpleInstances()
idx := 0
problem.Iterate(func(instance *TrainingInstance) bool {
check := instances[idx]
compareVectors(t, instance.Features, check.Features, "iterated")
if instance.Label != check.Label {
t.Errorf("label(iterated) = %f, want %f", instance.Label, check.Label)
}
idx++
return true
})
// Iteration should be cancelled if the function passed returns 'false'.
count := 0
problem.Iterate(func(*TrainingInstance) bool {
count++
return false
})
if count != 1 {
t.Error("Problem iteration does not cancel upon the closure's request")
}
}
func compareVectors(t *testing.T, candidate, check FeatureVector, candidateName string) {
// Sanity check
if len(candidate) != len(check) {
t.Errorf("len(%s) = %d, want %d", candidateName, len(candidate),
len(check))
return
}
for idx, val := range check {
if candidate[idx] != val {
t.Errorf("%s[%d] = (%d, %f), want (%d, %f)", candidateName, idx,
candidate[idx].Index, candidate[idx].Value, check[idx].Index,
check[idx].Value)
}
}
}