-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalgorithm_test.go
80 lines (77 loc) · 1.46 KB
/
algorithm_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
package gospline
import (
"math"
"testing"
)
func TestSolveTridiagonal(t *testing.T) {
const epsilon = 1e-9
a := [4]float64{2, 3, 4, 5}
b := [5]float64{1, 2, 3, 4, 5}
c := [4]float64{2, 3, 4, 5}
d := [5]float64{5, 15, 31, 53, 45}
x := triThomas(a[:], b[:], c[:], d[:])
if math.Abs(x[0]-1) > epsilon {
t.Error("x[0] should be 1")
}
if math.Abs(x[1]-2) > epsilon {
t.Error("x[1] should be 2")
}
if math.Abs(x[4]-5) > epsilon {
t.Error("x[4] should be 5")
}
}
func TestFindSegment(t *testing.T) {
testcases := []struct {
xs []float64
x float64
result int
}{
// in segment
{
xs: []float64{1, 2, 3, 4, 5},
x: 4.5,
result: 3,
},
// on endpoint
{
xs: []float64{1, 2, 3, 4, 5},
x: 3,
result: 2,
},
// negative endpoints
{
xs: []float64{-1.2, -1.0, 0.6, 1.3, 100},
x: 0,
result: 1,
},
// below any
{
xs: []float64{-1.2, -1.0, 0.6, 1.3, 100},
x: -100,
result: 0,
},
// above any
{
xs: []float64{-1.2, -1.0, 0.6, 1.3, 100},
x: 101,
result: 3,
},
// left endpoint
{
xs: []float64{-1.2, -1.0, 0.6, 1.3, 100},
x: -1.2,
result: 0,
},
// right endpoint
{
xs: []float64{-1.2, -1.0, 0.6, 1.3, 100},
x: 100,
result: 3,
},
}
for _, tc := range testcases {
if result := findSegment(tc.xs, tc.x); result != tc.result {
t.Errorf("testcase %v failed, expected %d, result %d", tc, tc.result, result)
}
}
}