Back to index

glibc  2.9
tst-strftime.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <time.h>
00005 
00006 
00007 static struct
00008 {
00009   const char *fmt;
00010   size_t min;
00011   size_t max;
00012 } tests[] =
00013   {
00014     { "%2000Y", 2000, 4000 },
00015     { "%02000Y", 2000, 4000 },
00016     { "%_2000Y", 2000, 4000 },
00017     { "%-2000Y", 2000, 4000 },
00018   };
00019 #define ntests (sizeof (tests) / sizeof (tests[0]))
00020 
00021 
00022 static int
00023 do_test (void)
00024 {
00025   size_t cnt;
00026   int result = 0;
00027 
00028   time_t tnow = time (NULL);
00029   struct tm *now = localtime (&tnow);
00030 
00031   for (cnt = 0; cnt < ntests; ++cnt)
00032     {
00033       size_t size = 0;
00034       int res;
00035       char *buf = NULL;
00036 
00037       do
00038        {
00039          size += 500;
00040          buf = (char *) realloc (buf, size);
00041          if (buf == NULL)
00042            {
00043              puts ("out of memory");
00044              exit (1);
00045            }
00046 
00047          res = strftime (buf, size, tests[cnt].fmt, now);
00048          if (res != 0)
00049            break;
00050        }
00051       while (size < tests[cnt].max);
00052 
00053       if (res == 0)
00054        {
00055          printf ("%Zu: %s: res == 0 despite size == %Zu\n",
00056                 cnt, tests[cnt].fmt, size);
00057          result = 1;
00058        }
00059       else if (size < tests[cnt].min)
00060        {
00061          printf ("%Zu: %s: size == %Zu was enough\n",
00062                 cnt, tests[cnt].fmt, size);
00063          result = 1;
00064        }
00065       else
00066        printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size);
00067 
00068       free (buf);
00069     }
00070 
00071   struct tm ttm =
00072     {
00073       /* Initialize the fields which are needed in the tests.  */
00074       .tm_mday = 1,
00075       .tm_hour = 2
00076     };
00077   const struct
00078   {
00079     const char *fmt;
00080     const char *exp;
00081     size_t n;
00082   } ftests[] =
00083     {
00084       { "%-e", "1", 1 },
00085       { "%-k", "2", 1 },
00086       { "%-l", "2", 1 },
00087     };
00088 #define nftests (sizeof (ftests) / sizeof (ftests[0]))
00089   for (cnt = 0; cnt < nftests; ++cnt)
00090     {
00091       char buf[100];
00092       size_t r = strftime (buf, sizeof (buf), ftests[cnt].fmt, &ttm);
00093       if (r != ftests[cnt].n)
00094        {
00095          printf ("strftime(\"%s\") returned %zu not %zu\n",
00096                 ftests[cnt].fmt, r, ftests[cnt].n);
00097          result = 1;
00098        }
00099       if (strcmp (buf, ftests[cnt].exp) != 0)
00100        {
00101          printf ("strftime(\"%s\") produced \"%s\" not \"%s\"\n",
00102                 ftests[cnt].fmt, buf, ftests[cnt].exp);
00103          result = 1;
00104        }
00105     }
00106 
00107   return result;
00108 }
00109 
00110 #define TEST_FUNCTION do_test ()
00111 #include "../test-skeleton.c"