blob: f17e6307d5b283aa0440c85ff05ac9713bfccc0e [file] [log] [blame]
Russ Cox079c00a2008-11-17 12:34:03 -08001// Copyright 2009 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
5package main
6
7import "strconv"
8
9type Test struct {
10 in string;
11 out string;
12}
13
14var tests = []Test {
15 Test{ "1", "1" },
16 Test{ "1e23", "1e+23" },
17 Test{ "100000000000000000000000", "1e+23" },
18 Test{ "1e-100", "1e-100" },
19 Test{ "123456700", "1.234567e+08" },
20 Test{ "99999999999999974834176", "9.999999999999997e+22" },
21 Test{ "100000000000000000000001", "1.0000000000000001e+23" },
22 Test{ "100000000000000008388608", "1.0000000000000001e+23" },
23 Test{ "100000000000000016777215", "1.0000000000000001e+23" },
24 Test{ "100000000000000016777216", "1.0000000000000003e+23" },
25 Test{ "-1", "-1" },
26 Test{ "-0", "0" },
Russ Coxed628ca2008-11-17 17:22:51 -080027 Test{ "1e-20", "1e-20" },
28
29 // largest float64
30 Test{ "1.7976931348623157e308", "1.7976931348623157e+308" },
31 Test{ "-1.7976931348623157e308", "-1.7976931348623157e+308" },
32 // next float64 - too large
33 Test{ "1.7976931348623159e308", "+Inf" },
34 Test{ "-1.7976931348623159e308", "-Inf" },
35 // the border is ...158079
36 // borderline - okay
37 Test{ "1.7976931348623158e308", "1.7976931348623157e+308" },
38 Test{ "-1.7976931348623158e308", "-1.7976931348623157e+308" },
39 // borderline - too large
40 Test{ "1.797693134862315808e308", "+Inf" },
41 Test{ "-1.797693134862315808e308", "-Inf" },
42
43 // a little too large
44 Test{ "1e308", "1e+308" },
45 Test{ "2e308", "+Inf" },
46 Test{ "1e309", "+Inf" },
47
48 // way too large
49 Test{ "1e310", "+Inf" },
50 Test{ "-1e310", "-Inf" },
51 Test{ "1e400", "+Inf" },
52 Test{ "-1e400", "-Inf" },
53 Test{ "1e400000", "+Inf" },
54 Test{ "-1e400000", "-Inf" },
55
56 // denormalized
57 Test{ "1e-305", "1e-305" },
58 Test{ "1e-306", "1e-306" },
59 Test{ "1e-307", "1e-307" },
60 Test{ "1e-308", "1e-308" },
61 Test{ "1e-309", "1e-309" },
62 Test{ "1e-310", "1e-310" },
63 Test{ "1e-322", "1e-322" },
64 // smallest denormal
65 Test{ "5e-324", "5e-324" },
66 // too small
67 Test{ "4e-324", "0" },
68 // way too small
69 Test{ "1e-350", "0" },
70 Test{ "1e-400000", "0" },
71
72 // try to overflow exponent
73 Test{ "1e-4294967296", "0" },
74 Test{ "1e+4294967296", "+Inf" },
75 Test{ "1e-18446744073709551616", "0" },
76 Test{ "1e+18446744073709551616", "+Inf" },
77
78 // Parse errors
79 Test{ "1e", "error" },
80 Test{ "1e-", "error" },
81 Test{ ".e-1", "error" },
Russ Cox079c00a2008-11-17 12:34:03 -080082}
83
84func main() {
85 bad := 0;
86 for i := 0; i < len(tests); i++ {
87 t := &tests[i];
88 f, overflow, ok := strconv.atof64(t.in);
Russ Coxed628ca2008-11-17 17:22:51 -080089 if !ok && t.out == "error" {
90 continue;
91 }
Russ Cox079c00a2008-11-17 12:34:03 -080092 if !ok {
Russ Coxed628ca2008-11-17 17:22:51 -080093 panicln("test:", t.in, "failed to parse");
94 }
95 if overflow && !sys.isInf(f, 0) {
96 panicln("overflow but not inf:", t.in, f);
97 }
98 if sys.isInf(f, 0) && !overflow {
99 panicln("inf but not overflow:", t.in, f);
Russ Cox079c00a2008-11-17 12:34:03 -0800100 }
101 s := strconv.ftoa64(f, 'g', -1);
102 if s != t.out {
103 println("test", t.in, "want", t.out, "got", s);
104 bad++;
105 }
106 }
107 if bad != 0 {
108 panic("failed");
109 }
110}