home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / REGEX011.TAZ / REGEX011 / regex-0.11 / test / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-08  |  21.8 KB  |  783 lines

  1. /* test.c: testing routines for regex.c.  */
  2.  
  3. #include <assert.h>
  4.  
  5. #ifdef STDC_HEADERS
  6. #include <stdlib.h>
  7. #else
  8. char *malloc ();
  9. char *realloc ();
  10. #endif
  11.  
  12. /* Just to be complete, we make both the system V/ANSI and the BSD
  13.    versions of the string functions available.  */
  14. #if USG || STDC_HEADERS
  15. #include <string.h>
  16. #define index strchr
  17. #define rindex strrchr
  18. #define bcmp(s1, s2, len) memcmp ((s1), (s2), (len))
  19. #define bcopy(from, to, len) memcpy ((to), (from), (len))
  20. #define bzero(s, len) memset ((s), 0, (len))
  21. #else
  22. #include <strings.h>
  23. #define strchr index
  24. #define strrchr rindex
  25. #ifndef NEED_MEMORY_H
  26. #define memcmp(s1, s2, n) bcmp ((s1), (s2), (n))
  27. #define memcpy(to, from, len) bcopy ((from), (to), (len))
  28. #endif
  29. extern char *strtok ();
  30. extern char *strstr ();
  31. #endif /* not USG or STDC_HEADERS */
  32.  
  33. /* SunOS 4.1 declares memchr in <memory.h>, not <string.h>.  I don't
  34.    understand why.  */
  35. #if NEED_MEMORY_H
  36. #include <memory.h>
  37. #endif
  38.  
  39. #include "test.h"
  40.  
  41. #define BYTEWIDTH 8
  42.  
  43. extern void print_partial_compiled_pattern ();
  44. extern void print_compiled_pattern ();
  45. extern void print_double_string ();
  46.  
  47. /* If nonzero, the results of every test are displayed.  */
  48. boolean verbose = false;
  49.  
  50. /* If nonzero, don't do register testing.  */
  51. boolean omit_register_tests = true;
  52.  
  53. /* Says whether the current test should match or fail to match.  */
  54. boolean test_should_match;
  55.  
  56.  
  57. static void
  58. set_all_registers (start0, end0, start1, end1, 
  59.                start2, end2, start3, end3,
  60.                    start4, end4, start5, end5, 
  61.                    start6, end6, start7, end7,
  62.                    start8, end8, start9, end9, regs)
  63.  
  64.   int start0; int end0; int start1; int end1; 
  65.   int start2; int end2; int start3; int end3;
  66.   int start4; int end4; int start5; int end5; 
  67.   int start6; int end6; int start7; int end7;
  68.   int start8; int end8; int start9; int end9; 
  69.   struct re_registers *regs;
  70.  
  71.   {
  72.     unsigned r;
  73.     
  74.     regs->start[0] = start0;   regs->end[0] = end0;      
  75.     regs->start[1] = start1;   regs->end[1] = end1;    
  76.     regs->start[2] = start2;   regs->end[2] = end2;    
  77.     regs->start[3] = start3;   regs->end[3] = end3;    
  78.     regs->start[4] = start4;   regs->end[4] = end4;    
  79.     regs->start[5] = start5;   regs->end[5] = end5;    
  80.     regs->start[6] = start6;   regs->end[6] = end6;    
  81.     regs->start[7] = start7;   regs->end[7] = end7;    
  82.     regs->start[8] = start8;   regs->end[8] = end8;    
  83.     regs->start[9] = start9;   regs->end[9] = end9;    
  84.     for (r = 10; r < regs->num_regs; r++)
  85.       {
  86.         regs->start[r] = -1;
  87.         regs->end[r] = -1;
  88.       }
  89.   }
  90.  
  91.  
  92.  
  93. /* Return the concatenation of S1 and S2.  This would be a prime place
  94.    to use varargs.  */
  95.  
  96. char *
  97. concat (s1, s2)
  98.     char *s1;
  99.     char *s2;
  100. {
  101.   char *answer = xmalloc (strlen (s1) + strlen (s2) + 1);
  102.  
  103.   strcpy (answer, s1);
  104.   strcat (answer, s2);
  105.  
  106.   return answer;
  107. }
  108.  
  109.  
  110. #define OK_TO_SEARCH  (nonconst_buf.fastmap_accurate && (str1 || str2))
  111.  
  112. /* We ignore the `can_be_null' argument.  Should just be removed.  */
  113.  
  114. void 
  115. general_test (pattern_should_be_valid, match_whole_string,
  116.           pat, str1, str2, start, range, end, correct_fastmap,
  117.               correct_regs, can_be_null)
  118.     unsigned pattern_should_be_valid; 
  119.     unsigned match_whole_string;
  120.     const char *pat;
  121.     char *str1, *str2;
  122.     int start, range, end; 
  123.     char *correct_fastmap; 
  124.     struct re_registers *correct_regs;
  125.     int can_be_null;
  126. {
  127.   struct re_pattern_buffer nonconst_buf;
  128.   struct re_pattern_buffer old_buf;
  129.   struct re_registers regs;
  130.   const char *r;
  131.   char fastmap[1 << BYTEWIDTH];
  132.   unsigned *regs_correct = NULL;
  133.   unsigned all_regs_correct = 1;
  134.   boolean fastmap_internal_error = false;
  135.   unsigned match = 0;
  136.   unsigned match_1 = 0;
  137.   unsigned match_2 = 0;
  138.   unsigned invalid_pattern = 0;
  139.   boolean internal_error_1 = false;
  140.   boolean internal_error_2 = false;
  141.   
  142.   
  143.   nonconst_buf.allocated = 8;
  144.   nonconst_buf.buffer = xmalloc (nonconst_buf.allocated);
  145.   nonconst_buf.fastmap = fastmap;
  146.   nonconst_buf.translate = 0;
  147.  
  148.   assert (pat != NULL);
  149.   r = re_compile_pattern (pat, strlen (pat), &nonconst_buf);
  150.   
  151.   /* Kludge: if we are doing POSIX testing, we really should have
  152.      called regcomp, not re_compile_pattern.  As it happens, the only
  153.      way in which it matters is that re_compile_pattern sets the
  154.      newline/anchor field for matching (part of what happens when
  155.      REG_NEWLINE is given to regcomp).  We have to undo that for POSIX
  156.      matching.  */
  157.   if (t == posix_basic_test || t == posix_extended_test)
  158.     nonconst_buf.newline_anchor = 0;
  159.     
  160.   invalid_pattern = r != NULL;
  161.  
  162.   if (!r)
  163.     {
  164.       int r;
  165.  
  166.       if (!pattern_should_be_valid)
  167.         printf ("\nShould have been an invalid pattern but wasn't:\n");
  168.       else
  169.         {
  170.           fastmap_internal_error = (re_compile_fastmap (&nonconst_buf) == -2);
  171.           
  172.           if (correct_fastmap)
  173.             nonconst_buf.fastmap_accurate = 
  174.               memcmp (nonconst_buf.fastmap, correct_fastmap, 1 << BYTEWIDTH)
  175.               == 0;
  176.         
  177.       if (OK_TO_SEARCH)
  178.        {
  179.               old_buf = nonconst_buf;
  180.               old_buf.buffer = (unsigned char *) xmalloc (nonconst_buf.used);
  181.               memcpy (old_buf.buffer, nonconst_buf.buffer, nonconst_buf.used);
  182.           
  183.               /* If only one string is null, call re_match or re_search,
  184.                 which is what the user would probably do.  */ 
  185.               if (str1 == NULL && str2 != NULL
  186.                   || str2 == NULL && str1 != NULL)
  187.                 {
  188.                   char *the_str = str1 == NULL ? str2 : str1;
  189.                   
  190.                   match_1
  191.                     = match_whole_string 
  192.                   ? (r = re_match (&nonconst_buf, the_str,
  193.                                        strlen (the_str), start, ®s))
  194.             == strlen (the_str)
  195.                       : (r = re_search (&nonconst_buf,
  196.                                         the_str, strlen (the_str),
  197.                                         start, range, ®s))
  198.             >= 0;
  199.  
  200.                   if (r == -2)
  201.                internal_error_1 = true;
  202.                  }
  203.           else  
  204.                 match_1 = 1;
  205.                 
  206.               /* Also call with re_match_2 or re_search_2, as they might
  207.                  do this.  (Also can check calling with either string1
  208.                  or string2 or both null.)  */
  209.               if (match_whole_string)
  210.                 {
  211.                   r = re_match_2 (&nonconst_buf,
  212.                                   str1, SAFE_STRLEN (str1), 
  213.                               str2, SAFE_STRLEN (str2),
  214.                                   start, ®s, end);
  215.                   match_2 = r == SAFE_STRLEN (str1) + SAFE_STRLEN (str2);
  216.                 }
  217.               else
  218.                 {
  219.                   r = re_search_2 (&nonconst_buf,
  220.                                    str1, SAFE_STRLEN (str1), 
  221.                    str2, SAFE_STRLEN (str2),
  222.                                    start, range, ®s, end);
  223.                   match_2 = r >= 0;
  224.                 }
  225.  
  226.               if (r == -2)
  227.            internal_error_2 = true;
  228.                 
  229.               match = match_1 & match_2;
  230.               
  231.               if (correct_regs)
  232.                 {
  233.           unsigned reg;
  234.           if (regs_correct != NULL)
  235.                     free (regs_correct);
  236.                     
  237.                   regs_correct 
  238.                     = (unsigned *) xmalloc (regs.num_regs * sizeof (unsigned));
  239.  
  240.                   for (reg = 0;
  241.                        reg < regs.num_regs && reg < correct_regs->num_regs;
  242.                        reg++)
  243.                     {
  244.                       regs_correct[reg]
  245.                         = (regs.start[reg] == correct_regs->start[reg]
  246.                    && regs.end[reg] == correct_regs->end[reg])
  247. #ifdef EMPTY_REGS_CONFUSED
  248.                           /* There is confusion in the standard about
  249.                              the registers in some patterns which can
  250.                              match either the empty string or not match.
  251.                              For example, in `((a*))*' against the empty
  252.                              string, the two registers can either match
  253.                              the empty string (be 0/0), or not match
  254.                              (because of the outer *) (be -1/-1).  (Or
  255.                              one can do one and one can do the other.)  */
  256.                           || (regs.start[reg] == -1 && regs.end[reg] == -1
  257.                               && correct_regs->start[reg]
  258.                                  == correct_regs->end[reg])
  259. #endif 
  260.                           ;
  261.                                                   
  262.                       all_regs_correct &= regs_correct[reg];
  263.                     }
  264.                 }
  265.            } /* OK_TO_SEARCH  */
  266.         }
  267.     }
  268.  
  269.   if (fastmap_internal_error)
  270.     printf ("\n\nInternal error in re_compile_fastmap:");
  271.     
  272.   if (internal_error_1)
  273.     {
  274.       if (!fastmap_internal_error)
  275.         printf ("\n");
  276.         
  277.       printf ("\nInternal error in re_match or re_search:");
  278.     }
  279.   
  280.   if (internal_error_2)
  281.     {
  282.       if (!internal_error_1)
  283.         printf ("\n");
  284.  
  285.       printf ("\nInternal error in re_match_2 or re_search_2:");
  286.     }
  287.  
  288.   if ((OK_TO_SEARCH && ((match && !test_should_match) 
  289.                || (!match && test_should_match))
  290.                        || (correct_regs && !all_regs_correct))
  291.       || !nonconst_buf.fastmap_accurate
  292.       || invalid_pattern 
  293.       || !pattern_should_be_valid
  294.       || internal_error_1 || internal_error_2
  295.       || verbose)
  296.     {
  297.       if (OK_TO_SEARCH && match && !test_should_match)
  298.         {
  299.           printf ("\n\nMatched but shouldn't have:\n");
  300.       if (match_1)
  301.         printf ("The single match/search succeeded.\n");
  302.  
  303.       if (match_2)
  304.         printf ("The double match/search succeeded.\n");
  305.         }
  306.       else if (OK_TO_SEARCH && !match && test_should_match)
  307.         {
  308.           printf ("\n\nDidn't match but should have:\n");
  309.       if (!match_1)
  310.         printf ("The single match/search failed.\n");
  311.  
  312.       if (!match_2)
  313.         printf ("The double match/search failed.\n");
  314.         }
  315.       else if (invalid_pattern && pattern_should_be_valid)
  316.         printf ("\n\nInvalid pattern (%s):\n", r);
  317.       else if (!nonconst_buf.fastmap_accurate && pattern_should_be_valid)
  318.     printf ("\n\nIncorrect fastmap:\n");
  319.       else if (OK_TO_SEARCH && correct_regs && !all_regs_correct)
  320.         printf ("\n\nNot all registers were correct:\n");
  321.       else if (verbose)
  322.         printf ("\n\nTest was OK:\n");
  323.  
  324.  
  325.       if ((!(invalid_pattern && !pattern_should_be_valid)) || verbose)
  326.         printf ("  Pattern:  `%s'.\n", pat);
  327.              
  328.       if (pattern_should_be_valid || verbose 
  329.           || internal_error_1 || internal_error_2)
  330.         {
  331.           printf("  Strings: ");
  332.           printf ("`%s' and ", str1 == NULL ? "NULL" : str1);
  333.           printf ("`%s'.\n", str2  == NULL ? "NULL" : str2);
  334.  
  335.           if ((OK_TO_SEARCH || verbose || internal_error_1 || internal_error_2)
  336.               && !invalid_pattern)
  337.         {
  338.               if (memcmp (old_buf.buffer, nonconst_buf.buffer, 
  339.                       nonconst_buf.used) != 0
  340.                   && !invalid_pattern)
  341.                 {
  342.                   printf("  (%s)\n", r ? r : "Valid regular expression");
  343.               printf ("\n  Compiled pattern before matching: ");
  344.               print_compiled_pattern (&old_buf);
  345.                   printf ("\n  Compiled pattern after matching:  ");
  346.         }
  347.           else
  348.         printf ("\n  Compiled pattern:   ");
  349.                 
  350.               print_compiled_pattern (&nonconst_buf);
  351.             }
  352.  
  353.           if (correct_fastmap && (!nonconst_buf.fastmap_accurate || verbose))
  354.         {
  355.           printf ("\n  The fastmap should have been: "); 
  356.               print_fastmap (correct_fastmap);
  357.  
  358.               printf ("\n  Fastmap: "); 
  359.           print_fastmap (fastmap);
  360.  
  361.               printf ("\n  Compiled pattern before matching: ");
  362.               print_compiled_pattern (&nonconst_buf);
  363.             }
  364.  
  365.           if ((!all_regs_correct || verbose) && correct_regs)
  366.             {
  367.               unsigned this_reg;
  368.           printf ("\n  Incorrect registers:");
  369.               
  370.           for (this_reg = 0; this_reg < regs.num_regs; this_reg++)
  371.                 {
  372.                   if (!regs_correct[this_reg])
  373.             {
  374.                         printf ("\n    Register %d's start was %2d.  ", this_reg,
  375.                                 regs.start[this_reg]);
  376.               printf ("\tIt should have been %d.\n", 
  377.                         correct_regs->start[this_reg]);
  378.                         printf ("    Register %d's end was   %2d.  ", this_reg,
  379.                                 regs.end[this_reg]);
  380.               printf ("\tIt should have been %d.\n", 
  381.                         correct_regs->end[this_reg]);
  382.                     }
  383.                 }
  384.             }
  385.         }
  386.     }
  387.     
  388.   if (nonconst_buf.buffer != NULL)
  389.     free (nonconst_buf.buffer);
  390.  
  391.   if (OK_TO_SEARCH)
  392.     {
  393.       free (old_buf.buffer);
  394.  
  395.       if (correct_regs)
  396.         free (regs_correct);
  397.  
  398.     }
  399.     
  400.   nonconst_buf.buffer = old_buf.buffer = NULL;
  401.   regs_correct = NULL;
  402.   regs.start = regs.end = NULL;
  403.  
  404. } /* general_test */
  405.  
  406.  
  407. void
  408. test_search_return (match_start_wanted, pattern, string)
  409.     int match_start_wanted;
  410.     const char *pattern;
  411.     char *string;
  412. {
  413.   struct re_pattern_buffer buf;
  414.   char fastmap[1 << BYTEWIDTH];
  415.   const char *compile_return;
  416.   int match_start;
  417.   static num_times_called = 0;
  418.  
  419.   num_times_called++;
  420.   buf.allocated = 1;
  421.   buf.buffer = xmalloc (buf.allocated);
  422.     
  423.   assert (pattern != NULL);
  424.   buf.translate = 0;
  425.   compile_return = re_compile_pattern (pattern, strlen (pattern), &buf);
  426.   
  427.   if (compile_return)
  428.     {
  429.       printf ("\n\nInvalid pattern in test_match_start:\n");
  430.       printf ("%s\n", compile_return);
  431.     }
  432.   else 
  433.     {
  434.       buf.fastmap = fastmap;
  435.       match_start = re_search (&buf, string, strlen (string),
  436.                                0, strlen (string), 0);
  437.  
  438.       if (match_start != match_start_wanted)
  439.     printf ("\nWanted search to start at %d but started at %d.\n",
  440.              match_start, match_start_wanted);
  441.     }
  442.   free (buf.buffer);
  443.   buf.buffer = NULL;
  444. }
  445.  
  446.  
  447. #define SET_FASTMAP()                            \
  448.   {                                    \
  449.     unsigned this_char;                            \
  450.                                      \
  451.     memset (correct_fastmap, invert, (1 << BYTEWIDTH));            \
  452.                                      \
  453.     for (this_char = 0; this_char < strlen (fastmap_string); this_char++)\
  454.       correct_fastmap[fastmap_string[this_char]] = !invert;        \
  455.     correct_fastmap['\n'] = match_newline;                \
  456.   }                    
  457.   
  458.  
  459. void
  460. test_fastmap (pat, fastmap_string, invert, match_newline)
  461.     const char *pat;
  462.     char *fastmap_string;
  463.     unsigned invert;
  464.     unsigned match_newline;
  465. {
  466.   char correct_fastmap[(1 << BYTEWIDTH)];                
  467.  
  468.   SET_FASTMAP ();
  469.   general_test (1, 0, pat, NULL, NULL, -1, 0, -1, correct_fastmap, 0, -1);
  470. }
  471.  
  472.  
  473. void
  474. test_fastmap_search (pat, str, fastmap_string, invert, match_newline, 
  475.                      can_be_null, start0, end0)
  476.     const char *pat; 
  477.     char *str; 
  478.     char *fastmap_string;
  479.     unsigned invert; 
  480.     unsigned match_newline; 
  481.     int can_be_null; 
  482.     int start0; 
  483.     int end0;
  484. {
  485.   char correct_fastmap[(1 << BYTEWIDTH)];                
  486.   struct re_registers correct_regs;
  487.  
  488.   correct_regs.num_regs = RE_NREGS;
  489.   correct_regs.start = (int *) xmalloc (RE_NREGS * sizeof (int));
  490.   correct_regs.end = (int *) xmalloc (RE_NREGS * sizeof (int));
  491.   
  492.   set_all_registers (start0, end0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
  493.                      -1, -1, -1, -1, -1, -1, -1, -1, &correct_regs);
  494.   SET_FASTMAP ();
  495.   general_test (1, 0, pat, str, NULL, 0, SAFE_STRLEN (str), SAFE_STRLEN (str),
  496.         correct_fastmap, &correct_regs, can_be_null);
  497.  
  498.   free (correct_regs.start);
  499.   free (correct_regs.end);
  500. }
  501.  
  502.  
  503.  
  504.  
  505. void
  506. test_all_registers (pat, str1, str2, 
  507.             start0, end0, start1, end1, 
  508.                     start2, end2, start3, end3, 
  509.                     start4, end4, start5, end5, 
  510.                     start6, end6, start7, end7, 
  511.                     start8, end8, start9, end9)
  512.   char *pat; char *str1; char *str2; 
  513.   int start0; int end0; int start1; int end1; 
  514.   int start2; int end2; int start3; int end3; 
  515.   int start4; int end4; int start5; int end5; 
  516.   int start6; int end6; int start7; int end7; 
  517.   int start8; int end8; int start9; int end9;
  518. {
  519.   struct re_registers correct_regs;
  520.   
  521.   if (omit_register_tests) return;
  522.   
  523.   correct_regs.num_regs = RE_NREGS;
  524.   correct_regs.start = (int *) xmalloc (RE_NREGS * sizeof (int));
  525.   correct_regs.end = (int *) xmalloc (RE_NREGS * sizeof (int));
  526.  
  527.   set_all_registers (start0, end0, start1, end1, start2, end2, start3, end3, 
  528.                      start4, end4, start5, end5, start6, end6, start7, end7, 
  529.                      start8, end8, start9, end9, &correct_regs);
  530.  
  531.   general_test (1, 0, pat, str1, str2, 0, 
  532.             SAFE_STRLEN (str1) + SAFE_STRLEN (str2), 
  533.         SAFE_STRLEN (str1) + SAFE_STRLEN (str2), 
  534.                 NULL, &correct_regs, -1);
  535.  
  536.   free (correct_regs.start);
  537.   free (correct_regs.end);
  538. }
  539.  
  540.  
  541. void
  542. invalid_pattern (error_code_expected, pattern)
  543.   int error_code_expected;
  544.   char *pattern;
  545. {
  546.   regex_t pattern_buffer;
  547.   int cflags
  548.     = re_syntax_options == RE_SYNTAX_POSIX_EXTENDED
  549.       || re_syntax_options == RE_SYNTAX_POSIX_MINIMAL_EXTENDED
  550.       ? REG_EXTENDED : 0; 
  551.   
  552.   test_compile (0, error_code_expected, pattern, &pattern_buffer, cflags);
  553. }
  554.  
  555.  
  556. void
  557. valid_pattern (pattern)
  558.   char *pattern;
  559. {
  560.   regex_t pattern_buffer;
  561.   int cflags
  562.     = re_syntax_options == RE_SYNTAX_POSIX_EXTENDED
  563.       || re_syntax_options == RE_SYNTAX_POSIX_MINIMAL_EXTENDED
  564.       ? REG_EXTENDED : 0; 
  565.   
  566.   test_compile (1, 0, pattern, &pattern_buffer, cflags);
  567. }
  568.  
  569.  
  570. char *
  571. delimiters_to_ops (source, left_delimiter, right_delimiter)
  572.   char *source;
  573.   char left_delimiter;
  574.   char right_delimiter;
  575. {
  576.   static char *answer = NULL;
  577.   char *tmp = NULL;
  578.   boolean double_size = false;
  579.   unsigned source_char;
  580.   unsigned answer_char = 0;
  581.   
  582.   assert (source != NULL);
  583.  
  584.   switch (left_delimiter)
  585.     {
  586.       case '(': if (!(re_syntax_options & RE_NO_BK_PARENS))
  587.               double_size = true;
  588.                    break;
  589.       case '{': if (!(re_syntax_options & RE_NO_BK_BRACES))
  590.               double_size = true;
  591.                    break;
  592.       default: printf ("Found strange delimiter %c in delimiter_to_ops.\n",
  593.                 left_delimiter);
  594.            printf ("The source was `%s'\n", source);
  595.            exit (0);
  596.     }
  597.  
  598.   if (answer == source)
  599.     {
  600.       tmp = (char *) xmalloc (strlen (source) + 1);
  601.       strcpy (tmp, source);
  602.       source = tmp;
  603.     }
  604.  
  605.   if (answer)
  606.     {
  607.       free (answer);
  608.       answer = NULL;
  609.     }
  610.  
  611.   answer = (char *) xmalloc ((double_size 
  612.                      ? strlen (source) << 1
  613.                              : strlen (source))
  614.                             + 1);
  615.   if (!double_size)
  616.     strcpy (answer, source);
  617.   else
  618.     {
  619.       for (source_char = 0; source_char < strlen (source); source_char++)
  620.         {
  621.           if (source[source_char] == left_delimiter 
  622.           || source[source_char] == right_delimiter)
  623.             answer[answer_char++] = '\\';
  624.  
  625.           answer[answer_char++] = source[source_char];
  626.         }
  627.       answer[answer_char] = 0;
  628.     }
  629.     
  630.   return answer;
  631. }
  632.  
  633.  
  634. void
  635. print_pattern_info (pattern, pattern_buffer_ptr)
  636.   const char *pattern; 
  637.   regex_t *pattern_buffer_ptr;
  638. {
  639.   printf ("  Pattern:  `%s'.\n", pattern);
  640.   printf ("  Compiled pattern:  ");
  641.   print_compiled_pattern (pattern_buffer_ptr);
  642. }
  643.  
  644.  
  645. void
  646. valid_nonposix_pattern (pattern)
  647.   char *pattern;
  648. {
  649.   struct re_pattern_buffer nonconst_buf;
  650.   
  651.   nonconst_buf.allocated = 0;
  652.   nonconst_buf.buffer = NULL;
  653.   nonconst_buf.translate = NULL;
  654.   
  655.   assert (pattern != NULL);
  656.   
  657.   if (re_compile_pattern (pattern, strlen (pattern), &nonconst_buf))
  658.     {
  659.       printf ("Couldn't compile the pattern.\n");
  660.       print_pattern_info (pattern, &nonconst_buf);
  661.     }
  662. }
  663.  
  664.  
  665. void
  666. compile_and_print_pattern (pattern)
  667.   char *pattern;
  668. {
  669.   struct re_pattern_buffer nonconst_buf;
  670.   
  671.   nonconst_buf.allocated = 0;
  672.   nonconst_buf.buffer = NULL;
  673.   
  674.   if (re_compile_pattern (pattern, strlen (pattern), &nonconst_buf))
  675.     printf ("Couldn't compile the pattern.\n");
  676.     
  677.   print_pattern_info (pattern, &nonconst_buf);
  678. }
  679.  
  680.  
  681. void
  682. test_case_fold (pattern, string)
  683.   const char *pattern;
  684.   char* string;
  685. {
  686.   struct re_pattern_buffer nonconst_buf;
  687.   const char *ret;
  688.  
  689.   init_pattern_buffer (&nonconst_buf);
  690.   nonconst_buf.translate = upcase;
  691.  
  692.   assert (pattern != NULL);
  693.   ret = re_compile_pattern (pattern, strlen (pattern), &nonconst_buf);
  694.   
  695.   if (ret)
  696.     {
  697.       printf ("\nShould have been a valid pattern but wasn't.\n");
  698.       print_pattern_info (pattern, &nonconst_buf);
  699.     }
  700.   else
  701.     {
  702.       if (test_should_match 
  703.           && re_match (&nonconst_buf, string, strlen (string), 0, 0) 
  704.              != strlen (string))
  705.        {
  706.          printf ("Match failed for case fold.\n");
  707.          printf ("  Pattern:  `%s'.\n", pattern);
  708.          printf ("  String: `%s'.\n", string == NULL ? "NULL" : string);
  709.        }
  710.     }
  711. }
  712.  
  713.  
  714. void
  715. test_match_n_times (n, pattern, string)
  716.   unsigned n;
  717.   char* pattern;
  718.   char* string;
  719. {
  720.   struct re_pattern_buffer buf;
  721.   const char *r;
  722.   unsigned match = 0;
  723.   unsigned this_match;
  724.  
  725.   buf.allocated = 0;
  726.   buf.buffer = NULL;
  727.   buf.translate = 0;
  728.  
  729.   assert (pattern != NULL);
  730.   
  731.   r = re_compile_pattern (pattern, strlen (pattern), &buf);
  732.   if (r)
  733.     {
  734.       printf ("Didn't compile.\n");
  735.       printf ("  Pattern: %s.\n", pattern);
  736.     }
  737.   else
  738.     {
  739.       for (this_match = 1; this_match <= n; this_match++)
  740.         match = (re_match (&buf, string, strlen (string),
  741.                            0, 0) 
  742.              == strlen (string));
  743.  
  744.       if (match && !test_should_match)
  745.           printf ("\n\nMatched but shouldn't have:\n");
  746.       else if (!match && test_should_match)
  747.           printf ("\n\nDidn't match but should have:\n");
  748.       
  749.       if ((match && !test_should_match) || (!match && test_should_match))
  750.         {
  751.       printf("  The string to match was:  ");
  752.           if (string)
  753.             printf ("`%s' and ", string);
  754.           else
  755.             printf ("`'");
  756.     
  757.           printf ("  Pattern: %s.\n", pattern);
  758.           printf ("  Compiled pattern: %s.\n", pattern);
  759.       print_compiled_pattern (&buf);
  760.         }
  761.     }
  762. }
  763.  
  764.  
  765. void 
  766. test_match_2 (pat, str1, str2)
  767.   const char *pat; 
  768.   char *str1; 
  769.   char *str2;
  770. {
  771.   general_test (1, 1, pat, str1, str2, 0, 1,
  772.           SAFE_STRLEN (str1) + SAFE_STRLEN (str2), NULL, 0, -1);
  773. }
  774.  
  775. void 
  776. test_match (pat, str)
  777.   const char *pat;
  778.   char *str;
  779. {
  780.   test_match_2 (pat, str, NULL);
  781.   test_match_2 (pat, NULL, str);
  782. }
  783.