package xkbcommon import ( "bytes" "errors" "io" "testing" "github.com/bnema/purego-xkbcommon/raw" ) func TestStateUTF8IntoBoundsAndLifetime(t *testing.T) { c := live(t) s := state(t, rules(t, c, "de")) var scratch [MaxUTF8Buffer]byte n, err := s.UTF8Into(WaylandKeycode(16), scratch[:]) if err != nil || !bytes.Equal(scratch[:n], []byte{'q'}) { t.Fatalf("ASCII length=%d err=%v", n, err) } mod, err := rules(t, c, "de").ModIndex("Mod5") if err != nil { t.Fatal(err) } if _, err := s.UpdateMask(1< 6 { t.Fatalf("UTF8Into allocations=%v", allocs) } } func TestUTF8IntoNilAndClosedReceiversClearScratch(t *testing.T) { var nilState *State var nilCompose *ComposeState buf := []byte{0xff, 0xff, 0xff} if n, err := nilState.UTF8Into(WaylandKeycode(16), buf); n != 0 || !errors.Is(err, ErrClosed) { t.Fatal(n, err) } if !bytes.Equal(buf, []byte{0, 0, 0}) { t.Fatal("nil state retained bytes") } for i := range buf { buf[i] = 0xff } if n, err := nilCompose.UTF8Into(buf); n != 0 || !errors.Is(err, ErrClosed) { t.Fatal(n, err) } if !bytes.Equal(buf, []byte{0, 0, 0}) { t.Fatal("nil compose retained bytes") } c := live(t) table, err := c.NewComposeTable("en_US.UTF-8") if err != nil { t.Fatal(err) } defer table.Close() compose, err := table.NewState() if err != nil { t.Fatal(err) } compose.Close() for i := range buf { buf[i] = 0xff } if n, err := compose.UTF8Into(buf); n != 0 || !errors.Is(err, ErrClosed) { t.Fatal(n, err) } if !bytes.Equal(buf, []byte{0, 0, 0}) { t.Fatal("closed compose retained bytes") } }