Debian Codegraph @

casefold_test.go

raw
1 package utils
2
3 import (
4 "bytes"
5 "math/rand"
6 "strings"
7 "testing"
8
9 casebytes "github.com/gofiber/utils/v2/bytes"
10 "github.com/gofiber/utils/v2/internal/unsafeconv"
11 casestrings "github.com/gofiber/utils/v2/strings"
12 "github.com/stretchr/testify/require"
13 )
14
15 // Test_CaseFold_Randomized cross-checks utils.EqualFold and the bytes/strings
16 // converters against the stdlib and a scalar ASCII fold over random inputs.
17 // The other folding helpers live in search_test.go and headerkey_test.go, and
18 // EqualFold is built on swar rather than internal/caseconv, so its assertions
19 // here and in byteseq_test.go are the only ones pinning it to strings.EqualFold.
20 func Test_CaseFold_Randomized(t *testing.T) {
21 t.Parallel()
22 rng := rand.New(rand.NewSource(42)) //nolint:gosec // deterministic test data
23
24 for range 20000 {
25 // Lengths 0..100 cover sub-word inputs, all %8 tails, and several
26 // full word iterations.
27 n := rng.Intn(101)
28 a := make([]byte, n)
29 b := make([]byte, n)
30 for i := range n {
31 a[i] = byte(rng.Intn(256))
32 // Bias b towards being a case-variant of a so EqualFold hits
33 // both outcomes.
34 if rng.Intn(2) == 0 {
35 b[i] = a[i] ^ 0x20
36 } else {
37 b[i] = byte(rng.Intn(256))
38 }
39 }
40 as, bs := string(a), string(b)
41
42 require.Equal(t, strings.ToLower(asciiOnly(as)), casestrings.ToLower(asciiOnly(as)))
43 require.Equal(t, strings.ToUpper(asciiOnly(as)), casestrings.ToUpper(asciiOnly(as)))
44 require.Equal(t, strings.EqualFold(asciiOnly(as), asciiOnly(bs)), EqualFold(asciiOnly(as), asciiOnly(bs)))
45
46 // Non-ASCII bytes must pass through unchanged (table semantics).
47 require.Equal(t, asciiFoldLower(as), casestrings.ToLower(as))
48 require.Equal(t, asciiFoldUpper(as), casestrings.ToUpper(as))
49 require.Equal(t, bytes.Equal(asciiFoldUpperB(a), asciiFoldUpperB(b)), EqualFold(a, b))
50
51 require.Equal(t, asciiFoldLower(as), string(casebytes.ToLower(append([]byte(nil), a...))))
52 require.Equal(t, asciiFoldUpper(as), string(casebytes.ToUpper(append([]byte(nil), a...))))
53 require.Equal(t, asciiFoldLower(as), string(casebytes.UnsafeToLower(append([]byte(nil), a...))))
54 require.Equal(t, asciiFoldUpper(as), string(casebytes.UnsafeToUpper(append([]byte(nil), a...))))
55
56 // The strings in-place wrappers mutate their backing bytes, so hand
57 // each a string view over a private mutable copy — string(a) may
58 // reference read-only interned memory (e.g. 1-byte strings).
59 la := append([]byte(nil), a...)
60 require.Equal(t, asciiFoldLower(as), casestrings.UnsafeToLower(unsafeconv.UnsafeString(la)))
61 ua := append([]byte(nil), a...)
62 require.Equal(t, asciiFoldUpper(as), casestrings.UnsafeToUpper(unsafeconv.UnsafeString(ua)))
63 }
64 }
65
66 func asciiOnly(s string) string {
67 b := []byte(s)
68 for i := range b {
69 b[i] &= 0x7f
70 }
71 return string(b)
72 }
73
74 func asciiFoldLower(s string) string {
75 b := []byte(s)
76 for i := range b {
77 if b[i] >= 'A' && b[i] <= 'Z' {
78 b[i] += 'a' - 'A'
79 }
80 }
81 return string(b)
82 }
83
84 func asciiFoldUpper(s string) string {
85 b := []byte(s)
86 for i := range b {
87 if b[i] >= 'a' && b[i] <= 'z' {
88 b[i] -= 'a' - 'A'
89 }
90 }
91 return string(b)
92 }
93
94 func asciiFoldUpperB(b []byte) []byte {
95 return []byte(asciiFoldUpper(string(b)))
96 }