| 1 |
package bytealg |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
"unicode/utf8" |
| 7 |
) |
| 8 |
|
| 9 |
// These tests fail with strcasestr. |
| 10 |
var unicodeIndexTests = []IndexTest{ |
| 11 |
// Map Kelvin 'K' (U+212A) to lowercase latin 'k'. |
| 12 |
{"abcK@", "k@", 3}, |
| 13 |
|
| 14 |
// Map the long 'S' 'ſ' to 'S' and 's'. |
| 15 |
{"abcſ@", "s@", 3}, |
| 16 |
{"abcS@", "ſ@", 3}, |
| 17 |
|
| 18 |
// Test with a unicode prefix in the substr to make sure the unicode |
| 19 |
// implementation is correct. |
| 20 |
{"abc☻K@", "☻k@", 3}, |
| 21 |
{"abc☻S@", "☻ſ@", 3}, |
| 22 |
|
| 23 |
// Sep longer (in bytes) |
| 24 |
{"a\u212a", "a\u212a", 0}, |
| 25 |
{"a\u212a", "a\u212a\u212a", -1}, |
| 26 |
|
| 27 |
// WARN: fix these |
| 28 |
{"İ", "İ", 0}, |
| 29 |
{"İİ", "İİ", 0}, |
| 30 |
{"İİİİ", "İİ", 0}, |
| 31 |
{"İİİİİİ", "İİ", 0}, |
| 32 |
{"0123456789İİ", "İİ", 10}, |
| 33 |
{"01234567890123456789İİ", "İİ", 20}, |
| 34 |
{"İİ" + strings.Repeat("a", 64), "İİ" + strings.Repeat("a", 64), 0}, |
| 35 |
|
| 36 |
// Tests discovered with fuzzing |
| 37 |
{"4=K ", "=\u212a", 1}, |
| 38 |
{"I", "\u0131", -1}, |
| 39 |
|
| 40 |
// Evil strings discovered fuzzing. |
| 41 |
{ |
| 42 |
s: "32ⱭⱭⱭⱭⱭ45678890ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ23456789ⱭⱭⱭⱭⱭⱭⱭⱭⱭⱭ", |
| 43 |
sep: "0ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ234567890ⱭⱭⱭⱭⱭ234567890", |
| 44 |
out: -1, |
| 45 |
}, |
| 46 |
{ |
| 47 |
s: "<<\ua7ac\x02\ub680\U0010f410\U0002ac40\n\x15\u2126\ufa12\x14", |
| 48 |
sep: "<\ua7ac\x02\ub680\U0010f410\U0002ac40\n\x15\u03c9\ufa12", |
| 49 |
out: 1, |
| 50 |
}, |
| 51 |
{ |
| 52 |
s: "\U00024b8a\u2c65I\u7c12\u313a/A\u027d\u017f=\x05", |
| 53 |
sep: "\U00024b8a\u2c65I\u7c12\u313a/a\u2c64\u017f=", |
| 54 |
out: 0, |
| 55 |
}, |
| 56 |
{ |
| 57 |
s: "\U0002a433\u3577\U000230d6\U001024b4\u73f1\u56f0\U0002d7db\U0010e3ac\U000204ca\u2575~\u8825\U0002ba82\U0002c0e4\u743aK]", |
| 58 |
sep: "\u743a\u212a", |
| 59 |
out: 48, |
| 60 |
}, |
| 61 |
{ |
| 62 |
s: "z0\U0010640b\U0001f326k-k\U00100621\U000240ff\U000e013fl", |
| 63 |
sep: "\u212a-\u212a", |
| 64 |
out: 10, |
| 65 |
}, |
| 66 |
{ |
| 67 |
s: "\U0007279d\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd", |
| 68 |
sep: "\ufffd\ufffd\ufffd\ufffd\ufffd", |
| 69 |
out: 4, |
| 70 |
}, |
| 71 |
} |
| 72 |
|
| 73 |
// TODO: do we need this? |
| 74 |
func init() { |
| 75 |
p0 := strings.Repeat("\u212a", 64) |
| 76 |
p1 := strings.Repeat("K", 64) |
| 77 |
n := utf8.RuneLen('\u212a') |
| 78 |
for i := 2; i <= 64; i += 2 { |
| 79 |
s0 := p0[:i*n] |
| 80 |
s1 := p1[:i] |
| 81 |
unicodeIndexTests = append(unicodeIndexTests, IndexTest{s0, s1, 0}, IndexTest{s1, s0, 0}) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func testIndexNonASCII(t *testing.T, name string, fn func(s string) int) { |
| 86 |
const maxFailures = 80 |
| 87 |
|
| 88 |
index := func(s string) int { |
| 89 |
for i, r := range s { |
| 90 |
if r >= utf8.RuneSelf { |
| 91 |
return i |
| 92 |
} |
| 93 |
} |
| 94 |
return -1 |
| 95 |
} |
| 96 |
|
| 97 |
t.Run("IndexTests", func(t *testing.T) { |
| 98 |
fails := 0 |
| 99 |
tests := append([]IndexTest(nil), indexTests...) |
| 100 |
tests = append(tests, unicodeIndexTests...) |
| 101 |
for _, test := range tests { |
| 102 |
want := index(test.s) |
| 103 |
got := fn(test.s) |
| 104 |
if got != want { |
| 105 |
fails++ |
| 106 |
if fails <= maxFailures { |
| 107 |
t.Errorf("%s(%q) = %d; want: %d", name, test.s, got, want) |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if fails > 0 { |
| 113 |
t.Errorf("Failed: %d/%d", fails, len(tests)) |
| 114 |
} |
| 115 |
}) |
| 116 |
|
| 117 |
t.Run("LongString", func(t *testing.T) { |
| 118 |
fails := 0 |
| 119 |
|
| 120 |
long := strings.Repeat("a", 4096) + "βaβa" |
| 121 |
idx := index(long) |
| 122 |
for i := 0; i < len(long); i++ { |
| 123 |
s := long[i:] |
| 124 |
want := idx - i |
| 125 |
if want < 0 { |
| 126 |
want = index(s) |
| 127 |
} |
| 128 |
got := fn(s) |
| 129 |
if got != want { |
| 130 |
fails++ |
| 131 |
if fails <= maxFailures { |
| 132 |
t.Errorf("%s(long[%d:]) = %d; want: %d", name, i, got, want) |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if fails > 0 { |
| 138 |
t.Errorf("Failed: %d/%d", fails, len(long)) |
| 139 |
} |
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
func TestIndexNonASCII(t *testing.T) { |
| 144 |
testIndexNonASCII(t, "IndexNonASCII", IndexNonASCII) |
| 145 |
} |
| 146 |
|
| 147 |
func TestIndexByteNonASCII(t *testing.T) { |
| 148 |
testIndexNonASCII(t, "IndexByteNonASCII", func(s string) int { |
| 149 |
return IndexByteNonASCII([]byte(s)) |
| 150 |
}) |
| 151 |
} |
| 152 |
|
| 153 |
func benchIndexNonASCII(b *testing.B, sizes []int, f func(b *testing.B, n int)) { |
| 154 |
for _, n := range sizes { |
| 155 |
b.Run(valName(n), func(b *testing.B) { |
| 156 |
if len(bmbuf) < n { |
| 157 |
bmbuf = make([]byte, n) |
| 158 |
} |
| 159 |
b.SetBytes(int64(n)) |
| 160 |
f(b, n) |
| 161 |
}) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func BenchmarkIndexByteNonASCII(b *testing.B) { |
| 166 |
benchIndexNonASCII(b, indexSizes, bmIndexNonASCII(IndexByteNonASCII)) |
| 167 |
} |
| 168 |
|
| 169 |
func bmIndexNonASCII(index func([]byte) int) func(b *testing.B, n int) { |
| 170 |
return func(b *testing.B, n int) { |
| 171 |
buf := bmbuf[0:n] |
| 172 |
for i := 0; i < b.N; i++ { |
| 173 |
_ = index(buf) // Search for uppercase variant |
| 174 |
} |
| 175 |
} |
| 176 |
} |