blob: 72cea492562d8f75a883fa9eeae43bd853b86475 [file] [log] [blame]
// Copyright 2009 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 (
"math"
"math/rand"
"reflect"
. "strconv"
"strings"
"testing"
"time"
)
type atofTest struct {
in string
out string
err error
}
var atoftests = []atofTest{
{"", "0", ErrSyntax},
{"1", "1", nil},
{"+1", "1", nil},
{"1x", "0", ErrSyntax},
{"1.1.", "0", ErrSyntax},
{"1e23", "1e+23", nil},
{"1E23", "1e+23", nil},
{"100000000000000000000000", "1e+23", nil},
{"1e-100", "1e-100", nil},
{"123456700", "1.234567e+08", nil},
{"99999999999999974834176", "9.999999999999997e+22", nil},
{"100000000000000000000001", "1.0000000000000001e+23", nil},
{"100000000000000008388608", "1.0000000000000001e+23", nil},
{"100000000000000016777215", "1.0000000000000001e+23", nil},
{"100000000000000016777216", "1.0000000000000003e+23", nil},
{"-1", "-1", nil},
{"-0.1", "-0.1", nil},
{"-0", "-0", nil},
{"1e-20", "1e-20", nil},
{"625e-3", "0.625", nil},
// NaNs
{"nan", "NaN", nil},
{"NaN", "NaN", nil},
{"NAN", "NaN", nil},
// Infs
{"inf", "+Inf", nil},
{"-Inf", "-Inf", nil},
{"+INF", "+Inf", nil},
{"-Infinity", "-Inf", nil},
{"+INFINITY", "+Inf", nil},
{"Infinity", "+Inf", nil},
// largest float64
{"1.7976931348623157e308", "1.7976931348623157e+308", nil},
{"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
// next float64 - too large
{"1.7976931348623159e308", "+Inf", ErrRange},
{"-1.7976931348623159e308", "-Inf", ErrRange},
// the border is ...158079
// borderline - okay
{"1.7976931348623158e308", "1.7976931348623157e+308", nil},
{"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
// borderline - too large
{"1.797693134862315808e308", "+Inf", ErrRange},
{"-1.797693134862315808e308", "-Inf", ErrRange},
// a little too large
{"1e308", "1e+308", nil},
{"2e308", "+Inf", ErrRange},
{"1e309", "+Inf", ErrRange},
// way too large
{"1e310", "+Inf", ErrRange},
{"-1e310", "-Inf", ErrRange},
{"1e400", "+Inf", ErrRange},
{"-1e400", "-Inf", ErrRange},
{"1e400000", "+Inf", ErrRange},
{"-1e400000", "-Inf", ErrRange},
// denormalized
{"1e-305", "1e-305", nil},
{"1e-306", "1e-306", nil},
{"1e-307", "1e-307", nil},
{"1e-308", "1e-308", nil},
{"1e-309", "1e-309", nil},
{"1e-310", "1e-310", nil},
{"1e-322", "1e-322", nil},
// smallest denormal
{"5e-324", "5e-324", nil},
{"4e-324", "5e-324", nil},
{"3e-324", "5e-324", nil},
// too small
{"2e-324", "0", nil},
// way too small
{"1e-350", "0", nil},
{"1e-400000", "0", nil},
// try to overflow exponent
{"1e-4294967296", "0", nil},
{"1e+4294967296", "+Inf", ErrRange},
{"1e-18446744073709551616", "0", nil},
{"1e+18446744073709551616", "+Inf", ErrRange},
// Parse errors
{"1e", "0", ErrSyntax},
{"1e-", "0", ErrSyntax},
{".e-1", "0", ErrSyntax},
// http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
{"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
// http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
{"2.2250738585072011e-308", "2.225073858507201e-308", nil},
// A very large number (initially wrongly parsed by the fast algorithm).
{"4.630813248087435e+307", "4.630813248087435e+307", nil},
// A different kind of very large number.
{"22.222222222222222", "22.22222222222222", nil},
{"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
// Exactly halfway between 1 and math.Nextafter(1, 2).
// Round to even (down).
{"1.00000000000000011102230246251565404236316680908203125", "1", nil},
// Slightly lower; still round down.
{"1.00000000000000011102230246251565404236316680908203124", "1", nil},
// Slightly higher; round up.
{"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
// Slightly higher, but you have to read all the way to the end.
{"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
}
type atofSimpleTest struct {
x float64
s string
}
var (
atofRandomTests []atofSimpleTest
benchmarksRandomBits [1024]string
benchmarksRandomNormal [1024]string
)
func init() {
// The atof routines return NumErrors wrapping
// the error and the string. Convert the table above.
for i := range atoftests {
test := &atoftests[i]
if test.err != nil {
test.err = &NumError{"ParseFloat", test.in, test.err}
}
}
// Generate random inputs for tests and benchmarks
rand.Seed(time.Now().UnixNano())
if testing.Short() {
atofRandomTests = make([]atofSimpleTest, 100)
} else {
atofRandomTests = make([]atofSimpleTest, 10000)
}
for i := range atofRandomTests {
n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
x := math.Float64frombits(n)
s := FormatFloat(x, 'g', -1, 64)
atofRandomTests[i] = atofSimpleTest{x, s}
}
for i := range benchmarksRandomBits {
bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
x := math.Float64frombits(bits)
benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64)
}
for i := range benchmarksRandomNormal {
x := rand.NormFloat64()
benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64)
}
}
func testAtof(t *testing.T, opt bool) {
oldopt := SetOptimize(opt)
for i := 0; i < len(atoftests); i++ {
test := &atoftests[i]
out, err := ParseFloat(test.in, 64)
outs := FormatFloat(out, 'g', -1, 64)
if outs != test.out || !reflect.DeepEqual(err, test.err) {
t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
test.in, out, err, test.out, test.err)
}
if float64(float32(out)) == out {
out, err := ParseFloat(test.in, 32)
out32 := float32(out)
if float64(out32) != out {
t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
continue
}
outs := FormatFloat(float64(out32), 'g', -1, 32)
if outs != test.out || !reflect.DeepEqual(err, test.err) {
t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
test.in, out32, err, test.out, test.err, out)
}
}
}
SetOptimize(oldopt)
}
func TestAtof(t *testing.T) { testAtof(t, true) }
func TestAtofSlow(t *testing.T) { testAtof(t, false) }
func TestAtofRandom(t *testing.T) {
for _, test := range atofRandomTests {
x, _ := ParseFloat(test.s, 64)
switch {
default:
t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x)
case x == test.x:
case math.IsNaN(test.x) && math.IsNaN(x):
}
}
t.Logf("tested %d random numbers", len(atofRandomTests))
}
func BenchmarkAtof64Decimal(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseFloat("33909", 64)
}
}
func BenchmarkAtof64Float(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseFloat("339.7784", 64)
}
}
func BenchmarkAtof64FloatExp(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseFloat("-5.09e75", 64)
}
}
func BenchmarkAtof64Big(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseFloat("123456789123456789123456789", 64)
}
}
func BenchmarkAtof64RandomBits(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseFloat(benchmarksRandomBits[i%1024], 64)
}
}
func BenchmarkAtof64RandomFloats(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseFloat(benchmarksRandomNormal[i%1024], 64)
}
}