| 1 |
package bytcase |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"unicode/utf8" |
| 6 |
|
| 7 |
"github.com/charlievieth/strcase/internal/test" |
| 8 |
) |
| 9 |
|
| 10 |
func TestUnicodeVersion(t *testing.T) { |
| 11 |
test.UnicodeVersion(t, UnicodeVersion) |
| 12 |
} |
| 13 |
|
| 14 |
func TestCompare(t *testing.T) { |
| 15 |
test.Compare(t, test.ByteIndexFunc(Compare)) |
| 16 |
} |
| 17 |
|
| 18 |
func TestEqualFold(t *testing.T) { |
| 19 |
test.EqualFold(t, test.ByteContainsFunc(EqualFold)) |
| 20 |
} |
| 21 |
|
| 22 |
func TestIndex(t *testing.T) { |
| 23 |
test.Index(t, test.ByteIndexFunc(Index)) |
| 24 |
} |
| 25 |
|
| 26 |
func TestIndexUnicode(t *testing.T) { |
| 27 |
test.IndexUnicode(t, test.ByteIndexFunc(Index)) |
| 28 |
} |
| 29 |
|
| 30 |
func TestIndexInvalid(t *testing.T) { |
| 31 |
test.IndexInvalid(t, test.ByteIndexFunc(Index)) |
| 32 |
} |
| 33 |
|
| 34 |
func TestIndexRuneIndexParity(t *testing.T) { |
| 35 |
test.IndexRuneIndexParity(t, test.ByteIndexFunc(Index), |
| 36 |
test.ByteIndexRuneFunc(IndexRune)) |
| 37 |
} |
| 38 |
|
| 39 |
func TestIndexAllAssigned(t *testing.T) { |
| 40 |
test.IndexAllAssigned(t, |
| 41 |
test.TestFunc{Name: "Compare", Index: test.ByteIndexFunc(Compare)}, |
| 42 |
test.TestFunc{Name: "Contains", Contains: test.ByteContainsFunc(Contains)}, |
| 43 |
test.TestFunc{Name: "EqualFold", Contains: test.ByteContainsFunc(EqualFold)}, |
| 44 |
test.TestFunc{Name: "HasPrefix", Contains: test.ByteContainsFunc(HasPrefix)}, |
| 45 |
test.TestFunc{Name: "HasSuffix", Contains: test.ByteContainsFunc(HasSuffix)}, |
| 46 |
test.TestFunc{Name: "Index", Index: test.ByteIndexFunc(Index)}, |
| 47 |
test.TestFunc{Name: "LastIndex", Index: test.ByteIndexFunc(LastIndex)}, |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
// Test our use of bytealg.IndexString |
| 52 |
func TestIndexNumeric(t *testing.T) { |
| 53 |
test.IndexNumeric(t, test.ByteIndexFunc(Index)) |
| 54 |
} |
| 55 |
|
| 56 |
// Extensively test the handling of Kelvin K since it is three times the size |
| 57 |
// of ASCII [Kk] it requires special handling. |
| 58 |
func TestIndexKelvin(t *testing.T) { |
| 59 |
test.IndexKelvin(t, test.ByteIndexFunc(Index)) |
| 60 |
} |
| 61 |
|
| 62 |
// Test the Rabin-Karp fallback logic directly since not all test cases will |
| 63 |
// trigger it. |
| 64 |
func TestRabinKarp(t *testing.T) { |
| 65 |
test.Index(t, test.WrapRabinKarp( |
| 66 |
test.ByteIndexFunc(indexRabinKarpUnicode), |
| 67 |
)) |
| 68 |
} |
| 69 |
|
| 70 |
// Test the Rabin-Karp fallback logic directly since not all test cases will |
| 71 |
// trigger it. |
| 72 |
func TestRabinKarpUnicode(t *testing.T) { |
| 73 |
test.IndexUnicode(t, test.WrapRabinKarp( |
| 74 |
test.ByteIndexFunc(indexRabinKarpUnicode), |
| 75 |
)) |
| 76 |
} |
| 77 |
|
| 78 |
func TestBruteForceIndexUnicode(t *testing.T) { |
| 79 |
test.IndexUnicode(t, func(s, substr string) int { |
| 80 |
n := len(substr) |
| 81 |
var size int |
| 82 |
if n > 0 { |
| 83 |
if substr[0] < utf8.RuneSelf { |
| 84 |
size = 1 |
| 85 |
} else { |
| 86 |
_, size = utf8.DecodeRuneInString(substr) |
| 87 |
} |
| 88 |
} |
| 89 |
if len(s) == 0 || len(substr) == 0 || n == size { |
| 90 |
// Can't use brute-force here |
| 91 |
return Index([]byte(s), []byte(substr)) |
| 92 |
} |
| 93 |
return bruteForceIndexUnicode([]byte(s), []byte(substr)) |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
func TestIndexAllocs(t *testing.T) { |
| 98 |
var ( |
| 99 |
haystack = []byte("test世界İ") |
| 100 |
n1 = []byte("世界İ") |
| 101 |
n2 = []byte("t世") |
| 102 |
n3 = []byte("test世界İ") |
| 103 |
) |
| 104 |
allocs := testing.AllocsPerRun(1000, func() { |
| 105 |
if i := Index(haystack, n1); i != 4 { |
| 106 |
t.Fatalf("'s' at %d; want 4", i) |
| 107 |
} |
| 108 |
if i := Index(haystack, n2); i != 3 { |
| 109 |
t.Fatalf("'世' at %d; want 3", i) |
| 110 |
} |
| 111 |
if i := Index(haystack, n3); i != 0 { |
| 112 |
t.Fatalf("'İ' at %d; want 0", i) |
| 113 |
} |
| 114 |
}) |
| 115 |
if allocs != 0 { |
| 116 |
t.Errorf("expected no allocations, got %f", allocs) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func TestContains(t *testing.T) { |
| 121 |
test.Contains(t, test.ByteContainsFunc(Contains)) |
| 122 |
} |
| 123 |
|
| 124 |
func TestContainsAny(t *testing.T) { |
| 125 |
test.ContainsAny(t, test.ByteContainsFunc(ContainsAny)) |
| 126 |
} |
| 127 |
|
| 128 |
func TestLastIndex(t *testing.T) { |
| 129 |
test.LastIndex(t, test.ByteIndexFunc(LastIndex)) |
| 130 |
} |
| 131 |
|
| 132 |
func TestLastIndexInvalid(t *testing.T) { |
| 133 |
test.LastIndexInvalid(t, test.ByteIndexFunc(LastIndex)) |
| 134 |
} |
| 135 |
|
| 136 |
func TestIndexRune(t *testing.T) { |
| 137 |
test.IndexRune(t, test.ByteIndexRuneFunc(IndexRune)) |
| 138 |
} |
| 139 |
|
| 140 |
func TestIndexRuneAllocs(t *testing.T) { |
| 141 |
haystack := []byte("test世界İ") |
| 142 |
allocs := testing.AllocsPerRun(1000, func() { |
| 143 |
if i := IndexRune(haystack, 's'); i != 2 { |
| 144 |
t.Fatalf("'s' at %d; want 2", i) |
| 145 |
} |
| 146 |
if i := IndexRune(haystack, '世'); i != 4 { |
| 147 |
t.Fatalf("'世' at %d; want 4", i) |
| 148 |
} |
| 149 |
if i := IndexRune(haystack, 'İ'); i != 10 { |
| 150 |
t.Fatalf("'İ' at %d; want 10", i) |
| 151 |
} |
| 152 |
}) |
| 153 |
if allocs != 0 { |
| 154 |
t.Errorf("expected no allocations, got %f", allocs) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestIndexRuneCase(t *testing.T) { |
| 159 |
test.IndexRuneCase(t, test.ByteIndexRuneFunc(indexRuneCase)) |
| 160 |
} |
| 161 |
|
| 162 |
func TestContainsRune(t *testing.T) { |
| 163 |
test.ContainsRune(t, func(s string, r rune) bool { |
| 164 |
return ContainsRune([]byte(s), r) |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
func TestLastIndexRune(t *testing.T) { |
| 169 |
test.LastIndexRune(t, test.ByteIndexRuneFunc(lastIndexRune)) |
| 170 |
} |
| 171 |
|
| 172 |
func TestIndexByte(t *testing.T) { |
| 173 |
test.IndexByte(t, test.ByteIndexByte(IndexByte)) |
| 174 |
} |
| 175 |
|
| 176 |
func TestLastIndexByte(t *testing.T) { |
| 177 |
test.LastIndexByte(t, test.ByteIndexByte(LastIndexByte)) |
| 178 |
} |
| 179 |
|
| 180 |
func TestIndexNonASCII(t *testing.T) { |
| 181 |
test.IndexNonASCII(t, func(s string) int { |
| 182 |
return IndexNonASCII([]byte(s)) |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
func TestContainsNonASCII(t *testing.T) { |
| 187 |
test.ContainsNonASCII(t, func(s string) bool { |
| 188 |
return ContainsNonASCII([]byte(s)) |
| 189 |
}) |
| 190 |
} |
| 191 |
|
| 192 |
func TestHasPrefix(t *testing.T) { |
| 193 |
test.HasPrefix(t, test.BytePrefixFunc(hasPrefixUnicode)) |
| 194 |
} |
| 195 |
|
| 196 |
func TestTrimPrefix(t *testing.T) { |
| 197 |
test.TrimPrefix(t, test.ByteTrimFunc(TrimPrefix)) |
| 198 |
} |
| 199 |
|
| 200 |
func TestHasSuffix(t *testing.T) { |
| 201 |
test.HasSuffix(t, func(s, suffix string) bool { |
| 202 |
return HasSuffix([]byte(s), []byte(suffix)) |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
func TestTrimSuffix(t *testing.T) { |
| 207 |
test.TrimSuffix(t, test.ByteTrimFunc(TrimSuffix)) |
| 208 |
} |
| 209 |
|
| 210 |
func TestCount(t *testing.T) { |
| 211 |
test.Count(t, test.ByteIndexFunc(Count)) |
| 212 |
} |
| 213 |
|
| 214 |
func TestTestIndexAny(t *testing.T) { |
| 215 |
test.IndexAny(t, test.ByteIndexFunc(IndexAny)) |
| 216 |
} |
| 217 |
|
| 218 |
func TestTestLastIndexAny(t *testing.T) { |
| 219 |
test.LastIndexAny(t, test.ByteIndexFunc(LastIndexAny)) |
| 220 |
} |
| 221 |
|
| 222 |
func TestCut(t *testing.T) { |
| 223 |
test.Cut(t, func(s, sep string) (before, after string, found bool) { |
| 224 |
b, a, ok := Cut([]byte(s), []byte(sep)) |
| 225 |
return string(b), string(a), ok |
| 226 |
}) |
| 227 |
} |
| 228 |
|
| 229 |
func TestCutPrefix(t *testing.T) { |
| 230 |
test.CutPrefix(t, func(s, sep string) (after string, found bool) { |
| 231 |
b, ok := CutPrefix([]byte(s), []byte(sep)) |
| 232 |
return string(b), ok |
| 233 |
}) |
| 234 |
} |
| 235 |
|
| 236 |
func TestCutSuffix(t *testing.T) { |
| 237 |
test.CutSuffix(t, func(s, sep string) (before string, found bool) { |
| 238 |
b, ok := CutSuffix([]byte(s), []byte(sep)) |
| 239 |
return string(b), ok |
| 240 |
}) |
| 241 |
} |
| 242 |
|
| 243 |
// Ensure that strings.EqualFold does not match 'İ' (U+0130) and ASCII 'i' or 'I'. |
| 244 |
// This is mostly a sanity check. |
| 245 |
func TestLatinCapitalLetterIWithDotAbove(t *testing.T) { |
| 246 |
test.LatinCapitalLetterIWithDotAbove(t, test.ByteIndexFunc(Compare)) |
| 247 |
} |
| 248 |
|
| 249 |
func TestNonLetterASCII(t *testing.T) { |
| 250 |
test.NonLetterASCII(t, func(s string) bool { |
| 251 |
return nonLetterASCII([]byte(s)) |
| 252 |
}) |
| 253 |
} |
| 254 |
|
| 255 |
func TestIndexFuzz(t *testing.T) { |
| 256 |
test.IndexFuzz(t, test.ByteIndexFunc(Index)) |
| 257 |
} |
| 258 |
|
| 259 |
func TestLastIndexFuzz(t *testing.T) { |
| 260 |
test.LastIndexFuzz(t, test.ByteIndexFunc(LastIndex)) |
| 261 |
} |
| 262 |
|
| 263 |
func TestHasPrefixFuzz(t *testing.T) { |
| 264 |
test.HasPrefixFuzz(t, func(s, prefix string) (bool, bool) { |
| 265 |
return hasPrefixUnicode([]byte(s), []byte(prefix)) |
| 266 |
}) |
| 267 |
} |
| 268 |
|
| 269 |
func TestHasSuffixFuzz(t *testing.T) { |
| 270 |
test.HasSuffixFuzz(t, test.ByteContainsFunc(HasSuffix)) |
| 271 |
} |
| 272 |
|
| 273 |
func TestCompareFuzz(t *testing.T) { |
| 274 |
test.CompareFuzz(t, test.ByteIndexFunc(Compare)) |
| 275 |
} |
| 276 |
|
| 277 |
func TestEqualFoldFuzz(t *testing.T) { |
| 278 |
test.EqualFoldFuzz(t, |
| 279 |
test.TestFunc{Name: "Contains", Contains: test.ByteContainsFunc(Contains)}, |
| 280 |
test.TestFunc{Name: "EqualFold", Contains: test.ByteContainsFunc(EqualFold)}, |
| 281 |
test.TestFunc{Name: "HasPrefix", Contains: test.ByteContainsFunc(HasPrefix)}, |
| 282 |
test.TestFunc{Name: "HasSuffix", Contains: test.ByteContainsFunc(HasSuffix)}, |
| 283 |
) |
| 284 |
} |