go-teeworlds-protocol/packer/packer_test.go

128 lines
2.2 KiB
Go
Raw Normal View History

2024-06-17 04:23:54 +00:00
package packer
import (
"reflect"
"testing"
)
// pack
func TestPackSmallPositiveInts(t *testing.T) {
2024-06-17 05:11:59 +00:00
got := PackInt(1)
2024-06-17 04:23:54 +00:00
want := []byte{0x01}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestPackMultiBytePositiveInts(t *testing.T) {
2024-06-17 05:11:59 +00:00
got := PackInt(63)
2024-06-17 04:23:54 +00:00
want := []byte{0x3F}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
2024-06-17 05:11:59 +00:00
got = PackInt(64)
2024-06-17 04:23:54 +00:00
want = []byte{0x80, 0x01}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
2024-06-17 05:11:59 +00:00
got = PackInt(65)
2024-06-17 04:23:54 +00:00
want = []byte{0x81, 0x01}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestPackSmallNegativeInts(t *testing.T) {
2024-06-17 05:11:59 +00:00
got := PackInt(-1)
2024-06-17 04:23:54 +00:00
want := []byte{0x40}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
2024-06-17 05:11:59 +00:00
got = PackInt(-2)
2024-06-17 04:23:54 +00:00
want = []byte{0x41}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestPackMultiByteNegativeInts(t *testing.T) {
2024-06-17 05:11:59 +00:00
got := PackInt(-63)
2024-06-17 04:23:54 +00:00
want := []byte{0x7E}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
2024-06-17 05:11:59 +00:00
got = PackInt(-64)
2024-06-17 04:23:54 +00:00
want = []byte{0x7F}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
2024-06-17 05:11:59 +00:00
got = PackInt(-65)
2024-06-17 04:23:54 +00:00
want = []byte{0xC0, 0x01}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
// unpack
func TestUnpackSmallPositiveInts(t *testing.T) {
got := UnpackInt([]byte{0x01})
want := 1
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
got = UnpackInt([]byte{0x02})
want = 2
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
got = UnpackInt([]byte{0x03})
want = 3
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}
func TestUnpackMultiBytePositiveInts(t *testing.T) {
got := UnpackInt([]byte{0x3f})
want := 63
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
got = UnpackInt([]byte{0x80, 0x01})
want = 64
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
got = UnpackInt([]byte{0x81, 0x01})
want = 65
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, wanted %v", got, want)
}
}