6 # Tests of post/pre - increment/decrement operators.
8 # Verify that addition/subtraction properly upgrade to doubles.
9 # These tests are only significant on machines with 32 bit longs,
10 # and two's complement negation, but shouldn't fail anywhere.
14 cmp_ok($a, '==', 2147483648, "postincrement properly upgrades to double");
18 cmp_ok($a, '==', 2147483648, "preincrement properly upgrades to double");
22 cmp_ok($a, '==', 2147483648, "addition properly upgrades to double");
26 cmp_ok($a, '==', -2147483649, "postdecrement properly upgrades to double");
30 cmp_ok($a, '==', -2147483649, "predecrement properly upgrades to double");
34 cmp_ok($a, '==', -2147483649, "subtraction properly upgrades to double");
39 cmp_ok($a, '==', -2147483649,
40 "negation and postdecrement properly upgrade to double");
45 cmp_ok($a, '==', -2147483649,
46 "negation and predecrement properly upgrade to double");
51 cmp_ok($a, '==', -2147483649,
52 "negation and subtraction properly upgrade to double");
57 cmp_ok($b, '==', -$a-1, "negation, postdecrement and additional negation");
62 cmp_ok($b, '==', -$a-1, "negation, predecrement and additional negation");
67 cmp_ok($b, '==', -(++$a),
68 "negation, subtraction, preincrement and additional negation");
71 is($a++, '0', "postinc undef returns '0'");
74 is($a--, undef, "postdec undef returns undef");
76 # Verify that shared hash keys become unshared.
79 my ($orig, $suspect) = @_;
81 while (my ($key, $value) = each %$suspect) {
82 if (exists $orig->{$key}) {
83 if ($orig->{$key} ne $value) {
84 print "# key '$key' was '$orig->{$key}' now '$value'\n";
88 print "# key '$key' is '$orig->{$key}', unexpect.\n";
92 foreach (keys %$orig) {
93 next if (exists $suspect->{$_});
94 print "# key '$_' was '$orig->{$_}' now missing\n";
97 ok (!$fail, "original hashes unchanged");
100 my (%orig) = my (%inc) = my (%dec) = my (%postinc) = my (%postdec)
101 = (1 => 1, ab => "ab");
102 my %up = (1=>2, ab => 'ac');
103 my %down = (1=>0, ab => -1);
105 foreach (keys %inc) {
109 is($up, $ans, "key '$_' incremented correctly");
110 is($@, '', "no error condition");
113 check_same (\%orig, \%inc);
115 foreach (keys %dec) {
119 is($down, $ans, "key '$_' decremented correctly");
120 is($@, '', "no error condition");
123 check_same (\%orig, \%dec);
125 foreach (keys %postinc) {
126 my $ans = $postinc{$_};
129 is($up, $ans, "assignment preceded postincrement");
130 is($@, '', "no error condition");
133 check_same (\%orig, \%postinc);
135 foreach (keys %postdec) {
136 my $ans = $postdec{$_};
139 is($down, $ans, "assignment preceded postdecrement");
140 is($@, '', "no error condition");
143 check_same (\%orig, \%postdec);
146 no warnings 'uninitialized';
152 cmp_ok($x, '==', 1, "preincrement of previously uninitialized variable");
153 is($@, '', "no error condition");
160 cmp_ok($p, '==', -1, "predecrement of previously uninitialized variable");
161 is($@, '', "no error condition");
166 cmp_ok($a, '==', 2147483647, "predecrement properly downgrades from double");
171 cmp_ok($a, '==', 2147483647, "postdecrement properly downgrades from double");
177 cmp_ok($x, '==', 1, "(void) i_postinc");
179 cmp_ok($x, '==', 0, "(void) i_postdec");
182 # I'm sure that there's an IBM format with a 48 bit mantissa
183 # IEEE doubles have a 53 bit mantissa
184 # 80 bit long doubles have a 64 bit mantissa
185 # sparcs have a 112 bit mantissa for their long doubles. Just to be awkward :-)
187 my $h_uv_max = 1 + (~0 >> 1);
189 for my $n (47..113) {
190 my $power_of_2 = 2**$n;
191 my $plus_1 = $power_of_2 + 1;
192 next if $plus_1 != $power_of_2;
193 my ($start_p, $start_n);
194 if ($h_uv_max > $power_of_2 / 2) {
195 my $uv_max = 1 + 2 * (~0 >> 1);
196 # UV_MAX is 2**$something - 1, so subtract 1 to get the start value
197 $start_p = $uv_max - 1;
198 # whereas IV_MIN is -(2**$something), so subtract 2
199 $start_n = -$h_uv_max + 2;
200 print "# Mantissa overflows at 2**$n ($power_of_2)\n";
201 print "# But max UV ($uv_max) is greater so testing that\n";
203 print "# Testing 2**$n ($power_of_2) which overflows the mantissa\n";
204 $start_p = int($power_of_2 - 2);
205 $start_n = -$start_p;
206 my $check = $power_of_2 - 2;
207 die "Something wrong with our rounding assumptions: $check vs $start_p"
208 unless $start_p == $check;
211 foreach ([$start_p, '++$i', 'pre-inc', 'inc'],
212 [$start_p, '$i++', 'post-inc', 'inc'],
213 [$start_n, '--$i', 'pre-dec', 'dec'],
214 [$start_n, '$i--', 'post-dec', 'dec']) {
215 my ($start, $action, $description, $act) = @$_;
216 my $code = eval << "EOC" or die $@;
218 no warnings 'imprecision';
226 warning_is($code, undef, "$description under no warnings 'imprecision'");
228 $code = eval << "EOC" or die $@;
230 use warnings 'imprecision';
238 warnings_like($code, [(qr/Lost precision when ${act}rementing -?\d+/) x 2],
239 "$description under use warnings 'imprecision'");
245 die "Could not find a value which overflows the mantissa" unless $found;
247 # these will segfault if they fail
249 sub PVBM () { 'foo' }
250 { my $dummy = index 'foo', PVBM }
252 isnt(scalar eval { my $pvbm = PVBM; $pvbm++ }, undef, "postincrement defined");
253 isnt(scalar eval { my $pvbm = PVBM; $pvbm-- }, undef, "postdecrement defined");
254 isnt(scalar eval { my $pvbm = PVBM; ++$pvbm }, undef, "preincrement defined");
255 isnt(scalar eval { my $pvbm = PVBM; --$pvbm }, undef, "predecrement defined");
259 # don't use pad TARG when the thing you're copying is a ref, or the referent
264 sub DESTROY { $x = 1 }
268 my $b = $_ ? $a++ : $a--;
270 ::is($x, 1, "9466 case $_");