-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcresults_test.go
108 lines (94 loc) · 2.43 KB
/
calcresults_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
package main
import (
"math"
"testing"
"github.com/brainsik/bae/plane"
)
func TestCalcResultsMerge(t *testing.T) {
dst := CalcResults{
plane.ImagePoint{X: 0, Y: 0}: &CalcResult{},
plane.ImagePoint{X: 1, Y: 1}: &CalcResult{complex(-1, 1), 1, true, false},
}
src := CalcResults{
plane.ImagePoint{X: 0, Y: 0}: &CalcResult{complex(-2, 2), 2, false, true},
plane.ImagePoint{X: 1, Y: 1}: &CalcResult{complex(-3, 3), 3, false, true},
}
dst.Merge(src)
result := dst
expect := CalcResults{
plane.ImagePoint{X: 0, Y: 0}: &CalcResult{0, 2, false, true},
plane.ImagePoint{X: 1, Y: 1}: &CalcResult{complex(-1, 1), 4, true, true},
}
for k := range result {
if *result[k] != *expect[k] {
t.Error(*result[k], *expect[k])
}
}
}
func TestCalcResultsAdd(t *testing.T) {
crs := make(CalcResults)
pt := plane.ImagePoint{X: 4, Y: 2}
z := complex(7, 7)
val := uint(42)
// Add new result
crs.Add(pt, z, val)
result, ok := crs[pt]
if !ok {
t.Fatalf("Expected %v to exist in CalcResults: %v", pt, crs)
}
if result.Val != val {
t.Errorf("Expected result to have value %d, got %d", val, result.Val)
}
if result.Z != z {
t.Errorf("Expected result to have value %v, got %v", z, result.Z)
}
// Add to existing result
crs.Add(pt, -z, val)
if result.Val != val*2 {
t.Errorf("Expected result to have value %d, got %d", val*2, result.Val)
}
if result.Z != z {
t.Errorf("Expected result to have value %v, got %v", z, result.Z)
}
}
func TestCalcResultsMaxEscaped(t *testing.T) {
crs := CalcResults{
plane.ImagePoint{X: 0, Y: 0}: &CalcResult{complex(0, 0), 10, true, false},
plane.ImagePoint{X: 1, Y: 1}: &CalcResult{complex(1, 1), 20, false, false},
}
result := crs.MaxEscaped()
expect := 10.0
if result != expect {
t.Error(expect, result)
}
}
func TestCalcResultsEmptyZero(t *testing.T) {
crs := make(CalcResults)
result := crs.Sum()
expect := uint(0)
if result != expect {
t.Errorf("Expected result to be %v, got %v", expect, result)
}
}
func TestCalcResultsEmptyNaN(t *testing.T) {
crs := make(CalcResults)
testCases := []struct {
name string
f func() float64
}{
{"Max", crs.Max},
{"MaxEscaped", crs.MaxEscaped},
{"Min", crs.Min},
{"Avg", crs.Avg},
{"AvgEscaped", crs.AvgEscaped},
{"Median", crs.Median},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.f()
if !math.IsNaN(tc.f()) {
t.Errorf("Expected result to be %v, got %v", math.NaN(), result)
}
})
}
}