diff options
author | Jim Cromie <jim.cromie@gmail.com> | 2011-06-01 14:13:40 -0700 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2011-06-01 14:39:15 -0700 |
commit | 183eb698e2ceb8ab2d581de28f0b067e3c67af0d (patch) | |
tree | cf5c7ed547d32869b5cd909b3ca9141ac9d65b29 /ext | |
parent | 1c70fb82f95ad11a9d4a425652926cdbd9db23f4 (diff) | |
download | perl-183eb698e2ceb8ab2d581de28f0b067e3c67af0d.tar.gz |
[perl #91946] add constant folding tests
test arithmetic folding, conditional folding (both true & false),
and string, lc() & uc() folds, and mixed string.int folds.
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
Diffstat (limited to 'ext')
-rw-r--r-- | ext/B/t/optree_constants.t | 153 |
1 files changed, 152 insertions, 1 deletions
diff --git a/ext/B/t/optree_constants.t b/ext/B/t/optree_constants.t index 7b1d2672a6..471000a859 100644 --- a/ext/B/t/optree_constants.t +++ b/ext/B/t/optree_constants.t @@ -16,7 +16,7 @@ BEGIN { use OptreeCheck; # ALSO DOES @ARGV HANDLING !!!!!! use Config; -plan tests => 57; +plan tests => 67; ################################# @@ -243,6 +243,157 @@ checkOptree ( name => 'call many in a print statement', strip_open_hints => 1, expect => $expect, expect_nt => $expect_nt); +# test constant expression folding + +checkOptree ( name => 'arithmetic constant folding in print', + code => 'print 1+2+3', + strip_open_hints => 1, + expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT'); +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 937 (eval 53):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const[IV 6] s ->4 +EOT_EOT +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 937 (eval 53):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const(IV 6) s ->4 +EONT_EONT + +checkOptree ( name => 'string constant folding in print', + code => 'print "foo"."bar"', + strip_open_hints => 1, + expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT'); +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 942 (eval 55):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const[PV "foobar"] s ->4 +EOT_EOT +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 942 (eval 55):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const(PV "foobar") s ->4 +EONT_EONT + +checkOptree ( name => 'boolean or folding', + code => 'print "foobar" if 1 or 0', + strip_open_hints => 1, + expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT'); +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 942 (eval 55):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const[PV "foobar"] s ->4 +EOT_EOT +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 942 (eval 55):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const(PV "foobar") s ->4 +EONT_EONT + +checkOptree ( name => 'lc*,uc*,gt,lt,ge,le,cmp', + code => sub { + $s = uc('foo.').ucfirst('bar.').lc('LOW.').lcfirst('LOW'); + print "a-lt-b" if "a" lt "b"; + print "b-gt-a" if "b" gt "a"; + print "a-le-b" if "a" le "b"; + print "b-ge-a" if "b" ge "a"; + print "b-cmp-a" if "b" cmp "a"; + print "a-gt-b" if "a" gt "b"; # should be suppressed + }, + strip_open_hints => 1, + expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT'); +# r <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->r +# 1 <;> nextstate(main 916 optree_constants.t:307) v:{ ->2 +# 4 <2> sassign vKS/2 ->5 +# 2 <$> const[PV "FOO.Bar.low.lOW"] s ->3 +# - <1> ex-rv2sv sKRM*/1 ->4 +# 3 <#> gvsv[*s] s ->4 +# 5 <;> nextstate(main 916 optree_constants.t:308) v:{ ->6 +# 8 <@> print vK ->9 +# 6 <0> pushmark s ->7 +# 7 <$> const[PV "a-lt-b"] s ->8 +# 9 <;> nextstate(main 916 optree_constants.t:309) v:{ ->a +# c <@> print vK ->d +# a <0> pushmark s ->b +# b <$> const[PV "b-gt-a"] s ->c +# d <;> nextstate(main 916 optree_constants.t:310) v:{ ->e +# g <@> print vK ->h +# e <0> pushmark s ->f +# f <$> const[PV "a-le-b"] s ->g +# h <;> nextstate(main 916 optree_constants.t:311) v:{ ->i +# k <@> print vK ->l +# i <0> pushmark s ->j +# j <$> const[PV "b-ge-a"] s ->k +# l <;> nextstate(main 916 optree_constants.t:312) v:{ ->m +# o <@> print vK ->p +# m <0> pushmark s ->n +# n <$> const[PV "b-cmp-a"] s ->o +# p <;> nextstate(main 916 optree_constants.t:313) v:{ ->q +# q <$> const[PVNV 0] s/SHORT ->r +EOT_EOT +# r <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->r +# 1 <;> nextstate(main 916 optree_constants.t:307) v:{ ->2 +# 4 <2> sassign vKS/2 ->5 +# 2 <$> const(PV "FOO.Bar.low.lOW") s ->3 +# - <1> ex-rv2sv sKRM*/1 ->4 +# 3 <$> gvsv(*s) s ->4 +# 5 <;> nextstate(main 916 optree_constants.t:308) v:{ ->6 +# 8 <@> print vK ->9 +# 6 <0> pushmark s ->7 +# 7 <$> const(PV "a-lt-b") s ->8 +# 9 <;> nextstate(main 916 optree_constants.t:309) v:{ ->a +# c <@> print vK ->d +# a <0> pushmark s ->b +# b <$> const(PV "b-gt-a") s ->c +# d <;> nextstate(main 916 optree_constants.t:310) v:{ ->e +# g <@> print vK ->h +# e <0> pushmark s ->f +# f <$> const(PV "a-le-b") s ->g +# h <;> nextstate(main 916 optree_constants.t:311) v:{ ->i +# k <@> print vK ->l +# i <0> pushmark s ->j +# j <$> const(PV "b-ge-a") s ->k +# l <;> nextstate(main 916 optree_constants.t:312) v:{ ->m +# o <@> print vK ->p +# m <0> pushmark s ->n +# n <$> const(PV "b-cmp-a") s ->o +# p <;> nextstate(main 916 optree_constants.t:313) v:{ ->q +# q <$> const(SPECIAL sv_no) s/SHORT ->r +EONT_EONT + +checkOptree ( name => 'mixed constant folding, with explicit braces', + code => 'print "foo"."bar".(2+3)', + strip_open_hints => 1, + expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT'); +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 977 (eval 28):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const[PV "foobar5"] s ->4 +EOT_EOT +# 5 <1> leavesub[1 ref] K/REFC,1 ->(end) +# - <@> lineseq KP ->5 +# 1 <;> nextstate(main 977 (eval 28):1) v ->2 +# 4 <@> print sK ->5 +# 2 <0> pushmark s ->3 +# 3 <$> const(PV "foobar5") s ->4 +EONT_EONT + __END__ =head NB |