1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ftoa: fix strconv/ftoa/f32_f64_to_string_test.v

This commit is contained in:
yuyi 2020-04-14 09:53:34 +08:00 committed by GitHub
parent df37597f00
commit 885612afea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 111 deletions

View File

@ -21,7 +21,7 @@ const (
'vlib/net/http/http_httpbin_test.v', 'vlib/net/http/http_httpbin_test.v',
'vlib/net/http/http_test.v', 'vlib/net/http/http_test.v',
'vlib/regex/regex_test.v', 'vlib/regex/regex_test.v',
'vlib/strconv/ftoa/f32_f64_to_string_test.v', 'vlib/v/tests/const_embed_test.v',
'vlib/v/tests/enum_bitfield_test.v', 'vlib/v/tests/enum_bitfield_test.v',
'vlib/v/tests/fixed_array_test.v', 'vlib/v/tests/fixed_array_test.v',
'vlib/v/tests/fn_test.v', 'vlib/v/tests/fn_test.v',

View File

@ -3,7 +3,7 @@
* Float to string Test * Float to string Test
* *
**********************************************************************/ **********************************************************************/
module ftoa import strconv.ftoa
import math import math
union Ufloat32 { union Ufloat32 {
@ -32,8 +32,8 @@ fn f32_from_bits1(b u32) f32 {
return x.f return x.f
} }
const( fn test_float_to_str() {
test_cases_f32 = [ test_cases_f32 := [
f32_from_bits1(0x0000_0000), // +0 f32_from_bits1(0x0000_0000), // +0
f32_from_bits1(0x8000_0000), // -0 f32_from_bits1(0x8000_0000), // -0
f32_from_bits1(0xFFC0_0001), // sNan f32_from_bits1(0xFFC0_0001), // sNan
@ -54,9 +54,9 @@ test_cases_f32 = [
f32_from_bits1(0x0080_0000), // smallest float32 f32_from_bits1(0x0080_0000), // smallest float32
math.max_f32, math.max_f32,
383260575764816448, 383260575764816448,
] ]
exp_result_f32 = [ exp_result_f32 := [
"0e+00", "0e+00",
"-0e+00", "-0e+00",
"nan", "nan",
@ -77,9 +77,9 @@ exp_result_f32 = [
"1.1754944e-38", // aprox from 1.1754943508 × 1038, "1.1754944e-38", // aprox from 1.1754943508 × 1038,
"3.4028235e+38", "3.4028235e+38",
"3.8326058e+17", "3.8326058e+17",
] ]
test_cases_f64 = [ test_cases_f64 := [
f64_from_bits1(0x0000_0000_0000_0000), // +0 f64_from_bits1(0x0000_0000_0000_0000), // +0
f64_from_bits1(0x8000_0000_0000_0000), // -0 f64_from_bits1(0x8000_0000_0000_0000), // -0
f64_from_bits1(0x7FF0_0000_0000_0001), // sNan f64_from_bits1(0x7FF0_0000_0000_0001), // sNan
@ -106,9 +106,9 @@ test_cases_f64 = [
123e-300, 123e-300,
5.e-324, 5.e-324,
-5.e-324, -5.e-324,
] ]
exp_result_f64 = [ exp_result_f64 := [
"0e+00", "0e+00",
"-0e+00", "-0e+00",
"nan", "nan",
@ -135,21 +135,20 @@ exp_result_f64 = [
"1.23e-298", "1.23e-298",
"5.e-324", "5.e-324",
"-5.e-324", "-5.e-324",
] ]
)
fn test_float_to_str(){
// test f32 // test f32
for c,x in test_cases_f32 { for c,x in test_cases_f32 {
s := f32_to_str(x,8) println(x)
s := ftoa.f32_to_str(x,8)
s1 := exp_result_f32[c] s1 := exp_result_f32[c]
//println("$s1 $s") println("$s1 $s")
assert s == s1 assert s == s1
} }
// test f64 // test f64
for c,x in test_cases_f64 { for c,x in test_cases_f64 {
s := f64_to_str(x,17) s := ftoa.f64_to_str(x,17)
s1 := exp_result_f64[c] s1 := exp_result_f64[c]
//println("$s1 $s") //println("$s1 $s")
assert s == s1 assert s == s1
@ -157,16 +156,16 @@ fn test_float_to_str(){
// test long format // test long format
for exp := 1 ; exp < 120 ; exp++ { for exp := 1 ; exp < 120 ; exp++ {
a := f64_to_str_l(("1e"+exp.str()).f64()) a := ftoa.f64_to_str_l(("1e"+exp.str()).f64())
//println(a) //println(a)
assert a.len == exp + 1 assert a.len == exp + 1
b := f64_to_str_l(("1e-"+exp.str()).f64()) b := ftoa.f64_to_str_l(("1e-"+exp.str()).f64())
//println(b) //println(b)
assert b.len == exp + 2 assert b.len == exp + 2
} }
// test rounding str conversion // test rounding str conversion
assert f64_to_str(0.3456789123456, 4)=="3.4568e-01" assert ftoa.f64_to_str(0.3456789123456, 4)=="3.4568e-01"
assert f32_to_str(0.345678, 3)=="3.457e-01" assert ftoa.f32_to_str(0.345678, 3)=="3.457e-01"
} }

View File

@ -0,0 +1,11 @@
import math
const (
max_float = math.max_f32
)
fn test_const_embed() {
println(max_float)
println(math.max_f32)
assert max_float == math.max_f32
}