summaryrefslogtreecommitdiff
path: root/go/luhn/luhn_test.go
blob: db7fc4a41ca067e76cdb3ba68e1920b33fdab48b (plain)
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
package luhn

import "testing"

var validTests = []struct {
	n  string
	ok bool
}{
	{"738", false},
	{"8739567", true},
	{"1111", false},
	{"8763", true},
	{"    ", false},
	{"", false},
	{"2323 2005 7766 3554", true},
}

var addTests = []struct{ raw, luhn string }{
	{"123", "1230"},
	{"873956", "8739567"},
	{"837263756", "8372637564"},
	{"2323 2005 7766 355", "2323 2005 7766 3554"},
	// bonus Unicode cases
	// {"2323·2005·7766·355", "2323·2005·7766·3554"},
	// {"123", "1230"},
}

func TestValid(t *testing.T) {
	for _, test := range validTests {
		if ok := Valid(test.n); ok != test.ok {
			t.Fatalf("Valid(%s) = %t, want %t.", test.n, ok, test.ok)
		}
	}
}

func TestAddCheck(t *testing.T) {
	for _, test := range addTests {
		if luhn := AddCheck(test.raw); luhn != test.luhn {
			t.Fatalf("AddCheck(%s) = %s, want %s.", test.raw, luhn, test.luhn)
		}
	}
}

func BenchmarkValid(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Valid("2323 2005 7766 3554")
	}
}

func BenchmarkAddCheck(b *testing.B) {
	for i := 0; i < b.N; i++ {
		AddCheck("2323 2005 7766 355")
	}
}