v/vlib/strconv/atof_test.v

75 lines
1.3 KiB
V
Raw Normal View History

2019-12-16 23:07:13 +01:00
/**********************************************************************
*
* String to float Test
*
**********************************************************************/
fn test_atof() {
//
// test set
//
// float64
src_num := [
2019-12-27 04:07:09 +01:00
f64(0.3),
-0.3,
0.004,
-0.004,
0.0,
-0.0,
31234567890123
2019-12-16 23:07:13 +01:00
]
// strings
src_num_str := [
'0.3',
'-0.3',
'0.004',
'-0.004',
'0.0',
'-0.0',
'31234567890123',
2019-12-16 23:07:13 +01:00
]
// check conversion case 1 string <=> string
2019-12-16 23:07:13 +01:00
for c,x in src_num {
// slow atof
2020-07-16 19:50:09 +02:00
assert atof64(src_num_str[c]).strlong() == x.strlong()
2019-12-27 04:07:09 +01:00
2020-04-04 23:28:47 +02:00
// quick atof
mut s1 := (atof_quick(src_num_str[c]).str())
mut s2 := (x.str())
delta := s1.f64() - s2.f64()
//println("$s1 $s2 $delta")
assert delta < f64(1e-16)
// test C.atof
n1 := x.strsci(18)
n2 := f64(C.atof(src_num_str[c].str)).strsci(18)
//println("$n1 $n2")
2020-04-04 23:28:47 +02:00
assert n1 == n2
2019-12-16 23:07:13 +01:00
}
// check conversion case 2 string <==> f64
// we don't test atof_quick beacuse we already know the rounding error
2019-12-16 23:07:13 +01:00
for c,x in src_num_str {
b := src_num[c].strlong()
2020-07-16 19:50:09 +02:00
a1 := atof64(x).strlong()
assert a1 == b
2019-12-16 23:07:13 +01:00
}
// special cases
mut f1 := f64(0.0)
2020-04-04 23:28:47 +02:00
mut ptr := &u64(&f1)
ptr = &u64(&f1)
2019-12-16 23:07:13 +01:00
2020-05-22 17:36:09 +02:00
// double_plus_zero
2019-12-16 23:07:13 +01:00
f1=0.0
assert *ptr == u64(0x0000000000000000)
2020-05-22 17:36:09 +02:00
// double_minus_zero
2019-12-16 23:07:13 +01:00
f1=-0.0
assert *ptr == u64(0x8000000000000000)
2020-04-08 01:20:55 +02:00
println("DONE!")
2019-12-27 04:07:09 +01:00
}