home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnurx.zip / rx / runtests.c < prev    next >
C/C++ Source or Header  |  1995-12-31  |  3KB  |  137 lines

  1. /***********************************************************
  2.  
  3. Copyright 1995 by Tom Lord
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of the copyright holder not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  19. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26.  
  27. #include "rxall.h"
  28. #include <stdio.h>
  29. #include "rxposix.h"
  30. #include "malloc.h"
  31.  
  32.  
  33.  
  34. struct a_test
  35. {
  36.   int expected;
  37.   char * pattern;
  38.   unsigned char * data;
  39. };
  40.  
  41. struct a_test the_tests[] = 
  42. {
  43. #include "testcases.h"
  44.   {-1, 0, 0}
  45. };
  46.  
  47.  
  48.  
  49.  
  50. void
  51. run_a_test (id, t)
  52.      int id;
  53.      struct a_test * t;
  54. {
  55.   static char * last_pattern = 0;
  56.   static regex_t r;
  57.   int err;
  58.   char errmsg[100];
  59.   int x;
  60.   regmatch_t regs[10];
  61.  
  62.   if (!last_pattern || strcmp (last_pattern, t->pattern))
  63.     {
  64.       if (last_pattern)
  65.     regfree (&r);
  66.       last_pattern = t->pattern;
  67.       err = regcomp (&r, t->pattern, REG_EXTENDED);
  68.       if (err)
  69.     {
  70.       if (t->expected)
  71.         return;
  72.       regerror (err, &r, errmsg, 100);
  73.       printf ("test %d\n", id);
  74.       puts (errmsg);
  75.       return;
  76.     }
  77.     }
  78.       
  79.   err = regexec (&r, t->data, 10, regs, 0);
  80.  
  81.   if (err != t->expected)
  82.     {
  83.       printf ("test %d\n", id);
  84.       printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n",
  85.           t->pattern, t->data, t->expected, err);
  86.       for (x = 0; x < 10; ++x)
  87.     printf ("reg %d == (%d, %d) %.*s\n",
  88.         x,
  89.         regs[x].rm_so,
  90.         regs[x].rm_eo,
  91.         regs[x].rm_eo - regs[x].rm_so,
  92.         t->data + regs[x].rm_so);
  93.     }
  94.  
  95. }
  96.  
  97.  
  98.  
  99. main(argc, argv)
  100.      int argc;
  101.      char * argv[];
  102. {
  103.   int x;
  104.   int lo;
  105.   int hi;
  106.  
  107. #if 0
  108.   {
  109.     union dbmalloptarg val;
  110.     val.i = 1; dbmallopt (MALLOC_CKCHAIN, &val);
  111.   }
  112. #endif
  113.  
  114.   lo = 0;
  115.   hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1;
  116.  
  117.   if (argc > 1)
  118.     {
  119.       lo = atoi (argv[1]);
  120.       hi = lo + 1;
  121.  
  122.       if (argc > 2)
  123.     hi = atoi (argv[2]);
  124.     }
  125.  
  126.   for (x = lo; x < hi; ++x)
  127.     {
  128.       printf ("#%d\n", x);
  129.       run_a_test (x, &the_tests[x]);
  130.     }
  131.   {
  132.     exit (0);
  133.   }
  134. }
  135.  
  136.  
  137.