home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / REGEXP.ZIP / TIMER.C < prev    next >
Text File  |  1986-01-21  |  4KB  |  183 lines

  1. /*
  2.  * Simple timing program for regcomp().
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *
  21.  * Usage: timer ncomp nexec nsub
  22.  *    or
  23.  *    timer ncomp nexec nsub regexp string [ answer [ sub ] ]
  24.  *
  25.  * The second form is for timing repetitions of a single test case.
  26.  * The first form's test data is a compiled-in copy of the "tests" file.
  27.  * Ncomp, nexec, nsub are how many times to do each regcomp, regexec,
  28.  * and regsub.  The way to time an operation individually is to do something
  29.  * like "timer 1 50 1".
  30.  */
  31. #include <stdio.h>
  32.  
  33. struct try {
  34.     char *re, *str, *ans, *src, *dst;
  35. } tests[] = {
  36. #include "timer.t.h"
  37. { NULL, NULL, NULL, NULL, NULL }
  38. };
  39.  
  40. #include <regexp.h>
  41.  
  42. int errreport = 0;        /* Report errors via errseen? */
  43. char *errseen = NULL;        /* Error message. */
  44.  
  45. char *progname;
  46.  
  47. /* ARGSUSED */
  48. main(argc, argv)
  49. int argc;
  50. char *argv[];
  51. {
  52.     int ncomp, nexec, nsub;
  53.     struct try one;
  54.     char dummy[512];
  55.  
  56.     if (argc < 4) {
  57.         ncomp = 1;
  58.         nexec = 1;
  59.         nsub = 1;
  60.     } else {
  61.         ncomp = atoi(argv[1]);
  62.         nexec = atoi(argv[2]);
  63.         nsub = atoi(argv[3]);
  64.     }
  65.     
  66.     progname = argv[0];
  67.     if (argc > 5) {
  68.         one.re = argv[4];
  69.         one.str = argv[5];
  70.         if (argc > 6)
  71.             one.ans = argv[6];
  72.         else
  73.             one.ans = "y";
  74.         if (argc > 7) {    
  75.             one.src = argv[7];
  76.             one.dst = "xxx";
  77.         } else {
  78.             one.src = "x";
  79.             one.dst = "x";
  80.         }
  81.         errreport = 1;
  82.         try(one, ncomp, nexec, nsub);
  83.     } else
  84.         multiple(ncomp, nexec, nsub);
  85.     exit(0);
  86. }
  87.  
  88. void
  89. regerror(s)
  90. char *s;
  91. {
  92.     if (errreport)
  93.         errseen = s;
  94.     else
  95.         error(s, "");
  96. }
  97.  
  98. #ifndef ERRAVAIL
  99. error(s1, s2)
  100. char *s1;
  101. char *s2;
  102. {
  103.     fprintf(stderr, "regexp: ");
  104.     fprintf(stderr, s1, s2);
  105.     fprintf(stderr, "\n");
  106.     exit(1);
  107. }
  108. #endif
  109.  
  110. int lineno = 0;
  111.  
  112. multiple(ncomp, nexec, nsub)
  113. int ncomp, nexec, nsub;
  114. {
  115.     register int i;
  116.     extern char *strchr();
  117.  
  118.     errreport = 1;
  119.     for (i = 0; tests[i].re != NULL; i++) {
  120.         lineno++;
  121.         try(tests[i], ncomp, nexec, nsub);
  122.     }
  123. }
  124.  
  125. try(fields, ncomp, nexec, nsub)
  126. struct try fields;
  127. int ncomp, nexec, nsub;
  128. {
  129.     regexp *r;
  130.     char dbuf[BUFSIZ];
  131.     register int i;
  132.  
  133.     errseen = NULL;
  134.     r = regcomp(fields.re);
  135.     if (r == NULL) {
  136.         if (*fields.ans != 'c')
  137.             complain("regcomp failure in `%s'", fields.re);
  138.         return;
  139.     }
  140.     if (*fields.ans == 'c') {
  141.         complain("unexpected regcomp success in `%s'", fields.re);
  142.         free((char *)r);
  143.         return;
  144.     }
  145.     for (i = ncomp-1; i > 0; i--) {
  146.         free((char *)r);
  147.         r = regcomp(fields.re);
  148.     }
  149.     if (!regexec(r, fields.str)) {
  150.         if (*fields.ans != 'n')
  151.             complain("regexec failure in `%s'", "");
  152.         free((char *)r);
  153.         return;
  154.     }
  155.     if (*fields.ans == 'n') {
  156.         complain("unexpected regexec success", "");
  157.         free((char *)r);
  158.         return;
  159.     }
  160.     for (i = nexec-1; i > 0; i--)
  161.         (void) regexec(r, fields.str);
  162.     errseen = NULL;
  163.     for (i = nsub; i > 0; i--)
  164.         regsub(r, fields.src, dbuf);
  165.     if (errseen != NULL) {    
  166.         complain("regsub complaint", "");
  167.         free((char *)r);
  168.         return;
  169.     }
  170.     if (strcmp(dbuf, fields.dst) != 0)
  171.         complain("regsub result `%s' wrong", dbuf);
  172.     free((char *)r);
  173. }
  174.  
  175. complain(s1, s2)
  176. char *s1;
  177. char *s2;
  178. {
  179.     fprintf(stderr, "try: %d: ", lineno);
  180.     fprintf(stderr, s1, s2);
  181.     fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
  182. }
  183.