-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
damm_test.go
109 lines (97 loc) · 1.56 KB
/
damm_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
package checkdigit_test
import (
"testing"
"github.com/osamingo/checkdigit"
)
func TestDamm_Verify(t *testing.T) {
t.Parallel()
cases := map[string]struct {
in string
out bool
}{
"Regular 1": {
in: "224564332323",
out: true,
},
"Regular 2": {
in: "543525432346",
out: true,
},
"Regular 3": {
in: "37",
out: true,
},
"Irregular 1": {
in: "835323233227",
},
"Irregular 2": {
in: "",
},
"Irregular 3": {
in: "a",
},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
ret := checkdigit.NewDamm().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
}
})
}
}
func TestDamm_Generate(t *testing.T) {
t.Parallel()
cases := map[string]struct {
in string
out int
isError bool
}{
"Regular 1": {
in: "22456433232",
out: 3,
},
"Regular 2": {
in: "54352543234",
out: 6,
},
"Regular 3": {
in: "10493839530",
out: 5,
},
"Regular 4": {
in: "08989435403",
out: 5,
},
"Regular 5": {
in: "54994384990",
out: 4,
},
"Regular 6": {
in: "3",
out: 7,
},
"Irregular 1": {
isError: true,
},
"Irregular 2": {
in: "a",
isError: true,
},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
r, err := checkdigit.NewDamm().Generate(c.in)
if c.isError && err == nil {
t.Error("unexpected error")
}
if c.out != r {
t.Errorf("not equal, expected = %d, given = %d", c.out, r)
}
})
}
}