lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <time.h> |
| 5 | |
| 6 | |
| 7 | static struct |
| 8 | { |
| 9 | const char *fmt; |
| 10 | size_t min; |
| 11 | size_t max; |
| 12 | } tests[] = |
| 13 | { |
| 14 | { "%2000Y", 2000, 4000 }, |
| 15 | { "%02000Y", 2000, 4000 }, |
| 16 | { "%_2000Y", 2000, 4000 }, |
| 17 | { "%-2000Y", 2000, 4000 }, |
| 18 | }; |
| 19 | #define ntests (sizeof (tests) / sizeof (tests[0])) |
| 20 | |
| 21 | |
| 22 | static int |
| 23 | do_test (void) |
| 24 | { |
| 25 | size_t cnt; |
| 26 | int result = 0; |
| 27 | |
| 28 | time_t tnow = time (NULL); |
| 29 | struct tm *now = localtime (&tnow); |
| 30 | |
| 31 | for (cnt = 0; cnt < ntests; ++cnt) |
| 32 | { |
| 33 | size_t size = 0; |
| 34 | int res; |
| 35 | char *buf = NULL; |
| 36 | |
| 37 | do |
| 38 | { |
| 39 | size += 500; |
| 40 | buf = (char *) realloc (buf, size); |
| 41 | if (buf == NULL) |
| 42 | { |
| 43 | puts ("out of memory"); |
| 44 | exit (1); |
| 45 | } |
| 46 | |
| 47 | res = strftime (buf, size, tests[cnt].fmt, now); |
| 48 | if (res != 0) |
| 49 | break; |
| 50 | } |
| 51 | while (size < tests[cnt].max); |
| 52 | |
| 53 | if (res == 0) |
| 54 | { |
| 55 | printf ("%Zu: %s: res == 0 despite size == %Zu\n", |
| 56 | cnt, tests[cnt].fmt, size); |
| 57 | result = 1; |
| 58 | } |
| 59 | else if (size < tests[cnt].min) |
| 60 | { |
| 61 | printf ("%Zu: %s: size == %Zu was enough\n", |
| 62 | cnt, tests[cnt].fmt, size); |
| 63 | result = 1; |
| 64 | } |
| 65 | else |
| 66 | printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size); |
| 67 | |
| 68 | free (buf); |
| 69 | } |
| 70 | |
| 71 | struct tm ttm = |
| 72 | { |
| 73 | /* Initialize the fields which are needed in the tests. */ |
| 74 | .tm_mday = 1, |
| 75 | .tm_hour = 2 |
| 76 | }; |
| 77 | const struct |
| 78 | { |
| 79 | const char *fmt; |
| 80 | const char *exp; |
| 81 | size_t n; |
| 82 | } ftests[] = |
| 83 | { |
| 84 | { "%-e", "1", 1 }, |
| 85 | { "%-k", "2", 1 }, |
| 86 | { "%-l", "2", 1 }, |
| 87 | }; |
| 88 | #define nftests (sizeof (ftests) / sizeof (ftests[0])) |
| 89 | for (cnt = 0; cnt < nftests; ++cnt) |
| 90 | { |
| 91 | char buf[100]; |
| 92 | size_t r = strftime (buf, sizeof (buf), ftests[cnt].fmt, &ttm); |
| 93 | if (r != ftests[cnt].n) |
| 94 | { |
| 95 | printf ("strftime(\"%s\") returned %zu not %zu\n", |
| 96 | ftests[cnt].fmt, r, ftests[cnt].n); |
| 97 | result = 1; |
| 98 | } |
| 99 | if (strcmp (buf, ftests[cnt].exp) != 0) |
| 100 | { |
| 101 | printf ("strftime(\"%s\") produced \"%s\" not \"%s\"\n", |
| 102 | ftests[cnt].fmt, buf, ftests[cnt].exp); |
| 103 | result = 1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | #define TEST_FUNCTION do_test () |
| 111 | #include "../test-skeleton.c" |