luhn_test.go 688 B

123456789101112131415161718192021222324252627282930
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "strings"
  9. "testing"
  10. )
  11. func TestLuhn32(t *testing.T) {
  12. c, err := luhn32("AB725E4GHIQPL3ZFGT")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if c != 'G' {
  17. t.Errorf("Incorrect check digit %c != G", c)
  18. }
  19. _, err = luhn32("3734EJEKMRHWPZQTWYQ1")
  20. if err == nil {
  21. t.Error("Unexpected nil error")
  22. }
  23. if !strings.Contains(err.Error(), "'1'") {
  24. t.Errorf("luhn32 should have errored on digit '1', got %v", err)
  25. }
  26. }