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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package cast
import (
"regexp"
"strconv"
"strings"
"testing"
qt "github.com/frankban/quicktest"
)
func BenchmarkToInt(b *testing.B) {
convert := func(num52 interface{}) {
if v := ToInt(num52); v != 52 {
b.Fatalf("ToInt returned wrong value, got %d, want %d", v, 32)
}
}
for i := 0; i < b.N; i++ {
convert("52")
convert(52.0)
convert(uint64(52))
}
}
func BenchmarkTrimZeroDecimal(b *testing.B) {
for i := 0; i < b.N; i++ {
trimZeroDecimal("")
trimZeroDecimal("123")
trimZeroDecimal("120")
trimZeroDecimal("120.00")
}
}
func TestTrimZeroDecimal(t *testing.T) {
c := qt.New(t)
c.Assert(trimZeroDecimal("10.0"), qt.Equals, "10")
c.Assert(trimZeroDecimal("10.00"), qt.Equals, "10")
c.Assert(trimZeroDecimal("10.010"), qt.Equals, "10.010")
c.Assert(trimZeroDecimal("0.0000000000"), qt.Equals, "0")
c.Assert(trimZeroDecimal("0.00000000001"), qt.Equals, "0.00000000001")
}
func TestTrimDecimal(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"10.0", "10"},
{"10.010", "10"},
{"00000.00001", "00000"},
{"-0001.0", "-0001"},
{".5", "0"},
{"+12.", "+12"},
{"+.25", "+0"},
{"-.25", "-0"},
{"0.0000000000", "0"},
{"0.0000000001", "0"},
{"10.0000000000", "10"},
{"10.0000000001", "10"},
{"10000000000000.0000000000", "10000000000000"},
{"10...17", "10...17"},
{"10.foobar", "10.foobar"},
{"10.0i", "10.0i"},
{"10.0E9", "10.0E9"},
}
for _, testCase := range testCases {
// TODO: remove after minimum Go version is >=1.22
testCase := testCase
t.Run(testCase.input, func(t *testing.T) {
c := qt.New(t)
c.Assert(trimDecimal(testCase.input), qt.Equals, testCase.expected)
})
}
}
// Analysis (in the order of performance):
//
// - Trimming decimals based on decimal point yields a lot of incorrectly parsed values.
// - Parsing to float might be better, but we still need to cast the number, it might overflow, problematic.
// - Regex parsing is an order of magnitude slower, but it yields correct results.
func BenchmarkDecimal(b *testing.B) {
testCases := []struct {
input string
expectError bool
}{
{"10.0", false},
{"10.00", false},
{"10.010", false},
{"0.0000000000", false},
{"0.0000000001", false},
{"10.0000000000", false},
{"10.0000000001", false},
{"10000000000000.0000000000", false},
// {"10...17", true},
// {"10.foobar", true},
// {"10.0i", true},
// {"10.0E9", true},
}
trimDecimalString := func(s string) string {
// trim the decimal part (if any)
if i := strings.Index(s, "."); i >= 0 {
s = s[:i]
}
return s
}
re := regexp.MustCompile(`^([-+]?\d*)(\.\d*)?$`)
trimDecimalRegex := func(s string) string {
matches := re.FindStringSubmatch(s)
if matches != nil {
// matches[1] is the captured integer part with sign
return matches[1]
}
return s
}
for _, testCase := range testCases {
// TODO: remove after minimum Go version is >=1.22
testCase := testCase
b.Run(testCase.input, func(b *testing.B) {
b.Run("ParseFloat", func(b *testing.B) {
// TODO: use b.Loop() once updated to Go 1.24
for i := 0; i < b.N; i++ {
v, err := strconv.ParseFloat(testCase.input, 64)
if (err != nil) != testCase.expectError {
if err != nil {
b.Fatal(err)
}
b.Fatal("expected error, but got none")
}
n := int64(v)
_ = n
}
})
b.Run("TrimDecimalString", func(b *testing.B) {
// TODO: use b.Loop() once updated to Go 1.24
for i := 0; i < b.N; i++ {
v, err := strconv.ParseInt(trimDecimalString(testCase.input), 0, 0)
if (err != nil) != testCase.expectError {
if err != nil {
b.Fatal(err)
}
b.Fatal("expected error, but got none")
}
_ = v
}
})
b.Run("TrimDecimalRegex", func(b *testing.B) {
// TODO: use b.Loop() once updated to Go 1.24
for i := 0; i < b.N; i++ {
v, err := strconv.ParseInt(trimDecimalRegex(testCase.input), 0, 0)
if (err != nil) != testCase.expectError {
if err != nil {
b.Fatal(err)
}
b.Fatal("expected error, but got none")
}
_ = v
}
})
})
}
}
|