Debian Codegraph @

internal/test/fuzz_test.go

raw
1 package test
2
3 import (
4 "math/rand"
5 "reflect"
6 "testing"
7 "time"
8 "unicode"
9 "unicode/utf8"
10
11 "github.com/charlievieth/strcase/internal/tables/assigned"
12 )
13
14 func TestInvalidRune(t *testing.T) {
15 rr := rand.New(rand.NewSource(time.Now().UnixNano()))
16 for i := 0; i < 10_000; i++ {
17 r := invalidRune(rr)
18 if utf8.ValidRune(r) {
19 t.Fatalf("utf8.ValidRune(%q) = %t; want: %t", r, true, false)
20 }
21 }
22 }
23
24 // Test that our generated tables contain all the desired Unicode points
25 // since we use a subset of Unicode categories to generate them for perf
26 // reasons.
27 func TestGeneratedRuneTables(t *testing.T) {
28 if testing.Short() {
29 t.Skip("short test")
30 }
31 all := assigned.Assigned(unicode.Version)
32 if all == nil {
33 t.Fatal("no assigned tables for Unicode version:", unicode.Version)
34 }
35
36 diff := func(got, want []rune) (extra, missing []rune) {
37 gm := make(map[rune]bool, len(got))
38 wm := make(map[rune]bool, len(want))
39 for _, r := range got {
40 gm[r] = true
41 }
42 for _, r := range want {
43 if !gm[r] {
44 missing = append(missing, r)
45 }
46 wm[r] = true
47 }
48 for _, r := range got {
49 if !wm[r] {
50 extra = append(extra, r)
51 }
52 }
53 return extra, missing
54 }
55 compare := func(name string, got, want []rune) {
56 t.Helper()
57 if reflect.DeepEqual(got, want) {
58 return
59 }
60 extra, missing := diff(got, want)
61 t.Errorf("%s table is missing or contains extra runes\n"+
62 "Extra: 0x%04X\n"+
63 "Missing: 0x%04X\n", name, extra, missing)
64 }
65
66 multiwidth, foldable := generateRuneTables(all)
67 compare("multiwidthRunes", multiwidth, multiwidthRunes)
68 compare("foldableRunes", foldableRunes, foldable)
69 }