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.
func TestStrings(t *testing.T) {
for _, s := range testStrings {
// Short string encoding is as follows:
// enc[0] = data type (typeString)
// end[1:3] = size
// enc[3:] = data
buf := make([]byte, len(s)+5)
for _, s := range testStrings {
buf := make([]byte, len(s)+7)
_, err := EncodeString(buf, s)
if err != nil {
t.Errorf("EncodeString failed")
@ -75,10 +75,26 @@ func TestStrings(t *testing.T) {
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.
// We don't test the others as they are just wrappers for standard functions in encoding/binary.
// TestNumbers tests number encoding and encoding.
func TestNumbers(t *testing.T) {
for _, n := range testNumbers {
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 {
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)
}
}
}