1 | #include <float.h>
|
---|
2 | #include <math.h>
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <string.h>
|
---|
5 | #include <wchar.h>
|
---|
6 |
|
---|
7 | static int
|
---|
8 | t1 (void)
|
---|
9 | {
|
---|
10 | int n = -1;
|
---|
11 | sscanf ("abc ", "abc %n", &n);
|
---|
12 | printf ("t1: count=%d\n", n);
|
---|
13 |
|
---|
14 | return n != 5;
|
---|
15 | }
|
---|
16 |
|
---|
17 | static int
|
---|
18 | t2 (void)
|
---|
19 | {
|
---|
20 | int result = 0;
|
---|
21 | int n;
|
---|
22 | long N;
|
---|
23 | int retval;
|
---|
24 | #define SCAN(INPUT, FORMAT, VAR, EXP_RES, EXP_VAL) \
|
---|
25 | VAR = -1; \
|
---|
26 | retval = sscanf (INPUT, FORMAT, &VAR); \
|
---|
27 | printf ("sscanf (\"%s\", \"%s\", &x) => %d, x = %ld\n", \
|
---|
28 | INPUT, FORMAT, retval, (long int) VAR); \
|
---|
29 | result |= retval != EXP_RES || VAR != EXP_VAL
|
---|
30 |
|
---|
31 | SCAN ("12345", "%ld", N, 1, 12345);
|
---|
32 | SCAN ("12345", "%llllld", N, 0, -1);
|
---|
33 | SCAN ("12345", "%LLLLLd", N, 0, -1);
|
---|
34 | SCAN ("test ", "%*s%n", n, 0, 4);
|
---|
35 | SCAN ("test ", "%2*s%n", n, 0, -1);
|
---|
36 | SCAN ("12 ", "%l2d", n, 0, -1);
|
---|
37 | SCAN ("12 ", "%2ld", N, 1, 12);
|
---|
38 |
|
---|
39 | n = -1;
|
---|
40 | N = -1;
|
---|
41 | retval = sscanf ("1 1", "%d %Z", &n, &N);
|
---|
42 | printf ("sscanf (\"1 1\", \"%%d %%Z\", &n, &N) => %d, n = %d, N = %ld\n", \
|
---|
43 | retval, n, N); \
|
---|
44 | result |= retval != 1 || n != 1 || N != -1;
|
---|
45 |
|
---|
46 | return result;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static int
|
---|
50 | F (void)
|
---|
51 | {
|
---|
52 | char buf[20];
|
---|
53 | wchar_t wbuf[10];
|
---|
54 | int result;
|
---|
55 |
|
---|
56 | snprintf (buf, sizeof buf, "%f %F", DBL_MAX * DBL_MAX - DBL_MAX * DBL_MAX,
|
---|
57 | DBL_MAX * DBL_MAX - DBL_MAX * DBL_MAX);
|
---|
58 | result = strcmp (buf, "nan NAN") != 0;
|
---|
59 | printf ("expected \"nan NAN\", got \"%s\"\n", buf);
|
---|
60 |
|
---|
61 | snprintf (buf, sizeof buf, "%f %F", DBL_MAX * DBL_MAX, DBL_MAX * DBL_MAX);
|
---|
62 | result |= strcmp (buf, "inf INF") != 0;
|
---|
63 | printf ("expected \"inf INF\", got \"%s\"\n", buf);
|
---|
64 |
|
---|
65 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%f %F",
|
---|
66 | DBL_MAX * DBL_MAX - DBL_MAX * DBL_MAX,
|
---|
67 | DBL_MAX * DBL_MAX - DBL_MAX * DBL_MAX);
|
---|
68 | result |= wcscmp (wbuf, L"nan NAN") != 0;
|
---|
69 | printf ("expected L\"nan NAN\", got L\"%S\"\n", wbuf);
|
---|
70 |
|
---|
71 | swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%f %F",
|
---|
72 | DBL_MAX * DBL_MAX, DBL_MAX * DBL_MAX);
|
---|
73 | result |= wcscmp (wbuf, L"inf INF") != 0;
|
---|
74 | printf ("expected L\"inf INF\", got L\"%S\"\n", wbuf);
|
---|
75 |
|
---|
76 | return result;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int
|
---|
80 | main (int argc, char *argv[])
|
---|
81 | {
|
---|
82 | int result = 0;
|
---|
83 |
|
---|
84 | result |= t1 ();
|
---|
85 | result |= t2 ();
|
---|
86 | result |= F ();
|
---|
87 |
|
---|
88 | result |= fflush (stdout) == EOF;
|
---|
89 |
|
---|
90 | return result;
|
---|
91 | }
|
---|