| 1 |
package scan |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"math/rand" |
| 11 |
) |
| 12 |
|
| 13 |
func TestPeek(t *testing.T) { |
| 14 |
tcases := []struct { |
| 15 |
name string |
| 16 |
in string |
| 17 |
peeked byte |
| 18 |
}{{ |
| 19 |
"empty", "", 0, |
| 20 |
}, { |
| 21 |
"123", "123", '1', |
| 22 |
}} |
| 23 |
|
| 24 |
for _, tc := range tcases { |
| 25 |
t.Run(tc.name, func(t *testing.T) { |
| 26 |
b := Bytes(tc.in) |
| 27 |
peeked := b.Peek() |
| 28 |
if string(b) != tc.in { |
| 29 |
t.Errorf("left: got: %s, want: %s", string(b), tc.in) |
| 30 |
} |
| 31 |
if peeked != tc.peeked { |
| 32 |
t.Errorf("peeked: got: %c, want: %c", peeked, tc.peeked) |
| 33 |
} |
| 34 |
}) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestPop(t *testing.T) { |
| 39 |
tcases := []struct { |
| 40 |
name string |
| 41 |
in string |
| 42 |
popped byte |
| 43 |
left string |
| 44 |
}{{ |
| 45 |
"empty", "", 0, "", |
| 46 |
}, { |
| 47 |
"123", "123", '1', "23", |
| 48 |
}} |
| 49 |
|
| 50 |
for _, tc := range tcases { |
| 51 |
t.Run(tc.name, func(t *testing.T) { |
| 52 |
b := Bytes(tc.in) |
| 53 |
popped := b.Pop() |
| 54 |
if string(b) != tc.left { |
| 55 |
t.Errorf("left: got: %s, want: %s", string(b), tc.left) |
| 56 |
} |
| 57 |
if popped != tc.popped { |
| 58 |
t.Errorf("popped: got: %c, want: %c", popped, tc.popped) |
| 59 |
} |
| 60 |
}) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestPopN(t *testing.T) { |
| 65 |
tcases := []struct { |
| 66 |
name string |
| 67 |
in string |
| 68 |
n int |
| 69 |
popped string |
| 70 |
left string |
| 71 |
}{{ |
| 72 |
"empty", "", 0, "", "", |
| 73 |
}, { |
| 74 |
"1,0", "1", 0, "", "1", |
| 75 |
}, { |
| 76 |
"12,0", "12", 0, "", "12", |
| 77 |
}, { |
| 78 |
"1,1", "1", 1, "1", "", |
| 79 |
}, { |
| 80 |
"12,1", "12", 1, "1", "2", |
| 81 |
}, { |
| 82 |
"123,1", "123", 1, "1", "23", |
| 83 |
}, { |
| 84 |
"123,2", "123", 2, "12", "3", |
| 85 |
}, { |
| 86 |
"123,3", "123", 3, "123", "", |
| 87 |
}, { |
| 88 |
"123,4", "123", 4, "", "123", |
| 89 |
}} |
| 90 |
|
| 91 |
for _, tc := range tcases { |
| 92 |
t.Run(tc.name, func(t *testing.T) { |
| 93 |
b := Bytes(tc.in) |
| 94 |
popped := b.PopN(tc.n) |
| 95 |
if string(b) != tc.left { |
| 96 |
t.Errorf("left: got: %s, want: %s", string(b), tc.left) |
| 97 |
} |
| 98 |
if string(popped) != tc.popped { |
| 99 |
t.Errorf("popped: got: %s, want: %s", string(popped), tc.popped) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
} |
| 104 |
func TestTrim(t *testing.T) { |
| 105 |
tcases := []struct { |
| 106 |
name string |
| 107 |
in string |
| 108 |
left string |
| 109 |
right string |
| 110 |
}{{ |
| 111 |
"empty", "", "", "", |
| 112 |
}, { |
| 113 |
"one space", " ", "", "", |
| 114 |
}, { |
| 115 |
"all spaces", " \r\n\t\x0c", "", "", |
| 116 |
}, { |
| 117 |
"one char and spaces", " \r\n\t\x0ca \r\n\t\x0c", "a \r\n\t\x0c", " \r\n\t\x0ca", |
| 118 |
}, { |
| 119 |
"one char", "a", "a", "a", |
| 120 |
}, { |
| 121 |
|
| 122 |
"unicode space ogham", " ", " ", " ", |
| 123 |
}, { |
| 124 |
|
| 125 |
"unicode em space", "\u2003", "\u2003", "\u2003", |
| 126 |
}} |
| 127 |
|
| 128 |
for _, tc := range tcases { |
| 129 |
t.Run(tc.name, func(t *testing.T) { |
| 130 |
b := Bytes(tc.in) |
| 131 |
b.TrimLWS() |
| 132 |
if string(b) != tc.left { |
| 133 |
t.Errorf("left: got: %s, want: %s", string(b), tc.left) |
| 134 |
} |
| 135 |
|
| 136 |
b = Bytes(tc.in) |
| 137 |
b.TrimRWS() |
| 138 |
if string(b) != tc.right { |
| 139 |
t.Errorf("right: got: %s, want: %s", string(b), tc.right) |
| 140 |
} |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestFirstNonWS(t *testing.T) { |
| 146 |
tcases := []struct { |
| 147 |
name string |
| 148 |
in string |
| 149 |
c byte |
| 150 |
}{{ |
| 151 |
"empty", "", 0x00, |
| 152 |
}, { |
| 153 |
"all ws", " ", 0x00, |
| 154 |
}, { |
| 155 |
"first char", "a", 'a', |
| 156 |
}, { |
| 157 |
"second char", " a", 'a', |
| 158 |
}, { |
| 159 |
"space then nil", " \x00", 0x00, |
| 160 |
}} |
| 161 |
|
| 162 |
for _, tc := range tcases { |
| 163 |
t.Run(tc.name, func(t *testing.T) { |
| 164 |
b := Bytes(tc.in) |
| 165 |
c := b.FirstNonWS() |
| 166 |
if c != tc.c { |
| 167 |
t.Errorf("got: %x, want: %x", c, tc.c) |
| 168 |
} |
| 169 |
}) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestAdvance(t *testing.T) { |
| 174 |
tcases := []struct { |
| 175 |
name string |
| 176 |
in string |
| 177 |
advance int |
| 178 |
want string |
| 179 |
shouldDo bool |
| 180 |
}{{ |
| 181 |
"empty 0", "", 0, "", true, |
| 182 |
}, { |
| 183 |
"empty 1", "", 1, "", false, |
| 184 |
}, { |
| 185 |
"empty -1", "", -1, "", false, |
| 186 |
}, { |
| 187 |
"123 0", "123", 0, "123", true, |
| 188 |
}, { |
| 189 |
"123 -1", "123", -1, "123", false, |
| 190 |
}, { |
| 191 |
"123 1", "123", 1, "23", true, |
| 192 |
}, { |
| 193 |
"123 4", "123", 4, "123", false, |
| 194 |
}} |
| 195 |
|
| 196 |
for _, tc := range tcases { |
| 197 |
t.Run(tc.name, func(t *testing.T) { |
| 198 |
b := Bytes(tc.in) |
| 199 |
did := b.Advance(tc.advance) |
| 200 |
if did != tc.shouldDo { |
| 201 |
t.Errorf("got: %t, want: %t", did, tc.shouldDo) |
| 202 |
} |
| 203 |
if string(b) != tc.want { |
| 204 |
t.Errorf("got: %s, want: %s", string(b), tc.want) |
| 205 |
} |
| 206 |
}) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestLine(t *testing.T) { |
| 211 |
tcases := []struct { |
| 212 |
name string |
| 213 |
in string |
| 214 |
line string |
| 215 |
leftover string |
| 216 |
}{{ |
| 217 |
"empty", "", "", "", |
| 218 |
}, { |
| 219 |
"one line", "abc", "abc", "", |
| 220 |
}, { |
| 221 |
"just a \\n", "\n", "", "", |
| 222 |
}, { |
| 223 |
"just two \\n", "\n\n", "", "\n", |
| 224 |
}, { |
| 225 |
"one line with \\n", "abc\n", "abc", "", |
| 226 |
}, { |
| 227 |
"two lines", "abc\ndef", "abc", "def", |
| 228 |
}, { |
| 229 |
"two lines with \\n", "abc\ndef\n", "abc", "def\n", |
| 230 |
}, { |
| 231 |
"drops final cr", "abc\r", "abc", "", |
| 232 |
}, { |
| 233 |
"cr inside line", "abc\rdef", "abc\rdef", "", |
| 234 |
}, { |
| 235 |
"nl and cr", "\n\r", "", "\r", |
| 236 |
}} |
| 237 |
|
| 238 |
for _, tc := range tcases { |
| 239 |
t.Run(tc.name, func(t *testing.T) { |
| 240 |
b := Bytes(tc.in) |
| 241 |
line := b.Line() |
| 242 |
if string(line) != tc.line { |
| 243 |
t.Errorf("line: got: %s, want: %s", line, []byte(tc.line)) |
| 244 |
} |
| 245 |
if string(b) != tc.leftover { |
| 246 |
t.Errorf("leftover: got: %s, want: %s", b, []byte(tc.leftover)) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
s := bufio.NewScanner(strings.NewReader(tc.in)) |
| 251 |
s.Scan() |
| 252 |
if string(line) != s.Text() { |
| 253 |
t.Errorf("Bytes.Line not like bufio.Scanner") |
| 254 |
} |
| 255 |
}) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func TestPopUntil(t *testing.T) { |
| 260 |
tcases := []struct { |
| 261 |
name string |
| 262 |
in string |
| 263 |
untilAny string |
| 264 |
popped string |
| 265 |
leftover string |
| 266 |
}{{ |
| 267 |
"empty", "", "", "", "", |
| 268 |
}, { |
| 269 |
"empty with until", "", "123", "", "", |
| 270 |
}, { |
| 271 |
"until empty", "123", "", "123", "", |
| 272 |
}, { |
| 273 |
"until 1", "123", "1", "", "123", |
| 274 |
}, { |
| 275 |
"until 2", "123", "2", "1", "23", |
| 276 |
}, { |
| 277 |
"until 3", "123", "3", "12", "3", |
| 278 |
}, { |
| 279 |
"until 4", "123", "4", "123", "", |
| 280 |
}, { |
| 281 |
"multiple untilAny", "123", "32", "1", "23", |
| 282 |
}} |
| 283 |
|
| 284 |
for _, tc := range tcases { |
| 285 |
t.Run(tc.name, func(t *testing.T) { |
| 286 |
b := Bytes(tc.in) |
| 287 |
popped := b.PopUntil([]byte(tc.untilAny)...) |
| 288 |
if string(popped) != tc.popped { |
| 289 |
t.Errorf("popped: got: %s, want: %s", popped, []byte(tc.popped)) |
| 290 |
} |
| 291 |
if string(b) != tc.leftover { |
| 292 |
t.Errorf("leftover: got: %s, want: %s", b, []byte(tc.leftover)) |
| 293 |
} |
| 294 |
}) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func TestReadSlice(t *testing.T) { |
| 299 |
tcases := []struct { |
| 300 |
name string |
| 301 |
in string |
| 302 |
stopAt byte |
| 303 |
popped string |
| 304 |
leftover string |
| 305 |
}{{ |
| 306 |
"both empty", "", 0, "", "", |
| 307 |
}, { |
| 308 |
"stop at not found", "abc", 'd', "abc", "", |
| 309 |
}, { |
| 310 |
"stop at the end", "abc", 'c', "abc", "", |
| 311 |
}, { |
| 312 |
"stop at in the middle", "abcdef", 'c', "abc", "def", |
| 313 |
}, { |
| 314 |
"stop at the beginning", "abcdef", 'a', "a", "bcdef", |
| 315 |
}, { |
| 316 |
"just one char", "a", 'a', "a", "", |
| 317 |
}, { |
| 318 |
"same char twice", "aa", 'a', "a", "a", |
| 319 |
}} |
| 320 |
|
| 321 |
for _, tc := range tcases { |
| 322 |
t.Run(tc.name, func(t *testing.T) { |
| 323 |
b := Bytes(tc.in) |
| 324 |
got := b.ReadSlice(tc.stopAt) |
| 325 |
if tc.popped != string(got) { |
| 326 |
t.Errorf("popped got: %s, want: %s", got, tc.popped) |
| 327 |
} |
| 328 |
if tc.leftover != string(b) { |
| 329 |
t.Errorf("leftover got: %s, want: %s", string(b), tc.leftover) |
| 330 |
} |
| 331 |
}) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func TestUint16(t *testing.T) { |
| 336 |
tcases := []struct { |
| 337 |
name string |
| 338 |
in []byte |
| 339 |
res uint16 |
| 340 |
ok bool |
| 341 |
}{{ |
| 342 |
"empty", nil, 0, false, |
| 343 |
}, { |
| 344 |
"too short", []byte{0}, 0, false, |
| 345 |
}, { |
| 346 |
"just enough", []byte{1, 0}, 1, true, |
| 347 |
}, { |
| 348 |
"longer", []byte{1, 0, 2}, 1, true, |
| 349 |
}} |
| 350 |
|
| 351 |
for _, tc := range tcases { |
| 352 |
t.Run(tc.name, func(t *testing.T) { |
| 353 |
b := Bytes(tc.in) |
| 354 |
res, ok := b.Uint16() |
| 355 |
if res != tc.res { |
| 356 |
t.Errorf("got: %d, want: %d", res, tc.res) |
| 357 |
} |
| 358 |
if ok != tc.ok { |
| 359 |
t.Errorf("ok: got: %t, want: %t", ok, tc.ok) |
| 360 |
} |
| 361 |
}) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
var searchTestcases = []struct { |
| 366 |
name string |
| 367 |
haystack string |
| 368 |
needle string |
| 369 |
flags Flags |
| 370 |
expectIdx int |
| 371 |
expectLen int |
| 372 |
}{{ |
| 373 |
"empty", "", "", 0, 0, 0, |
| 374 |
}, { |
| 375 |
"empty cws", "", "", CompactWS, 0, 0, |
| 376 |
}, { |
| 377 |
"empty ic", "", "", IgnoreCase, 0, 0, |
| 378 |
}, { |
| 379 |
"just haystack", "abc", "", 0, 0, 0, |
| 380 |
}, { |
| 381 |
"just haystack cws", "abc", "", CompactWS, 0, 0, |
| 382 |
}, { |
| 383 |
"just haystack ic", "abc", "", IgnoreCase, 0, 0, |
| 384 |
}, { |
| 385 |
"just needle", "", "abc", 0, -1, 0, |
| 386 |
}, { |
| 387 |
"just needle cws", "", "abc", CompactWS, -1, 0, |
| 388 |
}, { |
| 389 |
"just needle ic", "", "abc", IgnoreCase, -1, 0, |
| 390 |
}, { |
| 391 |
"simple", "abc", "abc", 0, 0, 3, |
| 392 |
}, { |
| 393 |
"not found", "abc", "def", 0, -1, 0, |
| 394 |
}, { |
| 395 |
"simple cws", "abc", "abc", CompactWS, 0, 3, |
| 396 |
}, { |
| 397 |
"simple ic", "abc", "abc", IgnoreCase, 0, 3, |
| 398 |
}, { |
| 399 |
"ic 1 upper", "aBc", "ABC", IgnoreCase, 0, 3, |
| 400 |
}, { |
| 401 |
"ic prefixed", "aaBcß", "ABC", IgnoreCase, 1, 3, |
| 402 |
}, { |
| 403 |
"ic prefixed utf8", "ßaBcß", "ABC", IgnoreCase, 2, 3ong |
| 404 |
}, { |
| 405 |
"simple cws|ic", " a", " A", CompactWS | IgnoreCase, 0, 3, |
| 406 |
}, { |
| 407 |
"simple cws|ic with suffix and prefix", "a ab", " A", CompactWS | IgnoreCase, 1, 3, |
| 408 |
}, { |
| 409 |
"trailing space in input", "a a ", " A", CompactWS | IgnoreCase, 1, 3, |
| 410 |
}, { |
| 411 |
"empty haystack with needle cws|ic", "", "abc", CompactWS | IgnoreCase, -1, 0, |
| 412 |
}, { |
| 413 |
"empty haystack with needle cws", "", "abc", CompactWS, -1, 0, |
| 414 |
}} |
| 415 |
|
| 416 |
func TestSearch(t *testing.T) { |
| 417 |
for _, tc := range searchTestcases { |
| 418 |
t.Run(tc.name, func(t *testing.T) { |
| 419 |
b := Bytes(tc.haystack) |
| 420 |
i, l := b.Search([]byte(tc.needle), tc.flags) |
| 421 |
if i != tc.expectIdx || l != tc.expectLen { |
| 422 |
t.Errorf("want: %d,%d got: %d,%d", tc.expectIdx, tc.expectLen, i, l) |
| 423 |
} |
| 424 |
}) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
func FuzzSearch(f *testing.F) { |
| 429 |
for _, tc := range searchTestcases { |
| 430 |
f.Add([]byte(tc.haystack), []byte(tc.needle), int(tc.flags)) |
| 431 |
} |
| 432 |
f.Fuzz(func(t *testing.T, haystack, needle []byte, flags int) { |
| 433 |
b := Bytes(haystack) |
| 434 |
b.Search(needle, Flags(flags)%CompactWS|IgnoreCase|FullWord) |
| 435 |
}) |
| 436 |
} |
| 437 |
|
| 438 |
var matchTestcases = []struct { |
| 439 |
name string |
| 440 |
b string |
| 441 |
p string |
| 442 |
flags Flags |
| 443 |
expectLen int |
| 444 |
}{{ |
| 445 |
"empty", "", "", 0, 0, |
| 446 |
}, { |
| 447 |
"empty compact ws", "", "", CompactWS, 0, |
| 448 |
}, { |
| 449 |
"empty ic", "", "", IgnoreCase, 0, |
| 450 |
}, { |
| 451 |
"empty cws|ic", "", "", CompactWS | IgnoreCase, 0, |
| 452 |
}, { |
| 453 |
"simple", "abc", "abc", 0, 3, |
| 454 |
}, { |
| 455 |
"simple cws|ic", "abc", "abc", CompactWS | IgnoreCase, 3, |
| 456 |
}, { |
| 457 |
"not found", "abc", "def", 0, -1, |
| 458 |
}, { |
| 459 |
"simple cws", "abc", "abc", CompactWS, 3, |
| 460 |
}, { |
| 461 |
"simple ic", "abc", "abc", IgnoreCase, 3, |
| 462 |
}, { |
| 463 |
"ic 1 upper", "aBc", "ABC", IgnoreCase, 3, |
| 464 |
}, { |
| 465 |
"ic prefixed", "aaBcß", "ABC", IgnoreCase, -1, |
| 466 |
}, { |
| 467 |
"ic prefixed utf8", "ßaBcß", "ABC", IgnoreCase, -1, |
| 468 |
}, { |
| 469 |
"simple cws|ic with space", " a", " A", CompactWS | IgnoreCase, 3, |
| 470 |
}, { |
| 471 |
"trailing space in input", "a a ", " A", CompactWS | IgnoreCase, -1, |
| 472 |
}, { |
| 473 |
"empty b with p", "", "/bin/bash", CompactWS, -1, |
| 474 |
}, { |
| 475 |
"failing", "asd", "asdf", IgnoreCase, -1, |
| 476 |
}, { |
| 477 |
"exact fw", "abc", "abc", FullWord, 3, |
| 478 |
}, { |
| 479 |
"success fw", "abc ", "abc", FullWord, 3, |
| 480 |
}, { |
| 481 |
"fail fw", "abcd", "abc", FullWord, -1, |
| 482 |
}} |
| 483 |
|
| 484 |
func TestMatch(t *testing.T) { |
| 485 |
for _, tc := range matchTestcases { |
| 486 |
t.Run(tc.name, func(t *testing.T) { |
| 487 |
b := Bytes(tc.b) |
| 488 |
l := b.Match([]byte(tc.p), tc.flags) |
| 489 |
if l != tc.expectLen { |
| 490 |
t.Errorf("want: %d got: %d", tc.expectLen, l) |
| 491 |
} |
| 492 |
}) |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
func FuzzMatch(f *testing.F) { |
| 497 |
for _, tc := range matchTestcases { |
| 498 |
f.Add([]byte(tc.b), []byte(tc.p), int(tc.flags)) |
| 499 |
} |
| 500 |
f.Fuzz(func(t *testing.T, b, p []byte, flags int) { |
| 501 |
Bytes(b).Match(p, Flags(flags)%CompactWS|IgnoreCase|FullWord) |
| 502 |
}) |
| 503 |
} |
| 504 |
|
| 505 |
func BenchmarkMatch(b *testing.B) { |
| 506 |
r := rand.New(rand.NewSource(0)) |
| 507 |
randData := make([]byte, 1024) |
| 508 |
if _, err := io.ReadFull(r, randData); err != io.ErrUnexpectedEOF && err != nil { |
| 509 |
b.Fatal(err) |
| 510 |
} |
| 511 |
b.ReportAllocs() |
| 512 |
for _, f := range []Flags{ |
| 513 |
0, |
| 514 |
CompactWS, |
| 515 |
IgnoreCase, |
| 516 |
FullWord, |
| 517 |
} { |
| 518 |
b.Run(fmt.Sprintf("%d", f), func(b *testing.B) { |
| 519 |
for b.Loop() { |
| 520 |
Bytes(randData).Match(randData, f) |
| 521 |
} |
| 522 |
}) |
| 523 |
} |
| 524 |
} |