5
5
package utils
6
6
7
7
import (
8
+ "math"
8
9
"reflect"
9
10
"strconv"
10
11
"testing"
@@ -244,6 +245,8 @@ func Test_AppendInt(t *testing.T) {
244
245
require .Equal (t , []byte ("-1" ), AppendInt (dst , - 1 ))
245
246
require .Equal (t , []byte ("-2" ), AppendInt (dst , - 2 ))
246
247
require .Equal (t , []byte ("-4500" ), AppendInt (dst , - 4500 ))
248
+ require .Equal (t , []byte (strconv .Itoa (math .MaxInt )), AppendInt (dst , math .MaxInt ))
249
+ //require.Equal(t, []byte("-4500"), AppendInt(dst, math.MinInt+1))
247
250
}
248
251
249
252
// go test -v -run=^$ -bench=ToString -benchmem -count=4
@@ -312,16 +315,16 @@ func Benchmark_UnsafeString(b *testing.B) {
312
315
313
316
// go test -v -run=^$ -bench=ItoA -benchmem -count=4
314
317
func Benchmark_ItoA (b * testing.B ) {
315
- number := 4242
318
+ number := 2
316
319
number64 := int64 (number )
317
- numberString := "4242 "
318
- numberN := - 4242
320
+ numberString := "2 "
321
+ numberN := - 2
319
322
number64N := int64 (numberN )
320
- numberNString := "-4242 "
323
+ numberNString := "-2 "
321
324
322
325
var resB []byte
323
326
var resS string
324
- b .Run ("fiber (positiv number )" , func (b * testing.B ) {
327
+ b .Run ("fiber (pos num )" , func (b * testing.B ) {
325
328
b .ReportAllocs ()
326
329
b .ResetTimer ()
327
330
@@ -331,7 +334,17 @@ func Benchmark_ItoA(b *testing.B) {
331
334
require .Equal (b , []byte (numberString ), resB )
332
335
})
333
336
334
- b .Run ("default - strconv.Itoa (positiv number)" , func (b * testing.B ) {
337
+ b .Run ("default - strconv.AppendInt (pos num)" , func (b * testing.B ) {
338
+ b .ReportAllocs ()
339
+ b .ResetTimer ()
340
+
341
+ for n := 0 ; n < b .N ; n ++ {
342
+ resB = strconv .AppendInt (resB [:0 ], number64 , 10 )
343
+ }
344
+ require .Equal (b , []byte (numberString ), resB )
345
+ })
346
+
347
+ b .Run ("default - strconv.Itoa (pos num)" , func (b * testing.B ) {
335
348
b .ReportAllocs ()
336
349
b .ResetTimer ()
337
350
for n := 0 ; n < b .N ; n ++ {
@@ -340,7 +353,7 @@ func Benchmark_ItoA(b *testing.B) {
340
353
require .Equal (b , numberString , resS )
341
354
})
342
355
343
- b .Run ("default - strconv.FormatInt (positiv number )" , func (b * testing.B ) {
356
+ b .Run ("default - strconv.FormatInt (pos num )" , func (b * testing.B ) {
344
357
b .ReportAllocs ()
345
358
b .ResetTimer ()
346
359
for n := 0 ; n < b .N ; n ++ {
@@ -349,7 +362,7 @@ func Benchmark_ItoA(b *testing.B) {
349
362
require .Equal (b , numberString , resS )
350
363
})
351
364
352
- b .Run ("fiber (negative number )" , func (b * testing.B ) {
365
+ b .Run ("fiber (neg num )" , func (b * testing.B ) {
353
366
b .ReportAllocs ()
354
367
b .ResetTimer ()
355
368
for n := 0 ; n < b .N ; n ++ {
@@ -358,7 +371,17 @@ func Benchmark_ItoA(b *testing.B) {
358
371
require .Equal (b , []byte (numberNString ), resB )
359
372
})
360
373
361
- b .Run ("default - strconv.Itoa (negative number)" , func (b * testing.B ) {
374
+ b .Run ("default - strconv.AppendInt (neg num)" , func (b * testing.B ) {
375
+ b .ReportAllocs ()
376
+ b .ResetTimer ()
377
+
378
+ for n := 0 ; n < b .N ; n ++ {
379
+ resB = strconv .AppendInt (resB [:0 ], number64N , 10 )
380
+ }
381
+ require .Equal (b , []byte (numberNString ), resB )
382
+ })
383
+
384
+ b .Run ("default - strconv.Itoa (neg num)" , func (b * testing.B ) {
362
385
b .ReportAllocs ()
363
386
b .ResetTimer ()
364
387
for n := 0 ; n < b .N ; n ++ {
@@ -367,7 +390,7 @@ func Benchmark_ItoA(b *testing.B) {
367
390
require .Equal (b , numberNString , resS )
368
391
})
369
392
370
- b .Run ("default - strconv.FormatInt (negative number )" , func (b * testing.B ) {
393
+ b .Run ("default - strconv.FormatInt (neg num )" , func (b * testing.B ) {
371
394
b .ReportAllocs ()
372
395
b .ResetTimer ()
373
396
for n := 0 ; n < b .N ; n ++ {
0 commit comments