| 1 |
// Copyright 2012 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package strconv_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"runtime" |
| 9 |
. "strconv" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
globalBuf [64]byte |
| 16 |
nextToOne = "1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1" |
| 17 |
|
| 18 |
mallocTest = []struct { |
| 19 |
count int |
| 20 |
desc string |
| 21 |
fn func() |
| 22 |
}{ |
| 23 |
{0, `AppendInt(localBuf[:0], 123, 10)`, func() { |
| 24 |
var localBuf [64]byte |
| 25 |
AppendInt(localBuf[:0], 123, 10) |
| 26 |
}}, |
| 27 |
{0, `AppendInt(globalBuf[:0], 123, 10)`, func() { AppendInt(globalBuf[:0], 123, 10) }}, |
| 28 |
{0, `AppendFloat(localBuf[:0], 1.23, 'g', 5, 64)`, func() { |
| 29 |
var localBuf [64]byte |
| 30 |
AppendFloat(localBuf[:0], 1.23, 'g', 5, 64) |
| 31 |
}}, |
| 32 |
{0, `AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64)`, func() { AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64) }}, |
| 33 |
// In practice we see 7 for the next one, but allow some slop. |
| 34 |
// Before pre-allocation in appendQuotedWith, we saw 39. |
| 35 |
{10, `AppendQuoteToASCII(nil, oneMB)`, func() { AppendQuoteToASCII(nil, string(oneMB)) }}, |
| 36 |
{0, `ParseFloat("123.45", 64)`, func() { ParseFloat("123.45", 64) }}, |
| 37 |
{0, `ParseFloat("123.456789123456789", 64)`, func() { ParseFloat("123.456789123456789", 64) }}, |
| 38 |
{0, `ParseFloat("1.000000000000000111022302462515654042363166809082031251", 64)`, func() { |
| 39 |
ParseFloat("1.000000000000000111022302462515654042363166809082031251", 64) |
| 40 |
}}, |
| 41 |
{0, `ParseFloat("1.0000000000000001110223024625156540423631668090820312500...001", 64)`, func() { |
| 42 |
ParseFloat(nextToOne, 64) |
| 43 |
}}, |
| 44 |
} |
| 45 |
) |
| 46 |
|
| 47 |
var oneMB []byte // Will be allocated to 1MB of random data by TestCountMallocs. |
| 48 |
|
| 49 |
func TestCountMallocs(t *testing.T) { |
| 50 |
if runtime.Compiler == "gccgo" { |
| 51 |
t.Skip("skipping on gccgo until escape analysis is turned on") |
| 52 |
} |
| 53 |
if testing.Short() { |
| 54 |
t.Skip("skipping malloc count in short mode") |
| 55 |
} |
| 56 |
if runtime.GOMAXPROCS(0) > 1 { |
| 57 |
t.Skip("skipping; GOMAXPROCS>1") |
| 58 |
} |
| 59 |
// Allocate a big messy buffer for AppendQuoteToASCII's test. |
| 60 |
oneMB = make([]byte, 1e6) |
| 61 |
for i := range oneMB { |
| 62 |
oneMB[i] = byte(i) |
| 63 |
} |
| 64 |
for _, mt := range mallocTest { |
| 65 |
allocs := testing.AllocsPerRun(100, mt.fn) |
| 66 |
if max := float64(mt.count); allocs > max { |
| 67 |
t.Errorf("%s: %v allocs, want <=%v", mt.desc, allocs, max) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestErrorPrefixes(t *testing.T) { |
| 73 |
_, errInt := Atoi("INVALID") |
| 74 |
_, errBool := ParseBool("INVALID") |
| 75 |
_, errFloat := ParseFloat("INVALID", 64) |
| 76 |
_, errInt64 := ParseInt("INVALID", 10, 64) |
| 77 |
_, errUint64 := ParseUint("INVALID", 10, 64) |
| 78 |
|
| 79 |
vectors := []struct { |
| 80 |
err error // Input error |
| 81 |
want string // Function name wanted |
| 82 |
}{ |
| 83 |
{errInt, "Atoi"}, |
| 84 |
{errBool, "ParseBool"}, |
| 85 |
{errFloat, "ParseFloat"}, |
| 86 |
{errInt64, "ParseInt"}, |
| 87 |
{errUint64, "ParseUint"}, |
| 88 |
} |
| 89 |
|
| 90 |
for _, v := range vectors { |
| 91 |
nerr, ok := v.err.(*NumError) |
| 92 |
if !ok { |
| 93 |
t.Errorf("test %s, error was not a *NumError", v.want) |
| 94 |
continue |
| 95 |
} |
| 96 |
if got := nerr.Func; got != v.want { |
| 97 |
t.Errorf("mismatching Func: got %s, want %s", got, v.want) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} |