Debian Codegraph @

src/algo/indexbyte2_test.go

raw
1 package algo
2
3 import (
4 "bytes"
5 "testing"
6 )
7
8 func TestIndexByteTwo(t *testing.T) {
9 tests := []struct {
10 name string
11 s string
12 b1 byte
13 b2 byte
14 want int
15 }{
16 {"empty", "", 'a', 'b', -1},
17 {"single_b1", "a", 'a', 'b', 0},
18 {"single_b2", "b", 'a', 'b', 0},
19 {"single_none", "c", 'a', 'b', -1},
20 {"b1_first", "xaxb", 'a', 'b', 1},
21 {"b2_first", "xbxa", 'a', 'b', 1},
22 {"same_byte", "xxa", 'a', 'a', 2},
23 {"at_end", "xxxxa", 'a', 'b', 4},
24 {"not_found", "xxxxxxxx", 'a', 'b', -1},
25 {"long_b1_at_3000", string(make([]byte, 3000)) + "a" + string(make([]byte, 1000)), 'a', 'b', 3000},
26 {"long_b2_at_3000", string(make([]byte, 3000)) + "b" + string(make([]byte, 1000)), 'a', 'b', 3000},
27 }
28
29 for _, tt := range tests {
30 t.Run(tt.name, func(t *testing.T) {
31 got := IndexByteTwo([]byte(tt.s), tt.b1, tt.b2)
32 if got != tt.want {
33 t.Errorf("IndexByteTwo(%q, %c, %c) = %d, want %d", tt.s[:min(len(tt.s), 40)], tt.b1, tt.b2, got, tt.want)
34 }
35 })
36 }
37
38 // Exhaustive test: compare against loop reference for various lengths,
39 // including sizes around SIMD block boundaries (16, 32, 64).
40 for n := 0; n <= 256; n++ {
41 data := make([]byte, n)
42 for i := range data {
43 data[i] = byte('c' + (i % 20))
44 }
45 // Test with match at every position
46 for pos := 0; pos < n; pos++ {
47 for _, b := range []byte{'A', 'B'} {
48 data[pos] = b
49 got := IndexByteTwo(data, 'A', 'B')
50 want := loopIndexByteTwo(data, 'A', 'B')
51 if got != want {
52 t.Fatalf("IndexByteTwo(len=%d, match=%c@%d) = %d, want %d", n, b, pos, got, want)
53 }
54 data[pos] = byte('c' + (pos % 20))
55 }
56 }
57 // Test with no match
58 got := IndexByteTwo(data, 'A', 'B')
59 if got != -1 {
60 t.Fatalf("IndexByteTwo(len=%d, no match) = %d, want -1", n, got)
61 }
62 // Test with both bytes present
63 if n >= 2 {
64 data[n/3] = 'A'
65 data[n*2/3] = 'B'
66 got := IndexByteTwo(data, 'A', 'B')
67 want := loopIndexByteTwo(data, 'A', 'B')
68 if got != want {
69 t.Fatalf("IndexByteTwo(len=%d, both@%d,%d) = %d, want %d", n, n/3, n*2/3, got, want)
70 }
71 data[n/3] = byte('c' + ((n / 3) % 20))
72 data[n*2/3] = byte('c' + ((n * 2 / 3) % 20))
73 }
74 }
75 }
76
77 func TestLastIndexByteTwo(t *testing.T) {
78 tests := []struct {
79 name string
80 s string
81 b1 byte
82 b2 byte
83 want int
84 }{
85 {"empty", "", 'a', 'b', -1},
86 {"single_b1", "a", 'a', 'b', 0},
87 {"single_b2", "b", 'a', 'b', 0},
88 {"single_none", "c", 'a', 'b', -1},
89 {"b1_last", "xbxa", 'a', 'b', 3},
90 {"b2_last", "xaxb", 'a', 'b', 3},
91 {"same_byte", "axx", 'a', 'a', 0},
92 {"at_start", "axxxx", 'a', 'b', 0},
93 {"both_present", "axbx", 'a', 'b', 2},
94 {"not_found", "xxxxxxxx", 'a', 'b', -1},
95 {"long_b1_at_3000", string(make([]byte, 3000)) + "a" + string(make([]byte, 1000)), 'a', 'b', 3000},
96 {"long_b2_at_end", string(make([]byte, 4000)) + "b", 'a', 'b', 4000},
97 }
98
99 for _, tt := range tests {
100 t.Run(tt.name, func(t *testing.T) {
101 got := lastIndexByteTwo([]byte(tt.s), tt.b1, tt.b2)
102 if got != tt.want {
103 t.Errorf("lastIndexByteTwo(%q, %c, %c) = %d, want %d", tt.s[:min(len(tt.s), 40)], tt.b1, tt.b2, got, tt.want)
104 }
105 })
106 }
107
108 // Exhaustive test against loop reference
109 for n := 0; n <= 256; n++ {
110 data := make([]byte, n)
111 for i := range data {
112 data[i] = byte('c' + (i % 20))
113 }
114 for pos := 0; pos < n; pos++ {
115 for _, b := range []byte{'A', 'B'} {
116 data[pos] = b
117 got := lastIndexByteTwo(data, 'A', 'B')
118 want := refLastIndexByteTwo(data, 'A', 'B')
119 if got != want {
120 t.Fatalf("lastIndexByteTwo(len=%d, match=%c@%d) = %d, want %d", n, b, pos, got, want)
121 }
122 data[pos] = byte('c' + (pos % 20))
123 }
124 }
125 // No match
126 got := lastIndexByteTwo(data, 'A', 'B')
127 if got != -1 {
128 t.Fatalf("lastIndexByteTwo(len=%d, no match) = %d, want -1", n, got)
129 }
130 // Both bytes present
131 if n >= 2 {
132 data[n/3] = 'A'
133 data[n*2/3] = 'B'
134 got := lastIndexByteTwo(data, 'A', 'B')
135 want := refLastIndexByteTwo(data, 'A', 'B')
136 if got != want {
137 t.Fatalf("lastIndexByteTwo(len=%d, both@%d,%d) = %d, want %d", n, n/3, n*2/3, got, want)
138 }
139 data[n/3] = byte('c' + ((n / 3) % 20))
140 data[n*2/3] = byte('c' + ((n * 2 / 3) % 20))
141 }
142 }
143 }
144
145 func FuzzIndexByteTwo(f *testing.F) {
146 f.Add([]byte("hello world"), byte('o'), byte('l'))
147 f.Add([]byte(""), byte('a'), byte('b'))
148 f.Add([]byte("aaa"), byte('a'), byte('a'))
149 f.Fuzz(func(t *testing.T, data []byte, b1, b2 byte) {
150 got := IndexByteTwo(data, b1, b2)
151 want := loopIndexByteTwo(data, b1, b2)
152 if got != want {
153 t.Errorf("IndexByteTwo(len=%d, b1=%d, b2=%d) = %d, want %d", len(data), b1, b2, got, want)
154 }
155 })
156 }
157
158 func FuzzLastIndexByteTwo(f *testing.F) {
159 f.Add([]byte("hello world"), byte('o'), byte('l'))
160 f.Add([]byte(""), byte('a'), byte('b'))
161 f.Add([]byte("aaa"), byte('a'), byte('a'))
162 f.Fuzz(func(t *testing.T, data []byte, b1, b2 byte) {
163 got := lastIndexByteTwo(data, b1, b2)
164 want := refLastIndexByteTwo(data, b1, b2)
165 if got != want {
166 t.Errorf("lastIndexByteTwo(len=%d, b1=%d, b2=%d) = %d, want %d", len(data), b1, b2, got, want)
167 }
168 })
169 }
170
171 // Reference implementations for correctness checking
172 func refIndexByteTwo(s []byte, b1, b2 byte) int {
173 i1 := bytes.IndexByte(s, b1)
174 if i1 == 0 {
175 return 0
176 }
177 scope := s
178 if i1 > 0 {
179 scope = s[:i1]
180 }
181 if i2 := bytes.IndexByte(scope, b2); i2 >= 0 {
182 return i2
183 }
184 return i1
185 }
186
187 func loopIndexByteTwo(s []byte, b1, b2 byte) int {
188 for i, b := range s {
189 if b == b1 || b == b2 {
190 return i
191 }
192 }
193 return -1
194 }
195
196 func refLastIndexByteTwo(s []byte, b1, b2 byte) int {
197 for i := len(s) - 1; i >= 0; i-- {
198 if s[i] == b1 || s[i] == b2 {
199 return i
200 }
201 }
202 return -1
203 }
204
205 func benchIndexByteTwo(b *testing.B, size int, pos int) {
206 data := make([]byte, size)
207 for i := range data {
208 data[i] = byte('a' + (i % 20))
209 }
210 data[pos] = 'Z'
211
212 type impl struct {
213 name string
214 fn func([]byte, byte, byte) int
215 }
216 impls := []impl{
217 {"asm", IndexByteTwo},
218 {"2xIndexByte", refIndexByteTwo},
219 {"loop", loopIndexByteTwo},
220 }
221 for _, im := range impls {
222 b.Run(im.name, func(b *testing.B) {
223 for i := 0; i < b.N; i++ {
224 im.fn(data, 'Z', 'z')
225 }
226 })
227 }
228 }
229
230 func benchLastIndexByteTwo(b *testing.B, size int, pos int) {
231 data := make([]byte, size)
232 for i := range data {
233 data[i] = byte('a' + (i % 20))
234 }
235 data[pos] = 'Z'
236
237 type impl struct {
238 name string
239 fn func([]byte, byte, byte) int
240 }
241 impls := []impl{
242 {"asm", lastIndexByteTwo},
243 {"loop", refLastIndexByteTwo},
244 }
245 for _, im := range impls {
246 b.Run(im.name, func(b *testing.B) {
247 for i := 0; i < b.N; i++ {
248 im.fn(data, 'Z', 'z')
249 }
250 })
251 }
252 }
253
254 func BenchmarkIndexByteTwo_10(b *testing.B) { benchIndexByteTwo(b, 10, 8) }
255 func BenchmarkIndexByteTwo_100(b *testing.B) { benchIndexByteTwo(b, 100, 80) }
256 func BenchmarkIndexByteTwo_1000(b *testing.B) { benchIndexByteTwo(b, 1000, 800) }
257 func BenchmarkLastIndexByteTwo_10(b *testing.B) { benchLastIndexByteTwo(b, 10, 2) }
258 func BenchmarkLastIndexByteTwo_100(b *testing.B) { benchLastIndexByteTwo(b, 100, 20) }
259 func BenchmarkLastIndexByteTwo_1000(b *testing.B) { benchLastIndexByteTwo(b, 1000, 200) }