1package xturbo_test
2
3import (
4 "bytes"
5 "encoding/binary"
6 "strings"
7 "testing"
8
9 "bnema.dev/xturbo"
10 "bnema.dev/xturbo/internal/xtest"
11 "bnema.dev/xturbo/render"
12 "bnema.dev/xturbo/res"
13 "bnema.dev/xturbo/xproto"
14)
15
16func TestListGoldens(t *testing.T) {
17 for _, name := range []string{"xproto/InternAtom", "xproto/ChangeProperty", "res/QueryClientIds"} {
18 t.Run(name, func(t *testing.T) {
19 want, err := xtest.LoadHex("testdata/golden/" + name + ".req.hex")
20 if err != nil {
21 t.Fatal(err)
22 }
23 b := xturbo.NewBuf(0)
24 switch name {
25 case "xproto/InternAtom":
26 xproto.InternAtom(b, false, "XTURBO_")
27 case "xproto/ChangeProperty":
28 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"))
29 case "res/QueryClientIds":
30 var ext xturbo.ExtTable
31 ext.Set(xturbo.ExtRes, want[0], 0, 0)
32 b.Ext = &ext
33 res.QueryClientIds(b, []res.ClientIdSpec{{Client: 0, Mask: res.ClientIdMaskClientXID}})
34 }
35 if b.Err() != nil || !bytes.Equal(b.Bytes(), want) {
36 t.Fatalf("err %v got %x want %x", b.Err(), b.Bytes(), want)
37 }
38 })
39 }
40}
41func TestListLengths(t *testing.T) {
42 b := xturbo.NewBuf(0)
43 if xproto.ChangeProperty(b, 0, 1, 1, 1, 16, 2, []byte{1, 2}) != 0 || b.Err() != xturbo.ErrBadLength || b.Len() != 0 {
44 t.Fatal("expression length mismatch")
45 }
46 b = xturbo.NewBuf(0)
47 if xproto.InternAtom(b, false, strings.Repeat("x", 65536)) != 0 || b.Err() != xturbo.ErrBadLength {
48 t.Fatal("derived length truncation")
49 }
50 b = xturbo.NewBuf(0)
51 xproto.SetFontPath(b, []xproto.Str{{NameLen: 3, Name: "abc"}, {NameLen: 1, Name: "d"}})
52 want := []byte{51, 0, 4, 0, 2, 0, 0, 0, 3, 'a', 'b', 'c', 1, 'd', 0, 0}
53 if b.Err() != nil || !bytes.Equal(b.Bytes(), want) {
54 t.Fatalf("variable structs: %x err %v", b.Bytes(), b.Err())
55 }
56 b = xturbo.NewBuf(0)
57 xproto.QueryTextExtents(b, 1, []xproto.Char2b{{Byte1: 0, Byte2: 'a'}})
58 if b.Bytes()[1] != 1 {
59 t.Fatal("implicit list length exprfield")
60 }
61 var ext xturbo.ExtTable
62 ext.Set(xturbo.ExtRender, 140, 0, 0)
63 b = xturbo.NewBuf(0)
64 b.Ext = &ext
65 if render.AddGlyphs(b, 1, 2, []uint32{1}, nil, nil) != 0 || b.Err() != xturbo.ErrBadLength {
66 t.Fatal("shared length must remain explicit")
67 }
68}
69func TestAllocListRequests(t *testing.T) {
70 b := xturbo.NewBuf(0)
71 data := make([]byte, 4096)
72 specs := []res.ClientIdSpec{{Client: 1, Mask: res.ClientIdMaskClientXID}}
73 var ext xturbo.ExtTable
74 ext.Set(xturbo.ExtRes, 141, 0, 0)
75 b.Ext = &ext
76 if n := testing.AllocsPerRun(1000, func() {
77 b.Reset()
78 xproto.InternAtom(b, false, "WM_CLASS")
79 xproto.ChangeProperty(b, 0, 1, 1, 1, 8, 4096, data)
80 res.QueryClientIds(b, specs)
81 }); n != 0 {
82 t.Fatal(n)
83 }
84 if b.Err() != nil {
85 t.Fatal(b.Err())
86 }
87}