| 1 |
package strconv |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
"math/rand" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/tdewolff/test" |
| 10 |
) |
| 11 |
|
| 12 |
func TestParseInt(t *testing.T) { |
| 13 |
intTests := []struct { |
| 14 |
i string |
| 15 |
expected int64 |
| 16 |
}{ |
| 17 |
{"5", 5}, |
| 18 |
{"99", 99}, |
| 19 |
{"999", 999}, |
| 20 |
{"-5", -5}, |
| 21 |
{"+5", 5}, |
| 22 |
{"9223372036854775807", 9223372036854775807}, |
| 23 |
{"-9223372036854775807", -9223372036854775807}, |
| 24 |
{"-9223372036854775808", -9223372036854775808}, |
| 25 |
} |
| 26 |
for _, tt := range intTests { |
| 27 |
t.Run(fmt.Sprint(tt.i), func(t *testing.T) { |
| 28 |
i, n := ParseInt([]byte(tt.i)) |
| 29 |
test.T(t, n, len(tt.i)) |
| 30 |
test.T(t, i, tt.expected) |
| 31 |
}) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestParseIntError(t *testing.T) { |
| 36 |
intTests := []struct { |
| 37 |
i string |
| 38 |
n int |
| 39 |
expected int64 |
| 40 |
}{ |
| 41 |
{"a", 0, 0}, |
| 42 |
{"+", 0, 0}, |
| 43 |
{"9223372036854775808", 0, 0}, |
| 44 |
{"-9223372036854775809", 0, 0}, |
| 45 |
{"18446744073709551620", 0, 0}, |
| 46 |
} |
| 47 |
for _, tt := range intTests { |
| 48 |
t.Run(fmt.Sprint(tt.i), func(t *testing.T) { |
| 49 |
i, n := ParseInt([]byte(tt.i)) |
| 50 |
test.T(t, n, tt.n) |
| 51 |
test.T(t, i, tt.expected) |
| 52 |
}) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestAppendInt(t *testing.T) { |
| 57 |
intTests := []struct { |
| 58 |
i int64 |
| 59 |
expected string |
| 60 |
}{ |
| 61 |
{0, "0"}, |
| 62 |
{5, "5"}, |
| 63 |
{99, "99"}, |
| 64 |
{999, "999"}, |
| 65 |
{-5, "-5"}, |
| 66 |
{9223372036854775807, "9223372036854775807"}, |
| 67 |
{-9223372036854775807, "-9223372036854775807"}, |
| 68 |
{-9223372036854775808, "-9223372036854775808"}, |
| 69 |
} |
| 70 |
for _, tt := range intTests { |
| 71 |
t.Run(fmt.Sprint(tt.i), func(t *testing.T) { |
| 72 |
b := AppendInt(nil, tt.i) |
| 73 |
test.T(t, string(b), tt.expected) |
| 74 |
}) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func FuzzParseInt(f *testing.F) { |
| 79 |
f.Add("5") |
| 80 |
f.Add("-99") |
| 81 |
f.Add("9223372036854775807") |
| 82 |
f.Add("-9223372036854775808") |
| 83 |
f.Fuzz(func(t *testing.T, s string) { |
| 84 |
ParseInt([]byte(s)) |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
func TestParseUint(t *testing.T) { |
| 89 |
intTests := []struct { |
| 90 |
i string |
| 91 |
expected uint64 |
| 92 |
}{ |
| 93 |
{"5", 5}, |
| 94 |
{"99", 99}, |
| 95 |
{"999", 999}, |
| 96 |
{"18446744073709551615", 18446744073709551615}, |
| 97 |
} |
| 98 |
for _, tt := range intTests { |
| 99 |
t.Run(fmt.Sprint(tt.i), func(t *testing.T) { |
| 100 |
i, n := ParseUint([]byte(tt.i)) |
| 101 |
test.T(t, n, len(tt.i)) |
| 102 |
test.T(t, i, tt.expected) |
| 103 |
}) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestParseUintError(t *testing.T) { |
| 108 |
intTests := []struct { |
| 109 |
i string |
| 110 |
n int |
| 111 |
expected uint64 |
| 112 |
}{ |
| 113 |
{"a", 0, 0}, |
| 114 |
{"18446744073709551616", 0, 0}, |
| 115 |
{"-1", 0, 0}, |
| 116 |
} |
| 117 |
for _, tt := range intTests { |
| 118 |
t.Run(fmt.Sprint(tt.i), func(t *testing.T) { |
| 119 |
i, n := ParseUint([]byte(tt.i)) |
| 120 |
test.T(t, n, tt.n) |
| 121 |
test.T(t, i, tt.expected) |
| 122 |
}) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func FuzzParseUint(f *testing.F) { |
| 127 |
f.Add("5") |
| 128 |
f.Add("99") |
| 129 |
f.Add("18446744073709551615") |
| 130 |
f.Fuzz(func(t *testing.T, s string) { |
| 131 |
ParseUint([]byte(s)) |
| 132 |
}) |
| 133 |
} |
| 134 |
|
| 135 |
func TestLenInt(t *testing.T) { |
| 136 |
lenIntTests := []struct { |
| 137 |
i int64 |
| 138 |
expected int |
| 139 |
}{ |
| 140 |
{0, 1}, |
| 141 |
{1, 1}, |
| 142 |
{10, 2}, |
| 143 |
{99, 2}, |
| 144 |
{9223372036854775807, 19}, |
| 145 |
{-9223372036854775808, 20}, |
| 146 |
|
| 147 |
// coverage |
| 148 |
{100, 3}, |
| 149 |
{1000, 4}, |
| 150 |
{10000, 5}, |
| 151 |
{100000, 6}, |
| 152 |
{1000000, 7}, |
| 153 |
{10000000, 8}, |
| 154 |
{100000000, 9}, |
| 155 |
{1000000000, 10}, |
| 156 |
{10000000000, 11}, |
| 157 |
{100000000000, 12}, |
| 158 |
{1000000000000, 13}, |
| 159 |
{10000000000000, 14}, |
| 160 |
{100000000000000, 15}, |
| 161 |
{1000000000000000, 16}, |
| 162 |
{10000000000000000, 17}, |
| 163 |
{100000000000000000, 18}, |
| 164 |
{1000000000000000000, 19}, |
| 165 |
} |
| 166 |
for _, tt := range lenIntTests { |
| 167 |
t.Run(fmt.Sprint(tt.i), func(t *testing.T) { |
| 168 |
test.T(t, LenInt(tt.i), tt.expected) |
| 169 |
}) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
//////////////////////////////////////////////////////////////// |
| 174 |
|
| 175 |
var num []int64 |
| 176 |
|
| 177 |
func TestMain(t *testing.T) { |
| 178 |
for j := 0; j < 1000; j++ { |
| 179 |
num = append(num, rand.Int63n(1000)) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func BenchmarkLenIntLog(b *testing.B) { |
| 184 |
n := 0 |
| 185 |
for i := 0; i < b.N; i++ { |
| 186 |
for j := 0; j < 1000; j++ { |
| 187 |
n += int(math.Log10(math.Abs(float64(num[j])))) + 1 |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func BenchmarkLenIntSwitch(b *testing.B) { |
| 193 |
n := 0 |
| 194 |
for i := 0; i < b.N; i++ { |
| 195 |
for j := 0; j < 1000; j++ { |
| 196 |
n += LenInt(num[j]) |
| 197 |
} |
| 198 |
} |
| 199 |
} |