| 1 |
// Copyright 2023 Charlie Vieth. All rights reserved. |
| 2 |
// Use of this source code is governed by the MIT license. |
| 3 |
|
| 4 |
package tables |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"unicode" |
| 10 |
"unicode/utf8" |
| 11 |
|
| 12 |
"github.com/charlievieth/strcase/internal/tables/assigned" |
| 13 |
) |
| 14 |
|
| 15 |
func TestCaseFold(t *testing.T) { |
| 16 |
t.Run("Limits", func(t *testing.T) { |
| 17 |
for r := unicode.MaxRune; r < unicode.MaxRune+10; r++ { |
| 18 |
x := CaseFold(r) |
| 19 |
if x != r { |
| 20 |
t.Errorf("CaseFold(0x%04X) = 0x%04X; want: 0x%04X", r, x, r) |
| 21 |
} |
| 22 |
} |
| 23 |
for r := rune(0); r < ' '; r++ { |
| 24 |
x := CaseFold(r) |
| 25 |
if x != r { |
| 26 |
t.Errorf("CaseFold(0x%04X) = 0x%04X; want: 0x%04X", r, x, r) |
| 27 |
} |
| 28 |
} |
| 29 |
if r := CaseFold(utf8.RuneError); r != utf8.RuneError { |
| 30 |
t.Errorf("CaseFold(0x%04X) = 0x%04X; want: 0x%04X", utf8.RuneError, r, utf8.RuneError) |
| 31 |
} |
| 32 |
}) |
| 33 |
t.Run("ValidFolds", func(t *testing.T) { |
| 34 |
for _, p := range _CaseFolds { |
| 35 |
if r := CaseFold(rune(p.From)); r != rune(p.To) { |
| 36 |
t.Errorf("CaseFold(0x%04X) = 0x%04X; want: 0x%04X", rune(p.From), r, rune(p.To)) |
| 37 |
} |
| 38 |
} |
| 39 |
}) |
| 40 |
t.Run("UnicodeCases", func(t *testing.T) { |
| 41 |
folds := make(map[rune]rune) |
| 42 |
for _, p := range _CaseFolds { |
| 43 |
if p.From != 0 { |
| 44 |
folds[rune(p.From)] = rune(p.To) |
| 45 |
} |
| 46 |
} |
| 47 |
for _, rt := range unicode.Categories { |
| 48 |
visit(rt, func(r rune) { |
| 49 |
if rr, ok := folds[r]; ok { |
| 50 |
r = rr |
| 51 |
} |
| 52 |
if got := CaseFold(r); got != r { |
| 53 |
t.Errorf("CaseFold(0x%04X) = 0x%04X; want: 0x%04X", r, got, r) |
| 54 |
} |
| 55 |
}) |
| 56 |
} |
| 57 |
}) |
| 58 |
// Test against all assigned Unicode code points. |
| 59 |
t.Run("Assigned", func(t *testing.T) { |
| 60 |
all := assigned.AssignedRunes(unicode.Version) |
| 61 |
if len(all) == 0 { |
| 62 |
t.Fatalf("missing assigned code points for Unicode version: %q", unicode.Version) |
| 63 |
} |
| 64 |
n := 0 |
| 65 |
for _, r := range all { |
| 66 |
sr := CaseFold(r) |
| 67 |
if sr != r { |
| 68 |
n++ |
| 69 |
} |
| 70 |
if !strings.EqualFold(string(sr), string(r)) { |
| 71 |
t.Errorf("CaseFold(%q) = %q is an invalid fold", r, sr) |
| 72 |
} |
| 73 |
} |
| 74 |
if n == 0 { |
| 75 |
t.Fatal("failed to fold any runes") |
| 76 |
} |
| 77 |
}) |
| 78 |
} |
| 79 |
|
| 80 |
func TestUpperLower(t *testing.T) { |
| 81 |
// Test against all assigned Unicode code points. |
| 82 |
all := assigned.AssignedRunes(unicode.Version) |
| 83 |
if len(all) == 0 { |
| 84 |
t.Fatalf("missing assigned code points for Unicode version: %q", unicode.Version) |
| 85 |
} |
| 86 |
for _, r := range all { |
| 87 |
u0, l0, _ := ToUpperLower(r) |
| 88 |
u1 := unicode.ToUpper(r) |
| 89 |
l1 := unicode.ToLower(r) |
| 90 |
if u0 != u1 || l0 != l1 { |
| 91 |
t.Errorf("ToUpperLower(0x%04X) = 0x%04X, 0x%04X want: 0x%04X, 0x%04X", |
| 92 |
r, u0, l0, u1, l1) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// visit visits all runes in the given RangeTable in order, calling fn for each. |
| 98 |
func visit(rt *unicode.RangeTable, fn func(rune)) { |
| 99 |
for _, r16 := range rt.R16 { |
| 100 |
for r := rune(r16.Lo); r <= rune(r16.Hi); r += rune(r16.Stride) { |
| 101 |
fn(r) |
| 102 |
} |
| 103 |
} |
| 104 |
for _, r32 := range rt.R32 { |
| 105 |
for r := rune(r32.Lo); r <= rune(r32.Hi); r += rune(r32.Stride) { |
| 106 |
fn(r) |
| 107 |
} |
| 108 |
} |
| 109 |
} |