go-teeworlds-protocol/packer/packer_test.go

96 lines
1.8 KiB
Go
Raw Normal View History

2024-06-17 04:23:54 +00:00
package packer
import (
"testing"
"github.com/teeworlds-go/go-teeworlds-protocol/internal/testutils/require"
2024-06-17 04:23:54 +00:00
)
// pack
2024-06-19 04:13:57 +00:00
func TestPackEmptyString(t *testing.T) {
want := []byte{0x00}
got := PackStr("")
require.Equal(t, want, got)
2024-06-19 04:13:57 +00:00
}
func TestPackSimpleString(t *testing.T) {
want := []byte{'f', 'o', 'o', 0x00}
got := PackStr("foo")
require.Equal(t, want, got)
2024-06-19 04:13:57 +00:00
}
2024-06-17 04:23:54 +00:00
func TestPackSmallPositiveInts(t *testing.T) {
want := []byte{0x01}
got := PackInt(1)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
}
func TestPackMultiBytePositiveInts(t *testing.T) {
want := []byte{0x3F}
got := PackInt(63)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = []byte{0x80, 0x01}
got = PackInt(64)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = []byte{0x81, 0x01}
got = PackInt(65)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
}
func TestPackSmallNegativeInts(t *testing.T) {
want := []byte{0x40}
got := PackInt(-1)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = []byte{0x41}
got = PackInt(-2)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
}
func TestPackMultiByteNegativeInts(t *testing.T) {
want := []byte{0x7E}
got := PackInt(-63)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
2024-06-17 05:11:59 +00:00
got = PackInt(-64)
2024-06-17 04:23:54 +00:00
want = []byte{0x7F}
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = []byte{0xC0, 0x01}
got = PackInt(-65)
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
}
// unpack
func TestUnpackSmallPositiveInts(t *testing.T) {
want := 1
got := UnpackInt([]byte{0x01})
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = 2
got = UnpackInt([]byte{0x02})
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = 3
got = UnpackInt([]byte{0x03})
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
}
func TestUnpackMultiBytePositiveInts(t *testing.T) {
want := 63
got := UnpackInt([]byte{0x3f})
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = 64
got = UnpackInt([]byte{0x80, 0x01})
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
want = 65
got = UnpackInt([]byte{0x81, 0x01})
require.Equal(t, want, got)
2024-06-17 04:23:54 +00:00
}