-
Notifications
You must be signed in to change notification settings - Fork 2
/
stepper_test.go
114 lines (90 loc) · 1.78 KB
/
stepper_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
109
110
111
112
113
114
package bigo
import (
"errors"
"reflect"
"testing"
)
func TestRangeStepper(t *testing.T) {
var (
min = -1.0
max = 1.0
size = 1.0
want = stepSequence{
{step: -1, ok: true},
{step: 0, ok: true},
{step: 1, ok: true},
{step: 0, ok: false},
}
s = NewRangeStepper(min, max, size)
)
assertTestSequence(t, s, want)
}
func TestNewRangeStepperPanicsForStepSizeZero(t *testing.T) {
want := ErrInvalidStepSize
assertPanic(t, want, func() {
NewRangeStepper(0, 3, 0)
})
assertPanic(t, want, func() {
NewRangeStepper(0, 3, -1)
})
}
func TestNewRangeStepperPanicsForUnorderedRange(t *testing.T) {
want := ErrUnorderedRange
assertPanic(t, want, func() {
NewRangeStepper(3, 3, 1)
})
assertPanic(t, want, func() {
NewRangeStepper(4, 3, 1)
})
}
func TestArrayStepper(t *testing.T) {
var (
steps = []float64{-1.0, 0, 1}
want = stepSequence{
{step: -1, ok: true},
{step: 0, ok: true},
{step: 1, ok: true},
{step: 0, ok: false},
}
s = NewArrayStepper(steps)
)
assertTestSequence(t, s, want)
}
func TestNewArrayStepper_PanicsForEmptyInput(t *testing.T) {
want := ErrEmptyStepsArray
assertPanic(t, want, func() {
NewArrayStepper([]float64{})
})
}
func assertPanic(t *testing.T, want error, f func()) {
var got error
func() {
defer func() {
if r := recover(); r != nil {
got = r.(error)
}
}()
f()
}()
if !errors.Is(got, want) {
t.Fatalf("want: %v, got: %v", want, got)
}
}
func assertTestSequence(t *testing.T, s Stepper, want stepSequence) {
var got stepSequence
for {
v, ok := s.Next()
got = append(got, step{step: v, ok: ok})
if !ok {
break
}
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("\nwant:\n%v\ngot:\n%v\n", want, got)
}
}
type stepSequence []step
type step struct {
step float64
ok bool
}