|
| 1 | +//go:build arm64 && !purego |
| 2 | + |
| 3 | +package ahocorasick |
| 4 | + |
| 5 | +// Kernel-level tests for the vector search kernels: exhaustive differential |
| 6 | +// against scalar oracles across sizes, positions, and pair distances. The |
| 7 | +// guard-page test proving the read contracts lives in |
| 8 | +// index_kernel_unix_test.go (mmap/mprotect are unix-only). |
| 9 | + |
| 10 | +import ( |
| 11 | + "math/rand" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func oraclePair2(p []byte, m int, a, b byte, d int) int { |
| 16 | + m &^= 31 |
| 17 | + for i := 0; i < m; i++ { |
| 18 | + if p[i] == a && p[i+d] == b { |
| 19 | + return i |
| 20 | + } |
| 21 | + } |
| 22 | + return -1 |
| 23 | +} |
| 24 | + |
| 25 | +func oracleOr2(p []byte, m int, a, b byte) int { |
| 26 | + m &^= 31 |
| 27 | + for i := 0; i < m; i++ { |
| 28 | + if p[i] == a || p[i] == b { |
| 29 | + return i |
| 30 | + } |
| 31 | + } |
| 32 | + return -1 |
| 33 | +} |
| 34 | + |
| 35 | +func TestIndexPair2Exhaustive(t *testing.T) { |
| 36 | + // Sizes crossing every block boundary; hit planted at every position; |
| 37 | + // distances covering typical pattern spans plus the beyond-one-block |
| 38 | + // cases (d is bounded only by pattern length, so d > 32 must place |
| 39 | + // stream B more than a whole block past stream A). |
| 40 | + for _, m := range []int{0, 1, 31, 32, 33, 63, 64, 65, 96, 127, 130} { |
| 41 | + for _, d := range []int{1, 2, 5, 7, 15, 32, 33, 64, 100} { |
| 42 | + buf := make([]byte, m+d) |
| 43 | + for i := range buf { |
| 44 | + buf[i] = 'x' |
| 45 | + } |
| 46 | + // No hit anywhere. |
| 47 | + if got := indexPair2(append([]byte{}, buf...), m, 'A', 'B', d); m > 0 && got != -1 { |
| 48 | + t.Fatalf("m=%d d=%d empty: got %d want -1", m, d, got) |
| 49 | + } |
| 50 | + for pos := 0; pos < m; pos++ { |
| 51 | + p := append([]byte{}, buf...) |
| 52 | + p[pos] = 'A' |
| 53 | + p[pos+d] = 'B' |
| 54 | + want := oraclePair2(p, m, 'A', 'B', d) |
| 55 | + got := indexPair2(p, m, 'A', 'B', d) |
| 56 | + if got != want { |
| 57 | + t.Fatalf("m=%d d=%d pos=%d: got %d want %d", m, d, pos, got, want) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestIndexPair2EqualBytes(t *testing.T) { |
| 65 | + // a == b and overlapping planted pairs. |
| 66 | + p := make([]byte, 128) |
| 67 | + for i := range p { |
| 68 | + p[i] = 'x' |
| 69 | + } |
| 70 | + p[40], p[43] = 'z', 'z' |
| 71 | + if got, want := indexPair2(p, 96, 'z', 'z', 3), 40; got != want { |
| 72 | + t.Fatalf("got %d want %d", got, want) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestIndexOr2Exhaustive(t *testing.T) { |
| 77 | + for _, m := range []int{0, 1, 31, 32, 33, 64, 65, 127, 130} { |
| 78 | + buf := make([]byte, m) |
| 79 | + for i := range buf { |
| 80 | + buf[i] = 'x' |
| 81 | + } |
| 82 | + for pos := 0; pos < m; pos++ { |
| 83 | + for _, c := range []byte{'A', 'B'} { |
| 84 | + p := append([]byte{}, buf...) |
| 85 | + p[pos] = c |
| 86 | + want := oracleOr2(p, m, 'A', 'B') |
| 87 | + got := indexOr2(p, m, 'A', 'B') |
| 88 | + if got != want { |
| 89 | + t.Fatalf("m=%d pos=%d c=%c: got %d want %d", m, pos, c, got, want) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestIndexKernelsRandomDifferential(t *testing.T) { |
| 97 | + rng := rand.New(rand.NewSource(77)) |
| 98 | + for iter := 0; iter < 5000; iter++ { |
| 99 | + m := rng.Intn(300) |
| 100 | + d := 1 + rng.Intn(16) |
| 101 | + p := make([]byte, m+d) |
| 102 | + for i := range p { |
| 103 | + p[i] = byte('a' + rng.Intn(4)) // small alphabet: many hits |
| 104 | + } |
| 105 | + a, b := byte('a'+rng.Intn(4)), byte('a'+rng.Intn(4)) |
| 106 | + if m > 0 { |
| 107 | + if got, want := indexPair2(p, m, a, b, d), oraclePair2(p, m, a, b, d); got != want { |
| 108 | + t.Fatalf("pair m=%d d=%d a=%c b=%c: got %d want %d (%q)", m, d, a, b, got, want, p) |
| 109 | + } |
| 110 | + if got, want := indexOr2(p, m, a, b), oracleOr2(p, m, a, b); got != want { |
| 111 | + t.Fatalf("or m=%d a=%c b=%c: got %d want %d (%q)", m, a, b, got, want, p) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments