File: tst-strftime.c

package info (click to toggle)
glibc 2.19-18+deb8u7
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 204,748 kB
  • sloc: ansic: 970,427; asm: 241,207; sh: 10,069; makefile: 8,476; cpp: 3,595; perl: 2,077; pascal: 1,839; awk: 1,704; yacc: 317; sed: 73
file content (161 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


static int
do_bz18985 (void)
{
  char buf[1000];
  struct tm ttm;
  int rc, ret = 0;

  memset (&ttm, 1, sizeof (ttm));
  ttm.tm_zone = NULL;  /* Dereferenced directly if non-NULL.  */
  rc = strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm);

  if (rc == 66)
    {
      const char expected[]
	= "? ? ? ? ? ? 16843009 16843009:16843009:16843009 16844909 +467836 ?";
      if (0 != strcmp (buf, expected))
	{
	  printf ("expected:\n  %s\ngot:\n  %s\n", expected, buf);
	  ret += 1;
	}
    }
  else
    {
      printf ("expected 66, got %d\n", rc);
      ret += 1;
    }

  /* Check negative values as well.  */
  memset (&ttm, 0xFF, sizeof (ttm));
  ttm.tm_zone = NULL;  /* Dereferenced directly if non-NULL.  */
  rc = strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm);

  if (rc == 30)
    {
      const char expected[] = "? ? ? ? ? ? -1 -1:-1:-1 1899  ";
      if (0 != strcmp (buf, expected))
	{
	  printf ("expected:\n  %s\ngot:\n  %s\n", expected, buf);
	  ret += 1;
	}
    }
  else
    {
      printf ("expected 30, got %d\n", rc);
      ret += 1;
    }

  return ret;
}

static struct
{
  const char *fmt;
  size_t min;
  size_t max;
} tests[] =
  {
    { "%2000Y", 2000, 4000 },
    { "%02000Y", 2000, 4000 },
    { "%_2000Y", 2000, 4000 },
    { "%-2000Y", 2000, 4000 },
  };
#define ntests (sizeof (tests) / sizeof (tests[0]))


static int
do_test (void)
{
  size_t cnt;
  int result = 0;

  time_t tnow = time (NULL);
  struct tm *now = localtime (&tnow);

  for (cnt = 0; cnt < ntests; ++cnt)
    {
      size_t size = 0;
      int res;
      char *buf = NULL;

      do
	{
	  size += 500;
	  buf = (char *) realloc (buf, size);
	  if (buf == NULL)
	    {
	      puts ("out of memory");
	      exit (1);
	    }

	  res = strftime (buf, size, tests[cnt].fmt, now);
	  if (res != 0)
	    break;
	}
      while (size < tests[cnt].max);

      if (res == 0)
	{
	  printf ("%Zu: %s: res == 0 despite size == %Zu\n",
		  cnt, tests[cnt].fmt, size);
	  result = 1;
	}
      else if (size < tests[cnt].min)
	{
	  printf ("%Zu: %s: size == %Zu was enough\n",
		  cnt, tests[cnt].fmt, size);
	  result = 1;
	}
      else
	printf ("%Zu: %s: size == %Zu: OK\n", cnt, tests[cnt].fmt, size);

      free (buf);
    }

  struct tm ttm =
    {
      /* Initialize the fields which are needed in the tests.  */
      .tm_mday = 1,
      .tm_hour = 2
    };
  const struct
  {
    const char *fmt;
    const char *exp;
    size_t n;
  } ftests[] =
    {
      { "%-e", "1", 1 },
      { "%-k", "2", 1 },
      { "%-l", "2", 1 },
    };
#define nftests (sizeof (ftests) / sizeof (ftests[0]))
  for (cnt = 0; cnt < nftests; ++cnt)
    {
      char buf[100];
      size_t r = strftime (buf, sizeof (buf), ftests[cnt].fmt, &ttm);
      if (r != ftests[cnt].n)
	{
	  printf ("strftime(\"%s\") returned %zu not %zu\n",
		  ftests[cnt].fmt, r, ftests[cnt].n);
	  result = 1;
	}
      if (strcmp (buf, ftests[cnt].exp) != 0)
	{
	  printf ("strftime(\"%s\") produced \"%s\" not \"%s\"\n",
		  ftests[cnt].fmt, buf, ftests[cnt].exp);
	  result = 1;
	}
    }

  return result + do_bz18985 ();
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"