Added more number and string encoding/decoding tests.

This commit is contained in:
scruzin 2019-01-13 19:06:04 +10:30
parent 137ff7990a
commit ffcd011220
1 changed files with 31 additions and 7 deletions

View File

@ -57,12 +57,12 @@ func TestSanity(t *testing.T) {
// TestStrings tests string encoding and decoding. // TestStrings tests string encoding and decoding.
func TestStrings(t *testing.T) { func TestStrings(t *testing.T) {
for _, s := range testStrings {
// Short string encoding is as follows: // Short string encoding is as follows:
// enc[0] = data type (typeString) // enc[0] = data type (typeString)
// end[1:3] = size // end[1:3] = size
// enc[3:] = data // enc[3:] = data
buf := make([]byte, len(s)+5) for _, s := range testStrings {
buf := make([]byte, len(s)+7)
_, err := EncodeString(buf, s) _, err := EncodeString(buf, s)
if err != nil { if err != nil {
t.Errorf("EncodeString failed") t.Errorf("EncodeString failed")
@ -75,10 +75,26 @@ func TestStrings(t *testing.T) {
t.Errorf("DecodeString did not produce original string, got %v", ds) t.Errorf("DecodeString did not produce original string, got %v", ds)
} }
} }
// Long string encoding is as follows:
// enc[0] = data type (typeString)
// end[1:5] = size
// enc[5:] = data
s := string(make([]byte, 65536))
buf := make([]byte, len(s)+7)
_, err := EncodeString(buf, s)
if err != nil {
t.Errorf("EncodeString failed")
}
if buf[0] != typeLongString {
t.Errorf("Expected typeLongString, got %v", buf[0])
}
ds := DecodeLongString(buf[1:])
if s != ds {
t.Errorf("DecodeLongString did not produce original string, got %v", ds)
}
} }
// TestNumbers tests 24-bit encoding and encoding. // TestNumbers tests number encoding and encoding.
// We don't test the others as they are just wrappers for standard functions in encoding/binary.
func TestNumbers(t *testing.T) { func TestNumbers(t *testing.T) {
for _, n := range testNumbers { for _, n := range testNumbers {
buf := make([]byte, 4) // NB: encoder requires an extra byte for some reason buf := make([]byte, 4) // NB: encoder requires an extra byte for some reason
@ -90,6 +106,14 @@ func TestNumbers(t *testing.T) {
if n != dn { if n != dn {
t.Errorf("DecodeInt24 did not produce original Number, got %v", dn) t.Errorf("DecodeInt24 did not produce original Number, got %v", dn)
} }
_, err = EncodeInt32(buf, n)
if err != nil {
t.Errorf("EncodeInt32 failed")
}
dn = int32(DecodeInt32(buf))
if n != dn {
t.Errorf("DecodeInt32 did not produce original Number, got %v", dn)
}
} }
} }