Blame posix/tst-regex2.c

Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <regex.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
Packit 6c4009
static clockid_t cl;
Packit 6c4009
static int use_clock;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
Packit 6c4009
# if _POSIX_CPUTIME == 0
Packit 6c4009
  if (sysconf (_SC_CPUTIME) < 0)
Packit 6c4009
    use_clock = 0;
Packit 6c4009
  else
Packit 6c4009
# endif
Packit 6c4009
    /* See whether we can use the CPU clock.  */
Packit 6c4009
    use_clock = clock_getcpuclockid (0, &cl) == 0;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  static const char *pat[] = {
Packit 6c4009
    ".?.?.?.?.?.?.?Log\\.13",
Packit 6c4009
    "(.?)(.?)(.?)(.?)(.?)(.?)(.?)Log\\.13",
Packit 6c4009
    "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
Packit 6c4009
    "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))"
Packit 6c4009
    "((((((((((.?))))))))))Log\\.13" };
Packit 6c4009
Packit 6c4009
  int fd = open ("../ChangeLog.old/ChangeLog.14", O_RDONLY);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Couldn't open ChangeLog.14: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  if (fstat64 (fd, &st) < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Couldn't fstat ChangeLog.14: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char *buf = malloc (st.st_size + 1);
Packit 6c4009
  if (buf == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Couldn't allocate buffer: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (read (fd, buf, st.st_size) != (ssize_t) st.st_size)
Packit 6c4009
    {
Packit 6c4009
      puts ("Couldn't read ChangeLog.14");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
  buf[st.st_size] = '\0';
Packit 6c4009
Packit 6c4009
  setlocale (LC_ALL, "de_DE.UTF-8");
Packit 6c4009
Packit 6c4009
  char *string = buf;
Packit 6c4009
  size_t len = st.st_size;
Packit 6c4009
Packit 6c4009
#ifndef WHOLE_FILE_TIMING
Packit 6c4009
  /* Don't search the whole file normally, it takes too long.  */
Packit 6c4009
  if (len > 500000 + 64)
Packit 6c4009
    {
Packit 6c4009
      string += 500000;
Packit 6c4009
      len -= 500000;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  for (int testno = 0; testno < 4; ++testno)
Packit 6c4009
    for (int i = 0; i < sizeof (pat) / sizeof (pat[0]); ++i)
Packit 6c4009
      {
Packit 6c4009
	printf ("test %d pattern %d", testno, i);
Packit 6c4009
Packit 6c4009
	regex_t rbuf;
Packit 6c4009
	struct re_pattern_buffer rpbuf;
Packit 6c4009
	int err;
Packit 6c4009
	if (testno < 2)
Packit 6c4009
	  {
Packit 6c4009
	    err = regcomp (&rbuf, pat[i],
Packit 6c4009
			   REG_EXTENDED | (testno ? REG_NOSUB : 0));
Packit 6c4009
	    if (err != 0)
Packit 6c4009
	      {
Packit 6c4009
		putchar ('\n');
Packit 6c4009
		char errstr[300];
Packit 6c4009
		regerror (err, &rbuf, errstr, sizeof (errstr));
Packit 6c4009
		puts (errstr);
Packit 6c4009
		return err;
Packit 6c4009
	      }
Packit 6c4009
	  }
Packit 6c4009
	else
Packit 6c4009
	  {
Packit 6c4009
	    re_set_syntax (RE_SYNTAX_POSIX_EGREP
Packit 6c4009
			   | (testno == 3 ? RE_NO_SUB : 0));
Packit 6c4009
Packit 6c4009
	    memset (&rpbuf, 0, sizeof (rpbuf));
Packit 6c4009
	    const char *s = re_compile_pattern (pat[i], strlen (pat[i]),
Packit 6c4009
						&rpbuf);
Packit 6c4009
	    if (s != NULL)
Packit 6c4009
	      {
Packit 6c4009
		printf ("\n%s\n", s);
Packit 6c4009
		return 1;
Packit 6c4009
	      }
Packit 6c4009
Packit 6c4009
	    /* Just so that this can be tested with earlier glibc as well.  */
Packit 6c4009
	    if (testno == 3)
Packit 6c4009
	      rpbuf.no_sub = 1;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
Packit 6c4009
      struct timespec start, stop;
Packit 6c4009
      if (use_clock)
Packit 6c4009
	use_clock = clock_gettime (cl, &start) == 0;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      if (testno < 2)
Packit 6c4009
	{
Packit 6c4009
	  regmatch_t pmatch[71];
Packit 6c4009
	  err = regexec (&rbuf, string, 71, pmatch, 0);
Packit 6c4009
	  if (err == REG_NOMATCH)
Packit 6c4009
	    {
Packit 6c4009
	      puts ("\nregexec failed");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (testno == 0)
Packit 6c4009
	    {
Packit 6c4009
	      if (pmatch[0].rm_eo != pmatch[0].rm_so + 13
Packit 6c4009
		  || pmatch[0].rm_eo > len
Packit 6c4009
		  || pmatch[0].rm_so < len - 100
Packit 6c4009
		  || strncmp (string + pmatch[0].rm_so,
Packit 6c4009
			      " ChangeLog.13 for earlier changes",
Packit 6c4009
			      sizeof " ChangeLog.13 for earlier changes" - 1)
Packit 6c4009
		     != 0)
Packit 6c4009
		{
Packit 6c4009
		  puts ("\nregexec without REG_NOSUB did not find the correct match");
Packit 6c4009
		  return 1;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (i > 0)
Packit 6c4009
		for (int j = 0, l = 1; j < 7; ++j)
Packit 6c4009
		  for (int k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
Packit 6c4009
		    if (pmatch[l].rm_so != pmatch[0].rm_so + j
Packit 6c4009
			|| pmatch[l].rm_eo != pmatch[l].rm_so + 1)
Packit 6c4009
		      {
Packit 6c4009
			printf ("\npmatch[%d] incorrect\n", l);
Packit 6c4009
			return 1;
Packit 6c4009
		      }
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  struct re_registers regs;
Packit 6c4009
Packit 6c4009
	  memset (&regs, 0, sizeof (regs));
Packit 6c4009
	  int match = re_search (&rpbuf, string, len, 0, len,
Packit 6c4009
				 ®s;;
Packit 6c4009
	  if (match < 0)
Packit 6c4009
	    {
Packit 6c4009
	      puts ("\nre_search failed");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (match + 13 > len
Packit 6c4009
	      || match < len - 100
Packit 6c4009
	      || strncmp (string + match,
Packit 6c4009
			  " ChangeLog.13 for earlier changes",
Packit 6c4009
			  sizeof " ChangeLog.13 for earlier changes" - 1)
Packit 6c4009
		  != 0)
Packit 6c4009
	    {
Packit 6c4009
	      puts ("\nre_search did not find the correct match");
Packit 6c4009
	      return 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (testno == 2)
Packit 6c4009
	    {
Packit 6c4009
	      if (regs.num_regs != 2 + (i == 0 ? 0 : i == 1 ? 7 : 70))
Packit 6c4009
		{
Packit 6c4009
		  printf ("\nincorrect num_regs %d\n", regs.num_regs);
Packit 6c4009
		  return 1;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (regs.start[0] != match || regs.end[0] != match + 13)
Packit 6c4009
		{
Packit 6c4009
		  printf ("\nincorrect regs.{start,end}[0] = { %d, %d}\n",
Packit 6c4009
			  regs.start[0], regs.end[0]);
Packit 6c4009
		  return 1;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (regs.start[regs.num_regs - 1] != -1
Packit 6c4009
		  || regs.end[regs.num_regs - 1] != -1)
Packit 6c4009
		{
Packit 6c4009
		  puts ("\nincorrect regs.{start,end}[num_regs - 1]");
Packit 6c4009
		  return 1;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (i > 0)
Packit 6c4009
		for (int j = 0, l = 1; j < 7; ++j)
Packit 6c4009
		  for (int k = 0; k < (i == 1 ? 1 : 10); ++k, ++l)
Packit 6c4009
		    if (regs.start[l] != match + j
Packit 6c4009
			|| regs.end[l] != regs.start[l] + 1)
Packit 6c4009
		      {
Packit 6c4009
			printf ("\nregs.{start,end}[%d] incorrect\n", l);
Packit 6c4009
			return 1;
Packit 6c4009
		      }
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
Packit 6c4009
      if (use_clock)
Packit 6c4009
	use_clock = clock_gettime (cl, &stop) == 0;
Packit 6c4009
      if (use_clock)
Packit 6c4009
	{
Packit 6c4009
	  stop.tv_sec -= start.tv_sec;
Packit 6c4009
	  if (stop.tv_nsec < start.tv_nsec)
Packit 6c4009
	    {
Packit 6c4009
	      stop.tv_sec--;
Packit 6c4009
	      stop.tv_nsec += 1000000000 - start.tv_nsec;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    stop.tv_nsec -= start.tv_nsec;
Packit 6c4009
	  printf (": %ld.%09lds\n", (long) stop.tv_sec, (long) stop.tv_nsec);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
#endif
Packit 6c4009
	putchar ('\n');
Packit 6c4009
Packit 6c4009
      if (testno < 2)
Packit 6c4009
	regfree (&rbuf);
Packit 6c4009
      else
Packit 6c4009
	regfree (&rpbuf);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 20
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"