File: int_test.go

package info (click to toggle)
golang-github-segmentio-encoding 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,468 kB
  • sloc: makefile: 286
file content (57 lines) | stat: -rw-r--r-- 1,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package json

import (
	"math"
	"strconv"
	"testing"
)

func TestAppendInt(t *testing.T) {
	var ints []int64
	for i := range 64 {
		u := uint64(1) << i
		ints = append(ints, int64(u-1), int64(u), int64(u+1), -int64(u))
	}

	var std, our [20]byte

	for _, i := range ints {
		expected := strconv.AppendInt(std[:], i, 10)
		actual := appendInt(our[:], i)
		if string(expected) != string(actual) {
			t.Fatalf("appendInt(%d) = %v, expected = %v", i, string(actual), string(expected))
		}
	}
}

func benchStd(b *testing.B, n int64) {
	var buf [20]byte
	b.ResetTimer()
	for range b.N {
		strconv.AppendInt(buf[:0], n, 10)
	}
}

func benchNew(b *testing.B, n int64) {
	var buf [20]byte
	b.ResetTimer()
	for range b.N {
		appendInt(buf[:0], n)
	}
}

func BenchmarkAppendIntStd1(b *testing.B) {
	benchStd(b, 1)
}

func BenchmarkAppendInt1(b *testing.B) {
	benchNew(b, 1)
}

func BenchmarkAppendIntStdMinI64(b *testing.B) {
	benchStd(b, math.MinInt64)
}

func BenchmarkAppendIntMinI64(b *testing.B) {
	benchNew(b, math.MinInt64)
}