1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package test_core_strconv
import "core:math"
import "core:strconv"
import "core:testing"
@(test)
test_float :: proc(t: ^testing.T) {
n: int
f: f64
ok: bool
f, ok = strconv.parse_f64("1.2", &n)
testing.expect_value(t, f, 1.2)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("1.2a", &n)
testing.expect_value(t, f, 1.2)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, false)
f, ok = strconv.parse_f64("+", &n)
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
f, ok = strconv.parse_f64("-", &n)
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
f, ok = strconv.parse_f64("0", &n)
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 1)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("0h", &n)
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 1)
testing.expect_value(t, ok, false)
f, ok = strconv.parse_f64("0h1", &n)
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, false)
f, ok = strconv.parse_f64("0h0000_0001", &n)
testing.expect_value(t, f, 0h0000_0001)
testing.expect_value(t, n, 11)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("0h4c60", &n)
testing.expect_value(t, f, 0h4c60)
testing.expect_value(t, f, 17.5)
testing.expect_value(t, n, 6)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("0h418c0000", &n)
testing.expect_value(t, f, 0h418c0000)
testing.expect_value(t, f, 17.5)
testing.expect_value(t, n, 10)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("0h4031_8000_0000_0000", &n)
testing.expect_value(t, f, 0h4031800000000000)
testing.expect_value(t, f, f64(17.5))
testing.expect_value(t, n, 21)
testing.expect_value(t, ok, true)
}
@(test)
test_nan :: proc(t: ^testing.T) {
n: int
f: f64
ok: bool
f, ok = strconv.parse_f64("nan", &n)
testing.expect_value(t, math.classify(f), math.Float_Class.NaN)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("nAN", &n)
testing.expect_value(t, math.classify(f), math.Float_Class.NaN)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, true)
f, ok = strconv.parse_f64("Nani", &n)
testing.expect_value(t, math.classify(f), math.Float_Class.NaN)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, false)
}
@(test)
test_infinity :: proc(t: ^testing.T) {
pos_inf := math.inf_f64(+1)
neg_inf := math.inf_f64(-1)
n: int
s := "infinity"
for i in 0 ..< len(s) + 1 {
ss := s[:i]
f, ok := strconv.parse_f64(ss, &n)
if i >= 3 { // "inf" .. "infinity"
expected_n := 8 if i == 8 else 3
expected_ok := i == 3 || i == 8
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, expected_n)
testing.expect_value(t, ok, expected_ok)
testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
} else { // invalid substring
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
testing.expect_value(t, math.classify(f), math.Float_Class.Zero)
}
}
s = "+infinity"
for i in 0 ..< len(s) + 1 {
ss := s[:i]
f, ok := strconv.parse_f64(ss, &n)
if i >= 4 { // "+inf" .. "+infinity"
expected_n := 9 if i == 9 else 4
expected_ok := i == 4 || i == 9
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, expected_n)
testing.expect_value(t, ok, expected_ok)
testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
} else { // invalid substring
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
testing.expect_value(t, math.classify(f), math.Float_Class.Zero)
}
}
s = "-infinity"
for i in 0 ..< len(s) + 1 {
ss := s[:i]
f, ok := strconv.parse_f64(ss, &n)
if i >= 4 { // "-inf" .. "infinity"
expected_n := 9 if i == 9 else 4
expected_ok := i == 4 || i == 9
testing.expect_value(t, f, neg_inf)
testing.expect_value(t, n, expected_n)
testing.expect_value(t, ok, expected_ok)
testing.expect_value(t, math.classify(f), math.Float_Class.Neg_Inf)
} else { // invalid substring
testing.expect_value(t, f, 0)
testing.expect_value(t, n, 0)
testing.expect_value(t, ok, false)
testing.expect_value(t, math.classify(f), math.Float_Class.Zero)
}
}
// Make sure odd casing works.
batch := [?]string {"INFiniTY", "iNfInItY", "InFiNiTy"}
for ss in batch {
f, ok := strconv.parse_f64(ss, &n)
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 8)
testing.expect_value(t, ok, true)
testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
}
// Explicitly check how trailing characters are handled.
s = "infinityyyy"
f, ok := strconv.parse_f64(s, &n)
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 8)
testing.expect_value(t, ok, false)
testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
s = "inflippity"
f, ok = strconv.parse_f64(s, &n)
testing.expect_value(t, f, pos_inf)
testing.expect_value(t, n, 3)
testing.expect_value(t, ok, false)
testing.expect_value(t, math.classify(f), math.Float_Class.Inf)
}
|