package xproto import ( "encoding/binary" "testing" ) func TestRectangleWire(t *testing.T) { v := Rectangle{X: -2, Y: 3, Width: 640, Height: 480} var p [8]byte v.Put(p[:]) want := [8]byte{254, 255, 3, 0, 128, 2, 224, 1} if p != want || GetRectangle(p[:]) != v { t.Fatalf("wire %x", p) } } func TestVariableStructWire(t *testing.T) { v := Str{NameLen: 3, Name: "abc"} p := make([]byte, v.Size()) v.Put(p) if string(p) != "\x03abc" { t.Fatalf("wire %x", p) } s := SetupRequest{ByteOrder: 'l', ProtocolMajorVersion: 11, AuthorizationProtocolNameLen: 1, AuthorizationProtocolDataLen: 2, AuthorizationProtocolName: "a", AuthorizationProtocolData: "bc"} p = make([]byte, s.Size()) s.Put(p) if len(p) != 20 || p[12] != 'a' || p[16] != 'b' || p[17] != 'c' || p[19] != 0 { t.Fatalf("aligned setup %x", p) } } func TestUnionView(t *testing.T) { var p [20]byte binary.LittleEndian.PutUint32(p[:], 0x12345678) v := GetClientMessageData(p[:]) if v.Data8()[0] != 0x78 || v.Data16()[0] != 0x5678 || v.Data32()[0] != 0x12345678 { t.Fatal("union members") } } func TestAllocStructPut(t *testing.T) { v := Rectangle{Width: 10} var p [8]byte if n := testing.AllocsPerRun(1000, func() { v.Put(p[:]); _ = GetRectangle(p[:]) }); n != 0 { t.Fatal(n) } } func BenchmarkRectanglePut(b *testing.B) { v := Rectangle{Width: 640, Height: 480} var p [8]byte b.ReportAllocs() for b.Loop() { v.Put(p[:]) } }