1 /* test.c: testing routines for regex.c. */
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
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))
24 #define strrchr rindex
26 #define memcmp(s1, s2, n) bcmp ((s1), (s2), (n))
27 #define memcpy(to, from, len) bcopy ((from), (to), (len))
29 extern char *strtok ();
30 extern char *strstr ();
31 #endif /* not USG or STDC_HEADERS */
33 /* SunOS 4.1 declares memchr in <memory.h>, not <string.h>. I don't
43 extern void print_partial_compiled_pattern ();
44 extern void print_compiled_pattern ();
45 extern void print_double_string ();
47 /* If nonzero, the results of every test are displayed. */
48 boolean verbose = false;
50 /* If nonzero, don't do register testing. */
51 boolean omit_register_tests = true;
53 /* Says whether the current test should match or fail to match. */
54 boolean test_should_match;
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)
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;
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++)
93 /* Return the concatenation of S1 and S2. This would be a prime place
101 char *answer = xmalloc (strlen (s1) + strlen (s2) + 1);
110 #define OK_TO_SEARCH (nonconst_buf.fastmap_accurate && (str1 || str2))
112 /* We ignore the `can_be_null' argument. Should just be removed. */
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;
122 int start, range, end;
123 char *correct_fastmap;
124 struct re_registers *correct_regs;
127 struct re_pattern_buffer nonconst_buf;
128 struct re_pattern_buffer old_buf;
129 struct re_registers regs;
131 char fastmap[1 << BYTEWIDTH];
132 unsigned *regs_correct = NULL;
133 unsigned all_regs_correct = 1;
134 boolean fastmap_internal_error = false;
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;
143 nonconst_buf.allocated = 8;
144 nonconst_buf.buffer = xmalloc (nonconst_buf.allocated);
145 nonconst_buf.fastmap = fastmap;
146 nonconst_buf.translate = 0;
148 assert (pat != NULL);
149 r = re_compile_pattern (pat, strlen (pat), &nonconst_buf);
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
157 if (t == posix_basic_test || t == posix_extended_test)
158 nonconst_buf.newline_anchor = 0;
160 invalid_pattern = r != NULL;
166 if (!pattern_should_be_valid)
167 printf ("\nShould have been an invalid pattern but wasn't:\n");
170 fastmap_internal_error = (re_compile_fastmap (&nonconst_buf) == -2);
173 nonconst_buf.fastmap_accurate =
174 memcmp (nonconst_buf.fastmap, correct_fastmap, 1 << BYTEWIDTH)
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);
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)
188 char *the_str = str1 == NULL ? str2 : str1;
192 ? (r = re_match (&nonconst_buf, the_str,
193 strlen (the_str), start, ®s))
195 : (r = re_search (&nonconst_buf,
196 the_str, strlen (the_str),
197 start, range, ®s))
201 internal_error_1 = true;
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)
211 r = re_match_2 (&nonconst_buf,
212 str1, SAFE_STRLEN (str1),
213 str2, SAFE_STRLEN (str2),
215 match_2 = r == SAFE_STRLEN (str1) + SAFE_STRLEN (str2);
219 r = re_search_2 (&nonconst_buf,
220 str1, SAFE_STRLEN (str1),
221 str2, SAFE_STRLEN (str2),
222 start, range, ®s, end);
227 internal_error_2 = true;
229 match = match_1 & match_2;
234 if (regs_correct != NULL)
238 = (unsigned *) xmalloc (regs.num_regs * sizeof (unsigned));
241 reg < regs.num_regs && reg < correct_regs->num_regs;
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])
262 all_regs_correct &= regs_correct[reg];
269 if (fastmap_internal_error)
270 printf ("\n\nInternal error in re_compile_fastmap:");
272 if (internal_error_1)
274 if (!fastmap_internal_error)
277 printf ("\nInternal error in re_match or re_search:");
280 if (internal_error_2)
282 if (!internal_error_1)
285 printf ("\nInternal error in re_match_2 or re_search_2:");
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
293 || !pattern_should_be_valid
294 || internal_error_1 || internal_error_2
297 if (OK_TO_SEARCH && match && !test_should_match)
299 printf ("\n\nMatched but shouldn't have:\n");
301 printf ("The single match/search succeeded.\n");
304 printf ("The double match/search succeeded.\n");
306 else if (OK_TO_SEARCH && !match && test_should_match)
308 printf ("\n\nDidn't match but should have:\n");
310 printf ("The single match/search failed.\n");
313 printf ("The double match/search failed.\n");
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");
322 printf ("\n\nTest was OK:\n");
325 if ((!(invalid_pattern && !pattern_should_be_valid)) || verbose)
326 printf (" Pattern: `%s'.\n", pat);
328 if (pattern_should_be_valid || verbose
329 || internal_error_1 || internal_error_2)
331 printf(" Strings: ");
332 printf ("`%s' and ", str1 == NULL ? "NULL" : str1);
333 printf ("`%s'.\n", str2 == NULL ? "NULL" : str2);
335 if ((OK_TO_SEARCH || verbose || internal_error_1 || internal_error_2)
338 if (memcmp (old_buf.buffer, nonconst_buf.buffer,
339 nonconst_buf.used) != 0
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: ");
348 printf ("\n Compiled pattern: ");
350 print_compiled_pattern (&nonconst_buf);
353 if (correct_fastmap && (!nonconst_buf.fastmap_accurate || verbose))
355 printf ("\n The fastmap should have been: ");
356 print_fastmap (correct_fastmap);
358 printf ("\n Fastmap: ");
359 print_fastmap (fastmap);
361 printf ("\n Compiled pattern before matching: ");
362 print_compiled_pattern (&nonconst_buf);
365 if ((!all_regs_correct || verbose) && correct_regs)
368 printf ("\n Incorrect registers:");
370 for (this_reg = 0; this_reg < regs.num_regs; this_reg++)
372 if (!regs_correct[this_reg])
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,
380 printf ("\tIt should have been %d.\n",
381 correct_regs->end[this_reg]);
388 if (nonconst_buf.buffer != NULL)
389 free (nonconst_buf.buffer);
393 free (old_buf.buffer);
400 nonconst_buf.buffer = old_buf.buffer = NULL;
402 regs.start = regs.end = NULL;
408 test_search_return (match_start_wanted, pattern, string)
409 int match_start_wanted;
413 struct re_pattern_buffer buf;
414 char fastmap[1 << BYTEWIDTH];
415 const char *compile_return;
417 static num_times_called = 0;
421 buf.buffer = xmalloc (buf.allocated);
423 assert (pattern != NULL);
425 compile_return = re_compile_pattern (pattern, strlen (pattern), &buf);
429 printf ("\n\nInvalid pattern in test_match_start:\n");
430 printf ("%s\n", compile_return);
434 buf.fastmap = fastmap;
435 match_start = re_search (&buf, string, strlen (string),
436 0, strlen (string), 0);
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);
447 #define SET_FASTMAP() \
449 unsigned this_char; \
451 memset (correct_fastmap, invert, (1 << BYTEWIDTH)); \
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; \
460 test_fastmap (pat, fastmap_string, invert, match_newline)
462 char *fastmap_string;
464 unsigned match_newline;
466 char correct_fastmap[(1 << BYTEWIDTH)];
469 general_test (1, 0, pat, NULL, NULL, -1, 0, -1, correct_fastmap, 0, -1);
474 test_fastmap_search (pat, str, fastmap_string, invert, match_newline,
475 can_be_null, start0, end0)
478 char *fastmap_string;
480 unsigned match_newline;
485 char correct_fastmap[(1 << BYTEWIDTH)];
486 struct re_registers correct_regs;
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));
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);
495 general_test (1, 0, pat, str, NULL, 0, SAFE_STRLEN (str), SAFE_STRLEN (str),
496 correct_fastmap, &correct_regs, can_be_null);
498 free (correct_regs.start);
499 free (correct_regs.end);
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;
519 struct re_registers correct_regs;
521 if (omit_register_tests) return;
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));
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);
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);
536 free (correct_regs.start);
537 free (correct_regs.end);
542 invalid_pattern (error_code_expected, pattern)
543 int error_code_expected;
546 regex_t pattern_buffer;
548 = re_syntax_options == RE_SYNTAX_POSIX_EXTENDED
549 || re_syntax_options == RE_SYNTAX_POSIX_MINIMAL_EXTENDED
552 test_compile (0, error_code_expected, pattern, &pattern_buffer, cflags);
557 valid_pattern (pattern)
560 regex_t pattern_buffer;
562 = re_syntax_options == RE_SYNTAX_POSIX_EXTENDED
563 || re_syntax_options == RE_SYNTAX_POSIX_MINIMAL_EXTENDED
566 test_compile (1, 0, pattern, &pattern_buffer, cflags);
571 delimiters_to_ops (source, left_delimiter, right_delimiter)
574 char right_delimiter;
576 static char *answer = NULL;
578 boolean double_size = false;
579 unsigned source_char;
580 unsigned answer_char = 0;
582 assert (source != NULL);
584 switch (left_delimiter)
586 case '(': if (!(re_syntax_options & RE_NO_BK_PARENS))
589 case '{': if (!(re_syntax_options & RE_NO_BK_BRACES))
592 default: printf ("Found strange delimiter %c in delimiter_to_ops.\n",
594 printf ("The source was `%s'\n", source);
598 if (answer == source)
600 tmp = (char *) xmalloc (strlen (source) + 1);
601 strcpy (tmp, source);
611 answer = (char *) xmalloc ((double_size
612 ? strlen (source) << 1
616 strcpy (answer, source);
619 for (source_char = 0; source_char < strlen (source); source_char++)
621 if (source[source_char] == left_delimiter
622 || source[source_char] == right_delimiter)
623 answer[answer_char++] = '\\';
625 answer[answer_char++] = source[source_char];
627 answer[answer_char] = 0;
635 print_pattern_info (pattern, pattern_buffer_ptr)
637 regex_t *pattern_buffer_ptr;
639 printf (" Pattern: `%s'.\n", pattern);
640 printf (" Compiled pattern: ");
641 print_compiled_pattern (pattern_buffer_ptr);
646 valid_nonposix_pattern (pattern)
649 struct re_pattern_buffer nonconst_buf;
651 nonconst_buf.allocated = 0;
652 nonconst_buf.buffer = NULL;
653 nonconst_buf.translate = NULL;
655 assert (pattern != NULL);
657 if (re_compile_pattern (pattern, strlen (pattern), &nonconst_buf))
659 printf ("Couldn't compile the pattern.\n");
660 print_pattern_info (pattern, &nonconst_buf);
666 compile_and_print_pattern (pattern)
669 struct re_pattern_buffer nonconst_buf;
671 nonconst_buf.allocated = 0;
672 nonconst_buf.buffer = NULL;
674 if (re_compile_pattern (pattern, strlen (pattern), &nonconst_buf))
675 printf ("Couldn't compile the pattern.\n");
677 print_pattern_info (pattern, &nonconst_buf);
682 test_case_fold (pattern, string)
686 struct re_pattern_buffer nonconst_buf;
689 init_pattern_buffer (&nonconst_buf);
690 nonconst_buf.translate = upcase;
692 assert (pattern != NULL);
693 ret = re_compile_pattern (pattern, strlen (pattern), &nonconst_buf);
697 printf ("\nShould have been a valid pattern but wasn't.\n");
698 print_pattern_info (pattern, &nonconst_buf);
702 if (test_should_match
703 && re_match (&nonconst_buf, string, strlen (string), 0, 0)
706 printf ("Match failed for case fold.\n");
707 printf (" Pattern: `%s'.\n", pattern);
708 printf (" String: `%s'.\n", string == NULL ? "NULL" : string);
715 test_match_n_times (n, pattern, string)
720 struct re_pattern_buffer buf;
729 assert (pattern != NULL);
731 r = re_compile_pattern (pattern, strlen (pattern), &buf);
734 printf ("Didn't compile.\n");
735 printf (" Pattern: %s.\n", pattern);
739 for (this_match = 1; this_match <= n; this_match++)
740 match = (re_match (&buf, string, strlen (string),
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");
749 if ((match && !test_should_match) || (!match && test_should_match))
751 printf(" The string to match was: ");
753 printf ("`%s' and ", string);
757 printf (" Pattern: %s.\n", pattern);
758 printf (" Compiled pattern: %s.\n", pattern);
759 print_compiled_pattern (&buf);
766 test_match_2 (pat, str1, str2)
771 general_test (1, 1, pat, str1, str2, 0, 1,
772 SAFE_STRLEN (str1) + SAFE_STRLEN (str2), NULL, 0, -1);
776 test_match (pat, str)
780 test_match_2 (pat, str, NULL);
781 test_match_2 (pat, NULL, str);