home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / regexps.zip / timer.c < prev    next >
C/C++ Source or Header  |  1996-09-05  |  3KB  |  165 lines

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