-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverhoeff_test.go
75 lines (62 loc) · 1.56 KB
/
verhoeff_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
package verhoeff
import "testing"
func TestVerhoeff(t *testing.T) {
expectDigit := func(a string, b int) {
c, err := Digit(a)
if err != nil {
t.Errorf("%v", err)
} else if b != c {
t.Errorf("for input " + a + " expected digit " + string(rune(b+48)) + " but got " + string(rune(c+48)) + " instead")
}
}
expectSignature := func(a string, b string) {
c, err := Generate(a)
if err != nil {
t.Errorf("%v", err)
} else if b != c {
t.Errorf("for input " + a + " expected signature " + b + " but got " + c + " instead")
}
if !Validate(c) {
t.Errorf("Unable to validate signature that was generated")
}
}
expectDigit("75872", 2)
expectDigit("12345", 1)
expectDigit("142857", 0)
expectDigit("123456789012", 0)
expectDigit("8473643095483728456789", 2)
if !Validate("84736430954837284567892") {
t.Errorf("Checksum failed for valid input")
}
if Validate("12") {
t.Errorf("Checksum passed for invalid input")
}
if Validate("xy-1") {
t.Errorf("Checksum passed for invalid alphabet")
}
expectSignature("8473643095483728456789", "84736430954837284567892")
}
func BenchmarkVerhoeffSmall(b *testing.B) {
for n := 0; n < b.N; n++ {
Digit("123")
}
}
func BenchmarkVerhoeffLarge(b *testing.B) {
for n := 0; n < b.N; n++ {
Digit("00123014764700968325")
}
}
func BenchmarkVerhoeffSmallParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Digit("123")
}
})
}
func BenchmarkVerhoeffLargeParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Digit("00123014764700968325")
}
})
}