| 1 |
package ascii |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"unicode/utf8" |
| 9 |
|
| 10 |
"github.com/segmentio/asm/internal/buffer" |
| 11 |
) |
| 12 |
|
| 13 |
var testStrings = [...]string{ |
| 14 |
"", |
| 15 |
"a", |
| 16 |
"ab", |
| 17 |
"abc", |
| 18 |
"abcd", |
| 19 |
"hello", |
| 20 |
"Hello World!", |
| 21 |
"Hello\"World!", |
| 22 |
"Hello\\World!", |
| 23 |
"Hello\nWorld!", |
| 24 |
"Hello\rWorld!", |
| 25 |
"Hello\tWorld!", |
| 26 |
"Hello\bWorld!", |
| 27 |
"Hello\fWorld!", |
| 28 |
"H~llo World!", |
| 29 |
"H~llo", |
| 30 |
"你好", |
| 31 |
"~", |
| 32 |
"\x80", |
| 33 |
"\x7F", |
| 34 |
"\xFF", |
| 35 |
"a string of 16B.", |
| 36 |
"an invalid string of 32B. \x00......", |
| 37 |
"some kind of long string with only ascii characters.", |
| 38 |
"some kind of long string with a non-ascii character at the end.\xff", |
| 39 |
strings.Repeat("1234567890", 1000), |
| 40 |
} |
| 41 |
|
| 42 |
var testStringsUTF8 []string |
| 43 |
|
| 44 |
func init() { |
| 45 |
for _, test := range testStrings { |
| 46 |
if utf8.ValidString(test) { |
| 47 |
testStringsUTF8 = append(testStringsUTF8, test) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func testString(s string, f func(byte) bool) bool { |
| 53 |
for i := range s { |
| 54 |
if !f(s[i]) { |
| 55 |
return false |
| 56 |
} |
| 57 |
} |
| 58 |
return true |
| 59 |
} |
| 60 |
|
| 61 |
func testValid(s string) bool { |
| 62 |
return testString(s, ValidByte) |
| 63 |
} |
| 64 |
|
| 65 |
func testValidPrint(s string) bool { |
| 66 |
return testString(s, ValidPrintByte) |
| 67 |
} |
| 68 |
|
| 69 |
func TestValid(t *testing.T) { |
| 70 |
buf := newBuffer(t, 1024) |
| 71 |
|
| 72 |
for _, input := range [2][]byte{buf.ProtectHead(), buf.ProtectTail()} { |
| 73 |
for i := 0; i < len(input); i++ { |
| 74 |
in := input[:i+1] |
| 75 |
|
| 76 |
for b := 0; b <= 0xFF; b++ { |
| 77 |
in[i] = byte(b) |
| 78 |
if b < 0x80 { |
| 79 |
if !Valid(in) { |
| 80 |
t.Errorf("should be valid: %v", in) |
| 81 |
} |
| 82 |
} else { |
| 83 |
if Valid(in) { |
| 84 |
t.Errorf("should not be valid: %v", in) |
| 85 |
} |
| 86 |
} |
| 87 |
in[i] = 'x' |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestValidPrint(t *testing.T) { |
| 94 |
buf := newBuffer(t, 1024) |
| 95 |
|
| 96 |
for _, input := range [2][]byte{buf.ProtectHead(), buf.ProtectTail()} { |
| 97 |
for i := 0; i < len(input); i++ { |
| 98 |
in := input[:i+1] |
| 99 |
|
| 100 |
for b := 0; b <= 0xFF; b++ { |
| 101 |
in[i] = byte(b) |
| 102 |
if ' ' <= b && b <= '~' { |
| 103 |
if !ValidPrint(in) { |
| 104 |
t.Errorf("should be valid: %v", in) |
| 105 |
} |
| 106 |
} else { |
| 107 |
if ValidPrint(in) { |
| 108 |
t.Errorf("should not be valid: %v", in) |
| 109 |
} |
| 110 |
} |
| 111 |
in[i] = 'x' |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestValidString(t *testing.T) { |
| 118 |
testValidationFunction(t, testValid, ValidString) |
| 119 |
} |
| 120 |
|
| 121 |
func TestValidPrintString(t *testing.T) { |
| 122 |
testValidationFunction(t, testValidPrint, ValidPrintString) |
| 123 |
} |
| 124 |
|
| 125 |
func testValidationFunction(t *testing.T, reference, function func(string) bool) { |
| 126 |
for _, test := range testStrings { |
| 127 |
t.Run(limit(test), func(t *testing.T) { |
| 128 |
expect := reference(test) |
| 129 |
|
| 130 |
if valid := function(test); expect != valid { |
| 131 |
t.Errorf("expected %t but got %t", expect, valid) |
| 132 |
} |
| 133 |
}) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func BenchmarkValid(b *testing.B) { |
| 138 |
benchmarkValidationFunction(b, ValidString) |
| 139 |
} |
| 140 |
|
| 141 |
func BenchmarkValidPrint(b *testing.B) { |
| 142 |
benchmarkValidationFunction(b, ValidPrintString) |
| 143 |
} |
| 144 |
|
| 145 |
func benchmarkValidationFunction(b *testing.B, function func(string) bool) { |
| 146 |
for _, test := range testStrings { |
| 147 |
b.Run(limit(test), func(b *testing.B) { |
| 148 |
for i := 0; i < b.N; i++ { |
| 149 |
_ = function(test) |
| 150 |
} |
| 151 |
b.SetBytes(int64(len(test))) |
| 152 |
}) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestHasPrefixFoldString(t *testing.T) { |
| 157 |
for _, test := range testStringsUTF8 { |
| 158 |
t.Run(limit(test), func(t *testing.T) { |
| 159 |
prefix := test |
| 160 |
if len(prefix) > 0 { |
| 161 |
prefix = prefix[:len(prefix)/2] |
| 162 |
} |
| 163 |
upper := strings.ToUpper(prefix) |
| 164 |
lower := strings.ToLower(prefix) |
| 165 |
|
| 166 |
if !HasPrefixFoldString(test, prefix) { |
| 167 |
t.Errorf("%q does not match %q", test, prefix) |
| 168 |
} |
| 169 |
|
| 170 |
if !HasPrefixFoldString(test, upper) { |
| 171 |
t.Errorf("%q does not match %q", test, upper) |
| 172 |
} |
| 173 |
|
| 174 |
if !HasPrefixFoldString(test, lower) { |
| 175 |
t.Errorf("%q does not match %q", test, lower) |
| 176 |
} |
| 177 |
}) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func TestHasSuffixFoldString(t *testing.T) { |
| 182 |
for _, test := range testStringsUTF8 { |
| 183 |
t.Run(limit(test), func(t *testing.T) { |
| 184 |
suffix := test |
| 185 |
if len(suffix) > 0 { |
| 186 |
suffix = suffix[len(suffix)/2:] |
| 187 |
} |
| 188 |
upper := strings.ToUpper(suffix) |
| 189 |
lower := strings.ToLower(suffix) |
| 190 |
|
| 191 |
if !HasSuffixFoldString(test, suffix) { |
| 192 |
t.Errorf("%q does not match %q", test, suffix) |
| 193 |
} |
| 194 |
|
| 195 |
if !HasSuffixFoldString(test, upper) { |
| 196 |
t.Errorf("%q does not match %q", test, upper) |
| 197 |
} |
| 198 |
|
| 199 |
if !HasSuffixFoldString(test, lower) { |
| 200 |
t.Errorf("%q does not match %q", test, lower) |
| 201 |
} |
| 202 |
}) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestEqualFoldString(t *testing.T) { |
| 207 |
// Only test valid UTF-8 otherwise ToUpper/ToLower will convert invalid |
| 208 |
// characters to UTF-8 placeholders, which breaks the case-insensitive |
| 209 |
// equality. |
| 210 |
for _, test := range testStringsUTF8 { |
| 211 |
t.Run(limit(test), func(t *testing.T) { |
| 212 |
upper := strings.ToUpper(test) |
| 213 |
lower := strings.ToLower(test) |
| 214 |
|
| 215 |
if !EqualFoldString(test, test) { |
| 216 |
t.Errorf("%q does not match %q", test, test) |
| 217 |
} |
| 218 |
|
| 219 |
if !EqualFoldString(test, upper) { |
| 220 |
t.Errorf("%q does not match %q", test, upper) |
| 221 |
} |
| 222 |
|
| 223 |
if !EqualFoldString(test, lower) { |
| 224 |
t.Errorf("%q does not match %q", test, lower) |
| 225 |
} |
| 226 |
|
| 227 |
if len(test) > 1 { |
| 228 |
reverse := make([]byte, len(test)) |
| 229 |
for i := range reverse { |
| 230 |
reverse[i] = test[len(test)-(i+1)] |
| 231 |
} |
| 232 |
|
| 233 |
if EqualFoldString(test, string(reverse)) { |
| 234 |
t.Errorf("%q matches %q", test, reverse) |
| 235 |
} |
| 236 |
} |
| 237 |
}) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func newBuffer(t *testing.T, n int) buffer.Buffer { |
| 242 |
buf, err := buffer.New(n) |
| 243 |
if err != nil { |
| 244 |
t.Fatal(err) |
| 245 |
} |
| 246 |
return buf |
| 247 |
} |
| 248 |
|
| 249 |
func TestEqualFold(t *testing.T) { |
| 250 |
ubuf := newBuffer(t, 1024) |
| 251 |
defer ubuf.Release() |
| 252 |
|
| 253 |
lbuf := newBuffer(t, 1024) |
| 254 |
defer lbuf.Release() |
| 255 |
|
| 256 |
mbuf := newBuffer(t, 1024) |
| 257 |
defer mbuf.Release() |
| 258 |
|
| 259 |
upper := ubuf.ProtectHead() |
| 260 |
lower := lbuf.ProtectTail() |
| 261 |
mixed := mbuf.ProtectHead() |
| 262 |
|
| 263 |
for i := 0; i < len(upper); i++ { |
| 264 |
u := upper[:i+1] |
| 265 |
l := lower[:i+1] |
| 266 |
m := mixed[:i+1] |
| 267 |
|
| 268 |
u[i] = 'Z' |
| 269 |
l[i] = 'a' |
| 270 |
if EqualFold(l, u) { |
| 271 |
t.Errorf("%q matches %q", l, u) |
| 272 |
} |
| 273 |
|
| 274 |
u[i] = byte(i % 128) |
| 275 |
l[i] = byte(i % 128) |
| 276 |
if 'A' <= l[i] && l[i] <= 'Z' { |
| 277 |
l[i] += 32 |
| 278 |
} |
| 279 |
if 'a' <= u[i] && u[i] <= 'z' { |
| 280 |
u[i] -= 32 |
| 281 |
} |
| 282 |
|
| 283 |
if rand.Int()%2 == 0 { |
| 284 |
m[i] = l[i] |
| 285 |
} else { |
| 286 |
m[i] = u[i] |
| 287 |
} |
| 288 |
|
| 289 |
if !EqualFold(l, u) { |
| 290 |
t.Errorf("%q does not match %q", l, u) |
| 291 |
} |
| 292 |
|
| 293 |
if !EqualFold(l, m) { |
| 294 |
t.Errorf("%q does not match %q", l, m) |
| 295 |
} |
| 296 |
|
| 297 |
if !EqualFold(u, m) { |
| 298 |
t.Errorf("%q does not match %q", u, m) |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func genValidString(n int, ch byte) (s string) { |
| 304 |
for i := 0; i < n; i++ { |
| 305 |
s += string(byte(i%26) + ch) |
| 306 |
} |
| 307 |
return |
| 308 |
} |
| 309 |
|
| 310 |
func genEqualStrings(n int) (l string, u string) { |
| 311 |
return genValidString(n, 'A'), genValidString(n, 'a') |
| 312 |
} |
| 313 |
|
| 314 |
func BenchmarkEqualFoldString(b *testing.B) { |
| 315 |
sizes := [...]int{7, 8, 9, 15, 16, 17, 31, 32, 33, 512, 2000} |
| 316 |
|
| 317 |
for _, s := range sizes { |
| 318 |
lower, upper := genEqualStrings(s) |
| 319 |
b.Run(fmt.Sprintf("%04d", s), func(b *testing.B) { |
| 320 |
for i := 0; i < b.N; i++ { |
| 321 |
EqualFoldString(lower, upper) |
| 322 |
} |
| 323 |
b.SetBytes(int64(len(lower) + len(upper))) |
| 324 |
}) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
func BenchmarkValidString(b *testing.B) { |
| 329 |
sizes := [...]int{7, 8, 9, 15, 16, 17, 31, 32, 33, 512, 2000} |
| 330 |
|
| 331 |
for _, s := range sizes { |
| 332 |
str := genValidString(s, 'a') |
| 333 |
b.Run(fmt.Sprintf("%04d", s), func(b *testing.B) { |
| 334 |
for i := 0; i < b.N; i++ { |
| 335 |
ValidString(str) |
| 336 |
} |
| 337 |
b.SetBytes(int64(s)) |
| 338 |
}) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func BenchmarkValidPrintString(b *testing.B) { |
| 343 |
sizes := [...]int{7, 8, 9, 15, 16, 17, 31, 32, 33, 512, 2000} |
| 344 |
|
| 345 |
for _, s := range sizes { |
| 346 |
str := genValidString(s, 'a') |
| 347 |
b.Run(fmt.Sprintf("%04d", s), func(b *testing.B) { |
| 348 |
for i := 0; i < b.N; i++ { |
| 349 |
ValidPrintString(str) |
| 350 |
} |
| 351 |
b.SetBytes(int64(s)) |
| 352 |
}) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
func limit(s string) string { |
| 357 |
if len(s) > 17 { |
| 358 |
return s[:17] + "..." |
| 359 |
} |
| 360 |
return s |
| 361 |
} |