tape: Test U16CastSafe

This commit is contained in:
Sasha Koshka 2025-01-19 15:41:43 -05:00
parent c748ca2223
commit aac5266c0c
2 changed files with 44 additions and 0 deletions

27
metadapta_test.go Normal file
View File

@ -0,0 +1,27 @@
package hopp
import "bytes"
import "slices"
import "testing"
func TestDecodeMessageA(test *testing.T) {
transID, method, payload, err := decodeMessageA(bytes.NewReader([]byte {
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
0x6B, 0x12,
0x00, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}))
if err != nil {
test.Fatal(err)
}
if got, correct := transID, int64(0x5800FEABC3104F04); got != correct {
test.Fatalf("not equal: %v %v", got, correct)
}
if got, correct := method, uint16(0x6B12); got != correct {
test.Fatalf("not equal: %v %v", got, correct)
}
correctPayload := []byte { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }
if got, correct := payload, correctPayload; !slices.Equal(got, correct) {
test.Fatalf("not equal: %v %v", got, correct)
}
}

View File

@ -246,6 +246,23 @@ func TestStringArray(test *testing.T) {
} }
} }
func TestU16CastSafe(test *testing.T) {
number, ok := U16CastSafe(90_000)
if ok { test.Fatalf("false positive: %v, %v", number, ok) }
number, ok = U16CastSafe(-478)
if ok { test.Fatalf("false positive: %v, %v", number, ok) }
number, ok = U16CastSafe(3870)
if !ok { test.Fatalf("false negative: %v, %v", number, ok) }
if got, correct := number, uint16(3870); got != correct {
test.Fatalf("not equal: %v %v", got, correct)
}
number, ok = U16CastSafe(0)
if !ok { test.Fatalf("false negative: %v, %v", number, ok) }
if got, correct := number, uint16(0); got != correct {
test.Fatalf("not equal: %v %v", got, correct)
}
}
func randString(length int) string { func randString(length int) string {
buffer := make([]byte, length) buffer := make([]byte, length)
for index := range buffer { for index := range buffer {