1// Code generated by Makefile, DO NOT EDIT.
2
3/*
4 * Copyright 2021 ByteDance Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package {{PACKAGE}}
20
21import (
22 `strconv`
23 `testing`
24 `fmt`
25
26 `github.com/stretchr/testify/assert`
27)
28
29func TestFastInt_IntToString(t *testing.T) {
30 var buf [32]byte
31 assert.Equal(t, "0" , string(buf[:i64toa(&buf[0], 0)]))
32 assert.Equal(t, "1" , string(buf[:i64toa(&buf[0], 1)]))
33 assert.Equal(t, "12" , string(buf[:i64toa(&buf[0], 12)]))
34 assert.Equal(t, "123" , string(buf[:i64toa(&buf[0], 123)]))
35 assert.Equal(t, "1234" , string(buf[:i64toa(&buf[0], 1234)]))
36 assert.Equal(t, "12345" , string(buf[:i64toa(&buf[0], 12345)]))
37 assert.Equal(t, "123456" , string(buf[:i64toa(&buf[0], 123456)]))
38 assert.Equal(t, "1234567" , string(buf[:i64toa(&buf[0], 1234567)]))
39 assert.Equal(t, "12345678" , string(buf[:i64toa(&buf[0], 12345678)]))
40 assert.Equal(t, "123456789" , string(buf[:i64toa(&buf[0], 123456789)]))
41 assert.Equal(t, "1234567890" , string(buf[:i64toa(&buf[0], 1234567890)]))
42 assert.Equal(t, "12345678901" , string(buf[:i64toa(&buf[0], 12345678901)]))
43 assert.Equal(t, "123456789012" , string(buf[:i64toa(&buf[0], 123456789012)]))
44 assert.Equal(t, "1234567890123" , string(buf[:i64toa(&buf[0], 1234567890123)]))
45 assert.Equal(t, "12345678901234" , string(buf[:i64toa(&buf[0], 12345678901234)]))
46 assert.Equal(t, "123456789012345" , string(buf[:i64toa(&buf[0], 123456789012345)]))
47 assert.Equal(t, "1234567890123456" , string(buf[:i64toa(&buf[0], 1234567890123456)]))
48 assert.Equal(t, "12345678901234567" , string(buf[:i64toa(&buf[0], 12345678901234567)]))
49 assert.Equal(t, "123456789012345678" , string(buf[:i64toa(&buf[0], 123456789012345678)]))
50 assert.Equal(t, "1234567890123456789" , string(buf[:i64toa(&buf[0], 1234567890123456789)]))
51 assert.Equal(t, "9223372036854775807" , string(buf[:i64toa(&buf[0], 9223372036854775807)]))
52 assert.Equal(t, "-1" , string(buf[:i64toa(&buf[0], -1)]))
53 assert.Equal(t, "-12" , string(buf[:i64toa(&buf[0], -12)]))
54 assert.Equal(t, "-123" , string(buf[:i64toa(&buf[0], -123)]))
55 assert.Equal(t, "-1234" , string(buf[:i64toa(&buf[0], -1234)]))
56 assert.Equal(t, "-12345" , string(buf[:i64toa(&buf[0], -12345)]))
57 assert.Equal(t, "-123456" , string(buf[:i64toa(&buf[0], -123456)]))
58 assert.Equal(t, "-1234567" , string(buf[:i64toa(&buf[0], -1234567)]))
59 assert.Equal(t, "-12345678" , string(buf[:i64toa(&buf[0], -12345678)]))
60 assert.Equal(t, "-123456789" , string(buf[:i64toa(&buf[0], -123456789)]))
61 assert.Equal(t, "-1234567890" , string(buf[:i64toa(&buf[0], -1234567890)]))
62 assert.Equal(t, "-12345678901" , string(buf[:i64toa(&buf[0], -12345678901)]))
63 assert.Equal(t, "-123456789012" , string(buf[:i64toa(&buf[0], -123456789012)]))
64 assert.Equal(t, "-1234567890123" , string(buf[:i64toa(&buf[0], -1234567890123)]))
65 assert.Equal(t, "-12345678901234" , string(buf[:i64toa(&buf[0], -12345678901234)]))
66 assert.Equal(t, "-123456789012345" , string(buf[:i64toa(&buf[0], -123456789012345)]))
67 assert.Equal(t, "-1234567890123456" , string(buf[:i64toa(&buf[0], -1234567890123456)]))
68 assert.Equal(t, "-12345678901234567" , string(buf[:i64toa(&buf[0], -12345678901234567)]))
69 assert.Equal(t, "-123456789012345678" , string(buf[:i64toa(&buf[0], -123456789012345678)]))
70 assert.Equal(t, "-1234567890123456789" , string(buf[:i64toa(&buf[0], -1234567890123456789)]))
71 assert.Equal(t, "-9223372036854775808" , string(buf[:i64toa(&buf[0], -9223372036854775808)]))
72}
73
74func TestFastInt_UintToString(t *testing.T) {
75 var buf [32]byte
76 assert.Equal(t, "0" , string(buf[:u64toa(&buf[0], 0)]))
77 assert.Equal(t, "1" , string(buf[:u64toa(&buf[0], 1)]))
78 assert.Equal(t, "12" , string(buf[:u64toa(&buf[0], 12)]))
79 assert.Equal(t, "123" , string(buf[:u64toa(&buf[0], 123)]))
80 assert.Equal(t, "1234" , string(buf[:u64toa(&buf[0], 1234)]))
81 assert.Equal(t, "12345" , string(buf[:u64toa(&buf[0], 12345)]))
82 assert.Equal(t, "123456" , string(buf[:u64toa(&buf[0], 123456)]))
83 assert.Equal(t, "1234567" , string(buf[:u64toa(&buf[0], 1234567)]))
84 assert.Equal(t, "12345678" , string(buf[:u64toa(&buf[0], 12345678)]))
85 assert.Equal(t, "123456789" , string(buf[:u64toa(&buf[0], 123456789)]))
86 assert.Equal(t, "1234567890" , string(buf[:u64toa(&buf[0], 1234567890)]))
87 assert.Equal(t, "12345678901" , string(buf[:u64toa(&buf[0], 12345678901)]))
88 assert.Equal(t, "123456789012" , string(buf[:u64toa(&buf[0], 123456789012)]))
89 assert.Equal(t, "1234567890123" , string(buf[:u64toa(&buf[0], 1234567890123)]))
90 assert.Equal(t, "12345678901234" , string(buf[:u64toa(&buf[0], 12345678901234)]))
91 assert.Equal(t, "123456789012345" , string(buf[:u64toa(&buf[0], 123456789012345)]))
92 assert.Equal(t, "1234567890123456" , string(buf[:u64toa(&buf[0], 1234567890123456)]))
93 assert.Equal(t, "12345678901234567" , string(buf[:u64toa(&buf[0], 12345678901234567)]))
94 assert.Equal(t, "123456789012345678" , string(buf[:u64toa(&buf[0], 123456789012345678)]))
95 assert.Equal(t, "1234567890123456789" , string(buf[:u64toa(&buf[0], 1234567890123456789)]))
96 assert.Equal(t, "12345678901234567890" , string(buf[:u64toa(&buf[0], 12345678901234567890)]))
97 assert.Equal(t, "18446744073709551615" , string(buf[:u64toa(&buf[0], 18446744073709551615)]))
98}
99
100func BenchmarkFastInt_IntToString(b *testing.B) {
101 benchmarks := []struct {
102 name string
103 test func(*testing.B)
104 }{{
105 name: "StdLib-Positive",
106 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendInt(buf[:0], int64(i), 10) }},
107 }, {
108 name: "StdLib-Negative",
109 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendInt(buf[:0], -int64(i), 10) }},
110 }, {
111 name: "FastInt-Positive",
112 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { i64toa(&buf[0], int64(i)) }},
113 }, {
114 name: "FastInt-Negative",
115 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { i64toa(&buf[0], -int64(i)) }},
116 }}
117 for _, bm := range benchmarks {
118 b.Run(bm.name, bm.test)
119 }
120}
121
122type utoaBench struct {
123 name string
124 num uint64
125}
126
127func BenchmarkFastInt_UintToString(b *testing.B) {
128 maxUint := "18446744073709551615"
129 benchs := make([]utoaBench, len(maxUint) + 1)
130 benchs[0].name = "Zero"
131 benchs[0].num = 0
132 for i := 1; i <= len(maxUint); i++ {
133 benchs[i].name = strconv.FormatInt(int64(i), 10) + "-Digs"
134 benchs[i].num, _ = strconv.ParseUint(string(maxUint[:i]), 10, 64)
135 }
136
137 for _, t := range(benchs) {
138 benchmarks := []struct {
139 name string
140 test func(*testing.B)
141 }{{
142 name: "StdLib",
143 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { strconv.AppendUint(buf[:0], t.num, 10) }},
144 }, {
145 name: "FastInt",
146 test: func(b *testing.B) { var buf [32]byte; for i := 0; i < b.N; i++ { u64toa(&buf[0], t.num) }},
147 }}
148 for _, bm := range benchmarks {
149 name := fmt.Sprintf("%s_%s", bm.name, t.name)
150 b.Run(name, bm.test)
151 }
152 }
153}
View as plain text