-
Notifications
You must be signed in to change notification settings - Fork 0
/
luhn_test.go
55 lines (44 loc) · 1.03 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
package checksum
import (
"math/rand"
"strconv"
"testing"
)
func TestLuhnCheck(t *testing.T) {
var lh Luhn
c, err := lh.Check("71767263188128")
if c != true || err != nil {
t.Errorf("Check(71767263188128) != true, got %v (%v)", c, err)
}
}
func TestLuhnCompute(t *testing.T) {
var lh Luhn
c, s, err := lh.Compute("7176726318812")
if c != 8 || err != nil {
t.Errorf("Compute(7176726318812) != 8, got %v, %v (%v)", c, s, err)
}
}
func TestLuhnInvalid(t *testing.T) {
var lh Luhn
c, err := lh.Check("7A1767263188128")
if c != false || err == nil {
t.Errorf("Check(7A1767263188128) != false, got %v (%v)", c, err)
}
}
func BenchmarkLuhn(b *testing.B) {
var lh Luhn
//b.StopTimer()
from := rand.Intn(1000000000) + 1000000000
to := from + b.N
for i := from; i <= to; i++ {
var s, ns string
var checks bool
s = strconv.Itoa(i)
//b.StartTimer()
_, ns, _ = lh.Compute(s)
if checks, _ = lh.Check(ns); checks != true {
b.Errorf("%v: failed, which should never have happened...", i)
}
//b.StopTimer()
}
}