Debian Codegraph @

bytes/case_test.go

raw
1 package bytes
2
3 import (
4 stdbytes "bytes"
5 "strings"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 type benchCase struct {
12 name string
13 input string
14 lower string
15 upper string
16 }
17
18 var benchmarkCoreCases = []benchCase{
19 {name: "empty", input: "", upper: "", lower: ""},
20 {name: "http-get", input: "get", upper: "GET", lower: "get"},
21 {name: "http-get-upper", input: "GET", upper: "GET", lower: "get"},
22 {
23 name: "header-content-type-mixed",
24 input: "Content-Type: text/html; charset=utf-8",
25 upper: "CONTENT-TYPE: TEXT/HTML; CHARSET=UTF-8",
26 lower: "content-type: text/html; charset=utf-8",
27 },
28 {name: "large-lower", input: strings.Repeat("a", 64), upper: strings.Repeat("A", 64), lower: strings.Repeat("a", 64)},
29 {name: "large-upper", input: strings.Repeat("A", 64), upper: strings.Repeat("A", 64), lower: strings.Repeat("a", 64)},
30 {name: "large-mixed", input: strings.Repeat("aB", 32), upper: strings.Repeat("AB", 32), lower: strings.Repeat("ab", 32)},
31 }
32
33 func Test_ToLowerBytes(t *testing.T) {
34 t.Parallel()
35
36 require.Equal(t, []byte("/my/name/is/:param/*"), UnsafeToLower([]byte("/MY/NAME/IS/:PARAM/*")))
37 require.Equal(t, []byte("/my1/name/is/:param/*"), UnsafeToLower([]byte("/MY1/NAME/IS/:PARAM/*")))
38 require.Equal(t, []byte("/my2/name/is/:param/*"), UnsafeToLower([]byte("/MY2/NAME/IS/:PARAM/*")))
39 require.Equal(t, []byte("/my3/name/is/:param/*"), UnsafeToLower([]byte("/MY3/NAME/IS/:PARAM/*")))
40 require.Equal(t, []byte("/my4/name/is/:param/*"), UnsafeToLower([]byte("/MY4/NAME/IS/:PARAM/*")))
41 }
42
43 func Test_ToUpperBytes(t *testing.T) {
44 t.Parallel()
45
46 require.Equal(t, []byte("/MY/NAME/IS/:PARAM/*"), UnsafeToUpper([]byte("/my/name/is/:param/*")))
47 require.Equal(t, []byte("/MY1/NAME/IS/:PARAM/*"), UnsafeToUpper([]byte("/my1/name/is/:param/*")))
48 require.Equal(t, []byte("/MY2/NAME/IS/:PARAM/*"), UnsafeToUpper([]byte("/my2/name/is/:param/*")))
49 require.Equal(t, []byte("/MY3/NAME/IS/:PARAM/*"), UnsafeToUpper([]byte("/my3/name/is/:param/*")))
50 require.Equal(t, []byte("/MY4/NAME/IS/:PARAM/*"), UnsafeToUpper([]byte("/my4/name/is/:param/*")))
51 }
52
53 func Test_ToLower_NoMutation(t *testing.T) {
54 in := []byte("Content-Type")
55 snapshot := append([]byte(nil), in...)
56 out := ToLower(in)
57
58 require.Equal(t, "content-type", string(out))
59 require.Equal(t, snapshot, in)
60 }
61
62 func Test_ToUpper_NoMutation(t *testing.T) {
63 in := []byte("content-type")
64 snapshot := append([]byte(nil), in...)
65 out := ToUpper(in)
66
67 require.Equal(t, "CONTENT-TYPE", string(out))
68 require.Equal(t, snapshot, in)
69 }
70
71 func TestUnsafeToLower_Mutates(t *testing.T) {
72 in := []byte("Content-Type")
73 out := UnsafeToLower(in)
74 require.Same(t, &out[0], &in[0])
75 require.Equal(t, "content-type", string(in))
76 }
77
78 func TestUnsafeToUpper_Mutates(t *testing.T) {
79 in := []byte("content-type")
80 out := UnsafeToUpper(in)
81 require.Same(t, &out[0], &in[0])
82 require.Equal(t, "CONTENT-TYPE", string(in))
83 }
84
85 func Test_ToLowerBytes_Edge(t *testing.T) {
86 t.Parallel()
87
88 cases := [][]byte{
89 {},
90 []byte("123"),
91 []byte("!@#"),
92 }
93 for _, c := range cases {
94 t.Run(string(c), func(t *testing.T) {
95 t.Parallel()
96 require.Equal(t, stdbytes.ToLower(c), UnsafeToLower(c))
97 })
98 }
99 }
100
101 func Test_ToUpperBytes_Edge(t *testing.T) {
102 t.Parallel()
103
104 cases := [][]byte{
105 {},
106 []byte("123"),
107 []byte("!@#"),
108 }
109 for _, c := range cases {
110 t.Run(string(c), func(t *testing.T) {
111 t.Parallel()
112 require.Equal(t, stdbytes.ToUpper(c), UnsafeToUpper(c))
113 })
114 }
115 }
116
117 func Benchmark_ToLowerBytes(b *testing.B) {
118 for _, tc := range benchmarkCoreCases {
119 b.Run(tc.name, func(b *testing.B) {
120 template := []byte(tc.input)
121 want := []byte(tc.lower)
122 var res []byte
123
124 b.Run("fiber", func(b *testing.B) {
125 b.ReportAllocs()
126 for i := 0; i < b.N; i++ {
127 res = ToLower(template)
128 }
129 require.Equal(b, want, res)
130 })
131 b.Run("fiber/unsafe", func(b *testing.B) {
132 b.ReportAllocs()
133 work := make([]byte, len(template))
134 for i := 0; i < b.N; i++ {
135 copy(work, template)
136 res = UnsafeToLower(work)
137 }
138 require.Equal(b, want, res)
139 })
140 b.Run("default", func(b *testing.B) {
141 b.ReportAllocs()
142 for i := 0; i < b.N; i++ {
143 res = stdbytes.ToLower(template)
144 }
145 require.Equal(b, want, res)
146 })
147 })
148 }
149 }
150
151 func Benchmark_ToUpperBytes(b *testing.B) {
152 for _, tc := range benchmarkCoreCases {
153 b.Run(tc.name, func(b *testing.B) {
154 template := []byte(tc.input)
155 want := []byte(tc.upper)
156 var res []byte
157
158 b.Run("fiber", func(b *testing.B) {
159 b.ReportAllocs()
160 for i := 0; i < b.N; i++ {
161 res = ToUpper(template)
162 }
163 require.Equal(b, want, res)
164 })
165 b.Run("fiber/unsafe", func(b *testing.B) {
166 b.ReportAllocs()
167 work := make([]byte, len(template))
168 for i := 0; i < b.N; i++ {
169 copy(work, template)
170 res = UnsafeToUpper(work)
171 }
172 require.Equal(b, want, res)
173 })
174 b.Run("default", func(b *testing.B) {
175 b.ReportAllocs()
176 for i := 0; i < b.N; i++ {
177 res = stdbytes.ToUpper(template)
178 }
179 require.Equal(b, want, res)
180 })
181 })
182 }
183 }
184
185 // Test_Case_PathCoverage exercises every dispatch path of the case
186 // converters in-package: sub-word inputs, word-path inputs with the first
187 // change at each interesting offset, identity returns when nothing changes,
188 // and non-ASCII passthrough.
189 func Test_Case_PathCoverage(t *testing.T) {
190 t.Parallel()
191
192 cases := []struct{ in, lower, upper string }{
193 {"", "", ""},
194 {"a", "a", "A"},
195 {"A", "a", "A"},
196 {"aBc", "abc", "ABC"},
197 {"1a2B3", "1a2b3", "1A2B3"},
198 {"\xe9\xff!", "\xe9\xff!", "\xe9\xff!"}, // sub-word non-ASCII
199 {"abcdefgh", "abcdefgh", "ABCDEFGH"}, // exactly one word
200 {"ABCDEFGH", "abcdefgh", "ABCDEFGH"}, //
201 {"nochangehere-1234", "nochangehere-1234", "NOCHANGEHERE-1234"},
202 {"aaaaaaaaaXbbbb", "aaaaaaaaaxbbbb", "AAAAAAAAAXBBBB"}, // change starts past word 0
203 {"aaaaaaaaaaaaY", "aaaaaaaaaaaay", "AAAAAAAAAAAAY"}, // change only in overlap tail
204 {"\xc9\xe9aaaaaaaaZz", "\xc9\xe9aaaaaaaazz", "\xc9\xe9AAAAAAAAZZ"}, // word path + non-ASCII
205 }
206 for _, tc := range cases {
207 require.Equal(t, tc.lower, string(ToLower([]byte(tc.in))), "ToLower %q", tc.in)
208 require.Equal(t, tc.upper, string(ToUpper([]byte(tc.in))), "ToUpper %q", tc.in)
209 require.Equal(t, tc.lower, string(UnsafeToLower([]byte(tc.in))), "UnsafeToLower %q", tc.in)
210 require.Equal(t, tc.upper, string(UnsafeToUpper([]byte(tc.in))), "UnsafeToUpper %q", tc.in)
211 }
212
213 // When no byte needs changing, ToLower/ToUpper must return the input
214 // slice itself, on both the sub-word and word paths.
215 for _, s := range []string{"x", "already-lower", strings.Repeat("k", 23)} {
216 in := []byte(s)
217 out := ToLower(in)
218 require.Equal(t, s, string(out))
219 require.Same(t, &in[0], &out[0], "ToLower must alias unchanged input %q", s)
220 }
221 for _, s := range []string{"X", "ALREADY-UPPER", strings.Repeat("K", 23)} {
222 in := []byte(s)
223 out := ToUpper(in)
224 require.Equal(t, s, string(out))
225 require.Same(t, &in[0], &out[0], "ToUpper must alias unchanged input %q", s)
226 }
227 }