This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dispatch signals when leaving an eval
[perl5.git] / t / op / inc.t
CommitLineData
3510b4a1 1#!./perl -w
760ac839 2
faa5b915
NC
3require './test.pl';
4use strict;
760ac839
LW
5
6# Verify that addition/subtraction properly upgrade to doubles.
1eb770ff 7# These tests are only significant on machines with 32 bit longs,
8# and two's complement negation, but shouldn't fail anywhere.
760ac839 9
3510b4a1
NC
10my $a = 2147483647;
11my $c=$a++;
faa5b915 12cmp_ok($a, '==', 2147483648);
760ac839
LW
13
14$a = 2147483647;
15$c=++$a;
faa5b915 16cmp_ok($a, '==', 2147483648);
760ac839
LW
17
18$a = 2147483647;
19$a=$a+1;
faa5b915 20cmp_ok($a, '==', 2147483648);
760ac839
LW
21
22$a = -2147483648;
23$c=$a--;
faa5b915 24cmp_ok($a, '==', -2147483649);
760ac839
LW
25
26$a = -2147483648;
27$c=--$a;
faa5b915 28cmp_ok($a, '==', -2147483649);
760ac839
LW
29
30$a = -2147483648;
31$a=$a-1;
faa5b915 32cmp_ok($a, '==', -2147483649);
9b0e499b
GS
33
34$a = 2147483648;
35$a = -$a;
36$c=$a--;
faa5b915 37cmp_ok($a, '==', -2147483649);
9b0e499b
GS
38
39$a = 2147483648;
40$a = -$a;
41$c=--$a;
faa5b915 42cmp_ok($a, '==', -2147483649);
9b0e499b
GS
43
44$a = 2147483648;
45$a = -$a;
46$a=$a-1;
faa5b915 47cmp_ok($a, '==', -2147483649);
9b0e499b
GS
48
49$a = 2147483648;
50$b = -$a;
51$c=$b--;
faa5b915 52cmp_ok($b, '==', -$a-1);
9b0e499b
GS
53
54$a = 2147483648;
55$b = -$a;
56$c=--$b;
faa5b915 57cmp_ok($b, '==', -$a-1);
9b0e499b
GS
58
59$a = 2147483648;
60$b = -$a;
61$b=$b-1;
faa5b915 62cmp_ok($b, '==', -(++$a));
3510b4a1 63
f9b9d3d6 64$a = undef;
faa5b915 65is($a++, '0', "postinc undef returns '0'");
f9b9d3d6
HS
66
67$a = undef;
faa5b915 68is($a--, undef, "postdec undef returns undef");
f9b9d3d6 69
3510b4a1
NC
70# Verify that shared hash keys become unshared.
71
72sub check_same {
73 my ($orig, $suspect) = @_;
74 my $fail;
75 while (my ($key, $value) = each %$suspect) {
76 if (exists $orig->{$key}) {
77 if ($orig->{$key} ne $value) {
78 print "# key '$key' was '$orig->{$key}' now '$value'\n";
79 $fail = 1;
80 }
81 } else {
82 print "# key '$key' is '$orig->{$key}', unexpect.\n";
83 $fail = 1;
84 }
85 }
86 foreach (keys %$orig) {
87 next if (exists $suspect->{$_});
88 print "# key '$_' was '$orig->{$_}' now missing\n";
89 $fail = 1;
90 }
91 ok (!$fail);
92}
93
94my (%orig) = my (%inc) = my (%dec) = my (%postinc) = my (%postdec)
95 = (1 => 1, ab => "ab");
96my %up = (1=>2, ab => 'ac');
97my %down = (1=>0, ab => -1);
98
99foreach (keys %inc) {
100 my $ans = $up{$_};
101 my $up;
102 eval {$up = ++$_};
faa5b915
NC
103 is($up, $ans);
104 is($@, '');
3510b4a1
NC
105}
106
107check_same (\%orig, \%inc);
108
109foreach (keys %dec) {
110 my $ans = $down{$_};
111 my $down;
112 eval {$down = --$_};
faa5b915
NC
113 is($down, $ans);
114 is($@, '');
3510b4a1
NC
115}
116
117check_same (\%orig, \%dec);
118
119foreach (keys %postinc) {
120 my $ans = $postinc{$_};
121 my $up;
122 eval {$up = $_++};
faa5b915
NC
123 is($up, $ans);
124 is($@, '');
3510b4a1
NC
125}
126
127check_same (\%orig, \%postinc);
128
129foreach (keys %postdec) {
130 my $ans = $postdec{$_};
131 my $down;
132 eval {$down = $_--};
faa5b915
NC
133 is($down, $ans);
134 is($@, '');
3510b4a1
NC
135}
136
137check_same (\%orig, \%postdec);
ef088171
NC
138
139{
140 no warnings 'uninitialized';
840378f5 141 my ($x, $y);
ef088171
NC
142 eval {
143 $y ="$x\n";
144 ++$x;
145 };
faa5b915
NC
146 cmp_ok($x, '==', 1);
147 is($@, '');
ef088171 148
840378f5 149 my ($p, $q);
ef088171
NC
150 eval {
151 $q ="$p\n";
152 --$p;
153 };
faa5b915
NC
154 cmp_ok($p, '==', -1);
155 is($@, '');
ef088171 156}
f4eee32f
NC
157
158$a = 2147483648;
159$c=--$a;
faa5b915 160cmp_ok($a, '==', 2147483647);
f4eee32f
NC
161
162
163$a = 2147483648;
164$c=$a--;
faa5b915 165cmp_ok($a, '==', 2147483647);
679d6c4e
HS
166
167{
168 use integer;
169 my $x = 0;
170 $x++;
faa5b915 171 cmp_ok($x, '==', 1, "(void) i_postinc");
679d6c4e 172 $x--;
faa5b915 173 cmp_ok($x, '==', 0, "(void) i_postdec");
679d6c4e 174}
b88df990
NC
175
176# I'm sure that there's an IBM format with a 48 bit mantissa
177# IEEE doubles have a 53 bit mantissa
178# 80 bit long doubles have a 64 bit mantissa
179# sparcs have a 112 bit mantissa for their long doubles. Just to be awkward :-)
180
b68c599a 181my $h_uv_max = 1 + (~0 >> 1);
b88df990
NC
182my $found;
183for my $n (47..113) {
184 my $power_of_2 = 2**$n;
185 my $plus_1 = $power_of_2 + 1;
186 next if $plus_1 != $power_of_2;
b68c599a
NC
187 my ($start_p, $start_n);
188 if ($h_uv_max > $power_of_2 / 2) {
189 my $uv_max = 1 + 2 * (~0 >> 1);
190 # UV_MAX is 2**$something - 1, so subtract 1 to get the start value
191 $start_p = $uv_max - 1;
192 # whereas IV_MIN is -(2**$something), so subtract 2
193 $start_n = -$h_uv_max + 2;
194 print "# Mantissa overflows at 2**$n ($power_of_2)\n";
195 print "# But max UV ($uv_max) is greater so testing that\n";
196 } else {
197 print "# Testing 2**$n ($power_of_2) which overflows the mantissa\n";
198 $start_p = int($power_of_2 - 2);
199 $start_n = -$start_p;
200 my $check = $power_of_2 - 2;
201 die "Something wrong with our rounding assumptions: $check vs $start_p"
202 unless $start_p == $check;
203 }
b88df990 204
2353548e
NC
205 foreach ([$start_p, '++$i', 'pre-inc', 'inc'],
206 [$start_p, '$i++', 'post-inc', 'inc'],
207 [$start_n, '--$i', 'pre-dec', 'dec'],
208 [$start_n, '$i--', 'post-dec', 'dec']) {
209 my ($start, $action, $description, $act) = @$_;
faa5b915
NC
210 my $code = eval << "EOC" or die $@;
211sub {
212 no warnings 'imprecision';
213 my \$i = \$start;
214 for(0 .. 3) {
215 my \$a = $action;
216 }
7db8714f 217}
7db8714f 218EOC
faa5b915
NC
219
220 warning_is($code, undef, "$description under no warnings 'imprecision'");
221
222 $code = eval << "EOC" or die $@;
223sub {
224 use warnings 'imprecision';
225 my \$i = \$start;
226 for(0 .. 3) {
227 my \$a = $action;
228 }
229}
230EOC
231
232 warnings_like($code, [(qr/Lost precision when ${act}rementing -?\d+/) x 2],
233 "$description under use warnings 'imprecision'");
b88df990
NC
234 }
235
236 $found = 1;
237 last;
238}
239die "Could not find a value which overflows the mantissa" unless $found;
6e592b3a
BM
240
241# these will segfault if they fail
242
243sub PVBM () { 'foo' }
244{ my $dummy = index 'foo', PVBM }
245
faa5b915
NC
246isnt(scalar eval { my $pvbm = PVBM; $pvbm++ }, undef);
247isnt(scalar eval { my $pvbm = PVBM; $pvbm-- }, undef);
248isnt(scalar eval { my $pvbm = PVBM; ++$pvbm }, undef);
249isnt(scalar eval { my $pvbm = PVBM; --$pvbm }, undef);
6e592b3a 250
7dcb9b98
DM
251# #9466
252
253# don't use pad TARG when the thing you're copying is a ref, or the referent
254# won't get freed.
255{
256 package P9466;
257 my $x;
258 sub DESTROY { $x = 1 }
259 for (0..1) {
260 $x = 0;
261 my $a = bless {};
262 my $b = $_ ? $a++ : $a--;
263 undef $a; undef $b;
faa5b915 264 ::is($x, 1, "9466 case $_");
7dcb9b98
DM
265 }
266}
faa5b915
NC
267
268done_testing();