// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package main
-
-import "strconv"
+package strconv
+import (
+ "fmt";
+ "os";
+ "strconv"
+)
type Test struct {
in string;
out string;
+ err *os.Error;
}
var tests = []Test {
- Test{ "1", "1" },
- Test{ "1e23", "1e+23" },
- Test{ "100000000000000000000000", "1e+23" },
- Test{ "1e-100", "1e-100" },
- Test{ "123456700", "1.234567e+08" },
- Test{ "99999999999999974834176", "9.999999999999997e+22" },
- Test{ "100000000000000000000001", "1.0000000000000001e+23" },
- Test{ "100000000000000008388608", "1.0000000000000001e+23" },
- Test{ "100000000000000016777215", "1.0000000000000001e+23" },
- Test{ "100000000000000016777216", "1.0000000000000003e+23" },
- Test{ "-1", "-1" },
- Test{ "-0", "0" },
- Test{ "1e-20", "1e-20" },
+ Test{ "1", "1", nil },
+ Test{ "1e23", "1e+23", nil },
+ Test{ "100000000000000000000000", "1e+23", nil },
+ Test{ "1e-100", "1e-100", nil },
+ Test{ "123456700", "1.234567e+08", nil },
+ Test{ "99999999999999974834176", "9.999999999999997e+22", nil },
+ Test{ "100000000000000000000001", "1.0000000000000001e+23", nil },
+ Test{ "100000000000000008388608", "1.0000000000000001e+23", nil },
+ Test{ "100000000000000016777215", "1.0000000000000001e+23", nil },
+ Test{ "100000000000000016777216", "1.0000000000000003e+23", nil },
+ Test{ "-1", "-1", nil },
+ Test{ "-0", "0", nil },
+ Test{ "1e-20", "1e-20", nil },
// largest float64
- Test{ "1.7976931348623157e308", "1.7976931348623157e+308" },
- Test{ "-1.7976931348623157e308", "-1.7976931348623157e+308" },
+ Test{ "1.7976931348623157e308", "1.7976931348623157e+308", nil },
+ Test{ "-1.7976931348623157e308", "-1.7976931348623157e+308", nil },
// next float64 - too large
- Test{ "1.7976931348623159e308", "+Inf" },
- Test{ "-1.7976931348623159e308", "-Inf" },
+ Test{ "1.7976931348623159e308", "+Inf", os.ERANGE },
+ Test{ "-1.7976931348623159e308", "-Inf", os.ERANGE },
// the border is ...158079
// borderline - okay
- Test{ "1.7976931348623158e308", "1.7976931348623157e+308" },
- Test{ "-1.7976931348623158e308", "-1.7976931348623157e+308" },
+ Test{ "1.7976931348623158e308", "1.7976931348623157e+308", nil },
+ Test{ "-1.7976931348623158e308", "-1.7976931348623157e+308", nil },
// borderline - too large
- Test{ "1.797693134862315808e308", "+Inf" },
- Test{ "-1.797693134862315808e308", "-Inf" },
+ Test{ "1.797693134862315808e308", "+Inf", os.ERANGE },
+ Test{ "-1.797693134862315808e308", "-Inf", os.ERANGE },
// a little too large
- Test{ "1e308", "1e+308" },
- Test{ "2e308", "+Inf" },
- Test{ "1e309", "+Inf" },
+ Test{ "1e308", "1e+308", nil },
+ Test{ "2e308", "+Inf", os.ERANGE },
+ Test{ "1e309", "+Inf", os.ERANGE },
// way too large
- Test{ "1e310", "+Inf" },
- Test{ "-1e310", "-Inf" },
- Test{ "1e400", "+Inf" },
- Test{ "-1e400", "-Inf" },
- Test{ "1e400000", "+Inf" },
- Test{ "-1e400000", "-Inf" },
+ Test{ "1e310", "+Inf", os.ERANGE },
+ Test{ "-1e310", "-Inf", os.ERANGE },
+ Test{ "1e400", "+Inf", os.ERANGE },
+ Test{ "-1e400", "-Inf", os.ERANGE },
+ Test{ "1e400000", "+Inf", os.ERANGE },
+ Test{ "-1e400000", "-Inf", os.ERANGE },
// denormalized
- Test{ "1e-305", "1e-305" },
- Test{ "1e-306", "1e-306" },
- Test{ "1e-307", "1e-307" },
- Test{ "1e-308", "1e-308" },
- Test{ "1e-309", "1e-309" },
- Test{ "1e-310", "1e-310" },
- Test{ "1e-322", "1e-322" },
+ Test{ "1e-305", "1e-305", nil },
+ Test{ "1e-306", "1e-306", nil },
+ Test{ "1e-307", "1e-307", nil },
+ Test{ "1e-308", "1e-308", nil },
+ Test{ "1e-309", "1e-309", nil },
+ Test{ "1e-310", "1e-310", nil },
+ Test{ "1e-322", "1e-322", nil },
// smallest denormal
- Test{ "5e-324", "5e-324" },
+ Test{ "5e-324", "5e-324", nil },
// too small
- Test{ "4e-324", "0" },
+ Test{ "4e-324", "0", nil },
// way too small
- Test{ "1e-350", "0" },
- Test{ "1e-400000", "0" },
+ Test{ "1e-350", "0", nil },
+ Test{ "1e-400000", "0", nil },
// try to overflow exponent
- Test{ "1e-4294967296", "0" },
- Test{ "1e+4294967296", "+Inf" },
- Test{ "1e-18446744073709551616", "0" },
- Test{ "1e+18446744073709551616", "+Inf" },
+ Test{ "1e-4294967296", "0", nil },
+ Test{ "1e+4294967296", "+Inf", os.ERANGE },
+ Test{ "1e-18446744073709551616", "0", nil },
+ Test{ "1e+18446744073709551616", "+Inf", os.ERANGE },
// Parse errors
- Test{ "1e", "error" },
- Test{ "1e-", "error" },
- Test{ ".e-1", "error" },
+ Test{ "1e", "0", os.EINVAL },
+ Test{ "1e-", "0", os.EINVAL },
+ Test{ ".e-1", "0", os.EINVAL },
}
-func main() {
- bad := 0;
+export func TestAtof() bool {
+ ok := true;
for i := 0; i < len(tests); i++ {
t := &tests[i];
- f, overflow, ok := strconv.atof64(t.in);
- if !ok && t.out == "error" {
- continue;
- }
- if !ok {
- panicln("test:", t.in, "failed to parse");
- }
- if overflow && !sys.isInf(f, 0) {
- panicln("overflow but not inf:", t.in, f);
+ out, err := strconv.atof64(t.in);
+ outs := strconv.ftoa64(out, 'g', -1);
+ if outs != t.out || err != t.err {
+ fmt.printf("strconv.atof64(%v) = %v, %v want %v, %v\n",
+ t.in, out, err, t.out, t.err);
+ ok = false;
}
- if sys.isInf(f, 0) && !overflow {
- panicln("inf but not overflow:", t.in, f);
- }
- s := strconv.ftoa64(f, 'g', -1);
- if s != t.out {
- println("test", t.in, "want", t.out, "got", s);
- bad++;
- }
- }
- if bad != 0 {
- panic("failed");
}
+ return ok;
}
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package main
-
+package strconv
import (
"bufio";
"fmt";
// Wrapper around strconv.atof64. Handles dddddp+ddd (binary exponent)
// itself, passes the rest on to strconv.atof64.
-func atof64(s string) (f float64, ok bool) {
+func myatof64(s string) (f float64, ok bool) {
a := strings.split(s, "p");
if len(a) == 2 {
- n, ok := strconv.atoi64(a[0]);
- if !ok {
+ n, err := strconv.atoi64(a[0]);
+ if err != nil {
return 0, false;
}
- e, ok1 := strconv.atoi(a[1]);
- if !ok1 {
+ e, err1 := strconv.atoi(a[1]);
+ if err1 != nil {
println("bad e", a[1]);
return 0, false;
}
}
return v*pow2(e), true;
}
- f1, overflow, ok1 := strconv.atof64(s);
- if !ok1 {
+ f1, err := strconv.atof64(s);
+ if err != nil {
return 0, false;
}
return f1, true;
// Wrapper around strconv.atof32. Handles dddddp+ddd (binary exponent)
// itself, passes the rest on to strconv.atof32.
-func atof32(s string) (f float32, ok bool) {
+func myatof32(s string) (f float32, ok bool) {
a := strings.split(s, "p");
if len(a) == 2 {
- n, ok := strconv.atoi(a[0]);
- if !ok {
+ n, err := strconv.atoi(a[0]);
+ if err != nil {
println("bad n", a[0]);
return 0, false;
}
- e, ok1 := strconv.atoi(a[1]);
- if !ok1 {
+ e, err1 := strconv.atoi(a[1]);
+ if err1 != nil {
println("bad p", a[1]);
return 0, false;
}
return float32(float64(n)*pow2(e)), true;
}
- f1, overflow, ok1 := strconv.atof32(s);
- if !ok1 {
+ f1, err1 := strconv.atof32(s);
+ if err1 != nil {
return 0, false;
}
return f1, true;
}
-func main()
-{
+export func TestFp() bool {
fd, err := os.Open("testfp.txt", os.O_RDONLY, 0);
if err != nil {
panicln("testfp: open testfp.txt:", err.String());
switch a[0] {
case "float64":
var ok bool;
- v, ok = atof64(a[2]);
+ v, ok = myatof64(a[2]);
if !ok {
print("testfp.txt:", lineno, ": cannot atof64 ", a[2]);
continue;
}
s = fmt.sprintf(a[1], v);
case "float32":
- v1, ok := atof32(a[2]);
+ v1, ok := myatof32(a[2]);
if !ok {
print("testfp.txt:", lineno, ": cannot atof32 ", a[2]);
continue;
}
//else print("testfp.txt:", lineno, ": worked! ", s, "\n");
}
- if !ok {
- panicln("testfp failed");
- }
+ return ok;
}