This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / not.t
CommitLineData
a758b0b5
MHM
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
85b7d9b3 9plan tests => 24;
a758b0b5 10
1b293ed1 11# not() tests
877c9ac8
JK
12pass("logical negation of empty list") if not();
13is(not(), 1, "logical negation of empty list in numeric comparison");
14is(not(), not(0),
15 "logical negation of empty list compared with logical negation of false value");
1b293ed1
SP
16
17# test not(..) and !
877c9ac8
JK
18note("parens needed around second argument in next two tests\nto preserve list context inside function call");
19is(! 1, (not 1),
20 "high- and low-precedence logical negation of true value");
21is(! 0, (not 0),
22 "high- and low-precedence logical negation of false value");
23is(! (0, 0), not(0, 0),
24 "high- and low-precedence logical negation of lists");
1b293ed1
SP
25
26# test the return of !
27{
28 my $not0 = ! 0;
29 my $not1 = ! 1;
30
31 no warnings;
877c9ac8
JK
32 ok($not1 == undef,
33 "logical negation (high-precedence) of true value is numerically equal to undefined value");
34 ok($not1 == (),
35 "logical negation (high-precedence) of true value is numerically equal to empty list");
1b293ed1
SP
36
37 use warnings;
877c9ac8
JK
38 ok($not1 eq '',
39 "logical negation (high-precedence) of true value in string context is equal to empty string");
40 ok($not1 == 0,
41 "logical negation (high-precedence) of true value is false in numeric context");
42 ok($not0 == 1,
43 "logical negation (high-precedence) of false value is true in numeric context");
1b293ed1
SP
44}
45
46# test the return of not
47{
48 my $not0 = not 0;
49 my $not1 = not 1;
50
51 no warnings;
877c9ac8
JK
52 ok($not1 == undef,
53 "logical negation (low-precedence) of true value is numerically equal to undefined value");
54 ok($not1 == (),
55 "logical negation (low-precedence) of true value is numerically equal to empty list");
1b293ed1
SP
56
57 use warnings;
877c9ac8
JK
58 ok($not1 eq '',
59 "logical negation (low-precedence) of true value in string context is equal to empty string");
60 ok($not1 == 0,
61 "logical negation (low-precedence) of true value is false in numeric context");
62 ok($not0 == 1,
63 "logical negation (low-precedence) of false value is true in numeric context");
1b293ed1 64}
762dbf22
FC
65
66# test truth of dualvars
67SKIP:
68{
69 my $got_dualvar;
70 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
71 skip "No Scalar::Util::dualvar", 3 unless $got_dualvar;
72 my $a = Scalar::Util::dualvar(3, "");
73 is not($a), 1, 'not(dualvar) ignores int when string is false';
74 my $b = Scalar::Util::dualvar(3.3,"");
75 is not($b), 1, 'not(dualvar) ignores float when string is false';
76 my $c = Scalar::Util::dualvar(0,"1");
77 is not($c), "", 'not(dualvar) ignores false int when string is true';
78}
95c52ddd 79
85b7d9b3
FC
80# test truth of regexps
81is not(${qr//}), "", 'dereferenced regexps are true';
82
95c52ddd
FC
83# not’s return value should be read-only, as it is the same global scalar
84# each time (and test that it is, too).
85*yes = \not 0;
86*no = \not 1;
87for (!0) { eval { $_ = 43 } }
88like $@, qr/^Modification of a read-only value attempted at /,
89 'not 0 is read-only';
90for (!1) { eval { $_ = 43 } }
91like $@, qr/^Modification of a read-only value attempted at /,
92 'not 1 is read-only';
93require Config;
95c52ddd
FC
94is \!0, \$yes, '!0 returns the same value each time [perl #114838]';
95is \!1, \$no, '!1 returns the same value each time [perl #114838]';