Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / strconv / ftoa_test.go
blob6044afdae6d64c022ed2f7a1795abab4d472032e
1 // 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.
5 package strconv_test
7 import (
8 "math"
9 . "strconv"
10 "testing"
13 type ftoaTest struct {
14 f float64
15 fmt byte
16 prec int
17 s string
20 func fdiv(a, b float64) float64 { return a / b } // keep compiler in the dark
22 const (
23 below1e23 = 99999999999999974834176
24 above1e23 = 100000000000000008388608
27 var ftoatests = []ftoaTest{
28 {1, 'e', 5, "1.00000e+00"},
29 {1, 'f', 5, "1.00000"},
30 {1, 'g', 5, "1"},
31 {1, 'g', -1, "1"},
32 {20, 'g', -1, "20"},
33 {1234567.8, 'g', -1, "1.2345678e+06"},
34 {200000, 'g', -1, "200000"},
35 {2000000, 'g', -1, "2e+06"},
37 // g conversion and zero suppression
38 {400, 'g', 2, "4e+02"},
39 {40, 'g', 2, "40"},
40 {4, 'g', 2, "4"},
41 {.4, 'g', 2, "0.4"},
42 {.04, 'g', 2, "0.04"},
43 {.004, 'g', 2, "0.004"},
44 {.0004, 'g', 2, "0.0004"},
45 {.00004, 'g', 2, "4e-05"},
46 {.000004, 'g', 2, "4e-06"},
48 {0, 'e', 5, "0.00000e+00"},
49 {0, 'f', 5, "0.00000"},
50 {0, 'g', 5, "0"},
51 {0, 'g', -1, "0"},
53 {-1, 'e', 5, "-1.00000e+00"},
54 {-1, 'f', 5, "-1.00000"},
55 {-1, 'g', 5, "-1"},
56 {-1, 'g', -1, "-1"},
58 {12, 'e', 5, "1.20000e+01"},
59 {12, 'f', 5, "12.00000"},
60 {12, 'g', 5, "12"},
61 {12, 'g', -1, "12"},
63 {123456700, 'e', 5, "1.23457e+08"},
64 {123456700, 'f', 5, "123456700.00000"},
65 {123456700, 'g', 5, "1.2346e+08"},
66 {123456700, 'g', -1, "1.234567e+08"},
68 {1.2345e6, 'e', 5, "1.23450e+06"},
69 {1.2345e6, 'f', 5, "1234500.00000"},
70 {1.2345e6, 'g', 5, "1.2345e+06"},
72 {1e23, 'e', 17, "9.99999999999999916e+22"},
73 {1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
74 {1e23, 'g', 17, "9.9999999999999992e+22"},
76 {1e23, 'e', -1, "1e+23"},
77 {1e23, 'f', -1, "100000000000000000000000"},
78 {1e23, 'g', -1, "1e+23"},
80 {below1e23, 'e', 17, "9.99999999999999748e+22"},
81 {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
82 {below1e23, 'g', 17, "9.9999999999999975e+22"},
84 {below1e23, 'e', -1, "9.999999999999997e+22"},
85 {below1e23, 'f', -1, "99999999999999970000000"},
86 {below1e23, 'g', -1, "9.999999999999997e+22"},
88 {above1e23, 'e', 17, "1.00000000000000008e+23"},
89 {above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
90 {above1e23, 'g', 17, "1.0000000000000001e+23"},
92 {above1e23, 'e', -1, "1.0000000000000001e+23"},
93 {above1e23, 'f', -1, "100000000000000010000000"},
94 {above1e23, 'g', -1, "1.0000000000000001e+23"},
96 {fdiv(5e-304, 1e20), 'g', -1, "5e-324"},
97 {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"},
99 {32, 'g', -1, "32"},
100 {32, 'g', 0, "3e+01"},
102 {100, 'x', -1, "%x"},
104 {math.NaN(), 'g', -1, "NaN"},
105 {-math.NaN(), 'g', -1, "NaN"},
106 {math.Inf(0), 'g', -1, "+Inf"},
107 {math.Inf(-1), 'g', -1, "-Inf"},
108 {-math.Inf(0), 'g', -1, "-Inf"},
110 {-1, 'b', -1, "-4503599627370496p-52"},
112 // fixed bugs
113 {0.9, 'f', 1, "0.9"},
114 {0.09, 'f', 1, "0.1"},
115 {0.0999, 'f', 1, "0.1"},
116 {0.05, 'f', 1, "0.1"},
117 {0.05, 'f', 0, "0"},
118 {0.5, 'f', 1, "0.5"},
119 {0.5, 'f', 0, "0"},
120 {1.5, 'f', 0, "2"},
123 func TestFtoa(t *testing.T) {
124 if FloatSize != 32 {
125 println("floatsize: ", FloatSize)
126 panic("floatsize")
128 for i := 0; i < len(ftoatests); i++ {
129 test := &ftoatests[i]
130 s := Ftoa64(test.f, test.fmt, test.prec)
131 if s != test.s {
132 t.Error("test", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
134 s = FtoaN(test.f, test.fmt, test.prec, 64)
135 if s != test.s {
136 t.Error("testN=64", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
138 if float64(float32(test.f)) == test.f && test.fmt != 'b' {
139 s := Ftoa32(float32(test.f), test.fmt, test.prec)
140 if s != test.s {
141 t.Error("test32", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
143 s = FtoaN(test.f, test.fmt, test.prec, 32)
144 if s != test.s {
145 t.Error("testN=32", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)