blob: c3ec88deef95f5f69c0bdb61155acd833956c1ee [file] [log] [blame]
Nigel Tao89ceb9f2020-06-29 14:36:37 +10001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Nigel Tao788479d2021-08-22 10:52:51 +100015//go:build ignore
Nigel Tao89ceb9f2020-06-29 14:36:37 +100016// +build ignore
17
18package main
19
20// print-render-number-f64-tests.go prints the
21// test_wuffs_strconv_render_number_f64 test cases.
22//
23// Usage: go run print-render-number-f64-tests.go
24
25import (
26 "fmt"
27 "math"
28 "os"
29 "sort"
30 "strconv"
31)
32
33func main() {
34 if err := main1(); err != nil {
35 os.Stderr.WriteString(err.Error() + "\n")
36 os.Exit(1)
37 }
38}
39
40func main1() error {
41 testCases := append([]uint64(nil), u64TestCases...)
42 for _, f := range f64TestCases {
43 testCases = append(testCases, math.Float64bits(f))
44 }
45
46 sort.Slice(testCases, func(i int, j int) bool {
47 return testCases[i] < testCases[j]
48 })
49
50 for i, tc := range testCases {
51 f := math.Float64frombits(tc)
52
53 if (i > 0) && (tc == testCases[i-1]) {
54 return fmt.Errorf("duplicate test case (f=%g, tc=0x%X)", f, tc)
55 }
56
57 // Check that calling strconv.FormatFloat with a precision of -1 (round
58 // to shortest) does indeed return a string that, when parsed, recovers
59 // the original number.
60 shortest := strconv.FormatFloat(f, 'g', -1, 64)
61 g, err := strconv.ParseFloat(shortest, 64)
62 if err != nil {
63 return fmt.Errorf("ParseFloat failed (f=%g, tc=0x%X): %v", f, tc, err)
64 }
65 equal := tc == math.Float64bits(g)
66 if math.IsNaN(f) {
67 equal = math.IsNaN(g)
68 }
69 if !equal {
70 return fmt.Errorf("round-trip failed (f=%g, tc=0x%X)", f, tc)
71 }
72 }
73
74 for _, tc := range testCases {
75 f := math.Float64frombits(tc)
76 fmt.Printf(`{
77 .x = 0x%016X,
78 .want__e = %s,
79 .want__f = %s,
80 .want_0g = %s,
81 .want_2e = %s,
82 .want_3f = %s,
83 .want_4g = %s,
84},`+"\n",
85 tc,
86 do(f, -1, 'e'),
87 do(f, -1, 'f'),
88 do(f, +0, 'g'),
89 do(f, +2, 'e'),
90 do(f, +3, 'f'),
91 do(f, +4, 'g'),
92 )
93 }
94 return nil
95}
96
97func do(f float64, precision int, format byte) (ret string) {
98 s := strconv.FormatFloat(f, format, precision, 64)
99 for ; len(s) > 50; s = s[50:] {
100 ret += fmt.Sprintf("%q\n\t\t", s[:50])
101 }
102 ret += fmt.Sprintf("%q", s)
103 if ret == `"+Inf"` {
104 ret = `"Inf"`
105 }
106 return ret
107}
108
109var f64TestCases = []float64{
110 // Approximations of e, the base of the natural logarithm.
111 2.7,
112 2.72,
113 2.718,
114 2.7183,
115 2.71828,
116 2.718282,
117 2.7182818,
118 2.71828183,
119
120 // Approximations of N_A, the Avogadro constant.
121 6.0e23,
122 6.02e23,
123 6.022e23,
124 6.0221e23,
125 6.02214e23,
126 6.022141e23,
127 6.0221408e23,
128 6.02214076e23,
129}
130
131var u64TestCases = []uint64{
132 0x0000000000000000,
133 0x0000000000000001,
134 0x0000000000000002,
135 0x0000000000000003,
Nigel Taoddac2c52020-09-15 21:17:33 +1000136 0x000730D67819E8D2,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000137 0x000FFFFFFFFFFFFF,
138 0x0010000000000000,
Nigel Taoddac2c52020-09-15 21:17:33 +1000139 0x0031FA182C40C60D,
Nigel Taoba3818c2020-09-28 12:51:45 +1000140 0x369C2DF8DA5B6CA8,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000141 0x369C314ABE948EB1,
Nigel Taoba3818c2020-09-28 12:51:45 +1000142 0x3E70000000000000,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000143 0x3F88000000000000,
144 0x3FD0000000000000,
145 0x3FD3333333333333,
146 0x3FD3333333333334,
147 0x3FD5555555555555,
148 0x3FEFFFFFFFFFFFFF,
149 0x3FF0000000000000,
150 0x3FF0000000000001,
151 0x3FF0000000000002,
152 0x3FF4000000000000,
153 0x3FF8000000000000,
154 0x4008000000000000,
155 0x400921F9F01B866E,
156 0x400921FB54442D11,
157 0x400921FB54442D18,
158 0x400C000000000000,
159 0x4014000000000000,
160 0x4036000000000000,
161 0x4037000000000000,
162 0x4038000000000000,
163 0x40FE240C9FCB0C02,
Nigel Tao57d47c62020-09-08 16:43:31 +1000164 0x41E0246690000001,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000165 0x4202A05F20000000,
166 0x4330000000000000,
167 0x4330000000000001,
168 0x4330000000000002,
Nigel Tao19b3ce22020-07-03 23:28:07 +1000169 0x433FFFFFFFFFFFFE,
170 0x433FFFFFFFFFFFFF,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000171 0x4340000000000000,
172 0x4340000000000001,
173 0x4340000000000002,
Nigel Tao86093c82020-07-06 23:12:51 +1000174 0x4370000000000000,
Nigel Taoba3818c2020-09-28 12:51:45 +1000175 0x43E158E460913D00,
Nigel Tao57d47c62020-09-08 16:43:31 +1000176 0x43F002F1776DDA67,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000177 0x4415AF1D78B58C40,
Nigel Tao86093c82020-07-06 23:12:51 +1000178 0x44B52D02C7E14AF6,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000179 0x46293E5939A08CEA,
180 0x54B249AD2594C37D,
Nigel Taoddac2c52020-09-15 21:17:33 +1000181 0x7BBA44DF832B8D46,
182 0x7BF06B0BB1FB384C,
183 0x7C2485CE9E7A065F,
184 0x7FAC7B1F3CAC7433,
185 0x7FE1CCF385EBC8A0,
Nigel Tao89ceb9f2020-06-29 14:36:37 +1000186 0x7FEFFFFFFFFFFFFF,
187 0x7FF0000000000000,
188 0x7FFFFFFFFFFFFFFF,
189 0x8000000000000000,
190 0xC008000000000000,
191 0xFFF0000000000000,
192 0xFFFFFFFFFFFFFFFF,
193}