This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
miniperl/minitest cannot do these tests.
[perl5.git] / t / op / lop.t
CommitLineData
bf4b1e52
GS
1#!./perl
2
3#
1fb2d101 4# test the logical operators '&&', '||', '!', 'and', 'or', , 'xor', 'not'
bf4b1e52
GS
5#
6
7BEGIN {
8 chdir 't' if -d 't';
73ee8f56 9 require './test.pl';
624c42e2 10 set_up_inc('../lib');
bf4b1e52
GS
11}
12
1fb2d101 13plan tests => 33;
bf4b1e52 14
bf4b1e52
GS
15for my $i (undef, 0 .. 2, "", "0 but true") {
16 my $true = 1;
17 my $false = 0;
18 for my $j (undef, 0 .. 2, "", "0 but true") {
19 $true &&= !(
20 ((!$i || !$j) != !($i && $j))
21 or (!($i || $j) != (!$i && !$j))
22 or (!!($i || $j) != !(!$i && !$j))
23 or (!(!$i || !$j) != !!($i && $j))
24 );
25 $false ||= (
26 ((!$i || !$j) == !!($i && $j))
27 and (!!($i || $j) == (!$i && !$j))
28 and ((!$i || $j) == ($i && !$j))
29 and (($i || !$j) != (!$i && $j))
30 );
31 }
73ee8f56
CK
32 my $m = ! defined $i ? 'undef'
33 : $i eq '' ? 'empty string'
34 : $i;
35 ok( $true, "true: $m");
36 ok( ! $false, "false: $m");
bf4b1e52
GS
37}
38
bf4b1e52
GS
39my $i = 0;
40(($i ||= 1) &&= 3) += 4;
6f02a299 41is( $i, 7, '||=, &&=');
edbe35ea
VP
42
43my ($x, $y) = (1, 8);
44$i = !$x || $y;
6f02a299 45is( $i, 8, 'negation precedence with ||' );
edbe35ea 46
07f3cdf5
VP
47++$y;
48$i = !$x || !$x || !$x || $y;
6f02a299 49is( $i, 9, 'negation precedence with ||, multiple operands' );
07f3cdf5
VP
50
51$x = 0;
52++$y;
53$i = !$x && $y;
6f02a299 54is( $i, 10, 'negation precedence with &&' );
07f3cdf5
VP
55
56++$y;
57$i = !$x && !$x && !$x && $y;
6f02a299 58is( $i, 11, 'negation precedence with &&, multiple operands' );
f15d0580
AC
59
60# [perl #127952]. This relates to OP_AND and OP_OR with a negated constant
61# on the lhs (either a negated bareword, or a negation of a do{} containing
62# a constant) and a negated non-foldable expression on the rhs. These cases
63# yielded 42 or "Bare" or "str" before the bug was fixed.
64{
65 $x = 42;
66
67 $i = !Bare || !$x;
68 is( $i, '', 'neg-bareword on lhs of || with non-foldable neg-true on rhs' );
69
70 $i = !Bare && !$x;
71 is( $i, '', 'neg-bareword on lhs of && with non-foldable neg-true on rhs' );
72
73 $i = do { !$x if !Bare };
74 is( $i, '', 'neg-bareword on rhs of modifier-if with non-foldable neg-true on lhs' );
75
76 $i = do { !$x unless !Bare };
77 is( $i, '', 'neg-bareword on rhs of modifier-unless with non-foldable neg-true on lhs' );
78
79 $i = !do { "str" } || !$x;
80 is( $i, '', 'neg-do-const on lhs of || with non-foldable neg-true on rhs' );
81
82 $i = !do { "str" } && !$x;
83 is( $i, '', 'neg-do-const on lhs of && with non-foldable neg-true on rhs' );
84}
1fb2d101
DM
85
86# RT #131820
87#
88# It turns out that in 2017, 23 years after the release of perl5,
89# the 'xor' logical operator was still untested in core.
90
91for my $test (
92 [ 0, 0, '' ],
93 [ 0, 1, 1 ],
94 [ 1, 0, 1 ],
95 [ 1, 1, '' ],
96
97 [ 0, 2, 1 ],
98 [ 2, 0, 1 ],
99 [ 2, 2, '' ],
100
101 [ 0, 3, 1 ],
102 [ 3, 0, 1 ],
103 [ 3, 4, '' ],
104) {
105 my ($a,$b, $exp) = @$test;
106 is(($a xor $b), $exp, "($a xor $b) == '$exp'");
107}