luhn_test.go 903 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "testing"
  5. )
  6. func TestGenerate(t *testing.T) {
  7. // Base 6 Luhn
  8. a := luhnAlphabet("abcdef")
  9. c, err := a.generate("abcdef")
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. if c != 'e' {
  14. t.Errorf("Incorrect check digit %c != e", c)
  15. }
  16. // Base 10 Luhn
  17. a = luhnAlphabet("0123456789")
  18. c, err = a.generate("7992739871")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if c != '3' {
  23. t.Errorf("Incorrect check digit %c != 3", c)
  24. }
  25. }
  26. func TestInvalidString(t *testing.T) {
  27. a := luhnAlphabet("ABC")
  28. _, err := a.generate("7992739871")
  29. t.Log(err)
  30. if err == nil {
  31. t.Error("Unexpected nil error")
  32. }
  33. }
  34. func TestValidate(t *testing.T) {
  35. a := luhnAlphabet("abcdef")
  36. if !a.luhnValidate("abcdefe") {
  37. t.Errorf("Incorrect validation response for abcdefe")
  38. }
  39. if a.luhnValidate("abcdefd") {
  40. t.Errorf("Incorrect validation response for abcdefd")
  41. }
  42. }