mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-18 19:15:19 +00:00
1e2379df1b
The previous implementation was very generic; its tests didn't cover the actual alphabet for device IDs. Benchmark results on amd64: name old time/op new time/op delta Luhnify-8 1.00µs ± 1% 0.28µs ± 4% -72.38% (p=0.000 n=9+10) Unluhnify-8 992ns ± 2% 274ns ± 1% -72.39% (p=0.000 n=10+9)
27 lines
481 B
Go
27 lines
481 B
Go
// Copyright (C) 2014 The Protocol Authors.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLuhn32(t *testing.T) {
|
|
c, err := luhn32("AB725E4GHIQPL3ZFGT")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c != 'G' {
|
|
t.Errorf("Incorrect check digit %c != G", c)
|
|
}
|
|
|
|
_, err = luhn32("3734EJEKMRHWPZQTWYQ1")
|
|
if err == nil {
|
|
t.Error("Unexpected nil error")
|
|
}
|
|
if !strings.Contains(err.Error(), "'1'") {
|
|
t.Errorf("luhn32 should have errored on digit '1', got %v", err)
|
|
}
|
|
}
|