From 65979454ba695387da00ea158785e48e644724e6 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Fri, 21 May 2021 23:52:08 +0200 Subject: [PATCH] stdlib: add tests for strtod, strtof and atof --- src/stdlib/fpconv.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/stdlib/fpconv.c b/src/stdlib/fpconv.c index a758fee..f53ab83 100644 --- a/src/stdlib/fpconv.c +++ b/src/stdlib/fpconv.c @@ -77,11 +77,12 @@ static void _ft_stdlib_fpconv(ft_test *t) assert_end("73.e2f", 5); assert_end(" 73E+2+1", 7); assert_end(" -1e5", 6); + assert_end("nan(120)+3", 8); assert_end("", 0); ft_log(t, "\nInfinity and NaN:\n"); - assert_conv("inf", __builtin_infl()); - assert_conv("-inFiNiTY", -__builtin_infl()); + assert_conv("inf", HUGE_VALL); + assert_conv("-inFiNiTY", -HUGE_VALL); ft_assert(t, isnan(strtold("+NAN", NULL))); ft_assert(t, isnan(strtold("-nan", NULL))); ft_assert(t, isnan(strtold("NaN(57)", NULL))); @@ -119,9 +120,34 @@ static void _ft_stdlib_fpconv(ft_test *t) ft_log(t, "\nHexadecimal notation:\n"); assert_conv("0xa47b.3f85p12", 0xa47b.3f85p12); -// ft_log(t, "\nConversion to double:\n"); + #undef func + #undef format + #define func strtod + #define format "l" -// ft_log(t, "\nConversion to float:\n"); + ft_log(t, "\n--- strtod ---\n"); + + ft_log(t, "\nConversion to double:\n"); + assert_conv(" -0.73e2", -73.0); + assert_conv("-inf", -HUGE_VAL); + ft_assert(t, atof("+37.5") == 37.5); + assert_errno("1e308", 0); + assert_errno("1e309", ERANGE); + ft_assert(t, isnan(strtod("nan", NULL))); + + #undef func + #undef format + #define func strtof + #define format "" + + ft_log(t, "\n--- strtof ---\n"); + + ft_log(t, "\nConversion to float:\n"); + assert_conv(" -0.73e2", -73.0f); + assert_conv("+inf", HUGE_VALF); + assert_errno("1e38", 0); + assert_errno("1e39", ERANGE); + ft_assert(t, isnan(strtof("nan", NULL))); } ft_test ft_stdlib_fpconv = {