Debian Codegraph @

internal/decoder/string_scan_amd64_test.go

raw
1 package decoder
2
3 import (
4 "math/rand"
5 "strings"
6 "testing"
7 "unsafe"
8
9 "github.com/goccy/go-json/internal/runtime"
10 )
11
12 func TestIndexStringSpecialAVX2(t *testing.T) {
13 // The index and the non-ASCII bytes before it are the ones of a scan byte by byte, for every length and
14 // every position of the special byte, including in the last block, which overlaps the previous one.
15 if !runtime.HasAVX2 {
16 t.Skip("AVX2 is not supported")
17 }
18 r := rand.New(rand.NewSource(1))
19 plain := []byte("azAZ09 !#~\x7f\x80\xe3\xff")
20 special := []byte{'"', '\\', 0x00, 0x1f, '\n'}
21 for n := 32; n <= 200; n++ {
22 for trial := 0; trial < 60; trial++ {
23 buf := make([]byte, n+1)
24 for i := 0; i < n; i++ {
25 buf[i] = plain[r.Intn(len(plain))]
26 if r.Intn(4) != 0 && buf[i] >= 0x80 {
27 buf[i] = 'a'
28 }
29 }
30 if trial%3 != 0 {
31 buf[r.Intn(n)] = special[r.Intn(len(special))]
32 }
33 wantIndex, wantHigh := n, false
34 for i := 0; i < n; i++ {
35 c := buf[i]
36 if c == '"' || c == '\\' || c < 0x20 {
37 wantIndex = i
38 break
39 }
40 if c >= 0x80 {
41 wantHigh = true
42 }
43 }
44 index, high := indexStringSpecialAVX2(unsafe.Pointer(&buf[0]), n)
45 if index != wantIndex || (high != 0) != wantHigh {
46 t.Fatalf("%q: got %d %v, want %d %v", buf[:n], index, high != 0, wantIndex, wantHigh)
47 }
48 }
49 }
50 }
51
52 func TestIndexStringSpecialHigh(t *testing.T) {
53 // The bytes which are not ASCII before the special byte are told in the form of the scan by words: msb.
54 if !runtime.HasAVX2 {
55 t.Skip("AVX2 is not supported")
56 }
57 for n := 32; n <= 100; n++ {
58 for pos := 0; pos < n-1; pos++ {
59 buf := []byte(strings.Repeat("a", n))
60 buf[pos] = 0xff
61 buf[n-1] = '"'
62 _, high, ok := indexStringSpecial(unsafe.Pointer(&buf[0]), n)
63 if !ok || high&msb == 0 {
64 t.Fatalf("%d %d: got %x", n, pos, high)
65 }
66 }
67 }
68 }