-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoluhn_test.go
129 lines (122 loc) · 2.79 KB
/
goluhn_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
package goluhn
import (
"log"
"strconv"
"testing"
)
func TestValidate(t *testing.T) {
tests := []struct {
number string
expectOK bool
}{
{"1234567812345670", true},
{"1111222233334444", true},
{"1111222233334441", false},
{"49927398716", true},
{"49927398717", false},
{"1234567812345678", false},
{"79927398710", false},
{"79927398711", false},
{"79927398712", false},
{"79927398713", true},
{"79927398714", false},
{"79927398715", false},
{"79927398716", false},
{"79927398717", false},
{"79927398718", false},
{"79927398719", false},
{"374652346956782346957823694857692364857368475368", true},
{"374652346956782346957823694857692364857387456834", false},
{"8", false},
{"0", true},
}
for _, test := range tests {
t.Run(test.number, func(t *testing.T) {
res := Validate(test.number)
if res != nil {
if test.expectOK {
log.Printf("Expected success but luhn check unsuccessful for %s.", test.number)
t.Fail()
}
}
})
}
}
func TestCalculate(t *testing.T) {
tests := []struct {
number string
luhnDigit string
expected string
}{
{"123456781234567", "0", "1234567812345670"},
{"111122223333444", "4", "1111222233334444"},
{"7992739871", "3", "79927398713"},
{"37465234695678234695782369485769236485736847536", "8", "374652346956782346957823694857692364857368475368"},
}
for _, test := range tests {
t.Run(test.expected, func(t *testing.T) {
l, n, err := Calculate(test.number)
if err != nil {
log.Printf("Unexpected err %+v", err)
t.Fail()
}
if test.luhnDigit != l {
log.Printf("Expected luhn digit %s. Actual luhn digit %s", test.luhnDigit, l)
t.Fail()
}
if n != test.expected {
log.Printf("Expected %s to generate luhn number %s", test.number, test.expected)
t.Fail()
}
err = Validate(n)
if err != nil {
log.Printf("Cannot validate derive luhn number %s. Error: %+v", n, err)
t.Fail()
}
})
}
}
func TestGenerate(t *testing.T) {
tests := []struct {
numberSize int
sampleSize int
}{
{1, 100},
{10, 1000},
{100, 1000},
{1000, 1000},
}
for _, test := range tests {
t.Run(strconv.Itoa(test.numberSize), func(t *testing.T) {
for i := 0; i < test.sampleSize; i++ {
err := Validate(Generate(test.numberSize))
if err != nil {
log.Printf("Unexpected err %+v", err)
t.Fail()
}
}
})
}
}
func TestGenerateWithPrefix(t *testing.T) {
tests := []struct {
numberSize int
sampleSize int
}{
{1, 100},
{10, 1000},
{100, 1000},
{1000, 1000},
}
for _, test := range tests {
t.Run(strconv.Itoa(test.numberSize), func(t *testing.T) {
for i := 0; i < test.sampleSize; i++ {
err := Validate(GenerateWithPrefix("123", test.numberSize))
if err != nil {
log.Printf("Unexpected err %+v", err)
t.Fail()
}
}
})
}
}