v / vlib / strconv / atof_test.c.v
142 lines · 125 sloc · 3.56 KB · ea22f6c9447ec6e6f3b3b2fe459ae343a9d8b55f
Raw
1import strconv
2
3/**********************************************************************
4*
5* String to float Test
6*
7**********************************************************************/
8
9fn test_atof() {
10 //
11 // test set
12 //
13
14 // float64
15 src_num := [
16 f64(0.3),
17 -0.3,
18 0.004,
19 -0.004,
20 0.0,
21 -0.0,
22 31234567890123,
23 0.01,
24 2000,
25 -300,
26 ]
27
28 // strings
29 src_num_str := [
30 '0.3',
31 '-0.3',
32 '0.004',
33 '-0.004',
34 '0.0',
35 '-0.0',
36 '31234567890123',
37 '1e-2',
38 '+2e+3',
39 '-3.0e+2',
40 ]
41
42 // check conversion case 1 string <=> string
43 for c, x in src_num {
44 // slow atof
45 val := strconv.atof64(src_num_str[c]) or { panic(err) }
46 assert val.strlong() == x.strlong()
47
48 // quick atof
49 mut s1 := (strconv.atof_quick(src_num_str[c]).str())
50 mut s2 := (x.str())
51 delta := s1.f64() - s2.f64()
52 // println("${s1} ${s2} ${delta}")
53 assert delta < f64(1e-16)
54
55 // test C.atof
56 n1 := x.strsci(18)
57 n2 := f64(C.atof(&char(src_num_str[c].str))).strsci(18)
58 // println("${n1} ${n2}")
59 assert n1 == n2
60 }
61
62 // check conversion case 2 string <==> f64
63 // we don't test atof_quick because we already know the rounding error
64 for c, x in src_num_str {
65 b := src_num[c].strlong()
66 value := strconv.atof64(x) or { panic(err) }
67 a1 := value.strlong()
68 assert a1 == b
69 }
70
71 // special cases
72 mut f1 := f64(0.0)
73 mut ptr := unsafe { &u64(&f1) }
74 ptr = unsafe { &u64(&f1) }
75
76 // double_plus_zero
77 f1 = 0.0
78 assert *ptr == u64(0x0000000000000000)
79 // double_minus_zero
80 f1 = -0.0
81 assert *ptr == u64(0x8000000000000000)
82 println('DONE!')
83}
84
85fn test_atof_subnormal() {
86 // Test subnormal (denormalized) float numbers and edge cases
87 // These are very small numbers close to the f64 minimum
88 // IMPORTANT: Compare with hardcoded f64 literals, not .f64() which uses the same parser
89
90 // Normal numbers
91 assert strconv.atof64('1.0e-250') or { panic('parse error') } == 1.0e-250
92 assert strconv.atof64('2.5e-260') or { panic('parse error') } == 2.5e-260
93
94 // Transition zone
95 assert strconv.atof64('1.0e-300') or { panic('parse error') } == 1.0e-300
96 assert strconv.atof64('2.2250738585072014e-308') or { panic('parse error') } == 2.2250738585072014e-308
97
98 // Subnormal numbers (these fail without the fix)
99 assert strconv.atof64('1.23e-308') or { panic('parse error') } == 1.23e-308
100 assert strconv.atof64('1.0e-310') or { panic('parse error') } == 1.0e-310
101 assert strconv.atof64('5.0e-320') or { panic('parse error') } == 5.0e-320
102 assert strconv.atof64('5e-324') or { panic('parse error') } == 5e-324
103
104 // Negative subnormal
105 assert strconv.atof64('-1.0e-320') or { panic('parse error') } == -1.0e-320
106}
107
108fn test_atof_small_decimal_with_many_leading_zeroes() {
109 assert strconv.atof64('0.0000000000000000005') or { panic('parse error') } == strconv.atof64('5e-19') or {
110 panic('parse error')
111 }
112 assert strconv.atof64('-0.0000000000000000005') or { panic('parse error') } == strconv.atof64('-5e-19') or {
113 panic('parse error')
114 }
115}
116
117fn test_atof_errors() {
118 if x := strconv.atof64('') {
119 eprintln('> x: ${x}')
120 assert false // strconv.atof64 should have failed
121 } else {
122 assert err.str() == 'expected a number found an empty string'
123 }
124 if x := strconv.atof64('####') {
125 eprintln('> x: ${x}')
126 assert false // strconv.atof64 should have failed
127 } else {
128 assert err.str() == 'not a number'
129 }
130 if x := strconv.atof64('uu577.01') {
131 eprintln('> x: ${x}')
132 assert false // strconv.atof64 should have failed
133 } else {
134 assert err.str() == 'not a number'
135 }
136 if x := strconv.atof64('123.33xyz') {
137 eprintln('> x: ${x}')
138 assert false // strconv.atof64 should have failed
139 } else {
140 assert err.str() == 'extra char after number'
141 }
142}
143