From a9baddd7d07b9fe15e212985a808a79773ec72e4 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 19 Mar 2011 22:26:06 -0400 Subject: initial check-in, taken from old libc svn repo with significant additions --- strtod.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 strtod.c (limited to 'strtod.c') diff --git a/strtod.c b/strtod.c new file mode 100644 index 0000000..ef931d4 --- /dev/null +++ b/strtod.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +/* r = place to store result + * f = function call to test (or any expression) + * x = expected result + * m = message to print on failure (with formats for r & x) +**/ + +#define TEST(r, f, x, m) ( \ +msg = #f, ((r) = (f)) == (x) || \ +(printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x, r-x), err++, 0) ) + +#define TEST2(r, f, x, m) ( \ +((r) = (f)) == (x) || \ +(printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, msg, r, x), err++, 0) ) + +int test_strtod(void) +{ + int i; + double d, d2; + char buf[1000]; + char *msg=""; + int err=0; + char *s, *c; + + for (i=0; i<100; i++) { + d = sin(i); + snprintf(buf, sizeof buf, "%.300f", d); + TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)"); + } + + TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a"); + TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a"); + + return err; +} -- cgit v1.2.1