2003-05-04 Roland McGrath <roland@redhat.com>
[glibc.git] / time / tst-strftime.c
blob1feb741793acada11dacdd832d41bd3e71ccec85
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
6 static struct
8 const char *fmt;
9 size_t min;
10 size_t max;
11 } tests[] =
13 { "%2000Y", 2000, 4000 },
14 { "%02000Y", 2000, 4000 },
15 { "%_2000Y", 2000, 4000 },
16 { "%-2000Y", 2000, 4000 },
18 #define ntests (sizeof (tests) / sizeof (tests[0]))
21 static int
22 do_test (void)
24 size_t cnt;
25 int result = 0;
27 time_t tnow = time (NULL);
28 struct tm *now = localtime (&tnow);
30 for (cnt = 0; cnt < ntests; ++cnt)
32 size_t size = 0;
33 int res;
34 char *buf = NULL;
38 size += 500;
39 buf = (char *) realloc (buf, size);
40 if (buf == NULL)
42 puts ("out of memory");
43 exit (1);
46 res = strftime (buf, size, tests[cnt].fmt, now);
47 if (res != 0)
48 break;
50 while (size < tests[cnt].max);
52 if (res == 0)
54 printf ("%Zu: %s: res == 0 despite size == %Zu\n",
55 cnt, tests[cnt].fmt, size);
56 result = 1;
58 else if (size < tests[cnt].min)
60 printf ("%Zu: %s: size == %Zu was enough\n",
61 cnt, tests[cnt].fmt, size);
62 result = 1;
64 else
65 printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size);
67 free (buf);
70 return result;
73 #define TEST_FUNCTION do_test ()
74 #include "../test-skeleton.c"