3 # The tests are in a separate file 't/re/re_tests'.
4 # Each line in that file is a separate test.
5 # There are five columns, separated by tabs.
6 # An optional sixth column is used to give a reason, only when skipping tests
8 # Column 1 contains the pattern, optionally enclosed in C<''> C<::> or
9 # C<//>. Modifiers can be put after the closing delimiter. C<''> will
10 # automatically be added to any other patterns.
12 # Column 2 contains the string to be matched.
14 # Column 3 contains the expected result:
18 # T the test is a TODO (can be combined with y/n/c)
19 # M skip test on miniperl (combine with y/n/c/T)
20 # B test exposes a known bug in Perl, should be skipped
21 # b test exposes a known bug in Perl, should be skipped if noamp
22 # t test exposes a bug with threading, TODO if qr_embed_thr
23 # s test should only be run for regex_sets_compat.t
24 # S test should not be run for regex_sets_compat.t
25 # a test should only be run on ASCII platforms
26 # e test should only be run on EBCDIC platforms
28 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
30 # Column 4 contains a string, usually C<$&>.
32 # Column 5 contains the expected result of double-quote
33 # interpolating that string after the match, or start of error message.
35 # Column 6, if present, contains a reason why the test is skipped.
36 # This is printed with "skipped", for harness to pick up.
38 # Column 7 can be used for comments
40 # \n in the tests are interpolated, as are variables of the form ${\w+}.
42 # Blanks lines are treated as PASSING tests to keep the line numbers
43 # linked to the test number.
45 # If you want to add a regular expression test that can't be expressed
46 # in this format, don't add it here: put it in re/pat.t instead.
48 # Note that the inputs get passed on as "m're'", so the re bypasses the lexer.
49 # This means this file cannot be used for testing anything that the lexer
50 # handles; in 5.12 this means just \N{NAME} and \N{U+...}.
52 # Note that columns 2,3 and 5 are all enclosed in double quotes and then
53 # evalled; so something like a\"\x{100}$1 has length 3+length($1).
55 # \x... and \o{...} constants are automatically converted to the native
56 # character set if necessary. \[0-7] constants aren't
60 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
62 # Do this open before any chdir
65 open TESTS, $file or die "Can't open $file";
69 @INC = qw '../lib ../ext/re';
70 if (!defined &DynaLoader::boot_DynaLoader) { # miniperl
71 print("1..0 # Skip Unicode tables not built yet\n"), exit
72 unless eval 'require "unicore/Heavy.pl"';
75 # Some of the tests need a locale; which one doesn't much matter, except
76 # that it be valid. Make sure of that
78 POSIX->import(qw(LC_ALL setlocale));
79 POSIX::setlocale(&LC_ALL, "C");
84 return map { /^#/ ? "$_\n" : "# $_\n" }
85 map { split /\n/ } @_;
88 sub convert_from_ascii {
92 # Convert \x{...}, \o{...}
93 $string =~ s/ (?<! \\ ) \\x\{ ( .*? ) } / "\\x{" . sprintf("%X", utf8::unicode_to_native(hex $1)) . "}" /gex;
94 $string =~ s/ (?<! \\ ) \\o\{ ( .*? ) } / "\\o{" . sprintf("%o", utf8::unicode_to_native(oct $1)) . "}" /gex;
97 $string =~ s/ (?<! \\ ) \\x ( [A-Fa-f0-9]{2} ) / "\\x" . sprintf("%02X", utf8::unicode_to_native(hex $1)) /gex;
100 $string =~ s/ (?<! \\ ) \\x ( [A-Fa-f0-9] ) (?! [A-Fa-f0-9] ) / "\\x" . sprintf("%X", utf8::unicode_to_native(hex $1)) /gex;
102 #print STDERR __LINE__, ": $save\n$string\n" if $save ne $string;
107 use warnings FATAL=>"all";
108 no warnings 'experimental::vlb';
109 our ($bang, $ffff, $nulnul); # used by the tests
110 our ($qr, $skip_amp, $qr_embed, $qr_embed_thr, $regex_sets, $alpha_assertions, $no_null); # set by our callers
112 my $expanded_text = "expanded name from original test number";
113 my $expanded_text_re = qr/$expanded_text/;
115 if (!defined $file) {
116 open TESTS, 're/re_tests' or die "Can't open re/re_tests: $!";
123 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
124 $ffff = chr(0xff) x 2;
126 my $OP = $qr ? 'qr' : 'm';
134 if (!/\S/ || /^\s*#/ || /^__END__$/) {
136 my ($not,$comment)= split /\s*#\s*/, $_, 2;
137 $comment ||= "(blank line)";
138 print "ok $test # $comment\n";
142 s/\\n/\n/g unless $regex_sets;
143 my ($pat, $subject, $result, $repl, $expect, $reason, $comment) = split(/\t/,$_,7);
144 $comment = "" unless defined $comment;
145 if (!defined $subject) {
146 die "Bad test definition on line $test: $_\n";
148 $reason = '' unless defined $reason;
149 my $input = join(':',$pat,$subject,$result,$repl,$expect);
151 # the double '' below keeps simple syntax highlighters from going crazy
152 $pat = "'$pat'" unless $pat =~ /^[:''\/]/;
153 $pat =~ s/(\$\{\w+\})/$1/eeg;
154 $pat =~ s/\\n/\n/g unless $regex_sets;
155 $pat = convert_from_ascii($pat) if ord("A") != 65;
158 if ($no_null && $pat =~ /^'(.*)'\z/) {
159 $no_null_pat = XS::APItest::string_without_null($1);
162 $subject = convert_from_ascii($subject) if ord("A") != 65;
163 $subject = eval qq("$subject"); die $@ if $@;
165 $expect = convert_from_ascii($expect) if ord("A") != 65;
166 $expect = eval qq("$expect"); die $@ if $@;
167 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
169 my $todo_qr = $qr_embed_thr && ($result =~ s/t//);
170 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
171 ++$skip if $result =~ s/M// && !defined &DynaLoader::boot_DynaLoader;
172 if ($result =~ s/ ( [Ss] ) //x) {
173 if (($1 eq 'S' && $regex_sets) || ($1 eq 's' && ! $regex_sets)) {
175 $reason = "Test not valid for $0";
178 if ($result =~ s/a// && ord("A") != 65) {
180 $reason = "Test is only valid for ASCII platforms. $reason";
182 if ($result =~ s/e// && ord("A") != 193) {
184 $reason = "Test is only valid for EBCDIC platforms. $reason";
186 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
187 $result =~ s/B//i unless $skip;
188 my $todo= $result =~ s/T// ? " # TODO" : "";
191 $comment=~s/^\s*(?:#\s*)?//;
192 $testname .= " - $comment" if $comment;
194 if (! $skip && $alpha_assertions) {
195 my $assertions_re = qr/ (?: \Q(?\E (?: > | <? [=>] ) ) /x;
196 if ($pat !~ $assertions_re && $comment !~ $expanded_text_re) {
198 $reason = "Pattern doesn't contain assertions";
200 elsif ($comment !~ $expanded_text_re) {
201 my $expanded_pat = $pat;
203 $pat =~ s/\( \? > /(*atomic:/xg;
205 if ($pat =~ s/\( \? = /(*pla:/xg) {
206 $expanded_pat =~ s//(*positive_lookahead:/g;
208 if ($pat =~ s/\( \? ! /(*nla:/xg) {
209 $expanded_pat =~ s//(*negative_lookahead:/g;
211 if ($pat =~ s/\( \? <= /(*plb:/xg) {
212 $expanded_pat =~ s//(*positive_lookbehind:/g;
214 if ($pat =~ s/\( \? <! /(*nlb:/xg) {
215 $expanded_pat =~ s//(*negative_lookbehind:/g;
217 if ($expanded_pat ne $pat) {
218 $comment .= " $expanded_text $test";
219 push @tests, join "\t", $expanded_pat,
229 elsif (! $skip && $regex_sets) {
231 # If testing regex sets, change the [bracketed] classes into
232 # (?[bracketed]). But note that '\[' and '\c[' don't introduce such a
233 # class. (We don't bother looking for an odd number of backslashes,
234 # as this hasn't been needed so far.)
235 if ($pat !~ / (?<!\\c) (?<!\\) \[ /x) {
237 $reason = "Pattern doesn't contain [brackets]";
239 else { # Use non-regex features of Perl to accomplish this.
243 # Go through the pattern character-by-character. We also add
244 # blanks around each token to test the /x parts of (?[ ])
245 my $pat_len = length($pat);
246 CHAR: for (my $i = 0; $i < $pat_len; $i++) {
247 my $curchar = substr($pat, $i, 1);
248 if ($curchar eq '\\') {
249 $modified .= " " if $in_brackets;
250 $modified .= $curchar;
253 # Get the character the backslash is escaping
254 $curchar = substr($pat, $i, 1);
255 $modified .= $curchar;
257 # If the character following that is a '{}', treat the
258 # entire amount as a single token
259 if ($i < $pat_len -1 && substr($pat, $i+1, 1) eq '{') {
260 my $j = index($pat, '}', $i+2);
262 last unless $in_brackets;
263 if ($result eq 'c') {
265 $reason = "Can't handle compilation errors with unmatched '{'";
268 print "not ok $testname # Problem in $0; original = '$pat'; mod = '$modified'\n";
272 $modified .= substr($pat, $i+1, $j - $i);
275 elsif ($curchar eq 'x') {
277 # \x without brackets is supposed to be followed by 2
278 # hex digits. Take up to 2, and then add a blank
279 # after the last one. This avoids getting errors from
280 # (?[ ]) for run-ons, like \xabc
282 for (; $j < $i + 3 && $j < $pat_len; $j++) {
283 my $curord = ord(substr($pat, $j, 1));
284 if (!(($curord >= ord("A") && $curord <= ord("F"))
285 || ($curord >= ord("a") && $curord <= ord("f"))
286 || ($curord >= ord("0") && $curord <= ord("9"))))
293 $modified .= substr($pat, $i + 1, $j - $i);
294 $modified .= " " if $in_brackets;
297 elsif (ord($curchar) >= ord('0')
298 && (ord($curchar) <= ord('7')))
300 # Similarly, octal constants have up to 3 digits.
302 for (; $j < $i + 3 && $j < $pat_len; $j++) {
303 my $curord = ord(substr($pat, $j, 1));
304 if (! ($curord >= ord("0") && $curord <= ord("7"))) {
310 $modified .= substr($pat, $i + 1, $j - $i);
315 } # End of processing a backslash sequence
317 if (! $in_brackets # Skip (?{ })
320 && substr($pat, $i+1, 1) eq '?'
321 && substr($pat, $i+2, 1) eq '{')
324 $reason = "Pattern contains '(?{'";
329 if ($curchar eq ']' && $in_brackets) {
330 $modified .= " ] ])";
335 # A regular character.
336 if ($curchar ne '[') {
337 $modified .= " " if $in_brackets;
338 $modified .= $curchar;
342 # Here is a '['; If not in a bracketed class, treat as the
344 if (! $in_brackets) {
346 $modified .= "(?[ [ ";
348 # An immediately following ']' or '^]' is not the ending
349 # of the class, but is to be treated literally.
350 if ($i < $pat_len - 1
351 && substr($pat, $i+1, 1) eq ']')
356 elsif ($i < $pat_len - 2
357 && substr($pat, $i+1, 1) eq '^'
358 && substr($pat, $i+2, 1) eq ']')
361 $modified .= " ^ ] ";
366 # Here is a plain '[' within [ ]. Could mean wants to
367 # match a '[', or it could be a posix class that has a
368 # corresponding ']'. Absorb either
371 last if $i >= $pat_len - 1;
374 $curchar = substr($pat, $i, 1);
375 if ($curchar =~ /[:=.]/) {
376 for (my $j = $i + 1; $j < $pat_len; $j++) {
377 next unless substr($pat, $j, 1) eq ']';
379 if (substr($pat, $j - 1, 1) eq $curchar) {
380 # Here, is a posix class
381 $modified .= substr($pat, $i, $j - $i + 1) . " ";
388 # Here wasn't a posix class, just process normally
389 $modified .= " $curchar ";
392 if ($in_brackets && ! $skip) {
393 if ($result eq 'c') {
395 $reason = "Can't figure out where to put the (?[ and ]) since is a compilation error";
398 print "not ok $testname # Problem in $0; original = '$pat'; mod = '$modified'\n";
403 # Use our modified pattern instead of the original
408 for my $study ('', 'study $subject;', 'utf8::upgrade($subject);',
409 'utf8::upgrade($subject); study $subject;') {
410 # Need to make a copy, else the utf8::upgrade of an already studied
411 # scalar confuses things.
412 my $subject = $subject;
413 $subject = XS::APItest::string_without_null($subject) if $no_null;
415 my ($code, $match, $got);
416 if ($repl eq 'pos') {
417 my $patcode = defined $no_null_pat ? '/$no_null_pat/g'
422 \$match = ( \$subject =~ $patcode );
423 \$got = pos(\$subject);
430 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
434 elsif ($qr_embed_thr) {
436 # Can't run the match in a subthread, but can do this and
437 # clone the pattern the other way.
438 my \$RE = threads->new(sub {qr$pat})->join();
440 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
445 my $patcode = defined $no_null_pat ? '/$no_null_pat/'
449 \$match = (\$subject =~ $OP$pat) while \$c--;
456 \$match = (\$subject =~ $OP$pat) while \$c--;
460 $code = "no warnings 'experimental::regex_sets';$code" if $regex_sets;
461 $code = "no warnings 'experimental::alpha_assertions';$code" if $alpha_assertions;
462 #$code.=qq[\n\$expect="$expect";\n];
464 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
466 # Probably we should annotate specific tests with which warnings
467 # categories they're known to trigger, and hence should be
468 # disabled just for that test
469 no warnings qw(uninitialized regexp deprecated);
472 chomp( my $err = $@ );
474 print "ok $testname # skipped", length($reason) ? ". $reason" : '', "\n";
477 elsif ($result eq 'c') {
478 if ($err !~ m!^\Q$expect!) { print "not ok $testname$todo (compile) $input => '$err'\n"; next TEST }
479 last; # no need to study a syntax error
482 print "not ok $testname # TODO", length($reason) ? " - $reason" : '', "\n";
486 print "not ok $testname$todo $input => error '$err'\n", _comment("$code\n$@\n"); next TEST;
488 elsif ($result =~ /^n/) {
489 if ($match) { print "not ok $testname$todo ($study) $input => false positive\n"; next TEST }
492 if (!$match || $got ne $expect) {
493 eval { require Data::Dumper };
494 no warnings "utf8"; # But handle should be utf8
495 if ($@ || !defined &DynaLoader::boot_DynaLoader) {
496 # Data::Dumper will load on miniperl, but fail when used in
497 # anger as it tries to load B. I'd prefer to keep the
498 # regular calls below outside of an eval so that real
499 # (unknown) failures get spotted, not ignored.
500 print "not ok $testname$todo ($study) $input => '$got', match=$match\n", _comment("$code\n");
502 else { # better diagnostics
503 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
504 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
505 my $e = Data::Dumper->new([$expect],['expected'])->Useqq(1)->Dump;
506 print "not ok $testname$todo ($study) $input => '$got', match=$match\n", _comment("$s\n$code\n$g\n$e\n");
512 print "ok $testname$todo\n";
515 printf "1..%d\n# $iters iterations\n", scalar @tests;