syncthing/luhn/luhn_test.go

62 lines
1.2 KiB
Go
Raw Normal View History

2014-09-04 06:31:38 +00:00
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package luhn_test
import (
"testing"
"github.com/syncthing/syncthing/luhn"
)
func TestGenerate(t *testing.T) {
2014-07-04 13:56:33 +00:00
// Base 6 Luhn
a := luhn.Alphabet("abcdef")
2014-07-04 14:16:50 +00:00
c, err := a.Generate("abcdef")
if err != nil {
t.Fatal(err)
}
if c != 'e' {
t.Errorf("Incorrect check digit %c != e", c)
}
2014-07-04 13:56:33 +00:00
// Base 10 Luhn
a = luhn.Alphabet("0123456789")
2014-07-04 14:16:50 +00:00
c, err = a.Generate("7992739871")
if err != nil {
t.Fatal(err)
}
2014-07-04 13:56:33 +00:00
if c != '3' {
t.Errorf("Incorrect check digit %c != 3", c)
}
}
2014-07-04 14:16:50 +00:00
func TestInvalidString(t *testing.T) {
a := luhn.Alphabet("ABC")
_, err := a.Generate("7992739871")
t.Log(err)
if err == nil {
t.Error("Unexpected nil error")
}
}
func TestBadAlphabet(t *testing.T) {
a := luhn.Alphabet("01234566789")
_, err := a.Generate("7992739871")
t.Log(err)
if err == nil {
t.Error("Unexpected nil error")
}
}
func TestValidate(t *testing.T) {
a := luhn.Alphabet("abcdef")
if !a.Validate("abcdefe") {
t.Errorf("Incorrect validation response for abcdefe")
}
if a.Validate("abcdefd") {
t.Errorf("Incorrect validation response for abcdefd")
}
}