Blame src/stdlib/strtod.c

nsz 1be84a
#include <stdio.h>
nsz 1be84a
#include <stdlib.h>
nsz 1be84a
#include <string.h>
nsz 1be84a
#include <math.h>
nsz 1be84a
#include "test.h"
nsz 1be84a
nsz 1be84a
/* r = place to store result
nsz 1be84a
 * f = function call to test (or any expression)
nsz 1be84a
 * x = expected result
nsz 1be84a
 * m = message to print on failure (with formats for r & x)
nsz 1be84a
**/
nsz 1be84a
nsz 1be84a
#define TEST(r, f, x, m) ( \
nsz 1be84a
	((r) = (f)) == (x) || \
nsz 1be84a
	(error("%s failed (" m ")\n", #f, r, x, r-x), 0) )
nsz 1be84a
nsz 1be84a
void test_strtod(void) {
nsz 1be84a
	int i;
nsz 1be84a
	double d, d2;
nsz 1be84a
	char buf[1000];
nsz 1be84a
nsz 1be84a
	for (i=0; i<100; i++) {
nsz 1be84a
		d = sin(i);
nsz 1be84a
		snprintf(buf, sizeof buf, "%.300f", d);
nsz 1be84a
		TEST(d2, strtod(buf, 0), d, "round trip fail %a != %a (%a)");
nsz 1be84a
	}
nsz 1be84a
nsz 1be84a
	TEST(d, strtod("0x1p4", 0), 16.0, "hex float %a != %a");
nsz 1be84a
	TEST(d, strtod("0x1.1p4", 0), 17.0, "hex float %a != %a");
nsz 1be84a
}