This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Give names to all tests in t/comp/fold.t
[perl5.git] / t / comp / fold.t
CommitLineData
2eaa2210
NC
1#!./perl -w
2
3require './test.pl';
4
5# Uncomment this for testing, but don't leave it in for "production", as
6# we've not yet verified that use works.
7# use strict;
a5871da3 8
5f2d9966 9plan (13);
a5871da3
NC
10
11# Historically constant folding was performed by evaluating the ops, and if
12# they threw an exception compilation failed. This was seen as buggy, because
13# even illegal constants in unreachable code would cause failure. So now
14# illegal expressions are reported at runtime, if the expression is reached,
15# making constant folding consistent with many other languages, and purely an
16# optimisation rather than a behaviour change.
17
5f2d9966 18
a5871da3
NC
19my $a;
20$a = eval '$b = 0/0 if 0; 3';
a6d95d3b
NC
21is ($a, 3, 'constants in conditionals don\'t affect constant folding');
22is ($@, '', 'no error');
a5871da3
NC
23
24my $b = 0;
25$a = eval 'if ($b) {return sqrt -3} 3';
a6d95d3b
NC
26is ($a, 3, 'variables in conditionals don\'t affect constant folding');
27is ($@, '', 'no error');
a5871da3
NC
28
29$a = eval q{
30 $b = eval q{if ($b) {return log 0} 4};
a6d95d3b
NC
31 is ($b, 4, 'inner eval folds constant');
32 is ($@, '', 'no error');
a5871da3
NC
33 5;
34};
a6d95d3b
NC
35is ($a, 5, 'outer eval folds constant');
36is ($@, '', 'no error');
a5871da3 37
5f2d9966
DM
38# warn and die hooks should be disabled during constant folding
39
40{
41 my $c = 0;
42 local $SIG{__WARN__} = sub { $c++ };
43 local $SIG{__DIE__} = sub { $c+= 2 };
44 eval q{
45 is($c, 0, "premature warn/die: $c");
46 my $x = "a"+5;
47 is($c, 1, "missing warn hook");
48 is($x, 5, "a+5");
49 $c = 0;
50 $x = 1/0;
51 };
52 like ($@, qr/division/, "eval caught division");
53 is($c, 2, "missing die hook");
54}