-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhn_test.go
127 lines (117 loc) · 2.52 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package luhn
import (
"testing"
)
var testcases = []struct {
string
int
valid bool
checkb byte
checki int
}{
{"5", 5, false, '9', 9},
{"059", 59, true, '6', 6},
{"7992739871", 7992739871, false, '3', 3},
{"79927398713", 79927398713, true, '8', 8},
{"4992739871", 4992739871, false, '6', 6},
{"49927398716", 49927398716, true, '8', 8},
{"468192572269901", 468192572269901, false, '0', 0},
{"4681925722699010", 4681925722699010, true, '9', 9},
{"123456781234567", 123456781234567, false, '0', 0},
}
func TestCheckDigit(t *testing.T) {
for _, test := range testcases {
test := test
var r byte
t.Run(test.string, func(t *testing.T) {
r = CheckDigit(test.string)
if r != test.checkb {
t.Errorf("Failure for %s: expected %d; got %s",
test.string, test.checki, string(r))
}
})
}
}
func TestCheckDigitInt(t *testing.T) {
for _, test := range testcases {
test := test
var r int
t.Run(test.string, func(t *testing.T) {
r = CheckDigitInt(test.int)
if r != test.checki {
t.Errorf("Failure for %s: expected %d; got %d",
test.string, test.checki, r)
}
})
}
}
func TestIsValid(t *testing.T) {
for _, test := range testcases {
test := test
var r bool
t.Run(test.string, func(t *testing.T) {
r = IsValid(test.string)
if r != test.valid {
t.Errorf("Failure for %s: expected valid == %t",
test.string, test.valid)
}
})
}
}
func TestIsValidInt(t *testing.T) {
for _, test := range testcases {
test := test
var r bool
t.Run(test.string, func(t *testing.T) {
r = IsValidInt(test.int)
if r != test.valid {
t.Errorf("Failure for %s: expected valid == %t",
test.string, test.valid)
}
})
}
}
func BenchmarkCheckDigit(b *testing.B) {
for _, test := range testcases {
test := test
var r byte
b.Run(test.string, func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = CheckDigit(test.string)
}
})
}
}
func BenchmarkCheckDigitInt(b *testing.B) {
for _, test := range testcases {
test := test
var r int
b.Run(test.string, func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = CheckDigitInt(test.int)
}
})
}
}
func BenchmarkIsValid(b *testing.B) {
for _, test := range testcases {
test := test
var r bool
b.Run(test.string, func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = IsValid(test.string)
}
})
}
}
func BenchmarkIsValidInt(b *testing.B) {
for _, test := range testcases {
test := test
var r bool
b.Run(test.string, func(b *testing.B) {
for i := 0; i < b.N; i++ {
r = IsValidInt(test.int)
}
})
}
}