-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (48 loc) · 876 Bytes
/
main.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
package main
import (
"errors"
"testing"
)
var ERR_WOULD_OVERFLOW = errors.New("would overflow")
func Adder(list ...uint8) (uint8, error) {
var t uint8
for i := 0; i < len(list); i++ {
if int(t)+int(list[i]) > 255 {
return 0, ERR_WOULD_OVERFLOW
}
t += list[i]
}
return t, nil
}
func TestAdder(t *testing.T) {
testCases := []struct {
name string
inputs []uint8
wantRes uint8
wantErr error
}{
{
"simple total",
[]uint8{1, 1, 7},
9,
nil,
},
{
"overflow prevented",
[]uint8{255, 1},
0,
ERR_WOULD_OVERFLOW,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotRes, gotErr := Adder(tc.inputs...)
if gotRes != tc.wantRes {
t.Errorf("wanted %v got %v", tc.wantRes, gotRes)
}
if gotErr != tc.wantErr {
t.Errorf("wanted error %v got %v", tc.wantErr, gotErr)
}
})
}
}