~mna/zzcsi

ref: v0.1.0 zzcsi/csi_test.go -rw-r--r-- 3.9 KiB
3a39bd0aMartin Angers add decode args, test, benchmark, example 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package zzcsi

import (
	"strings"
	"testing"
)

func TestFunc_NoArg(t *testing.T) {
	for i, seq := range csiSeqs {
		t.Run(string(seq), func(t *testing.T) {
			csi := CSI(i)
			want := strings.ReplaceAll(strings.ReplaceAll(string(seq), "\x01", ""), "\x02", "")
			got := csi.FuncString()
			if want != got {
				t.Fatalf("want %q, got %q", want, got)
			}
		})
	}
}

func TestFunc_Args(t *testing.T) {
	cases := []struct {
		csi  CSI
		args []int
		want string
	}{
		{InsCh, nil, "\x1b[@"},
		{InsCh, []int{1}, "\x1b[1@"},
		{InsCh, []int{123}, "\x1b[123@"},
		{InsCh, []int{2, 3, 4}, "\x1b[2@"},
		{CurPos, nil, "\x1b[;H"},
		{CurPos, []int{1}, "\x1b[1;H"},
		{CurPos, []int{1, 2}, "\x1b[1;2H"},
		{CurPos, []int{1, 2, 3}, "\x1b[1;2H"},
		{RstTitleMode, nil, "\x1b[>T"},
		{RstTitleMode, []int{1}, "\x1b[>1T"},
		{RstTitleMode, []int{123, 456}, "\x1b[>123;456T"},
		{RstTitleMode, []int{123, 456, 789}, "\x1b[>123;456;789T"},
		{ChAttrFgIRGB, nil, "\x1b[38;2;;;;m"},
		{ChAttrFgIRGB, []int{1}, "\x1b[38;2;1;;;m"},
		{ChAttrFgIRGB, []int{1, 2, 3}, "\x1b[38;2;1;2;3;m"},
		{ChAttrFgIRGB, []int{1, 2, 3, 4, 5}, "\x1b[38;2;1;2;3;4m"},
		{CSI(255), nil, ""},
	}

	buf := make([]byte, 128)
	for _, c := range cases {
		buf = buf[:0]
		t.Run(c.want, func(t *testing.T) {
			got := string(c.csi.AppendFunc(buf, c.args...))
			if c.want != got {
				t.Fatalf("want %q, got %q", c.want, got)
			}
		})
	}
}

func TestDecodeArgs(t *testing.T) {
	cases := []struct {
		in    string
		want  []uint64
		count int
	}{
		{"\x1b[0n", nil, 0},
		{"\x1b[0n", []uint64{0}, 1},
		{"\x1b[0n", []uint64{0, 0, 0}, 1},
		{"\x1b[0;1n", []uint64{0, 1, 0}, 2},
		{"\x1b[0;1;2n", []uint64{0, 1, 2}, 3},
		{"\x1b[0;1;2;3n", []uint64{0, 1, 2}, 3},
		{"\x1b[127n", []uint64{127, 0, 0}, 1},
		{"\x1b[127;87650n", []uint64{127, 87650, 0}, 2},
		{"\x1b[127; 1", []uint64{127, 0}, 1},
		{"\x1b[1;2;3", nil, 0},
		{"\x1b[1;2;3p", []uint64{1}, 1},
	}
	for _, c := range cases {
		t.Run(c.in, func(t *testing.T) {
			args := make([]*uint64, len(c.want))
			for i := range args {
				args[i] = new(uint64)
			}

			n := DecodeArgs([]byte(c.in), args...)
			if n != c.count {
				t.Fatalf("want %d args, got %d", c.count, n)
			}

			for j := 0; j < n; j++ {
				want, got := c.want[j], *args[j]
				if want != got {
					t.Fatalf("%d: want %d, got %d", j, want, got)
				}
			}
		})
	}
}

func TestIsCSI(t *testing.T) {
	for _, seq := range csiSeqs {
		if len(seq) == 0 {
			continue
		}

		t.Run(string(seq), func(t *testing.T) {
			if !IsCSI(seq) {
				t.Fatalf("sequence not detected as CSI: %q", seq)
			}
			if !IsCSIString(string(seq)) {
				t.Fatalf("string sequence not detected as CSI: %q", seq)
			}
		})

		// invert the sequence, should NOT be CSI
		iseq := make([]byte, len(seq))
		copy(iseq, seq)
		for i := len(iseq)/2 - 1; i >= 0; i-- {
			opp := len(iseq) - 1 - i
			iseq[i], iseq[opp] = iseq[opp], iseq[i]
		}

		t.Run(string(iseq), func(t *testing.T) {
			if IsCSI(iseq) {
				t.Fatalf("inverted sequence detected as CSI: %q", iseq)
			}
			if IsCSIString(string(iseq)) {
				t.Fatalf("inverted string sequence detected as CSI: %q", iseq)
			}
		})
	}
}

var (
	BenchmarkResultString string
	BenchmarkResultBytes  []byte
	BenchmarkResultInt    int
)

func BenchmarkCSI(b *testing.B) {
	b.Run("Func", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			BenchmarkResultBytes = ChLnCol.Func(12, 80)
		}
	})

	b.Run("FuncString", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			BenchmarkResultString = ChLnCol.FuncString(12, 80)
		}
	})

	b.Run("AppendFunc", func(b *testing.B) {
		buf := make([]byte, 0, 8)
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			BenchmarkResultBytes = ChLnCol.AppendFunc(buf, 12, 80)
		}
	})

	b.Run("DecodeArgs", func(b *testing.B) {
		var r, c uint64
		seq := []byte("\x1b[12;33R")
		b.ResetTimer()

		for i := 0; i < b.N; i++ {
			BenchmarkResultInt = DecodeArgs(seq, &r, &c)
			if r != 12 || c != 33 {
				b.Fatalf("got %d, %d", r, c)
			}
		}
	})
}