Commit | Line | Data |
---|---|---|
8705167b SP |
1 | #!./perl -w |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
8705167b | 5 | require './test.pl'; |
624c42e2 | 6 | set_up_inc('../lib'); |
8705167b SP |
7 | } |
8 | ||
031c3a38 | 9 | plan tests => 48; |
8705167b SP |
10 | |
11 | # Some of these will cause warnings if left on. Here we're checking the | |
12 | # functionality, not the warnings. | |
13 | no warnings "numeric"; | |
14 | ||
15 | # test cases based on [perl #36675] -'-10' eq '+10' | |
16 | is(- 10, -10, "Simple numeric negation to negative"); | |
17 | is(- -10, 10, "Simple numeric negation to positive"); | |
18 | is(-"10", -10, "Negation of a positive string to negative"); | |
19 | is(-"10.0", -10, "Negation of a positive decimal sting to negative"); | |
20 | is(-"10foo", -10, "Negation of a numeric-lead string returns negation of numeric"); | |
1ccd2973 | 21 | is(-"-10", 10, 'Negation of string starting with "-" returns a positive number - integer'); |
e38171cf FC |
22 | "-10" =~ /(.*)/; |
23 | is(-$1, 10, 'Negation of magical string starting with "-" - integer'); | |
1ccd2973 | 24 | is(-"-10.0", 10.0, 'Negation of string starting with "-" returns a positive number - decimal'); |
e38171cf FC |
25 | "-10.0" =~ /(.*)/; |
26 | is(-$1, 10.0, 'Negation of magical string starting with "-" - decimal'); | |
8705167b SP |
27 | is(-"-10foo", "+10foo", 'Negation of string starting with "-" returns a string starting with "+" - non-numeric'); |
28 | is(-"xyz", "-xyz", 'Negation of a negative string adds "-" to the front'); | |
29 | is(-"-xyz", "+xyz", "Negation of a negative string to positive"); | |
30 | is(-"+xyz", "-xyz", "Negation of a positive string to negative"); | |
31 | is(-bareword, "-bareword", "Negation of bareword treated like a string"); | |
32 | is(- -bareword, "+bareword", "Negation of -bareword returns string +bareword"); | |
33 | is(-" -10", 10, "Negation of a whitespace-lead numeric string"); | |
34 | is(-" -10.0", 10, "Negation of a whitespace-lead decimal string"); | |
8a5decd8 FC |
35 | is(-" -10foo", 10, |
36 | "Negation of a whitespace-lead sting starting with a numeric"); | |
031c3a38 | 37 | is(-"-e1", "+e1", "Negation of e1"); |
8a5decd8 FC |
38 | |
39 | $x = "dogs"; | |
40 | ()=0+$x; | |
41 | is -$x, '-dogs', 'cached numeric value does not sabotage string negation'; | |
42 | ||
43 | is(-"97656250000000000", -97656250000000000, '-bigint vs -"bigint"'); | |
7dbe3150 FC |
44 | "9765625000000000" =~ /(\d+)/; |
45 | is -$1, -"$1", '-$1 vs -"$1" with big int'; | |
d5aa71b8 FC |
46 | |
47 | $a = "%apples"; | |
48 | chop($au = "%apples\x{100}"); | |
49 | is(-$au, -$a, 'utf8 flag makes no difference for string negation'); | |
50 | is -"\x{100}", 0, '-(non-ASCII) is equivalent to -(punct)'; | |
7dbe3150 FC |
51 | |
52 | sub TIESCALAR { bless[] } | |
53 | sub STORE { $_[0][0] = $_[1] } | |
54 | sub FETCH { $_[0][0] } | |
55 | ||
56 | tie $t, ""; | |
57 | $a = "97656250000000000"; | |
58 | () = 0+$a; | |
59 | $t = $a; | |
60 | is -$t, -97656250000000000, 'magic str+int dualvar'; | |
1c2b3fd6 FC |
61 | |
62 | { # Repeat most of the tests under use integer | |
63 | use integer; | |
64 | is(- 10, -10, "Simple numeric negation to negative"); | |
65 | is(- -10, 10, "Simple numeric negation to positive"); | |
66 | is(-"10", -10, "Negation of a positive string to negative"); | |
67 | is(-"10.0", -10, "Negation of a positive decimal sting to negative"); | |
68 | is(-"10foo", -10, | |
69 | "Negation of a numeric-lead string returns negation of numeric"); | |
70 | is(-"-10", 10, | |
71 | 'Negation of string starting with "-" returns a positive number -' | |
72 | .' integer'); | |
73 | "-10" =~ /(.*)/; | |
74 | is(-$1, 10, 'Negation of magical string starting with "-" - integer'); | |
75 | is(-"-10.0", 10, | |
76 | 'Negation of string starting with "-" returns a positive number - ' | |
77 | .'decimal'); | |
78 | "-10.0" =~ /(.*)/; | |
79 | is(-$1, 10, 'Negation of magical string starting with "-" - decimal'); | |
80 | is(-"-10foo", "+10foo", | |
81 | 'Negation of string starting with "-" returns a string starting ' | |
82 | .'with "+" - non-numeric'); | |
83 | is(-"xyz", "-xyz", | |
84 | 'Negation of a negative string adds "-" to the front'); | |
85 | is(-"-xyz", "+xyz", "Negation of a negative string to positive"); | |
86 | is(-"+xyz", "-xyz", "Negation of a positive string to negative"); | |
87 | is(-bareword, "-bareword", | |
88 | "Negation of bareword treated like a string"); | |
89 | is(- -bareword, "+bareword", | |
90 | "Negation of -bareword returns string +bareword"); | |
91 | is(-" -10", 10, "Negation of a whitespace-lead numeric string"); | |
92 | is(-" -10.0", 10, "Negation of a whitespace-lead decimal string"); | |
93 | is(-" -10foo", 10, | |
94 | "Negation of a whitespace-lead sting starting with a numeric"); | |
031c3a38 | 95 | is(-"-e1", "+e1", "Negation of e1 (use integer)"); |
1c2b3fd6 FC |
96 | |
97 | $x = "dogs"; | |
98 | ()=0+$x; | |
99 | is -$x, '-dogs', | |
100 | 'cached numeric value does not sabotage string negation'; | |
101 | ||
102 | $a = "%apples"; | |
103 | chop($au = "%apples\x{100}"); | |
104 | is(-$au, -$a, 'utf8 flag makes no difference for string negation'); | |
105 | is -"\x{100}", 0, '-(non-ASCII) is equivalent to -(punct)'; | |
106 | } | |
fcbc518d FC |
107 | |
108 | # [perl #120288] use integer should not stop barewords from being quoted | |
109 | { | |
110 | use strict; | |
111 | use integer; | |
112 | is eval "return -a"||$@, "-a", '-bareword under strict+integer'; | |
113 | } |