forked from Avalanche-io/counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_test.go
142 lines (138 loc) · 2.05 KB
/
counter_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package counter
import (
"testing"
)
func TestCounter_Down(t *testing.T) {
tests := map[string]struct {
c *Counter
want int64
}{
"over 0": {
func() *Counter {
c := New()
c.Set(20)
return c
}(),
19,
},
"0 when Down from 1": {
func() *Counter {
c := New()
c.Set(1)
return c
}(),
0,
},
"0 when Down from 0": {
func() *Counter {
c := New()
c.Set(0)
return c
}(),
0,
},
"0 when Down from -1": {
func() *Counter {
c := New()
c.Set(-1)
return c
}(),
0,
},
}
for name, tt := range tests {
tt := tt
name := name
t.Run(name, func(t *testing.T) {
t.Parallel()
if got := tt.c.Down(); got != tt.want {
t.Errorf("Counter.Down() = %v, want %v", got, tt.want)
}
})
}
}
func TestCounter_Subtract(t *testing.T) {
type args struct {
val int64
}
tests := map[string]struct {
c *Counter
args args
want int64
}{
"over 0": {
func() *Counter {
c := New()
c.Set(20)
return c
}(),
args{10},
10,
},
"0 when 10 - 10": {
func() *Counter {
c := New()
c.Set(10)
return c
}(),
args{10},
0,
},
"0 when 1 - 10": {
func() *Counter {
c := New()
c.Set(1)
return c
}(),
args{10},
0,
},
}
for name, tt := range tests {
tt := tt
name := name
t.Run(name, func(t *testing.T) {
t.Parallel()
if got := tt.c.Subtract(tt.args.val); got != tt.want {
t.Errorf("Counter.Subtract() = %v, want %v", got, tt.want)
}
})
}
}
func TestCounter_Set(t *testing.T) {
type args struct {
v int64
}
tests := map[string]struct {
c *Counter
args args
want int64
}{
"10 when Set(10)": {
New(),
args{10},
10,
},
"0 when Set(0)": {
New(),
args{0},
0,
},
"0 when Set(-1)": {
New(),
args{-1},
0,
},
}
for name, tt := range tests {
tt := tt
name := name
t.Run(name, func(t *testing.T) {
t.Parallel()
tt.c.Set(tt.args.v)
if got := tt.c.Get(); got != tt.want {
t.Errorf("Counter.Set() set to %v, want %v", got, tt.want)
}
})
}
}