Commit | Line | Data |
---|---|---|
bf4b1e52 GS |
1 | #!./perl |
2 | ||
3 | # | |
4 | # test the logical operators '&&', '||', '!', 'and', 'or', 'not' | |
5 | # | |
6 | ||
7 | BEGIN { | |
8 | chdir 't' if -d 't'; | |
20822f61 | 9 | @INC = '../lib'; |
73ee8f56 | 10 | require './test.pl'; |
bf4b1e52 GS |
11 | } |
12 | ||
73ee8f56 | 13 | plan tests => 17; |
bf4b1e52 | 14 | |
bf4b1e52 GS |
15 | for 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 |
39 | my $i = 0; |
40 | (($i ||= 1) &&= 3) += 4; | |
6f02a299 | 41 | is( $i, 7, '||=, &&='); |
edbe35ea VP |
42 | |
43 | my ($x, $y) = (1, 8); | |
44 | $i = !$x || $y; | |
6f02a299 | 45 | is( $i, 8, 'negation precedence with ||' ); |
edbe35ea | 46 | |
07f3cdf5 VP |
47 | ++$y; |
48 | $i = !$x || !$x || !$x || $y; | |
6f02a299 | 49 | is( $i, 9, 'negation precedence with ||, multiple operands' ); |
07f3cdf5 VP |
50 | |
51 | $x = 0; | |
52 | ++$y; | |
53 | $i = !$x && $y; | |
6f02a299 | 54 | is( $i, 10, 'negation precedence with &&' ); |
07f3cdf5 VP |
55 | |
56 | ++$y; | |
57 | $i = !$x && !$x && !$x && $y; | |
6f02a299 | 58 | is( $i, 11, 'negation precedence with &&, multiple operands' ); |