Debian Codegraph @

format_test.go

raw
1 package utils
2
3 import (
4 "math"
5 "math/rand"
6 "strconv"
7 "testing"
8
9 "github.com/stretchr/testify/require"
10 )
11
12 func Test_FormatUint(t *testing.T) {
13 t.Parallel()
14 tests := []uint64{
15 0, 1, 9, 10, 11, 99, 100, 101, 999, 1000,
16 12345, 123456789, 9999999999,
17 math.MaxUint32, math.MaxUint64,
18 }
19 for _, tt := range tests {
20 expected := strconv.FormatUint(tt, 10)
21 result := FormatUint(tt)
22 require.Equal(t, expected, result, "FormatUint(%d)", tt)
23 }
24 }
25
26 func Test_FormatInt(t *testing.T) {
27 t.Parallel()
28 tests := []int64{
29 0, 1, -1, 9, -9, 10, -10, 99, -99, 100, -100,
30 12345, -12345, 123456789, -123456789,
31 math.MaxInt32, math.MinInt32,
32 math.MaxInt64, math.MinInt64,
33 }
34 for _, tt := range tests {
35 expected := strconv.FormatInt(tt, 10)
36 result := FormatInt(tt)
37 require.Equal(t, expected, result, "FormatInt(%d)", tt)
38 }
39 }
40
41 func Test_FormatUint32(t *testing.T) {
42 t.Parallel()
43 tests := []uint32{0, 1, 99, 100, 12345, math.MaxUint32}
44 for _, tt := range tests {
45 expected := strconv.FormatUint(uint64(tt), 10)
46 result := FormatUint32(tt)
47 require.Equal(t, expected, result, "FormatUint32(%d)", tt)
48 }
49 }
50
51 func Test_FormatInt32(t *testing.T) {
52 t.Parallel()
53 tests := []int32{0, 1, -1, 99, -99, math.MaxInt32, math.MinInt32}
54 for _, tt := range tests {
55 expected := strconv.FormatInt(int64(tt), 10)
56 result := FormatInt32(tt)
57 require.Equal(t, expected, result, "FormatInt32(%d)", tt)
58 }
59 }
60
61 func Test_FormatUint16(t *testing.T) {
62 t.Parallel()
63 tests := []uint16{0, 1, 99, 100, 12345, math.MaxUint16}
64 for _, tt := range tests {
65 expected := strconv.FormatUint(uint64(tt), 10)
66 result := FormatUint16(tt)
67 require.Equal(t, expected, result, "FormatUint16(%d)", tt)
68 }
69 }
70
71 func Test_FormatInt16(t *testing.T) {
72 t.Parallel()
73 tests := []int16{0, 1, -1, 99, -99, math.MaxInt16, math.MinInt16}
74 for _, tt := range tests {
75 expected := strconv.FormatInt(int64(tt), 10)
76 result := FormatInt16(tt)
77 require.Equal(t, expected, result, "FormatInt16(%d)", tt)
78 }
79 }
80
81 func Test_FormatUint8(t *testing.T) {
82 t.Parallel()
83 tests := []uint8{0, 1, 99, 100, math.MaxUint8}
84 for _, tt := range tests {
85 expected := strconv.FormatUint(uint64(tt), 10)
86 result := FormatUint8(tt)
87 require.Equal(t, expected, result, "FormatUint8(%d)", tt)
88 }
89 }
90
91 func Test_FormatInt8(t *testing.T) {
92 t.Parallel()
93 tests := []int8{0, 1, -1, 99, -99, math.MaxInt8, math.MinInt8}
94 for _, tt := range tests {
95 expected := strconv.FormatInt(int64(tt), 10)
96 result := FormatInt8(tt)
97 require.Equal(t, expected, result, "FormatInt8(%d)", tt)
98 }
99 }
100
101 func Test_AppendUint(t *testing.T) {
102 t.Parallel()
103 tests := []uint64{0, 1, 99, 100, 12345, 123456789, math.MaxUint64}
104 for _, tt := range tests {
105 expected := strconv.AppendUint([]byte("prefix"), tt, 10)
106 result := AppendUint([]byte("prefix"), tt)
107 require.Equal(t, expected, result, "AppendUint(%d)", tt)
108 }
109 }
110
111 func Test_AppendInt(t *testing.T) {
112 t.Parallel()
113 tests := []int64{0, 1, -1, 99, -99, 100, -100, 12345, -12345, math.MaxInt64, math.MinInt64}
114 for _, tt := range tests {
115 expected := strconv.AppendInt([]byte("prefix"), tt, 10)
116 result := AppendInt([]byte("prefix"), tt)
117 require.Equal(t, expected, result, "AppendInt(%d)", tt)
118 }
119 }
120
121 func Test_AppendInt_SmallNegativeCache(t *testing.T) {
122 t.Parallel()
123 // Test all small negative integers that should use the cache (-1 to -99)
124 for i := int64(-1); i >= -99; i-- {
125 t.Run(strconv.FormatInt(i, 10), func(t *testing.T) {
126 t.Parallel()
127 expected := strconv.AppendInt([]byte("prefix"), i, 10)
128 result := AppendInt([]byte("prefix"), i)
129 require.Equal(t, expected, result)
130 })
131 }
132 // Verify boundary: -100 should NOT use the cache
133 t.Run("boundary/-100", func(t *testing.T) {
134 t.Parallel()
135 expected := strconv.AppendInt([]byte("prefix"), -100, 10)
136 result := AppendInt([]byte("prefix"), -100)
137 require.Equal(t, expected, result)
138 })
139 }
140
141 // Benchmarks
142
143 func Benchmark_FormatUint(b *testing.B) {
144 inputs := []struct {
145 name string
146 value uint64
147 }{
148 {"small", 42},
149 {"medium", 123456789},
150 {"large", math.MaxUint64},
151 }
152
153 for _, input := range inputs {
154 b.Run(input.name+"/fiber", func(b *testing.B) {
155 b.ReportAllocs()
156 for b.Loop() {
157 FormatUint(input.value)
158 }
159 })
160 b.Run(input.name+"/strconv", func(b *testing.B) {
161 b.ReportAllocs()
162 for b.Loop() {
163 strconv.FormatUint(input.value, 10)
164 }
165 })
166 }
167 }
168
169 func Benchmark_FormatInt(b *testing.B) {
170 inputs := []struct {
171 name string
172 value int64
173 }{
174 {"small_pos", 42},
175 {"small_neg", -42},
176 {"medium_pos", 123456789},
177 {"medium_neg", -123456789},
178 {"large_pos", math.MaxInt64},
179 {"large_neg", math.MinInt64},
180 }
181
182 for _, input := range inputs {
183 b.Run(input.name+"/fiber", func(b *testing.B) {
184 b.ReportAllocs()
185 for b.Loop() {
186 FormatInt(input.value)
187 }
188 })
189 b.Run(input.name+"/strconv", func(b *testing.B) {
190 b.ReportAllocs()
191 for b.Loop() {
192 strconv.FormatInt(input.value, 10)
193 }
194 })
195 }
196 }
197
198 func Benchmark_FormatUint32(b *testing.B) {
199 input := uint32(123456789)
200
201 b.Run("fiber", func(b *testing.B) {
202 b.ReportAllocs()
203 for b.Loop() {
204 FormatUint32(input)
205 }
206 })
207 b.Run("strconv", func(b *testing.B) {
208 b.ReportAllocs()
209 for b.Loop() {
210 strconv.FormatUint(uint64(input), 10)
211 }
212 })
213 }
214
215 func Benchmark_FormatInt32(b *testing.B) {
216 input := int32(-123456789)
217
218 b.Run("fiber", func(b *testing.B) {
219 b.ReportAllocs()
220 for b.Loop() {
221 FormatInt32(input)
222 }
223 })
224 b.Run("strconv", func(b *testing.B) {
225 b.ReportAllocs()
226 for b.Loop() {
227 strconv.FormatInt(int64(input), 10)
228 }
229 })
230 }
231
232 func Benchmark_FormatUint16(b *testing.B) {
233 input := uint16(12345)
234
235 b.Run("fiber", func(b *testing.B) {
236 b.ReportAllocs()
237 for b.Loop() {
238 FormatUint16(input)
239 }
240 })
241 b.Run("strconv", func(b *testing.B) {
242 b.ReportAllocs()
243 for b.Loop() {
244 strconv.FormatUint(uint64(input), 10)
245 }
246 })
247 }
248
249 func Benchmark_FormatInt16(b *testing.B) {
250 input := int16(-12345)
251
252 b.Run("fiber", func(b *testing.B) {
253 b.ReportAllocs()
254 for b.Loop() {
255 FormatInt16(input)
256 }
257 })
258 b.Run("strconv", func(b *testing.B) {
259 b.ReportAllocs()
260 for b.Loop() {
261 strconv.FormatInt(int64(input), 10)
262 }
263 })
264 }
265
266 func Benchmark_FormatUint8(b *testing.B) {
267 input := uint8(255)
268
269 b.Run("fiber", func(b *testing.B) {
270 b.ReportAllocs()
271 for b.Loop() {
272 FormatUint8(input)
273 }
274 })
275 b.Run("strconv", func(b *testing.B) {
276 b.ReportAllocs()
277 for b.Loop() {
278 strconv.FormatUint(uint64(input), 10)
279 }
280 })
281 }
282
283 func Benchmark_FormatInt8(b *testing.B) {
284 input := int8(-128)
285
286 b.Run("fiber", func(b *testing.B) {
287 b.ReportAllocs()
288 for b.Loop() {
289 FormatInt8(input)
290 }
291 })
292 b.Run("strconv", func(b *testing.B) {
293 b.ReportAllocs()
294 for b.Loop() {
295 strconv.FormatInt(int64(input), 10)
296 }
297 })
298 }
299
300 func Benchmark_AppendUint(b *testing.B) {
301 input := uint64(123456789)
302 dst := make([]byte, 0, 32)
303
304 b.Run("fiber", func(b *testing.B) {
305 b.ReportAllocs()
306 for b.Loop() {
307 AppendUint(dst, input)
308 }
309 })
310 b.Run("strconv", func(b *testing.B) {
311 b.ReportAllocs()
312 for b.Loop() {
313 strconv.AppendUint(dst, input, 10)
314 }
315 })
316 }
317
318 func Benchmark_AppendInt(b *testing.B) {
319 inputs := []struct {
320 name string
321 value int64
322 }{
323 {"small_neg", -42},
324 {"medium_neg", -123456789},
325 }
326 dst := make([]byte, 0, 32)
327
328 for _, input := range inputs {
329 b.Run(input.name+"/fiber", func(b *testing.B) {
330 b.ReportAllocs()
331 for b.Loop() {
332 AppendInt(dst, input.value)
333 }
334 })
335 b.Run(input.name+"/strconv", func(b *testing.B) {
336 b.ReportAllocs()
337 for b.Loop() {
338 strconv.AppendInt(dst, input.value, 10)
339 }
340 })
341 }
342 }
343
344 func Test_UintDigits_IntDigits(t *testing.T) {
345 t.Parallel()
346 checkU := func(n uint64) {
347 t.Helper()
348 require.Equal(t, len(strconv.FormatUint(n, 10)), uintDigits(n), "n=%d", n)
349 }
350 for n := range uint64(100000) {
351 checkU(n)
352 }
353 // Every power-of-ten boundary up to the uint64 limit.
354 p := uint64(1)
355 for range 20 {
356 checkU(p - 1)
357 checkU(p)
358 checkU(p + 1)
359 if p > math.MaxUint64/10 {
360 break
361 }
362 p *= 10
363 }
364 checkU(math.MaxUint64)
365
366 for n := int64(-100000); n < 100000; n++ {
367 require.Equal(t, len(strconv.FormatInt(n, 10)), intDigits(n), "n=%d", n)
368 }
369 require.Equal(t, 20, intDigits(math.MinInt64))
370 require.Equal(t, 19, intDigits(math.MaxInt64))
371 }
372
373 // Test_Format_Sweep pins the formatters to strconv over a dense sweep, every decimal and group boundary, and a random spread.
374 func Test_Format_Sweep(t *testing.T) {
375 t.Parallel()
376 // The sweep runs hundreds of thousands of values, so it compares directly instead of through require.
377 eq := func(name, got, want string) {
378 t.Helper()
379 if got != want {
380 t.Fatalf("%s: got %q, want %q", name, got, want)
381 }
382 }
383 check := func(n uint64) {
384 t.Helper()
385 want := strconv.FormatUint(n, 10)
386 eq("FormatUint", FormatUint(n), want)
387 eq("AppendUint", string(AppendUint([]byte("x"), n))[1:], want)
388 if n <= math.MaxUint32 {
389 eq("FormatUint32", FormatUint32(uint32(n)), want)
390 }
391 if n <= math.MaxUint16 {
392 eq("FormatUint16", FormatUint16(uint16(n)), want)
393 }
394 if n <= math.MaxInt64 {
395 i := int64(n)
396 neg := strconv.FormatInt(-i, 10)
397 eq("FormatInt", FormatInt(i), want)
398 eq("FormatInt", FormatInt(-i), neg)
399 eq("AppendInt", string(AppendInt([]byte("x"), -i))[1:], neg)
400 }
401 if n <= math.MaxInt32 {
402 i := int32(n)
403 eq("FormatInt32", FormatInt32(i), want)
404 eq("FormatInt32", FormatInt32(-i), strconv.FormatInt(int64(-i), 10))
405 }
406 if n <= math.MaxInt16 {
407 i := int16(n)
408 eq("FormatInt16", FormatInt16(i), want)
409 eq("FormatInt16", FormatInt16(-i), strconv.FormatInt(int64(-i), 10))
410 }
411 }
412
413 for n := range uint64(300_000) {
414 check(n)
415 }
416 for _, p := range pow10 {
417 check(p - 1)
418 check(p)
419 check(p + 1)
420 }
421 for _, n := range []uint64{
422 99999999, 100000000, 100000001, // one lane group
423 9999999999999999, 10000000000000000, 10000000000000001, // two lane groups
424 math.MaxUint16, math.MaxInt16, math.MaxUint32, math.MaxInt32,
425 math.MaxInt64, math.MaxInt64 + 1, math.MaxUint64,
426 1844674407370955161, 1844674407370955162, // largest top groups
427 } {
428 check(n)
429 }
430 require.Equal(t, "-9223372036854775808", FormatInt(math.MinInt64))
431 require.Equal(t, "-9223372036854775808", string(AppendInt(nil, math.MinInt64)))
432 require.Equal(t, "-2147483648", FormatInt32(math.MinInt32))
433 require.Equal(t, "-32768", FormatInt16(math.MinInt16))
434
435 // A deterministic pseudo-random spread: a few values at every bit length.
436 rng := rand.New(rand.NewSource(1)) //nolint:gosec // deterministic test data
437 for range 20000 {
438 x := rng.Uint64()
439 check(x)
440 check(x >> (x % 64))
441 }
442 }