Repository navigation
Expand file tree
/
Copy pathluhn_test.go
More file actions
107 lines (99 loc) · 2.31 KB
/
Copy pathluhn_test.go
File metadata and controls
107 lines (99 loc) · 2.31 KB
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
package luhn
import (
"testing"
)
func TestLuhnAlgorithm(t *testing.T) {
tests := []struct {
name string
input int64
wantFull int64
wantChecksum int
wantIsValid bool
expectError bool
}{
{
name: "valid credit card (Visa)",
input: 400000000000000,
wantFull: 4000000000000002,
wantChecksum: 2,
wantIsValid: true,
},
{
name: "valid credit card (Mastercard)",
input: 555555555555444,
wantFull: 5555555555554444,
wantChecksum: 4,
wantIsValid: true,
},
{
name: "valid Canadian SIN",
input: 46838723,
wantFull: 468387238,
wantChecksum: 8,
wantIsValid: true,
},
{
name: "invalid credit card",
input: 400000000000001,
wantFull: 4000000000000018,
wantChecksum: 8,
wantIsValid: false,
},
{
name: "zero input",
input: 0,
wantFull: 0,
wantChecksum: 0,
wantIsValid: false,
expectError: true,
},
{
name: "single digit",
input: 7,
wantFull: 75,
wantChecksum: 5,
wantIsValid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test GetFullNumber
gotFull, err := FullNumber(tt.input)
if (err != nil) != tt.expectError {
t.Errorf("GetFullNumber() error = %v, expectError %v", err, tt.expectError)
return
}
if gotFull != tt.wantFull && tt.wantIsValid {
t.Errorf("GetFullNumber() = %v, want %v", gotFull, tt.wantFull)
}
// Test GetCheckDigit
gotChecksum, err := CheckDigit(tt.input)
if (err != nil) != tt.expectError {
t.Errorf("GetCheckDigit() error = %v, expectError %v", err, tt.expectError)
return
}
if gotChecksum != tt.wantChecksum && tt.wantIsValid {
t.Errorf("GetCheckDigit() = %v, want %v", gotChecksum, tt.wantChecksum)
}
// Test IsValidNumber
if gotIsValid := IsValid(tt.wantFull); gotIsValid != tt.wantIsValid {
t.Errorf("IsValidNumber() = %v, want %v", gotIsValid, tt.wantIsValid)
}
})
}
}
func BenchmarkCheckDigit(b *testing.B) {
for i := 0; i < b.N; i++ {
CheckDigit(7992739871)
}
}
func BenchmarkFullNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
FullNumber(7992739871)
}
}
func BenchmarkIsValid(b *testing.B) {
for i := 0; i < b.N; i++ {
IsValid(79927398713)
}
}