File: grep.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (296 lines) | stat: -rw-r--r-- 8,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!./perl

#
# grep() and map() tests
#

BEGIN {
    chdir 't' if -d 't'; 
    require "./test.pl";
    set_up_inc( qw(. ../lib) );
}

plan( tests => 77 );

{
    my @lol = ([qw(a b c)], [], [qw(1 2 3)]);
    my @mapped = map  {scalar @$_} @lol;
    cmp_ok("@mapped", 'eq', "3 0 3", 'map scalar list of list');

    my @grepped = grep {scalar @$_} @lol;
    cmp_ok("@grepped", 'eq', "$lol[0] $lol[2]", 'grep scalar list of list');
    $test++;

    @grepped = grep { $_ } @mapped;
    cmp_ok( "@grepped", 'eq',  "3 3", 'grep basic');
}

{
    my @res;

    @res = map({$_} ("geronimo"));
    cmp_ok( scalar(@res), '==', 1, 'basic map nr');
    cmp_ok( $res[0], 'eq', 'geronimo', 'basic map is');

    @res = map
             ({$_} ("yoyodyne"));
    cmp_ok( scalar(@res), '==', 1, 'linefeed map nr');
    cmp_ok( $res[0], 'eq', 'yoyodyne', 'linefeed map is');

    @res = (map(
       {a =>$_},
     ("chobb")))[0]->{a};
    cmp_ok( scalar(@res), '==', 1, 'deref map nr');
    cmp_ok( $res[0], 'eq', 'chobb', 'deref map is');

    @res = map {$_} ("geronimo");
    cmp_ok( scalar(@res), '==', 1, 'no paren basic map nr');
    cmp_ok( $res[0], 'eq', 'geronimo', 'no paren basic map is');

    @res = map
             {$_} ("yoyodyne");
    cmp_ok( scalar(@res), '==', 1, 'no paren linefeed map nr');
    cmp_ok( $res[0], 'eq', 'yoyodyne', 'no paren linefeed map is');

    @res = (map
           {a =>$_},
       ("chobb"))[0]->{a};
    cmp_ok( scalar(@res), '==', 1, 'no paren deref map nr');
    cmp_ok( $res[0], 'eq', 'chobb', 'no paren deref map is');

    my $x = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\n";

    @res = map($_&$x,("sferics\n"));
    cmp_ok( scalar(@res), '==', 1, 'binand map nr 1');
    cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 1');

    @res = map
            ($_ & $x, ("sferics\n"));
    cmp_ok( scalar(@res), '==', 1, 'binand map nr 2');
    cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 2');

    @res = map { $_ & $x } ("sferics\n");
    cmp_ok( scalar(@res), '==', 1, 'binand map nr 3');
    cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 3');

    @res = map
             { $_&$x } ("sferics\n");
    cmp_ok( scalar(@res), '==', 1, 'binand map nr 4');
    cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 4');

    @res = grep({$_} ("geronimo"));
    cmp_ok( scalar(@res), '==', 1, 'basic grep nr');
    cmp_ok( $res[0], 'eq', 'geronimo', 'basic grep is');

    @res = grep
                ({$_} ("yoyodyne"));
    cmp_ok( scalar(@res), '==', 1, 'linefeed grep nr');
    cmp_ok( $res[0], 'eq', 'yoyodyne', 'linefeed grep is');

    @res = grep
        ({a=>$_}->{a},
        ("chobb"));
    cmp_ok( scalar(@res), '==', 1, 'deref grep nr');
    cmp_ok( $res[0], 'eq', 'chobb', 'deref grep is');

    @res = grep {$_} ("geronimo");
    cmp_ok( scalar(@res), '==', 1, 'no paren basic grep nr');
    cmp_ok( $res[0], 'eq', 'geronimo', 'no paren basic grep is');

    @res = grep
                {$_} ("yoyodyne");
    cmp_ok( scalar(@res), '==', 1, 'no paren linefeed grep nr');
    cmp_ok( $res[0], 'eq', 'yoyodyne', 'no paren linefeed grep is');

    @res = grep {a=>$_}->{a}, ("chobb");
    cmp_ok( scalar(@res), '==', 1, 'no paren deref grep nr');
    cmp_ok( $res[0], 'eq', 'chobb', 'no paren deref grep is');

    @res = grep
         {a=>$_}->{a}, ("chobb");
    cmp_ok( scalar(@res), '==', 1, 'no paren deref linefeed  nr');
    cmp_ok( $res[0], 'eq', 'chobb', 'no paren deref linefeed  is');

    @res = grep($_&"X", ("bodine"));
    cmp_ok( scalar(@res), '==', 1, 'binand X grep nr');
    cmp_ok( $res[0], 'eq', 'bodine', 'binand X grep is');

    @res = grep
           ($_&"X", ("bodine"));
    cmp_ok( scalar(@res), '==', 1, 'binand X linefeed grep nr');
    cmp_ok( $res[0], 'eq', 'bodine', 'binand X linefeed grep is');

    @res = grep {$_&"X"} ("bodine");
    cmp_ok( scalar(@res), '==', 1, 'no paren binand X grep nr');
    cmp_ok( $res[0], 'eq', 'bodine', 'no paren binand X grep is');

    @res = grep
           {$_&"X"} ("bodine");
    cmp_ok( scalar(@res), '==', 1, 'no paren binand X linefeed grep nr');
    cmp_ok( $res[0], 'eq', 'bodine', 'no paren binand X linefeed grep is');
}

{
    # Tests for "for" in "map" and "grep"
    # Used to dump core, bug [perl #17771]

    my @x;
    my $y = '';
    @x = map { $y .= $_ for 1..2; 1 } 3..4;
    cmp_ok( "@x,$y",'eq',"1 1,1212", '[perl #17771] for in map 1');

    $y = '';
    @x = map { $y .= $_ for 1..2; $y .= $_ } 3..4;
    cmp_ok( "@x,$y",'eq',"123 123124,123124", '[perl #17771] for in map 2');

    $y = '';
    @x = map { for (1..2) { $y .= $_ } $y .= $_ } 3..4;
    cmp_ok( "@x,$y",'eq',"123 123124,123124", '[perl #17771] for in map 3');

    $y = '';
    @x = grep { $y .= $_ for 1..2; 1 } 3..4;
    cmp_ok( "@x,$y",'eq',"3 4,1212", '[perl #17771] for in grep 1');

    $y = '';
    @x = grep { for (1..2) { $y .= $_ } 1 } 3..4;
    cmp_ok( "@x,$y",'eq',"3 4,1212", '[perl #17771] for in grep 2');

    # Add also a sample test from [perl #18153].  (The same bug).
    $a = 1; map {if ($a){}} (2);
    pass( '[perl #18153] (not dead yet)' ); # no core dump is all we need
}

{
    sub add_an_x(@){
        map {"${_}x"} @_;
    };
    cmp_ok( join("-",add_an_x(1,2,3,4)), 'eq', "1x-2x-3x-4x", 'add-an-x');
}

{
    my $gimme;

    sub gimme {
        my $want = wantarray();
        if (defined $want) {
            $gimme = $want ? 'list' : 'scalar';
        } else {
            $gimme = 'void';
        }
    }

    my @list = 0..9;

    undef $gimme; gimme for @list;      cmp_ok($gimme, 'eq', 'void',   'gimme a V!');
    undef $gimme; grep { gimme } @list; cmp_ok($gimme, 'eq', 'scalar', 'gimme an S!');
    undef $gimme; map { gimme } @list;  cmp_ok($gimme, 'eq', 'list',   'gimme an L!');
}

{
    # test scalar context return
    my @list = (7, 14, 21);

    my $x = map {$_ *= 2} @list;
    cmp_ok("@list", 'eq', "14 28 42", 'map scalar return');
    cmp_ok($x, '==', 3, 'map scalar count');

    @list = (9, 16, 25, 36);
    $x = grep {$_ % 2} @list;
    cmp_ok($x, '==', 2, 'grep scalar count');

    my @res = grep {$_ % 2} @list;
    cmp_ok("@res", 'eq', "9 25", 'grep extract');
}

{
    # This shouldn't loop indefinitely.
    my @empty = map { while (1) {} } ();
    cmp_ok("@empty", 'eq', '', 'staying alive');
}

{
    my $x;
    eval 'grep $x (1,2,3);';
    like($@, qr/Missing comma after first argument to grep function/,
         "proper error on variable as block. [perl #37314]");
}

# [perl #78194] grep/map aliasing op return values
grep is(\$_, \$_, '[perl #78194] \$_ == \$_ inside grep ..., "$x"'),
     "${\''}", "${\''}";
map is(\$_, \$_, '[perl #78194] \$_ == \$_ inside map ..., "$x"'),
     "${\''}", "${\''}";

# [perl #92254] freeing $_ in gremap block
{
    my $y;
    grep { undef *_ } $y;
    map { undef *_ } $y;
}
pass 'no double frees with grep/map { undef *_ }';

# Don't mortalise PADTMPs.
# This failed while I was messing with leave stuff (but not in a simple
# test, so add one). The '1;' ensures the block is wrapped in ENTER/LEAVE;
# the stringify returns a PADTMP. DAPM.

{
    my @a = map { 1; "$_" } 1,2;
    is("@a", "1 2", "PADTMP");
}


package FOO {
    my $count;
    sub DESTROY { $count++ }
    my @a;

    # check all grep arguments are immediately released

    $count = 0;
    @a = (bless([]), bless([]), bless([]));
    grep 1, @a;
    ::is ($count, 0, "grep void pre");
    @a = ();
    ::is ($count, 3, "grep void post");

    $count = 0;
    @a = (bless([]), bless([]), bless([]));
    my $x = grep 1, @a;
    ::is ($count, 0, "grep scalar pre");
    @a = ();
    ::is ($count, 3, "grep scalar post");

    $count = 0;
    @a = (bless([]), bless([]), bless([]));
    () = grep 1, @a;
    ::is ($count, 0, "grep list pre");
    @a = ();
    ::is ($count, 3, "grep list post");

    # check check map expression results are immediately released
    # in void context

    $count = 1;
    map {
            ::is ($count, 1, "block map void $_");
            $count = 0;
            bless[];
        } 1,2,3;
}

# At one point during development, this code SEGVed on PERL_RC_STACK
# builds, as NULL filler pointers on the stack during a map were getting
# copied to the tmps stack, and the tmps stack can't handle NULL pointers.
# The bug only occurred in IO::Socket::SSL rather than core. It required
# perl doing a call_sv(.., G_EVAL) to call the sub containing the map. In
# the original bug this was triggered by a use/require, but here we use a
# BEGIN within an eval as simpler variant.

{
    my @res;
    eval q{
        BEGIN { @res = map { $_ => eval {die} || -1 } qw( ABC XYZ); }
    };
    is("@res", "ABC -1 XYZ -1", "no NULL tmps");
}