2012-04-14 05:34:08 -06:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package strconv_test
|
|
|
|
|
|
|
|
import (
|
2013-03-06 16:52:32 -07:00
|
|
|
"runtime"
|
2012-04-14 05:34:08 -06:00
|
|
|
. "strconv"
|
2012-04-21 05:56:51 -06:00
|
|
|
"strings"
|
2012-04-14 05:34:08 -06:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
globalBuf [64]byte
|
2012-04-21 05:56:51 -06:00
|
|
|
nextToOne = "1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1"
|
2012-04-14 05:34:08 -06:00
|
|
|
|
|
|
|
mallocTest = []struct {
|
|
|
|
count int
|
|
|
|
desc string
|
|
|
|
fn func()
|
|
|
|
}{
|
2013-04-04 18:04:18 -06:00
|
|
|
{0, `AppendInt(localBuf[:0], 123, 10)`, func() {
|
2012-04-14 05:34:08 -06:00
|
|
|
var localBuf [64]byte
|
|
|
|
AppendInt(localBuf[:0], 123, 10)
|
|
|
|
}},
|
|
|
|
{0, `AppendInt(globalBuf[:0], 123, 10)`, func() { AppendInt(globalBuf[:0], 123, 10) }},
|
2013-04-04 18:04:18 -06:00
|
|
|
{0, `AppendFloat(localBuf[:0], 1.23, 'g', 5, 64)`, func() {
|
2012-04-14 05:34:08 -06:00
|
|
|
var localBuf [64]byte
|
|
|
|
AppendFloat(localBuf[:0], 1.23, 'g', 5, 64)
|
|
|
|
}},
|
|
|
|
{0, `AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64)`, func() { AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64) }},
|
2012-04-21 05:56:51 -06:00
|
|
|
{0, `ParseFloat("123.45", 64)`, func() { ParseFloat("123.45", 64) }},
|
|
|
|
{0, `ParseFloat("123.456789123456789", 64)`, func() { ParseFloat("123.456789123456789", 64) }},
|
|
|
|
{0, `ParseFloat("1.000000000000000111022302462515654042363166809082031251", 64)`, func() {
|
|
|
|
ParseFloat("1.000000000000000111022302462515654042363166809082031251", 64)
|
|
|
|
}},
|
|
|
|
{0, `ParseFloat("1.0000000000000001110223024625156540423631668090820312500...001", 64)`, func() {
|
|
|
|
ParseFloat(nextToOne, 64)
|
|
|
|
}},
|
2012-04-14 05:34:08 -06:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCountMallocs(t *testing.T) {
|
2013-08-20 22:00:45 -06:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping malloc count in short mode")
|
|
|
|
}
|
2013-03-06 16:52:32 -07:00
|
|
|
if runtime.GOMAXPROCS(0) > 1 {
|
|
|
|
t.Skip("skipping; GOMAXPROCS>1")
|
|
|
|
}
|
2012-04-14 05:34:08 -06:00
|
|
|
for _, mt := range mallocTest {
|
2013-02-02 20:52:29 -07:00
|
|
|
allocs := testing.AllocsPerRun(100, mt.fn)
|
|
|
|
if max := float64(mt.count); allocs > max {
|
|
|
|
t.Errorf("%s: %v allocs, want <=%v", mt.desc, allocs, max)
|
2012-04-14 05:34:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|