github.com/segmentio/encoding@v0.4.0/json/int_test.go (about) 1 package json 2 3 import ( 4 "math" 5 "strconv" 6 "testing" 7 ) 8 9 func TestAppendInt(t *testing.T) { 10 var ints []int64 11 for i := 0; i < 64; i++ { 12 u := uint64(1) << i 13 ints = append(ints, int64(u-1), int64(u), int64(u+1), -int64(u)) 14 } 15 16 var std, our [20]byte 17 18 for _, i := range ints { 19 expected := strconv.AppendInt(std[:], i, 10) 20 actual := appendInt(our[:], i) 21 if string(expected) != string(actual) { 22 t.Fatalf("appendInt(%d) = %v, expected = %v", i, string(actual), string(expected)) 23 } 24 } 25 } 26 27 func benchStd(b *testing.B, n int64) { 28 var buf [20]byte 29 b.ResetTimer() 30 for i := 0; i < b.N; i++ { 31 strconv.AppendInt(buf[:0], n, 10) 32 } 33 } 34 35 func benchNew(b *testing.B, n int64) { 36 var buf [20]byte 37 b.ResetTimer() 38 for i := 0; i < b.N; i++ { 39 appendInt(buf[:0], n) 40 } 41 } 42 43 func BenchmarkAppendIntStd1(b *testing.B) { 44 benchStd(b, 1) 45 } 46 47 func BenchmarkAppendInt1(b *testing.B) { 48 benchNew(b, 1) 49 } 50 51 func BenchmarkAppendIntStdMinI64(b *testing.B) { 52 benchStd(b, math.MinInt64) 53 } 54 55 func BenchmarkAppendIntMinI64(b *testing.B) { 56 benchNew(b, math.MinInt64) 57 }