2024-06-17 04:23:54 +00:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2024-06-23 19:18:54 +00:00
|
|
|
|
|
|
|
"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}
|
2024-06-23 19:18:54 +00:00
|
|
|
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}
|
2024-06-23 19:18:54 +00:00
|
|
|
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}
|
2024-06-23 19:18:54 +00:00
|
|
|
got := PackInt(1)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackMultiBytePositiveInts(t *testing.T) {
|
|
|
|
want := []byte{0x3F}
|
2024-06-23 19:18:54 +00:00
|
|
|
got := PackInt(63)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = []byte{0x80, 0x01}
|
2024-06-23 19:18:54 +00:00
|
|
|
got = PackInt(64)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = []byte{0x81, 0x01}
|
2024-06-23 19:18:54 +00:00
|
|
|
got = PackInt(65)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackSmallNegativeInts(t *testing.T) {
|
|
|
|
want := []byte{0x40}
|
2024-06-23 19:18:54 +00:00
|
|
|
got := PackInt(-1)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = []byte{0x41}
|
2024-06-23 19:18:54 +00:00
|
|
|
got = PackInt(-2)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackMultiByteNegativeInts(t *testing.T) {
|
|
|
|
want := []byte{0x7E}
|
2024-06-23 19:18:54 +00:00
|
|
|
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}
|
2024-06-23 19:18:54 +00:00
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = []byte{0xC0, 0x01}
|
2024-06-23 19:18:54 +00:00
|
|
|
got = PackInt(-65)
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// unpack
|
|
|
|
|
|
|
|
func TestUnpackSmallPositiveInts(t *testing.T) {
|
|
|
|
want := 1
|
2024-06-23 19:18:54 +00:00
|
|
|
got := UnpackInt([]byte{0x01})
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = 2
|
2024-06-23 19:18:54 +00:00
|
|
|
got = UnpackInt([]byte{0x02})
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = 3
|
2024-06-23 19:18:54 +00:00
|
|
|
got = UnpackInt([]byte{0x03})
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackMultiBytePositiveInts(t *testing.T) {
|
|
|
|
want := 63
|
2024-06-23 19:18:54 +00:00
|
|
|
got := UnpackInt([]byte{0x3f})
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = 64
|
2024-06-23 19:18:54 +00:00
|
|
|
got = UnpackInt([]byte{0x80, 0x01})
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
|
|
|
|
want = 65
|
2024-06-23 19:18:54 +00:00
|
|
|
got = UnpackInt([]byte{0x81, 0x01})
|
|
|
|
require.Equal(t, want, got)
|
2024-06-17 04:23:54 +00:00
|
|
|
}
|