1package json 2 3import ( 4 "math" 5 "strconv" 6 "testing" 7) 8 9func 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 [20]byte 17 var our [20]byte 18 19 for _, i := range ints { 20 expected := strconv.AppendInt(std[:], i, 10) 21 actual := appendInt(our[:], i) 22 if string(expected) != string(actual) { 23 t.Fatalf("appendInt(%d) = %v, expected = %v", i, string(actual), string(expected)) 24 } 25 } 26} 27 28func benchStd(b *testing.B, n int64) { 29 var buf [20]byte 30 b.ResetTimer() 31 for i := 0; i < b.N; i++ { 32 strconv.AppendInt(buf[:0], n, 10) 33 } 34} 35 36func benchNew(b *testing.B, n int64) { 37 var buf [20]byte 38 b.ResetTimer() 39 for i := 0; i < b.N; i++ { 40 appendInt(buf[:0], n) 41 } 42} 43 44func BenchmarkAppendIntStd1(b *testing.B) { 45 benchStd(b, 1) 46} 47 48func BenchmarkAppendInt1(b *testing.B) { 49 benchNew(b, 1) 50} 51 52func BenchmarkAppendIntStdMinI64(b *testing.B) { 53 benchStd(b, math.MinInt64) 54} 55 56func BenchmarkAppendIntMinI64(b *testing.B) { 57 benchNew(b, math.MinInt64) 58} 59