2008-11-17 13:34:03 -07:00
|
|
|
// 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.
|
|
|
|
|
2008-11-18 17:13:25 -07:00
|
|
|
package strconv
|
|
|
|
import (
|
|
|
|
"fmt";
|
|
|
|
"os";
|
2008-11-19 15:38:05 -07:00
|
|
|
"strconv";
|
|
|
|
"testing"
|
2008-11-18 17:13:25 -07:00
|
|
|
)
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atofTest struct {
|
2008-11-17 13:34:03 -07:00
|
|
|
in string;
|
|
|
|
out string;
|
2009-04-17 01:08:24 -06:00
|
|
|
err os.Error;
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
var atoftests = []atofTest {
|
|
|
|
atofTest{ "", "0", os.EINVAL },
|
|
|
|
atofTest{ "1", "1", nil },
|
|
|
|
atofTest{ "+1", "1", nil },
|
|
|
|
atofTest{ "1x", "0", os.EINVAL },
|
|
|
|
atofTest{ "1.1.", "0", os.EINVAL },
|
|
|
|
atofTest{ "1e23", "1e+23", nil },
|
|
|
|
atofTest{ "100000000000000000000000", "1e+23", nil },
|
|
|
|
atofTest{ "1e-100", "1e-100", nil },
|
|
|
|
atofTest{ "123456700", "1.234567e+08", nil },
|
|
|
|
atofTest{ "99999999999999974834176", "9.999999999999997e+22", nil },
|
|
|
|
atofTest{ "100000000000000000000001", "1.0000000000000001e+23", nil },
|
|
|
|
atofTest{ "100000000000000008388608", "1.0000000000000001e+23", nil },
|
|
|
|
atofTest{ "100000000000000016777215", "1.0000000000000001e+23", nil },
|
|
|
|
atofTest{ "100000000000000016777216", "1.0000000000000003e+23", nil },
|
|
|
|
atofTest{ "-1", "-1", nil },
|
|
|
|
atofTest{ "-0", "-0", nil },
|
|
|
|
atofTest{ "1e-20", "1e-20", nil },
|
|
|
|
atofTest{ "625e-3", "0.625", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// largest float64
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1.7976931348623157e308", "1.7976931348623157e+308", nil },
|
|
|
|
atofTest{ "-1.7976931348623157e308", "-1.7976931348623157e+308", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
// next float64 - too large
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1.7976931348623159e308", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "-1.7976931348623159e308", "-Inf", os.ERANGE },
|
2008-11-17 18:22:51 -07:00
|
|
|
// the border is ...158079
|
|
|
|
// borderline - okay
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1.7976931348623158e308", "1.7976931348623157e+308", nil },
|
|
|
|
atofTest{ "-1.7976931348623158e308", "-1.7976931348623157e+308", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
// borderline - too large
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1.797693134862315808e308", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "-1.797693134862315808e308", "-Inf", os.ERANGE },
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// a little too large
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1e308", "1e+308", nil },
|
|
|
|
atofTest{ "2e308", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "1e309", "+Inf", os.ERANGE },
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// way too large
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1e310", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "-1e310", "-Inf", os.ERANGE },
|
|
|
|
atofTest{ "1e400", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "-1e400", "-Inf", os.ERANGE },
|
|
|
|
atofTest{ "1e400000", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "-1e400000", "-Inf", os.ERANGE },
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// denormalized
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1e-305", "1e-305", nil },
|
|
|
|
atofTest{ "1e-306", "1e-306", nil },
|
|
|
|
atofTest{ "1e-307", "1e-307", nil },
|
|
|
|
atofTest{ "1e-308", "1e-308", nil },
|
|
|
|
atofTest{ "1e-309", "1e-309", nil },
|
|
|
|
atofTest{ "1e-310", "1e-310", nil },
|
|
|
|
atofTest{ "1e-322", "1e-322", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
// smallest denormal
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "5e-324", "5e-324", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
// too small
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "4e-324", "0", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
// way too small
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1e-350", "0", nil },
|
|
|
|
atofTest{ "1e-400000", "0", nil },
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// try to overflow exponent
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1e-4294967296", "0", nil },
|
|
|
|
atofTest{ "1e+4294967296", "+Inf", os.ERANGE },
|
|
|
|
atofTest{ "1e-18446744073709551616", "0", nil },
|
|
|
|
atofTest{ "1e+18446744073709551616", "+Inf", os.ERANGE },
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// Parse errors
|
2009-03-03 09:39:12 -07:00
|
|
|
atofTest{ "1e", "0", os.EINVAL },
|
|
|
|
atofTest{ "1e-", "0", os.EINVAL },
|
|
|
|
atofTest{ ".e-1", "0", os.EINVAL },
|
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
func testAtof(t *testing.T, opt bool) {
|
2008-11-19 13:50:34 -07:00
|
|
|
oldopt := strconv.optimize;
|
|
|
|
strconv.optimize = opt;
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoftests); i++ {
|
|
|
|
test := &atoftests[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atof64(test.in);
|
|
|
|
outs := strconv.Ftoa64(out, 'g', -1);
|
2008-11-19 15:38:05 -07:00
|
|
|
if outs != test.out || err != test.err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atof64(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
|
|
|
|
if float64(float32(out)) == out {
|
2009-01-15 18:22:17 -07:00
|
|
|
out32, err := strconv.Atof32(test.in);
|
|
|
|
outs := strconv.Ftoa32(out32, 'g', -1);
|
2008-11-19 15:38:05 -07:00
|
|
|
if outs != test.out || err != test.err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atof32(%v) = %v, %v want %v, %v # %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out32, err, test.out, test.err, out);
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 17:16:42 -07:00
|
|
|
if FloatSize == 64 || float64(float32(out)) == out {
|
2009-01-15 18:22:17 -07:00
|
|
|
outf, err := strconv.Atof(test.in);
|
|
|
|
outs := strconv.Ftoa(outf, 'g', -1);
|
2008-11-19 15:38:05 -07:00
|
|
|
if outs != test.out || err != test.err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Ftoa(%v) = %v, %v want %v, %v # %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, outf, err, test.out, test.err, out);
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
strconv.optimize = oldopt;
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2008-11-19 13:50:34 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAtof(t *testing.T) {
|
2009-01-15 18:22:17 -07:00
|
|
|
testAtof(t, true);
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAtofSlow(t *testing.T) {
|
2009-01-15 18:22:17 -07:00
|
|
|
testAtof(t, false);
|
2008-11-19 13:50:34 -07:00
|
|
|
}
|