This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
libperl leaks a THREAD_KEY each time it is reloaded
[perl5.git] / ext / B / t / f_map.t
CommitLineData
cc02ea56
JC
1#!perl
2
3BEGIN {
5638aaac
SM
4 if ($ENV{PERL_CORE}){
5 chdir('t') if -d 't';
6 @INC = ('.', '../lib', '../ext/B/t');
7 } else {
8 unshift @INC, 't';
9 push @INC, "../../t";
10 }
9cd8f857
NC
11 require Config;
12 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13 print "1..0 # Skip -- Perl configured without B module\n";
14 exit 0;
15 }
8dd2f9d4 16 if ($Config::Config{'extensions'} !~ /\bData\/Dumper\b/) {
2799c206
NC
17 print
18 "1..0 # Skip: Data::Dumper was not built, needed by OptreeCheck\n";
19 exit 0;
2799c206 20 }
7f046282
NC
21 if (!$Config::Config{useperlio}) {
22 print "1..0 # Skip -- need perlio to walk the optree\n";
23 exit 0;
24 }
7252851f
NC
25 if ($] < 5.009) {
26 print "1..0 # Skip -- TODO - provide golden result regexps for 5.8\n";
27 exit 0;
28 }
5638aaac 29 require q(test.pl);
cc02ea56
JC
30}
31use OptreeCheck;
32plan tests => 9;
33
34
35=for gentest
36
37# chunk: #!perl
38# examples shamelessly snatched from perldoc -f map
39
40=cut
41
42=for gentest
43
44# chunk: # translates a list of numbers to the corresponding characters.
45@chars = map(chr, @nums);
46
47=cut
48
49checkOptree(note => q{},
50 bcopts => q{-exec},
51 code => q{@chars = map(chr, @nums); },
52 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
53# 1 <;> nextstate(main 475 (eval 10):1) v
54# 2 <0> pushmark s
55# 3 <0> pushmark s
56# 4 <#> gv[*nums] s
57# 5 <1> rv2av[t7] lKM/1
58# 6 <@> mapstart lK
59# 7 <|> mapwhile(other->8)[t8] lK
60# 8 <#> gvsv[*_] s
61# 9 <1> chr[t5] sK/1
62# goto 7
63# a <0> pushmark s
64# b <#> gv[*chars] s
65# c <1> rv2av[t2] lKRM*/1
66# d <2> aassign[t9] KS/COMMON
67# e <1> leavesub[1 ref] K/REFC,1
68EOT_EOT
69# 1 <;> nextstate(main 559 (eval 15):1) v
70# 2 <0> pushmark s
71# 3 <0> pushmark s
72# 4 <$> gv(*nums) s
73# 5 <1> rv2av[t4] lKM/1
74# 6 <@> mapstart lK
75# 7 <|> mapwhile(other->8)[t5] lK
76# 8 <$> gvsv(*_) s
77# 9 <1> chr[t3] sK/1
78# goto 7
79# a <0> pushmark s
80# b <$> gv(*chars) s
81# c <1> rv2av[t1] lKRM*/1
82# d <2> aassign[t6] KS/COMMON
83# e <1> leavesub[1 ref] K/REFC,1
84EONT_EONT
85
86
87=for gentest
88
89# chunk: %hash = map { getkey($_) => $_ } @array;
90
91=cut
92
93checkOptree(note => q{},
94 bcopts => q{-exec},
95 code => q{%hash = map { getkey($_) => $_ } @array; },
96 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
97# 1 <;> nextstate(main 476 (eval 10):1) v
98# 2 <0> pushmark s
99# 3 <0> pushmark s
100# 4 <#> gv[*array] s
101# 5 <1> rv2av[t8] lKM/1
102# 6 <@> mapstart lK*
103# 7 <|> mapwhile(other->8)[t9] lK
104# 8 <0> enter l
105# 9 <;> nextstate(main 475 (eval 10):1) v
106# a <0> pushmark s
107# b <0> pushmark s
108# c <#> gvsv[*_] s
109# d <#> gv[*getkey] s/EARLYCV
110# e <1> entersub[t5] lKS/TARG,1
111# f <#> gvsv[*_] s
112# g <@> list lK
113# h <@> leave lKP
114# goto 7
115# i <0> pushmark s
116# j <#> gv[*hash] s
117# k <1> rv2hv[t2] lKRM*/1
118# l <2> aassign[t10] KS/COMMON
119# m <1> leavesub[1 ref] K/REFC,1
120EOT_EOT
121# 1 <;> nextstate(main 560 (eval 15):1) v
122# 2 <0> pushmark s
123# 3 <0> pushmark s
124# 4 <$> gv(*array) s
125# 5 <1> rv2av[t3] lKM/1
126# 6 <@> mapstart lK*
127# 7 <|> mapwhile(other->8)[t4] lK
128# 8 <0> enter l
129# 9 <;> nextstate(main 559 (eval 15):1) v
130# a <0> pushmark s
131# b <0> pushmark s
132# c <$> gvsv(*_) s
133# d <$> gv(*getkey) s/EARLYCV
134# e <1> entersub[t2] lKS/TARG,1
135# f <$> gvsv(*_) s
136# g <@> list lK
137# h <@> leave lKP
138# goto 7
139# i <0> pushmark s
140# j <$> gv(*hash) s
141# k <1> rv2hv[t1] lKRM*/1
142# l <2> aassign[t5] KS/COMMON
143# m <1> leavesub[1 ref] K/REFC,1
144EONT_EONT
145
146
147=for gentest
148
149# chunk: {
150 %hash = ();
151 foreach $_ (@array) {
152 $hash{getkey($_)} = $_;
153 }
154}
155
156=cut
157
158checkOptree(note => q{},
159 bcopts => q{-exec},
160 code => q{{ %hash = (); foreach $_ (@array) { $hash{getkey($_)} = $_; } } },
161 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
162# 1 <;> nextstate(main 478 (eval 10):1) v
163# 2 <{> enterloop(next->u last->u redo->3)
164# 3 <;> nextstate(main 475 (eval 10):1) v
165# 4 <0> pushmark s
166# 5 <0> pushmark s
167# 6 <#> gv[*hash] s
168# 7 <1> rv2hv[t2] lKRM*/1
169# 8 <2> aassign[t3] vKS
170# 9 <;> nextstate(main 476 (eval 10):1) v
171# a <0> pushmark sM
172# b <#> gv[*array] s
173# c <1> rv2av[t6] sKRM/1
174# d <#> gv[*_] s
175# e <1> rv2gv sKRM/1
176# f <{> enteriter(next->q last->t redo->g) lKS
177# r <0> iter s
178# s <|> and(other->g) K/1
179# g <;> nextstate(main 475 (eval 10):1) v
180# h <#> gvsv[*_] s
181# i <#> gv[*hash] s
182# j <1> rv2hv sKR/1
183# k <0> pushmark s
184# l <#> gvsv[*_] s
185# m <#> gv[*getkey] s/EARLYCV
186# n <1> entersub[t10] sKS/TARG,1
187# o <2> helem sKRM*/2
188# p <2> sassign vKS/2
189# q <0> unstack s
190# goto r
191# t <2> leaveloop K/2
192# u <2> leaveloop K/2
193# v <1> leavesub[1 ref] K/REFC,1
194EOT_EOT
195# 1 <;> nextstate(main 562 (eval 15):1) v
196# 2 <{> enterloop(next->u last->u redo->3)
197# 3 <;> nextstate(main 559 (eval 15):1) v
198# 4 <0> pushmark s
199# 5 <0> pushmark s
200# 6 <$> gv(*hash) s
201# 7 <1> rv2hv[t1] lKRM*/1
202# 8 <2> aassign[t2] vKS
203# 9 <;> nextstate(main 560 (eval 15):1) v
204# a <0> pushmark sM
205# b <$> gv(*array) s
206# c <1> rv2av[t3] sKRM/1
207# d <$> gv(*_) s
208# e <1> rv2gv sKRM/1
209# f <{> enteriter(next->q last->t redo->g) lKS
210# r <0> iter s
211# s <|> and(other->g) K/1
212# g <;> nextstate(main 559 (eval 15):1) v
213# h <$> gvsv(*_) s
214# i <$> gv(*hash) s
215# j <1> rv2hv sKR/1
216# k <0> pushmark s
217# l <$> gvsv(*_) s
218# m <$> gv(*getkey) s/EARLYCV
219# n <1> entersub[t4] sKS/TARG,1
220# o <2> helem sKRM*/2
221# p <2> sassign vKS/2
222# q <0> unstack s
223# goto r
224# t <2> leaveloop K/2
225# u <2> leaveloop K/2
226# v <1> leavesub[1 ref] K/REFC,1
227EONT_EONT
228
229
230=for gentest
231
232# chunk: #%hash = map { "\L$_", 1 } @array; # perl guesses EXPR. wrong
233%hash = map { +"\L$_", 1 } @array; # perl guesses BLOCK. right
234
235=cut
236
237checkOptree(note => q{},
238 bcopts => q{-exec},
239 code => q{%hash = map { +"\L$_", 1 } @array; },
240 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
241# 1 <;> nextstate(main 476 (eval 10):1) v
242# 2 <0> pushmark s
243# 3 <0> pushmark s
244# 4 <#> gv[*array] s
245# 5 <1> rv2av[t7] lKM/1
246# 6 <@> mapstart lK*
247# 7 <|> mapwhile(other->8)[t9] lK
248# 8 <0> pushmark s
249# 9 <#> gvsv[*_] s
250# a <1> lc[t4] sK/1
251# b <@> stringify[t5] sK/1
252# c <$> const[IV 1] s
253# d <@> list lK
254# - <@> scope lK
255# goto 7
256# e <0> pushmark s
257# f <#> gv[*hash] s
258# g <1> rv2hv[t2] lKRM*/1
259# h <2> aassign[t10] KS/COMMON
260# i <1> leavesub[1 ref] K/REFC,1
261EOT_EOT
262# 1 <;> nextstate(main 560 (eval 15):1) v
263# 2 <0> pushmark s
264# 3 <0> pushmark s
265# 4 <$> gv(*array) s
266# 5 <1> rv2av[t4] lKM/1
267# 6 <@> mapstart lK*
268# 7 <|> mapwhile(other->8)[t5] lK
269# 8 <0> pushmark s
270# 9 <$> gvsv(*_) s
271# a <1> lc[t2] sK/1
272# b <@> stringify[t3] sK/1
273# c <$> const(IV 1) s
274# d <@> list lK
275# - <@> scope lK
276# goto 7
277# e <0> pushmark s
278# f <$> gv(*hash) s
279# g <1> rv2hv[t1] lKRM*/1
280# h <2> aassign[t6] KS/COMMON
281# i <1> leavesub[1 ref] K/REFC,1
282EONT_EONT
283
284
285=for gentest
286
287# chunk: %hash = map { ("\L$_", 1) } @array; # this also works
288
289=cut
290
291checkOptree(note => q{},
292 bcopts => q{-exec},
293 code => q{%hash = map { ("\L$_", 1) } @array; },
294 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
295# 1 <;> nextstate(main 476 (eval 10):1) v
296# 2 <0> pushmark s
297# 3 <0> pushmark s
298# 4 <#> gv[*array] s
299# 5 <1> rv2av[t7] lKM/1
300# 6 <@> mapstart lK*
301# 7 <|> mapwhile(other->8)[t9] lK
302# 8 <0> pushmark s
303# 9 <#> gvsv[*_] s
304# a <1> lc[t4] sK/1
305# b <@> stringify[t5] sK/1
306# c <$> const[IV 1] s
307# d <@> list lKP
308# - <@> scope lK
309# goto 7
310# e <0> pushmark s
311# f <#> gv[*hash] s
312# g <1> rv2hv[t2] lKRM*/1
313# h <2> aassign[t10] KS/COMMON
314# i <1> leavesub[1 ref] K/REFC,1
315EOT_EOT
316# 1 <;> nextstate(main 560 (eval 15):1) v
317# 2 <0> pushmark s
318# 3 <0> pushmark s
319# 4 <$> gv(*array) s
320# 5 <1> rv2av[t4] lKM/1
321# 6 <@> mapstart lK*
322# 7 <|> mapwhile(other->8)[t5] lK
323# 8 <0> pushmark s
324# 9 <$> gvsv(*_) s
325# a <1> lc[t2] sK/1
326# b <@> stringify[t3] sK/1
327# c <$> const(IV 1) s
328# d <@> list lKP
329# - <@> scope lK
330# goto 7
331# e <0> pushmark s
332# f <$> gv(*hash) s
333# g <1> rv2hv[t1] lKRM*/1
334# h <2> aassign[t6] KS/COMMON
335# i <1> leavesub[1 ref] K/REFC,1
336EONT_EONT
337
338
339=for gentest
340
341# chunk: %hash = map { lc($_), 1 } @array; # as does this.
342
343=cut
344
345checkOptree(note => q{},
346 bcopts => q{-exec},
347 code => q{%hash = map { lc($_), 1 } @array; },
348 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
349# 1 <;> nextstate(main 476 (eval 10):1) v
350# 2 <0> pushmark s
351# 3 <0> pushmark s
352# 4 <#> gv[*array] s
353# 5 <1> rv2av[t6] lKM/1
354# 6 <@> mapstart lK*
355# 7 <|> mapwhile(other->8)[t8] lK
356# 8 <0> pushmark s
357# 9 <#> gvsv[*_] s
358# a <1> lc[t4] sK/1
359# b <$> const[IV 1] s
360# c <@> list lK
361# - <@> scope lK
362# goto 7
363# d <0> pushmark s
364# e <#> gv[*hash] s
365# f <1> rv2hv[t2] lKRM*/1
366# g <2> aassign[t9] KS/COMMON
367# h <1> leavesub[1 ref] K/REFC,1
368EOT_EOT
369# 1 <;> nextstate(main 589 (eval 26):1) v
370# 2 <0> pushmark s
371# 3 <0> pushmark s
372# 4 <$> gv(*array) s
373# 5 <1> rv2av[t3] lKM/1
374# 6 <@> mapstart lK*
375# 7 <|> mapwhile(other->8)[t4] lK
376# 8 <0> pushmark s
377# 9 <$> gvsv(*_) s
378# a <1> lc[t2] sK/1
379# b <$> const(IV 1) s
380# c <@> list lK
381# - <@> scope lK
382# goto 7
383# d <0> pushmark s
384# e <$> gv(*hash) s
385# f <1> rv2hv[t1] lKRM*/1
386# g <2> aassign[t5] KS/COMMON
387# h <1> leavesub[1 ref] K/REFC,1
388EONT_EONT
389
390
391=for gentest
392
393# chunk: %hash = map +( lc($_), 1 ), @array; # this is EXPR and works!
394
395=cut
396
397checkOptree(note => q{},
398 bcopts => q{-exec},
399 code => q{%hash = map +( lc($_), 1 ), @array; },
400 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
401# 1 <;> nextstate(main 475 (eval 10):1) v
402# 2 <0> pushmark s
403# 3 <0> pushmark s
404# 4 <#> gv[*array] s
405# 5 <1> rv2av[t6] lKM/1
406# 6 <@> mapstart lK
407# 7 <|> mapwhile(other->8)[t7] lK
408# 8 <0> pushmark s
409# 9 <#> gvsv[*_] s
410# a <1> lc[t4] sK/1
411# b <$> const[IV 1] s
412# c <@> list lKP
413# goto 7
414# d <0> pushmark s
415# e <#> gv[*hash] s
416# f <1> rv2hv[t2] lKRM*/1
417# g <2> aassign[t8] KS/COMMON
418# h <1> leavesub[1 ref] K/REFC,1
419EOT_EOT
420# 1 <;> nextstate(main 593 (eval 28):1) v
421# 2 <0> pushmark s
422# 3 <0> pushmark s
423# 4 <$> gv(*array) s
424# 5 <1> rv2av[t3] lKM/1
425# 6 <@> mapstart lK
426# 7 <|> mapwhile(other->8)[t4] lK
427# 8 <0> pushmark s
428# 9 <$> gvsv(*_) s
429# a <1> lc[t2] sK/1
430# b <$> const(IV 1) s
431# c <@> list lKP
432# goto 7
433# d <0> pushmark s
434# e <$> gv(*hash) s
435# f <1> rv2hv[t1] lKRM*/1
436# g <2> aassign[t5] KS/COMMON
437# h <1> leavesub[1 ref] K/REFC,1
438EONT_EONT
439
440
441=for gentest
442
443# chunk: %hash = map ( lc($_), 1 ), @array; # evaluates to (1, @array)
444
445=cut
446
447checkOptree(note => q{},
448 bcopts => q{-exec},
449 code => q{%hash = map ( lc($_), 1 ), @array; },
450 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
451# 1 <;> nextstate(main 475 (eval 10):1) v
452# 2 <0> pushmark s
453# 3 <0> pushmark s
454# 4 <0> pushmark s
455# 5 <$> const[IV 1] sM
456# 6 <@> mapstart lK
457# 7 <|> mapwhile(other->8)[t5] lK
458# 8 <#> gvsv[*_] s
459# 9 <1> lc[t4] sK/1
460# goto 7
461# a <0> pushmark s
462# b <#> gv[*hash] s
463# c <1> rv2hv[t2] lKRM*/1
464# d <2> aassign[t6] KS/COMMON
465# e <#> gv[*array] s
466# f <1> rv2av[t8] K/1
467# g <@> list K
468# h <1> leavesub[1 ref] K/REFC,1
469EOT_EOT
470# 1 <;> nextstate(main 597 (eval 30):1) v
471# 2 <0> pushmark s
472# 3 <0> pushmark s
473# 4 <0> pushmark s
474# 5 <$> const(IV 1) sM
475# 6 <@> mapstart lK
476# 7 <|> mapwhile(other->8)[t3] lK
477# 8 <$> gvsv(*_) s
478# 9 <1> lc[t2] sK/1
479# goto 7
480# a <0> pushmark s
481# b <$> gv(*hash) s
482# c <1> rv2hv[t1] lKRM*/1
483# d <2> aassign[t4] KS/COMMON
484# e <$> gv(*array) s
485# f <1> rv2av[t5] K/1
486# g <@> list K
487# h <1> leavesub[1 ref] K/REFC,1
488EONT_EONT
489
490
491=for gentest
492
493# chunk: @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
494
495=cut
496
497checkOptree(note => q{},
498 bcopts => q{-exec},
499 code => q{@hashes = map +{ lc($_), 1 }, @array },
500 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
501# 1 <;> nextstate(main 475 (eval 10):1) v
502# 2 <0> pushmark s
503# 3 <0> pushmark s
504# 4 <#> gv[*array] s
505# 5 <1> rv2av[t6] lKM/1
506# 6 <@> mapstart lK
507# 7 <|> mapwhile(other->8)[t7] lK
508# 8 <0> pushmark s
509# 9 <#> gvsv[*_] s
510# a <1> lc[t4] sK/1
511# b <$> const[IV 1] s
512# c <@> anonhash sKRM/1
513# d <1> srefgen sK/1
514# goto 7
515# e <0> pushmark s
516# f <#> gv[*hashes] s
517# g <1> rv2av[t2] lKRM*/1
518# h <2> aassign[t8] KS/COMMON
519# i <1> leavesub[1 ref] K/REFC,1
520EOT_EOT
521# 1 <;> nextstate(main 601 (eval 32):1) v
522# 2 <0> pushmark s
523# 3 <0> pushmark s
524# 4 <$> gv(*array) s
525# 5 <1> rv2av[t3] lKM/1
526# 6 <@> mapstart lK
527# 7 <|> mapwhile(other->8)[t4] lK
528# 8 <0> pushmark s
529# 9 <$> gvsv(*_) s
530# a <1> lc[t2] sK/1
531# b <$> const(IV 1) s
532# c <@> anonhash sKRM/1
533# d <1> srefgen sK/1
534# goto 7
535# e <0> pushmark s
536# f <$> gv(*hashes) s
537# g <1> rv2av[t1] lKRM*/1
538# h <2> aassign[t5] KS/COMMON
539# i <1> leavesub[1 ref] K/REFC,1
540EONT_EONT