| 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/caseconv" |
| 11 |
"github.com/gofiber/utils/v2/internal/unsafeconv" |
| 12 |
casestrings "github.com/gofiber/utils/v2/strings" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
func Test_SWAR_Randomized(t *testing.T) { |
| 17 |
t.Parallel() |
| 18 |
rng := rand.New(rand.NewSource(42)) //nolint:gosec // deterministic test data |
| 19 |
|
| 20 |
for range 20000 { |
| 21 |
// Lengths 0..100 cover sub-word inputs, all %8 tails, and several |
| 22 |
// full word iterations. |
| 23 |
n := rng.Intn(101) |
| 24 |
a := make([]byte, n) |
| 25 |
b := make([]byte, n) |
| 26 |
for i := range n { |
| 27 |
a[i] = byte(rng.Intn(256)) |
| 28 |
// Bias b towards being a case-variant of a so EqualFold hits |
| 29 |
// both outcomes. |
| 30 |
if rng.Intn(2) == 0 { |
| 31 |
b[i] = a[i] ^ 0x20 |
| 32 |
} else { |
| 33 |
b[i] = byte(rng.Intn(256)) |
| 34 |
} |
| 35 |
} |
| 36 |
as, bs := string(a), string(b) |
| 37 |
|
| 38 |
require.Equal(t, strings.ToLower(asciiOnly(as)), casestrings.ToLower(asciiOnly(as))) |
| 39 |
require.Equal(t, strings.ToUpper(asciiOnly(as)), casestrings.ToUpper(asciiOnly(as))) |
| 40 |
require.Equal(t, strings.EqualFold(asciiOnly(as), asciiOnly(bs)), EqualFold(asciiOnly(as), asciiOnly(bs))) |
| 41 |
|
| 42 |
// Non-ASCII bytes must pass through unchanged (table semantics). |
| 43 |
require.Equal(t, asciiFoldLower(as), casestrings.ToLower(as)) |
| 44 |
require.Equal(t, asciiFoldUpper(as), casestrings.ToUpper(as)) |
| 45 |
require.Equal(t, bytes.Equal(asciiFoldUpperB(a), asciiFoldUpperB(b)), EqualFold(a, b)) |
| 46 |
|
| 47 |
require.Equal(t, asciiFoldLower(as), string(casebytes.ToLower(append([]byte(nil), a...)))) |
| 48 |
require.Equal(t, asciiFoldUpper(as), string(casebytes.ToUpper(append([]byte(nil), a...)))) |
| 49 |
require.Equal(t, asciiFoldLower(as), string(casebytes.UnsafeToLower(append([]byte(nil), a...)))) |
| 50 |
require.Equal(t, asciiFoldUpper(as), string(casebytes.UnsafeToUpper(append([]byte(nil), a...)))) |
| 51 |
|
| 52 |
// The strings in-place wrappers mutate their backing bytes, so hand |
| 53 |
// each a string view over a private mutable copy — string(a) may |
| 54 |
// reference read-only interned memory (e.g. 1-byte strings). |
| 55 |
la := append([]byte(nil), a...) |
| 56 |
require.Equal(t, asciiFoldLower(as), casestrings.UnsafeToLower(unsafeconv.UnsafeString(la))) |
| 57 |
ua := append([]byte(nil), a...) |
| 58 |
require.Equal(t, asciiFoldUpper(as), casestrings.UnsafeToUpper(unsafeconv.UnsafeString(ua))) |
| 59 |
|
| 60 |
// Exercise the caseconv helpers directly so their sub-word tails are |
| 61 |
// covered even though the public wrappers route short inputs away. |
| 62 |
lowered := append([]byte(nil), a...) |
| 63 |
caseconv.ToLowerInPlace(lowered) |
| 64 |
require.Equal(t, asciiFoldLower(as), string(lowered)) |
| 65 |
uppered := append([]byte(nil), a...) |
| 66 |
caseconv.ToUpperInPlace(uppered) |
| 67 |
require.Equal(t, asciiFoldUpper(as), string(uppered)) |
| 68 |
|
| 69 |
require.Equal(t, stdFirstIndex(a, 'A', 'Z'), caseconv.FirstUpperIndex(a)) |
| 70 |
require.Equal(t, stdFirstIndex(a, 'a', 'z'), caseconv.FirstLowerIndex(a)) |
| 71 |
|
| 72 |
loweredCopy := make([]byte, n) |
| 73 |
caseconv.ToLowerCopy(loweredCopy, a, 0) |
| 74 |
require.Equal(t, asciiFoldLower(as), string(loweredCopy)) |
| 75 |
upperedCopy := make([]byte, n) |
| 76 |
caseconv.ToUpperCopy(upperedCopy, a, 0) |
| 77 |
require.Equal(t, asciiFoldUpper(as), string(upperedCopy)) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func stdFirstIndex(b []byte, lo, hi byte) int { |
| 82 |
for i, c := range b { |
| 83 |
if c >= lo && c <= hi { |
| 84 |
return i |
| 85 |
} |
| 86 |
} |
| 87 |
return -1 |
| 88 |
} |
| 89 |
|
| 90 |
func asciiOnly(s string) string { |
| 91 |
b := []byte(s) |
| 92 |
for i := range b { |
| 93 |
b[i] &= 0x7f |
| 94 |
} |
| 95 |
return string(b) |
| 96 |
} |
| 97 |
|
| 98 |
func asciiFoldLower(s string) string { |
| 99 |
b := []byte(s) |
| 100 |
for i := range b { |
| 101 |
if b[i] >= 'A' && b[i] <= 'Z' { |
| 102 |
b[i] += 'a' - 'A' |
| 103 |
} |
| 104 |
} |
| 105 |
return string(b) |
| 106 |
} |
| 107 |
|
| 108 |
func asciiFoldUpper(s string) string { |
| 109 |
b := []byte(s) |
| 110 |
for i := range b { |
| 111 |
if b[i] >= 'a' && b[i] <= 'z' { |
| 112 |
b[i] -= 'a' - 'A' |
| 113 |
} |
| 114 |
} |
| 115 |
return string(b) |
| 116 |
} |
| 117 |
|
| 118 |
func asciiFoldUpperB(b []byte) []byte { |
| 119 |
return []byte(asciiFoldUpper(string(b))) |
| 120 |
} |
| 121 |
|
| 122 |
// Test_CaseCopy_FromOffsets pins the from > 0 contract of ToLowerCopy and |
| 123 |
// ToUpperCopy directly: from is word-aligned and at or below the first byte |
| 124 |
// the conversion changes, so the overlapping tail store may rewrite |
| 125 |
// dst[n-8:from) only as a no-op. The public wrappers exercise this path |
| 126 |
// indirectly; this test does it without them. |
| 127 |
func Test_CaseCopy_FromOffsets(t *testing.T) { |
| 128 |
t.Parallel() |
| 129 |
for n := 8; n <= 26; n++ { |
| 130 |
for from := 0; from < n; from += 8 { |
| 131 |
// Case-stable filler (digits): bytes before firstChange must not |
| 132 |
// change under either conversion, or from would be invalid. |
| 133 |
base := make([]byte, n) |
| 134 |
for i := range base { |
| 135 |
base[i] = byte('0' + i%10) |
| 136 |
} |
| 137 |
for firstChange := from; firstChange < n; firstChange++ { |
| 138 |
lsrc := append([]byte(nil), base...) |
| 139 |
lsrc[firstChange] = 'A' + byte(firstChange%26) |
| 140 |
if firstChange+1 < n { |
| 141 |
lsrc[firstChange+1] = 0xE9 // non-ASCII passthrough |
| 142 |
} |
| 143 |
dst := make([]byte, n) |
| 144 |
copy(dst[:from], lsrc[:from]) |
| 145 |
caseconv.ToLowerCopy(dst, lsrc, from) |
| 146 |
require.Equal(t, asciiFoldLower(string(lsrc)), string(dst), |
| 147 |
"lower n=%d from=%d change=%d", n, from, firstChange) |
| 148 |
|
| 149 |
usrc := append([]byte(nil), base...) |
| 150 |
usrc[firstChange] = 'a' + byte(firstChange%26) |
| 151 |
if firstChange+1 < n { |
| 152 |
usrc[firstChange+1] = 0xE9 |
| 153 |
} |
| 154 |
dst2 := make([]byte, n) |
| 155 |
copy(dst2[:from], usrc[:from]) |
| 156 |
caseconv.ToUpperCopy(dst2, usrc, from) |
| 157 |
require.Equal(t, asciiFoldUpper(string(usrc)), string(dst2), |
| 158 |
"upper n=%d from=%d change=%d", n, from, firstChange) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |