1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | |
4 | struct test |
5 | { |
6 | const char *str; |
7 | double result; |
8 | size_t offset; |
9 | } tests[] = |
10 | { |
11 | { "0xy" , 0.0, 1 }, |
12 | { "0x.y" , 0.0, 1 }, |
13 | { "0x0.y" , 0.0, 4 }, |
14 | { "0x.0y" , 0.0, 4 }, |
15 | { ".y" , 0.0, 0 }, |
16 | { "0.y" , 0.0, 2 }, |
17 | { ".0y" , 0.0, 2 } |
18 | }; |
19 | |
20 | static int |
21 | do_test (void) |
22 | { |
23 | int status = 0; |
24 | for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i) |
25 | { |
26 | char *ep; |
27 | double r = strtod (tests[i].str, &ep); |
28 | if (r != tests[i].result) |
29 | { |
30 | printf (format: "test %zu r = %g, expect %g\n" , i, r, tests[i].result); |
31 | status = 1; |
32 | } |
33 | if (ep != tests[i].str + tests[i].offset) |
34 | { |
35 | printf (format: "test %zu strtod parsed %tu characters, expected %zu\n" , |
36 | i, ep - tests[i].str, tests[i].offset); |
37 | status = 1; |
38 | } |
39 | } |
40 | return status; |
41 | } |
42 | |
43 | #define TEST_FUNCTION do_test () |
44 | #include "../test-skeleton.c" |
45 | |