tst-strftime.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. static int
  6. do_bz18985 (void)
  7. {
  8. char buf[1000];
  9. struct tm ttm;
  10. int rc, ret = 0;
  11. memset (&ttm, 1, sizeof (ttm));
  12. ttm.tm_zone = NULL; /* Dereferenced directly if non-NULL. */
  13. rc = strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm);
  14. if (rc == 66)
  15. {
  16. const char expected[]
  17. = "? ? ? ? ? ? 16843009 16843009:16843009:16843009 16844909 +467836 ?";
  18. if (0 != strcmp (buf, expected))
  19. {
  20. printf ("expected:\n %s\ngot:\n %s\n", expected, buf);
  21. ret += 1;
  22. }
  23. }
  24. else
  25. {
  26. printf ("expected 66, got %d\n", rc);
  27. ret += 1;
  28. }
  29. /* Check negative values as well. */
  30. memset (&ttm, 0xFF, sizeof (ttm));
  31. ttm.tm_zone = NULL; /* Dereferenced directly if non-NULL. */
  32. rc = strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm);
  33. if (rc == 30)
  34. {
  35. const char expected[] = "? ? ? ? ? ? -1 -1:-1:-1 1899 ";
  36. if (0 != strcmp (buf, expected))
  37. {
  38. printf ("expected:\n %s\ngot:\n %s\n", expected, buf);
  39. ret += 1;
  40. }
  41. }
  42. else
  43. {
  44. printf ("expected 30, got %d\n", rc);
  45. ret += 1;
  46. }
  47. return ret;
  48. }
  49. static struct
  50. {
  51. const char *fmt;
  52. size_t min;
  53. size_t max;
  54. } tests[] =
  55. {
  56. { "%2000Y", 2000, 4000 },
  57. { "%02000Y", 2000, 4000 },
  58. { "%_2000Y", 2000, 4000 },
  59. { "%-2000Y", 2000, 4000 },
  60. };
  61. #define ntests (sizeof (tests) / sizeof (tests[0]))
  62. static int
  63. do_test (void)
  64. {
  65. size_t cnt;
  66. int result = 0;
  67. time_t tnow = time (NULL);
  68. struct tm *now = localtime (&tnow);
  69. for (cnt = 0; cnt < ntests; ++cnt)
  70. {
  71. size_t size = 0;
  72. int res;
  73. char *buf = NULL;
  74. do
  75. {
  76. size += 500;
  77. buf = (char *) realloc (buf, size);
  78. if (buf == NULL)
  79. {
  80. puts ("out of memory");
  81. exit (1);
  82. }
  83. res = strftime (buf, size, tests[cnt].fmt, now);
  84. if (res != 0)
  85. break;
  86. }
  87. while (size < tests[cnt].max);
  88. if (res == 0)
  89. {
  90. printf ("%Zu: %s: res == 0 despite size == %Zu\n",
  91. cnt, tests[cnt].fmt, size);
  92. result = 1;
  93. }
  94. else if (size < tests[cnt].min)
  95. {
  96. printf ("%Zu: %s: size == %Zu was enough\n",
  97. cnt, tests[cnt].fmt, size);
  98. result = 1;
  99. }
  100. else
  101. printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size);
  102. free (buf);
  103. }
  104. struct tm ttm =
  105. {
  106. /* Initialize the fields which are needed in the tests. */
  107. .tm_mday = 1,
  108. .tm_hour = 2
  109. };
  110. const struct
  111. {
  112. const char *fmt;
  113. const char *exp;
  114. size_t n;
  115. } ftests[] =
  116. {
  117. { "%-e", "1", 1 },
  118. { "%-k", "2", 1 },
  119. { "%-l", "2", 1 },
  120. };
  121. #define nftests (sizeof (ftests) / sizeof (ftests[0]))
  122. for (cnt = 0; cnt < nftests; ++cnt)
  123. {
  124. char buf[100];
  125. size_t r = strftime (buf, sizeof (buf), ftests[cnt].fmt, &ttm);
  126. if (r != ftests[cnt].n)
  127. {
  128. printf ("strftime(\"%s\") returned %zu not %zu\n",
  129. ftests[cnt].fmt, r, ftests[cnt].n);
  130. result = 1;
  131. }
  132. if (strcmp (buf, ftests[cnt].exp) != 0)
  133. {
  134. printf ("strftime(\"%s\") produced \"%s\" not \"%s\"\n",
  135. ftests[cnt].fmt, buf, ftests[cnt].exp);
  136. result = 1;
  137. }
  138. }
  139. return result + do_bz18985 ();
  140. }
  141. #define TEST_FUNCTION do_test ()
  142. #include "../test-skeleton.c"