-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitoa_test.go
104 lines (88 loc) · 1.93 KB
/
itoa_test.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package itoa
import (
"fmt"
"math/rand"
"strconv"
"testing"
)
const (
printerIterations = 10000
)
func testPrinter(t *testing.T, fn func(out []byte) (value interface{}, result string)) {
rand.Seed(0)
buf := make([]byte, 20)
for i := 0; i < printerIterations; i++ {
value, actual := fn(buf)
expected := fmt.Sprintf("%d", value)
if string(expected) != string(actual) {
t.Errorf("Expected %q, got %q", expected, actual)
}
}
}
func TestItoaHundred(t *testing.T) {
testPrinter(t, func(out []byte) (value interface{}, result string) {
v := uint64(rand.Intn(100))
return v, FormatUint(v)
})
}
func TestItoaTenThousand(t *testing.T) {
testPrinter(t, func(out []byte) (value interface{}, result string) {
v := uint64(rand.Intn(10000))
return v, FormatUint(v)
})
}
func TestUint(t *testing.T) {
testPrinter(t, func(out []byte) (value interface{}, result string) {
v := rand.Uint64() >> uint(rand.Intn(64))
return v, FormatUint(v)
})
}
func TestInt(t *testing.T) {
testPrinter(t, func(out []byte) (value interface{}, result string) {
v := rand.Int63() >> uint(rand.Intn(64))
if rand.Intn(1) == 1 {
v = -v
}
return v, FormatInt(v)
})
}
var smallInt = 35
var bigInt = 999999999999999
func BenchmarkItoa(b *testing.B) {
for i := 0; i < b.N; i++ {
val := strconv.Itoa(smallInt)
_ = val
}
}
func BenchmarkItoaBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := strconv.Itoa(bigInt)
_ = val
}
}
func BenchmarkAnItoa(b *testing.B) {
buf := make([]byte, 80)
for i := 0; i < b.N; i++ {
val := Anltoa(buf, uint64(smallInt))
_ = val
}
}
func BenchmarkAnItoaBig(b *testing.B) {
buf := make([]byte, 80)
for i := 0; i < b.N; i++ {
val := Anltoa(buf, uint64(bigInt))
_ = val
}
}
func BenchmarkFmt(b *testing.B) {
for i := 0; i < b.N; i++ {
val := fmt.Sprintf("%d", smallInt)
_ = val
}
}
func BenchmarkFmtBig(b *testing.B) {
for i := 0; i < b.N; i++ {
val := fmt.Sprintf("%d", bigInt)
_ = val
}
}