-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
luhn_test.go
109 lines (97 loc) · 1.58 KB
/
luhn_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 TestLuhn_Verify(t *testing.T) {
t.Parallel()
cases := map[string]struct {
in string
out bool
}{
"Regular 1": {
in: "4242424242424242",
out: true,
},
"Regular 2": {
in: "5105105105105100",
out: true,
},
"Regular 3": {
in: "34",
out: true,
},
"Irregular 1": {
in: "510510510510511",
},
"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.NewLuhn().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
}
})
}
}
func TestLuhn_Generate(t *testing.T) {
t.Parallel()
cases := map[string]struct {
in string
out int
isError bool
}{
"Regular 1": {
in: "424242424242424",
out: 2,
},
"Regular 2": {
in: "510510510510510",
out: 0,
},
"Regular 3": {
in: "37144963539843",
out: 1,
},
"Regular 4": {
in: "3056930902590",
out: 4,
},
"Regular 5": {
in: "353011133330000",
out: 0,
},
"Regular 6": {
in: "3",
out: 4,
},
"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.NewLuhn().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)
}
})
}
}