package xturbo_test import ( "bytes" "encoding/binary" "strings" "testing" "bnema.dev/xturbo" "bnema.dev/xturbo/internal/xtest" "bnema.dev/xturbo/render" "bnema.dev/xturbo/res" "bnema.dev/xturbo/xproto" ) func TestListGoldens(t *testing.T) { for _, name := range []string{"xproto/InternAtom", "xproto/ChangeProperty", "res/QueryClientIds"} { t.Run(name, func(t *testing.T) { want, err := xtest.LoadHex("testdata/golden/" + name + ".req.hex") if err != nil { t.Fatal(err) } b := xturbo.NewBuf(0) switch name { case "xproto/InternAtom": xproto.InternAtom(b, false, "XTURBO_") case "xproto/ChangeProperty": xproto.ChangeProperty(b, xproto.PropModeReplace, xproto.Window(binary.LittleEndian.Uint32(want[4:])), xproto.Atom(binary.LittleEndian.Uint32(want[8:])), xproto.AtomString, 8, 3, []byte("abc")) case "res/QueryClientIds": var ext xturbo.ExtTable ext.Set(xturbo.ExtRes, want[0], 0, 0) b.Ext = &ext res.QueryClientIds(b, []res.ClientIdSpec{{Client: 0, Mask: res.ClientIdMaskClientXID}}) } if b.Err() != nil || !bytes.Equal(b.Bytes(), want) { t.Fatalf("err %v got %x want %x", b.Err(), b.Bytes(), want) } }) } } func TestListLengths(t *testing.T) { b := xturbo.NewBuf(0) if xproto.ChangeProperty(b, 0, 1, 1, 1, 16, 2, []byte{1, 2}) != 0 || b.Err() != xturbo.ErrBadLength || b.Len() != 0 { t.Fatal("expression length mismatch") } b = xturbo.NewBuf(0) if xproto.InternAtom(b, false, strings.Repeat("x", 65536)) != 0 || b.Err() != xturbo.ErrBadLength { t.Fatal("derived length truncation") } b = xturbo.NewBuf(0) xproto.SetFontPath(b, []xproto.Str{{NameLen: 3, Name: "abc"}, {NameLen: 1, Name: "d"}}) want := []byte{51, 0, 4, 0, 2, 0, 0, 0, 3, 'a', 'b', 'c', 1, 'd', 0, 0} if b.Err() != nil || !bytes.Equal(b.Bytes(), want) { t.Fatalf("variable structs: %x err %v", b.Bytes(), b.Err()) } b = xturbo.NewBuf(0) xproto.QueryTextExtents(b, 1, []xproto.Char2b{{Byte1: 0, Byte2: 'a'}}) if b.Bytes()[1] != 1 { t.Fatal("implicit list length exprfield") } var ext xturbo.ExtTable ext.Set(xturbo.ExtRender, 140, 0, 0) b = xturbo.NewBuf(0) b.Ext = &ext if render.AddGlyphs(b, 1, 2, []uint32{1}, nil, nil) != 0 || b.Err() != xturbo.ErrBadLength { t.Fatal("shared length must remain explicit") } } func TestAllocListRequests(t *testing.T) { b := xturbo.NewBuf(0) data := make([]byte, 4096) specs := []res.ClientIdSpec{{Client: 1, Mask: res.ClientIdMaskClientXID}} var ext xturbo.ExtTable ext.Set(xturbo.ExtRes, 141, 0, 0) b.Ext = &ext if n := testing.AllocsPerRun(1000, func() { b.Reset() xproto.InternAtom(b, false, "WM_CLASS") xproto.ChangeProperty(b, 0, 1, 1, 1, 8, 4096, data) res.QueryClientIds(b, specs) }); n != 0 { t.Fatal(n) } if b.Err() != nil { t.Fatal(b.Err()) } }