github.com/kamalshkeir/kencoding@v0.0.2-0.20230409043843-44b609a0475a/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 [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 28 func 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 36 func 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 44 func BenchmarkAppendIntStd1(b *testing.B) { 45 benchStd(b, 1) 46 } 47 48 func BenchmarkAppendInt1(b *testing.B) { 49 benchNew(b, 1) 50 } 51 52 func BenchmarkAppendIntStdMinI64(b *testing.B) { 53 benchStd(b, math.MinInt64) 54 } 55 56 func BenchmarkAppendIntMinI64(b *testing.B) { 57 benchNew(b, math.MinInt64) 58 }