xref: /openbsd/gnu/usr.bin/perl/ext/File-Glob/t/rt131211.t (revision f3efcd01)
15759b3d2Safresh1# tests for RT 131211
25759b3d2Safresh1#
35759b3d2Safresh1# non-matching glob("a*a*a*...") went exponential time on number of a*'s
45759b3d2Safresh1
55759b3d2Safresh1
65759b3d2Safresh1use strict;
75759b3d2Safresh1use warnings;
85759b3d2Safresh1use v5.16.0;
95759b3d2Safresh1use File::Temp 'tempdir';
105759b3d2Safresh1use File::Spec::Functions;
115759b3d2Safresh1use Test::More;
125759b3d2Safresh1use Time::HiRes qw(time);
135759b3d2Safresh1use Config;
145759b3d2Safresh1
155759b3d2Safresh1plan skip_all => 'This platform doesn\'t use File::Glob'
165759b3d2Safresh1                    if $Config{ccflags} =~ /\b{wb}-DPERL_EXTERNAL_GLOB\b{wb}/;
175759b3d2Safresh1plan tests => 13;
185759b3d2Safresh1
195759b3d2Safresh1my $path = tempdir uc cleanup => 1;
205759b3d2Safresh1my @files= (
215759b3d2Safresh1    "x".("a" x 50)."b", # 0
225759b3d2Safresh1    "abbbbbbbbbbbbc",   # 1
235759b3d2Safresh1    "abbbbbbbbbbbbd",   # 2
245759b3d2Safresh1    "aaabaaaabaaaabc",  # 3
255759b3d2Safresh1    "pq",               # 4
265759b3d2Safresh1    "r",                # 5
275759b3d2Safresh1    "rttiiiiiii",       # 6
285759b3d2Safresh1    "wewewewewewe",     # 7
295759b3d2Safresh1    "weeeweeeweee",     # 8
305759b3d2Safresh1    "weewweewweew",     # 9
315759b3d2Safresh1    "wewewewewewewewewewewewewewewewewq", # 10
325759b3d2Safresh1    "wtttttttetttttttwr", # 11
335759b3d2Safresh1);
345759b3d2Safresh1
355759b3d2Safresh1
365759b3d2Safresh1# VMS needs a real extension.
375759b3d2Safresh1map { $_ .= '.tmp' } @files if $^O eq 'VMS';
385759b3d2Safresh1
395759b3d2Safresh1foreach (@files) {
405759b3d2Safresh1    open(my $f, ">", catfile $path, $_);
415759b3d2Safresh1}
425759b3d2Safresh1
435759b3d2Safresh1my $elapsed_fail= 0;
445759b3d2Safresh1my $elapsed_match= 0;
455759b3d2Safresh1my @got_files;
465759b3d2Safresh1my @no_files;
475759b3d2Safresh1my $count = 0;
485759b3d2Safresh1
495759b3d2Safresh1while (++$count < 10) {
505759b3d2Safresh1    $elapsed_match -= time;
515759b3d2Safresh1    @got_files= glob catfile $path, "x".("a*" x $count) . "b";
525759b3d2Safresh1    $elapsed_match += time;
535759b3d2Safresh1
545759b3d2Safresh1    $elapsed_fail -= time;
555759b3d2Safresh1    @no_files= glob catfile $path, "x".("a*" x $count) . "c";
565759b3d2Safresh1    $elapsed_fail += time;
575759b3d2Safresh1    last if $elapsed_fail > ($elapsed_match < 0.2 ? 0.2 : $elapsed_match) * 100;
585759b3d2Safresh1}
595759b3d2Safresh1
605759b3d2Safresh1is $count,10,
615759b3d2Safresh1    "tried all the patterns without bailing out"
625759b3d2Safresh1    or diag("elapsed_match=$elapsed_match elapsed_fail=$elapsed_fail");
635759b3d2Safresh1
64*f3efcd01Safresh1ok $elapsed_fail < 1 || $elapsed_fail <= 10 * $elapsed_match,
65*f3efcd01Safresh1    "time to fail should be less than 10x the time to match"
665759b3d2Safresh1    or diag("elapsed_match=$elapsed_match elapsed_fail=$elapsed_fail");
675759b3d2Safresh1
685759b3d2Safresh1is "@got_files", catfile($path, $files[0]),
695759b3d2Safresh1    "only got the expected file for xa*..b";
705759b3d2Safresh1is "@no_files", "", "shouldnt have files for xa*..c";
715759b3d2Safresh1
725759b3d2Safresh1
735759b3d2Safresh1@got_files= glob catfile $path, "a*b*b*b*bc";
745759b3d2Safresh1is "@got_files", catfile($path, $files[1]),
755759b3d2Safresh1    "only got the expected file for a*b*b*b*bc";
765759b3d2Safresh1
775759b3d2Safresh1@got_files= sort glob catfile $path, "a*b*b*bc";
785759b3d2Safresh1is "@got_files", catfile($path, $files[3])." ".catfile($path,$files[1]),
795759b3d2Safresh1    "got the expected two files for a*b*b*bc";
805759b3d2Safresh1
815759b3d2Safresh1@got_files= sort glob catfile $path, "p*";
825759b3d2Safresh1is "@got_files", catfile($path, $files[4]),
835759b3d2Safresh1    "p* matches pq";
845759b3d2Safresh1
855759b3d2Safresh1@got_files= sort glob catfile $path, "r*???????";
865759b3d2Safresh1is "@got_files", catfile($path, $files[6]),
875759b3d2Safresh1    "r*??????? works as expected";
885759b3d2Safresh1
895759b3d2Safresh1@got_files= sort glob catfile $path, "w*e*w??e";
905759b3d2Safresh1is "@got_files", join(" ", sort map { catfile($path, $files[$_]) } (7,8)),
915759b3d2Safresh1    "w*e*w??e works as expected";
925759b3d2Safresh1
935759b3d2Safresh1@got_files= sort glob catfile $path, "w*e*we??";
945759b3d2Safresh1is "@got_files", join(" ", sort map { catfile($path, $files[$_]) } (7,8,9,10)),
955759b3d2Safresh1    "w*e*we?? works as expected";
965759b3d2Safresh1
975759b3d2Safresh1@got_files= sort glob catfile $path, "w**e**w";
985759b3d2Safresh1is "@got_files", join(" ", sort map { catfile($path, $files[$_]) } (9)),
995759b3d2Safresh1    "w**e**w works as expected";
1005759b3d2Safresh1
1015759b3d2Safresh1@got_files= sort glob catfile $path, "*wee*";
1025759b3d2Safresh1is "@got_files", join(" ", sort map { catfile($path, $files[$_]) } (8,9)),
1035759b3d2Safresh1    "*wee* works as expected";
1045759b3d2Safresh1
1055759b3d2Safresh1@got_files= sort glob catfile $path, "we*";
1065759b3d2Safresh1is "@got_files", join(" ", sort map { catfile($path, $files[$_]) } (7,8,9,10)),
1075759b3d2Safresh1    "we* works as expected";
1085759b3d2Safresh1
109