summaryrefslogtreecommitdiff
path: root/go/luhn/luhn_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/luhn/luhn_test.go')
-rw-r--r--go/luhn/luhn_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/go/luhn/luhn_test.go b/go/luhn/luhn_test.go
new file mode 100644
index 0000000..db7fc4a
--- /dev/null
+++ b/go/luhn/luhn_test.go
@@ -0,0 +1,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")
+ }
+}