2020-07-17 03:35:42 +03:00
|
|
|
import strconv
|
2021-06-18 17:59:56 +03:00
|
|
|
|
2019-12-17 01:07:13 +03:00
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* String to float Test
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
fn test_atof() {
|
|
|
|
//
|
|
|
|
// test set
|
|
|
|
//
|
|
|
|
|
|
|
|
// float64
|
|
|
|
src_num := [
|
2019-12-27 06:07:09 +03:00
|
|
|
f64(0.3),
|
|
|
|
-0.3,
|
|
|
|
0.004,
|
|
|
|
-0.004,
|
|
|
|
0.0,
|
2019-12-19 03:37:53 +03:00
|
|
|
-0.0,
|
2021-06-18 17:59:56 +03:00
|
|
|
31234567890123,
|
2019-12-17 01:07:13 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
// strings
|
|
|
|
src_num_str := [
|
|
|
|
'0.3',
|
|
|
|
'-0.3',
|
|
|
|
'0.004',
|
|
|
|
'-0.004',
|
|
|
|
'0.0',
|
2019-12-19 03:37:53 +03:00
|
|
|
'-0.0',
|
|
|
|
'31234567890123',
|
2019-12-17 01:07:13 +03:00
|
|
|
]
|
|
|
|
|
2019-12-19 03:37:53 +03:00
|
|
|
// check conversion case 1 string <=> string
|
2021-06-18 17:59:56 +03:00
|
|
|
for c, x in src_num {
|
2019-12-19 03:37:53 +03:00
|
|
|
// slow atof
|
2022-02-10 14:27:32 +03:00
|
|
|
val := strconv.atof64(src_num_str[c]) or { panic(err) }
|
|
|
|
assert val.strlong() == x.strlong()
|
2019-12-27 06:07:09 +03:00
|
|
|
|
2019-12-19 03:37:53 +03:00
|
|
|
// quick atof
|
2020-07-17 03:35:42 +03:00
|
|
|
mut s1 := (strconv.atof_quick(src_num_str[c]).str())
|
2019-12-19 03:37:53 +03:00
|
|
|
mut s2 := (x.str())
|
2020-02-26 14:14:06 +03:00
|
|
|
delta := s1.f64() - s2.f64()
|
2021-06-18 17:59:56 +03:00
|
|
|
// println("$s1 $s2 $delta")
|
2020-02-26 14:14:06 +03:00
|
|
|
assert delta < f64(1e-16)
|
2019-12-19 03:37:53 +03:00
|
|
|
|
|
|
|
// test C.atof
|
2020-02-26 14:14:06 +03:00
|
|
|
n1 := x.strsci(18)
|
2021-04-14 12:47:24 +03:00
|
|
|
n2 := f64(C.atof(&char(src_num_str[c].str))).strsci(18)
|
2021-06-18 17:59:56 +03:00
|
|
|
// println("$n1 $n2")
|
2020-04-05 00:28:47 +03:00
|
|
|
assert n1 == n2
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 03:37:53 +03:00
|
|
|
// check conversion case 2 string <==> f64
|
|
|
|
// we don't test atof_quick beacuse we already know the rounding error
|
2021-06-18 17:59:56 +03:00
|
|
|
for c, x in src_num_str {
|
2019-12-19 03:37:53 +03:00
|
|
|
b := src_num[c].strlong()
|
2022-02-10 14:27:32 +03:00
|
|
|
value := strconv.atof64(x) or { panic(err) }
|
|
|
|
a1 := value.strlong()
|
2019-12-19 03:37:53 +03:00
|
|
|
assert a1 == b
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// special cases
|
|
|
|
mut f1 := f64(0.0)
|
2021-06-18 17:59:56 +03:00
|
|
|
mut ptr := unsafe { &u64(&f1) }
|
|
|
|
ptr = unsafe { &u64(&f1) }
|
2019-12-17 01:07:13 +03:00
|
|
|
|
2020-05-22 18:36:09 +03:00
|
|
|
// double_plus_zero
|
2021-06-18 17:59:56 +03:00
|
|
|
f1 = 0.0
|
2019-12-17 01:07:13 +03:00
|
|
|
assert *ptr == u64(0x0000000000000000)
|
2020-05-22 18:36:09 +03:00
|
|
|
// double_minus_zero
|
2021-06-18 17:59:56 +03:00
|
|
|
f1 = -0.0
|
2019-12-17 01:07:13 +03:00
|
|
|
assert *ptr == u64(0x8000000000000000)
|
2021-06-18 17:59:56 +03:00
|
|
|
println('DONE!')
|
2019-12-27 06:07:09 +03:00
|
|
|
}
|
2022-02-10 14:27:32 +03:00
|
|
|
|
|
|
|
fn test_atof_errors() {
|
|
|
|
if x := strconv.atof64('') {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('> x: ${x}')
|
2022-02-10 14:27:32 +03:00
|
|
|
assert false // strconv.atof64 should have failed
|
|
|
|
} else {
|
|
|
|
assert err.str() == 'expected a number found an empty string'
|
|
|
|
}
|
|
|
|
if x := strconv.atof64('####') {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('> x: ${x}')
|
2022-02-10 14:27:32 +03:00
|
|
|
assert false // strconv.atof64 should have failed
|
|
|
|
} else {
|
|
|
|
assert err.str() == 'not a number'
|
|
|
|
}
|
|
|
|
}
|