| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
"unicode/utf8" |
| 10 |
) |
| 11 |
|
| 12 |
// Reference test using regex: this will identify bad test cases and is more |
| 13 |
// accurate than our reference Index (since it might have bugs). |
| 14 |
func TestIndexRegex(t *testing.T) { |
| 15 |
index := func(s, sep string) int { |
| 16 |
i := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(sep)).FindStringIndex(s) |
| 17 |
if len(i) == 2 { |
| 18 |
return i[0] |
| 19 |
} |
| 20 |
return -1 |
| 21 |
} |
| 22 |
tests := append(indexTests, unicodeIndexTests...) |
| 23 |
runIndexTests(t, index, "Regexp", tests, false) |
| 24 |
} |
| 25 |
|
| 26 |
// Make sure our reference implementation is correct. |
| 27 |
func TestIndexRunesReference(t *testing.T) { |
| 28 |
// Test that the Index tests are valid |
| 29 |
reference := func(s, sep string) int { |
| 30 |
return IndexRunesReference([]rune(s), []rune(sep)) |
| 31 |
} |
| 32 |
tests := append(indexTests, unicodeIndexTests...) |
| 33 |
runIndexTests(t, reference, "IndexReference", tests, false) |
| 34 |
} |
| 35 |
|
| 36 |
// Reference test using regex: this will identify bad test cases and is more |
| 37 |
// accurate than our reference LastIndex (since it might have bugs). |
| 38 |
func TestLastIndexRegex(t *testing.T) { |
| 39 |
index := func(s, sep string) int { |
| 40 |
a := regexp.MustCompile(`(?i)`+regexp.QuoteMeta(sep)).FindAllStringIndex(s, -1) |
| 41 |
if len(a) != 0 { |
| 42 |
return a[len(a)-1][0] |
| 43 |
} |
| 44 |
return -1 |
| 45 |
} |
| 46 |
runIndexTests(t, index, "Regexp", lastIndexTests, false) |
| 47 |
} |
| 48 |
|
| 49 |
func TestLastIndexReference(t *testing.T) { |
| 50 |
reference := func(s, sep string) int { |
| 51 |
return LastIndexRunesReference([]rune(s), []rune(sep)) |
| 52 |
} |
| 53 |
runIndexTests(t, reference, "LastIndexReference", lastIndexTests, false) |
| 54 |
} |
| 55 |
|
| 56 |
func TestLastIndexReference_XXX(t *testing.T) { |
| 57 |
want := 0 |
| 58 |
got := IndexRunesReference([]rune{'\uFFFD', '\uFFFD', '\u0061', '\u0043', '\uFFFD'}, []rune{'\uFFFD'}) |
| 59 |
if got != want { |
| 60 |
t.Errorf("got: %d want: %d", got, want) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestHasPrefix(t *testing.T) { |
| 65 |
// Make sure the tests cases are valid |
| 66 |
for i, test := range prefixTests { |
| 67 |
s := []rune(test.s) |
| 68 |
prefix := []rune(test.prefix) |
| 69 |
out, exhausted := HasPrefixRunes(s, prefix) |
| 70 |
if out != test.out || exhausted != test.exhausted { |
| 71 |
t.Errorf("invalid test: %d: %+v", i, test) |
| 72 |
} |
| 73 |
if n := len(prefix); len(s) >= n { |
| 74 |
if out := strings.EqualFold(string(s[:n]), string(prefix)); out != test.out { |
| 75 |
t.Errorf("strings.EqualFold(%q, %q) = %t; want: %t", |
| 76 |
test.s, test.prefix, out, test.out) |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
if t.Failed() { |
| 81 |
t.Fatal("Invalid tests cases") |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestEqualRune(t *testing.T) { |
| 86 |
rr := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 87 |
fails := 0 |
| 88 |
invalid := 0 |
| 89 |
for i := 0; i < 100_000; i++ { |
| 90 |
sr := rr.Int31n(utf8.MaxRune * 2) |
| 91 |
tr := sr |
| 92 |
switch n := rr.Int31n(100); { |
| 93 |
case n <= 10: |
| 94 |
tr = utf8.RuneError |
| 95 |
case n <= 50: |
| 96 |
tr = rr.Int31n(utf8.MaxRune * 2) |
| 97 |
} |
| 98 |
if !utf8.ValidRune(sr) || !utf8.ValidRune(tr) { |
| 99 |
invalid++ |
| 100 |
} |
| 101 |
got := EqualRune(sr, tr) |
| 102 |
want := strings.EqualFold(string(sr), string(tr)) |
| 103 |
if got != want { |
| 104 |
t.Errorf("EqualRune(%U, %U) = %t; want: %t", sr, tr, got, want) |
| 105 |
fails++ |
| 106 |
if fails >= 50 { |
| 107 |
t.Fatal("Too many errors") |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestEncodedLen(t *testing.T) { |
| 114 |
s := []rune{'a', utf8.RuneError, 'Ꝃ', '𞤔', utf8.MaxRune + 1, utf8.UTFMax + 1} |
| 115 |
want := len(string(s)) |
| 116 |
got := EncodedLen(s) |
| 117 |
if got != want { |
| 118 |
t.Errorf("EncodedLen(%q) = %d want: %d", string(s), got, want) |
| 119 |
} |
| 120 |
} |