lux/tests/lux_buffer_test.go
2024-12-29 12:29:07 +02:00

60 lines
1.1 KiB
Go

package tests
import (
"bytes"
"lux/proto"
"testing"
)
func TestWriteGrow(t *testing.T) {
first, second := []byte{1, 2, 3, 4}, []byte{5, 6, 7, 8}
must := []byte{1, 2, 3, 4, 5, 6, 7, 8}
buf := proto.AllocLuxBuffer(4)
buf.WriteBytes(first)
t.Log(buf.AllBytes())
buf.WriteBytes(second)
t.Log(buf.AllBytes())
if !bytes.Equal(must, buf.AllBytes()) {
t.Fail()
}
}
func TestInts(t *testing.T) {
wd := proto.AllocLuxBuffer(1)
wd.WriteUint16(1234)
wd.WriteUint16(4321)
wd.WriteUint64(69)
wd.WriteUint32(2967279234)
if rd := wd.ReadUint16(); rd != 1234 {
t.Fatal(rd)
}
if rd := wd.ReadUint16(); rd != 4321 {
t.Fatal(rd)
}
if rd := wd.ReadUint64(); rd != 69 {
t.Fatal(rd)
}
if rd := wd.ReadUint32(); rd != 2967279234 {
t.Fatal(rd)
}
}
func TestStrings(t *testing.T) {
wd := proto.AllocLuxBuffer(1)
wd.WriteString("a") // 4
wd.WriteString("hello") // 8
if rd := wd.ReadString(); rd != "a" {
t.Fatal(rd)
}
if rd := wd.ReadString(); rd != "hello" {
t.Fatal(rd)
}
if wd.Length() != 12 {
t.Fatalf("string misaligned size: %d\n", wd.Length())
}
}