-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmaximum_test.go
105 lines (95 loc) · 2.14 KB
/
maximum_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
/*
Copyright 2020 Binh Nguyen
Licensed under terms of MIT license (see LICENSE)
*/
package tago
import (
"math"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)
func TestNewMaximum(t *testing.T) {
data := make([]float64, 9)
for i := range data {
data[i] = math.Inf(-1)
}
tests := map[string]struct {
input int
want *Maximum
wantErr error
}{
"negative n": {input: -3, want: nil, wantErr: ErrInvalidParameters},
"zero n": {input: 0, want: nil, wantErr: ErrInvalidParameters},
"positive n": {input: 9, want: &Maximum{n: 9, data: data}, wantErr: nil},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
gotSD, gotErr := NewMaximum(tc.input)
if tc.wantErr != nil { // only check error returned if expecting one
assert.EqualError(t, gotErr, tc.wantErr.Error(), "must return the correct error")
}
assert.Equal(t, tc.want, gotSD, "must return the correct value")
})
}
}
func TestMaximumNext(t *testing.T) {
sd, _ := NewMaximum(3)
tests := []struct {
input float64
want float64
}{
{input: 4., want: 4.},
{input: 1.2, want: 4.},
{input: 5., want: 5.},
{input: 3., want: 5.},
{input: 4., want: 5.},
{input: 0., want: 4.},
{input: -1., want: 4.},
{input: -2., want: 0.},
{input: -1.5, want: -1.},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
}
func TestMaximumReset(t *testing.T) {
sd, _ := NewMaximum(3)
tests := []struct {
input float64
want float64
}{
{input: 4., want: 4.},
{input: 1.2, want: 4.},
{input: 5., want: 5.},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
sd.Reset()
diff := cmp.Diff(0., sd.Next(0.), floatComparer)
if diff != "" {
t.Fatalf(diff)
}
}
func TestMaximumString(t *testing.T) {
sd, _ := NewMaximum(4)
want := "Max(4)"
got := sd.String()
diff := cmp.Diff(want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
}