..
[glibc.git] / time / tst-strftime.c
blob374fba426293b2cc43f0efb0b36f0473c9e2c9c7
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
7 static struct
9 const char *fmt;
10 size_t min;
11 size_t max;
12 } tests[] =
14 { "%2000Y", 2000, 4000 },
15 { "%02000Y", 2000, 4000 },
16 { "%_2000Y", 2000, 4000 },
17 { "%-2000Y", 2000, 4000 },
19 #define ntests (sizeof (tests) / sizeof (tests[0]))
22 static int
23 do_test (void)
25 size_t cnt;
26 int result = 0;
28 time_t tnow = time (NULL);
29 struct tm *now = localtime (&tnow);
31 for (cnt = 0; cnt < ntests; ++cnt)
33 size_t size = 0;
34 int res;
35 char *buf = NULL;
39 size += 500;
40 buf = (char *) realloc (buf, size);
41 if (buf == NULL)
43 puts ("out of memory");
44 exit (1);
47 res = strftime (buf, size, tests[cnt].fmt, now);
48 if (res != 0)
49 break;
51 while (size < tests[cnt].max);
53 if (res == 0)
55 printf ("%Zu: %s: res == 0 despite size == %Zu\n",
56 cnt, tests[cnt].fmt, size);
57 result = 1;
59 else if (size < tests[cnt].min)
61 printf ("%Zu: %s: size == %Zu was enough\n",
62 cnt, tests[cnt].fmt, size);
63 result = 1;
65 else
66 printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size);
68 free (buf);
71 struct tm ttm =
73 /* Initialize the fields which are needed in the tests. */
74 .tm_mday = 1,
75 .tm_hour = 2
77 const struct
79 const char *fmt;
80 const char *exp;
81 size_t n;
82 } ftests[] =
84 { "%-e", "1", 1 },
85 { "%-k", "2", 1 },
86 { "%-l", "2", 1 },
88 #define nftests (sizeof (ftests) / sizeof (ftests[0]))
89 for (cnt = 0; cnt < nftests; ++cnt)
91 char buf[100];
92 size_t r = strftime (buf, sizeof (buf), ftests[cnt].fmt, &ttm);
93 if (r != ftests[cnt].n)
95 printf ("strftime(\"%s\") returned %zu not %zu\n",
96 ftests[cnt].fmt, r, ftests[cnt].n);
97 result = 1;
99 if (strcmp (buf, ftests[cnt].exp) != 0)
101 printf ("strftime(\"%s\") produced \"%s\" not \"%s\"\n",
102 ftests[cnt].fmt, buf, ftests[cnt].exp);
103 result = 1;
107 return result;
110 #define TEST_FUNCTION do_test ()
111 #include "../test-skeleton.c"