/* hexfloat-scanf-test.c - test 0x#[Pp]# sscanf and strtod support */ #include #include int main(void) { const double expected_x = 0x1p+0; double x = -0x1p+0; const int expected_result = 2; int result = 0; int ok = 1; const char * test = "0x1p+0"; char * endp = NULL; char buff[BUFSIZ] = { 0 }; /* test sscanf() */ result = sscanf(test, "%la %s", &x, buff); printf("sscanf returned %d", result); if (result != expected_result) { ok = 0; printf(" (expected %d)", expected_result); } printf(", unscanned '%s', x = %g", buff, x); if (x != expected_x) { ok = 0; printf(" (expected %g)", expected_x); } printf("%s\n", ok ? ", PASSED" : ", FAILED"); /* test strtod() */ x = strtod(test, &endp); printf("strtod unscanned '%s'", endp); if (!(ok = (NULL != endp && !*endp))) { printf(" (expected '')"); } printf(", x = %g", x); if (x != expected_x) { ok = 0; printf(" (expected %g)", expected_x); } printf("%s\n", ok ? ", PASSED" : ", FAILED"); }