Blame time/tst-getdate.c

Packit 6c4009
/* Test for getdate.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Andreas Jaeger <aj@suse.de>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
static const struct
Packit 6c4009
{
Packit 6c4009
  const char *str;
Packit 6c4009
  const char *tz;
Packit 6c4009
  int err;
Packit 6c4009
  struct tm tm;
Packit 6c4009
} tests [] =
Packit 6c4009
{
Packit 6c4009
  {"21:01:10 1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
Packit 6c4009
  {"21:01:10    1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
Packit 6c4009
  {"   21:01:10 1999-1-31", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
Packit 6c4009
  {"21:01:10 1999-1-31   ", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
Packit 6c4009
  {"    21:01:10 1999-1-31   ", "Universal", 0, {10, 1, 21, 31, 0, 99, 0, 0, 0}},
Packit 6c4009
  {"21:01:10 1999-2-28", "Universal", 0, {10, 1, 21, 28, 1, 99, 0, 0, 0}},
Packit 6c4009
  {"16:30:46 2000-2-29", "Universal", 0, {46, 30,16, 29, 1, 100, 0, 0, 0}},
Packit 6c4009
  {"01-08-2000 05:06:07", "Europe/Berlin", 0, {7, 6, 5, 1, 7, 100, 0, 0, 0}}
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
report_date_error (int err)
Packit 6c4009
{
Packit 6c4009
  switch(err)
Packit 6c4009
    {
Packit 6c4009
    case 1:
Packit 6c4009
      printf ("The environment variable DATEMSK is not defined or null.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 2:
Packit 6c4009
      printf ("The template file denoted by the DATEMSK environment variable cannot be opened.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 3:
Packit 6c4009
      printf ("Information about the template file cannot retrieved.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 4:
Packit 6c4009
      printf ("The template file is not a regular file.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 5:
Packit 6c4009
      printf ("An I/O error occurred while reading the template file.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 6:
Packit 6c4009
      printf ("Not enough memory available to execute the function.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 7:
Packit 6c4009
      printf ("The template file contains no matching template.\n");
Packit 6c4009
      break;
Packit 6c4009
    case 8:
Packit 6c4009
      printf ("The input date is invalid, but would match a template otherwise.\n");
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      printf("Unknown error code.\n");
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int errors = 0;
Packit 6c4009
  size_t i;
Packit 6c4009
  struct tm *tm;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
Packit 6c4009
    {
Packit 6c4009
      setenv ("TZ", tests[i].tz, 1);
Packit 6c4009
Packit 6c4009
      tm = getdate (tests[i].str);
Packit 6c4009
Packit 6c4009
      if (getdate_err != tests[i].err)
Packit 6c4009
	{
Packit 6c4009
	  printf ("Failure for getdate (\"%s\"):\n", tests[i].str);
Packit 6c4009
	  printf ("getdate_err should be %d but returned: %d which means:\n",
Packit 6c4009
		  tests[i].err, getdate_err);
Packit 6c4009
	  report_date_error (getdate_err);
Packit 6c4009
	  ++errors;
Packit 6c4009
	}
Packit 6c4009
      else if (tests[i].tm.tm_mon != tm->tm_mon
Packit 6c4009
	       || tests[i].tm.tm_year != tm->tm_year
Packit 6c4009
	       || tests[i].tm.tm_mday != tm->tm_mday
Packit 6c4009
	       || tests[i].tm.tm_hour != tm->tm_hour
Packit 6c4009
	       || tests[i].tm.tm_min != tm->tm_min
Packit 6c4009
	       || tests[i].tm.tm_sec != tm->tm_sec)
Packit 6c4009
	{
Packit 6c4009
	  printf ("Failure for getdate (\"%s\"):\n", tests[i].str);
Packit 6c4009
	  printf ("struct tm is:  %d-%d-%d %d:%d:%d\n",
Packit 6c4009
		  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
Packit 6c4009
		  tm->tm_hour, tm->tm_min, tm->tm_sec);
Packit 6c4009
	  printf ("but should be: %d-%d-%d %d:%d:%d\n",
Packit 6c4009
		  tests[i].tm.tm_year + 1900, tests[i].tm.tm_mon + 1,
Packit 6c4009
		  tests[i].tm.tm_mday,
Packit 6c4009
		  tests[i].tm.tm_hour, tests[i].tm.tm_min, tests[i].tm.tm_sec);
Packit 6c4009
	  ++errors;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!errors)
Packit 6c4009
    printf ("No errors found.\n");
Packit 6c4009
  return errors != 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"