Ready for testing.
This commit is contained in:
@@ -1,24 +1,37 @@
|
||||
package serial
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxIndex = block
|
||||
|
||||
letters = 26
|
||||
numbers = 900 // 100–999
|
||||
block = letters * letters * letters * numbers // 26^3 * 900
|
||||
)
|
||||
|
||||
func Encode(code string) (uint32, error) {
|
||||
log.Printf("code: %v", code)
|
||||
// Verwacht formaat: L1L2-NNN-L3-C => lengte 10, posities: 0,1,3,4,5,7,9
|
||||
if len(code) != 10 || code[2] != '-' || code[6] != '-' || code[8] != '-' {
|
||||
return 0, errors.New("invalid format, expected LL-NNN-L-C")
|
||||
}
|
||||
|
||||
l1, l2, l3 := code[0], code[1], code[7]
|
||||
n1, n2, n3 := code[3], code[4], code[5]
|
||||
|
||||
// letters checken
|
||||
// forbidden alleen op l1,l2
|
||||
// nummer parsen uit code[3:6]
|
||||
|
||||
L1 := int(l1 - 'A')
|
||||
L2 := int(l2 - 'A')
|
||||
L3 := int(l3 - 'A')
|
||||
L1 := uint32(l1 - 'A')
|
||||
L2 := uint32(l2 - 'A')
|
||||
L3 := uint32(l3 - 'A')
|
||||
num := uint32(n1*100 + n2*10 + n3)
|
||||
N := num - 100
|
||||
|
||||
idx := uint32((((L1*letters+L2)*letters + L3) * numbers) + N)
|
||||
@@ -27,10 +40,12 @@ func Encode(code string) (uint32, error) {
|
||||
return 0, errors.New("invalid checksum")
|
||||
}
|
||||
|
||||
log.Printf("idx: %v", idx)
|
||||
return idx, nil
|
||||
}
|
||||
|
||||
func Decode(idx uint32) (string, error) {
|
||||
log.Printf("idx: %v", idx)
|
||||
if idx >= block {
|
||||
return "", errors.New("index out of range")
|
||||
}
|
||||
@@ -50,12 +65,22 @@ func Decode(idx uint32) (string, error) {
|
||||
l2 := byte('A' + L2)
|
||||
l3 := byte('A' + L3)
|
||||
|
||||
if isForbiddenPair(l1, l2) {
|
||||
if IsForbiddenPair(l1, l2) {
|
||||
return "", errors.New("forbidden letter combination")
|
||||
}
|
||||
|
||||
c := checksumLetter(idx)
|
||||
|
||||
return fmt.Sprintf("%c%c-%03d-%c-%c", l1, l2, N+100, l3, c), nil
|
||||
code := fmt.Sprintf("%c%c-%03d-%c-%c", l1, l2, N+100, l3, c)
|
||||
|
||||
log.Printf("code: %v", code)
|
||||
return code, nil
|
||||
}
|
||||
|
||||
func checksumLetter(idx uint32) byte {
|
||||
return byte('A' + idx%26)
|
||||
}
|
||||
|
||||
func IsForbiddenPair(l1, l2 byte) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
"testing/quick"
|
||||
|
||||
"yourmodule/serial"
|
||||
"git.hoiting.org/micha/triplex/serial"
|
||||
)
|
||||
|
||||
// Property: Decode(Encode(code)) == code (voor geldige codes)
|
||||
@@ -125,4 +125,3 @@ func TestDecodedFormatLooksCorrect(t *testing.T) {
|
||||
t.Fatalf("Decoded format property failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user