128 lines
2.4 KiB
Go
128 lines
2.4 KiB
Go
package serial_test
|
|
|
|
import (
|
|
"testing"
|
|
"testing/quick"
|
|
|
|
"git.hoiting.org/micha/triplex/serial"
|
|
)
|
|
|
|
// Property: Decode(Encode(code)) == code (voor geldige codes)
|
|
func TestEncodeDecodeRoundtrip(t *testing.T) {
|
|
f := func(idx uint32) bool {
|
|
if idx >= serial.MaxIndex {
|
|
return true
|
|
}
|
|
|
|
code, err := serial.Decode(idx)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
idx2, err := serial.Encode(code)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return idx == idx2
|
|
}
|
|
|
|
if err := quick.Check(f, nil); err != nil {
|
|
t.Fatalf("Encode/Decode roundtrip failed: %v", err)
|
|
}
|
|
}
|
|
|
|
// Property: Decode(Encode(Decode(i))) == Decode(i) (idempotent op geldige indices)
|
|
func TestDecodeEncodeDecodeIdempotent(t *testing.T) {
|
|
f := func(idx uint32) bool {
|
|
if idx >= serial.MaxIndex {
|
|
return true
|
|
}
|
|
|
|
code1, err := serial.Decode(idx)
|
|
if err != nil {
|
|
return true // decode mag indices weigeren (bv. forbidden pair)
|
|
}
|
|
|
|
idx2, err := serial.Encode(code1)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
code2, err := serial.Decode(idx2)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return code1 == code2
|
|
}
|
|
|
|
if err := quick.Check(f, nil); err != nil {
|
|
t.Fatalf("Decode/Encode/Decode idempotence failed: %v", err)
|
|
}
|
|
}
|
|
|
|
// Property: forbidden eerste twee letters komen nooit uit Decode
|
|
func TestForbiddenPairsNeverDecoded(t *testing.T) {
|
|
f := func(idx uint32) bool {
|
|
if idx >= serial.MaxIndex {
|
|
return true
|
|
}
|
|
|
|
code, err := serial.Decode(idx)
|
|
if err != nil {
|
|
return true // decode mag indices weigeren
|
|
}
|
|
|
|
l1 := code[0]
|
|
l2 := code[1]
|
|
|
|
return !serial.IsForbiddenPair(l1, l2)
|
|
}
|
|
|
|
if err := quick.Check(f, nil); err != nil {
|
|
t.Fatalf("Forbidden pairs appeared in Decode: %v", err)
|
|
}
|
|
}
|
|
|
|
// Optional: basis-check op formaat LL-NNN-L-C
|
|
func TestDecodedFormatLooksCorrect(t *testing.T) {
|
|
f := func(idx uint32) bool {
|
|
if idx >= serial.MaxIndex {
|
|
return true
|
|
}
|
|
|
|
code, err := serial.Decode(idx)
|
|
if err != nil {
|
|
return true
|
|
}
|
|
|
|
if len(code) != 10 {
|
|
return false
|
|
}
|
|
if code[2] != '-' || code[6] != '-' || code[8] != '-' {
|
|
return false
|
|
}
|
|
|
|
// letters op posities 0,1,7,9
|
|
for _, p := range []int{0, 1, 7, 9} {
|
|
if code[p] < 'A' || code[p] > 'Z' {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// cijfers op posities 3,4,5
|
|
for _, p := range []int{3, 4, 5} {
|
|
if code[p] < '0' || code[p] > '9' {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
if err := quick.Check(f, nil); err != nil {
|
|
t.Fatalf("Decoded format property failed: %v", err)
|
|
}
|
|
}
|