This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add comment at undef postinc special case
[perl5.git] / t / op / inc.t
CommitLineData
3510b4a1 1#!./perl -w
760ac839 2
3510b4a1
NC
3# use strict;
4
5print "1..24\n";
6
7my $test = 1;
8
9sub ok {
10 my ($pass, $wrong, $err) = @_;
11 if ($pass) {
12 print "ok $test\n";
13 $test = $test + 1; # Would be doubleplusbad to use ++ in the ++ test.
14 return 1;
15 } else {
16 if ($err) {
17 chomp $err;
18 print "not ok $test # $err\n";
19 } else {
20 if (defined $wrong) {
21 $wrong = ", got $wrong";
22 } else {
23 $wrong = '';
24 }
25 printf "not ok $test # line %d$wrong\n", (caller)[2];
26 }
27 }
28 $test = $test + 1;
29 return;
30}
760ac839
LW
31
32# Verify that addition/subtraction properly upgrade to doubles.
1eb770ff
PP
33# These tests are only significant on machines with 32 bit longs,
34# and two's complement negation, but shouldn't fail anywhere.
760ac839 35
3510b4a1
NC
36my $a = 2147483647;
37my $c=$a++;
38ok ($a == 2147483648, $a);
760ac839
LW
39
40$a = 2147483647;
41$c=++$a;
3510b4a1 42ok ($a == 2147483648, $a);
760ac839
LW
43
44$a = 2147483647;
45$a=$a+1;
3510b4a1 46ok ($a == 2147483648, $a);
760ac839
LW
47
48$a = -2147483648;
49$c=$a--;
3510b4a1 50ok ($a == -2147483649, $a);
760ac839
LW
51
52$a = -2147483648;
53$c=--$a;
3510b4a1 54ok ($a == -2147483649, $a);
760ac839
LW
55
56$a = -2147483648;
57$a=$a-1;
3510b4a1 58ok ($a == -2147483649, $a);
9b0e499b
GS
59
60$a = 2147483648;
61$a = -$a;
62$c=$a--;
3510b4a1 63ok ($a == -2147483649, $a);
9b0e499b
GS
64
65$a = 2147483648;
66$a = -$a;
67$c=--$a;
3510b4a1 68ok ($a == -2147483649, $a);
9b0e499b
GS
69
70$a = 2147483648;
71$a = -$a;
72$a=$a-1;
3510b4a1 73ok ($a == -2147483649, $a);
9b0e499b
GS
74
75$a = 2147483648;
76$b = -$a;
77$c=$b--;
3510b4a1 78ok ($b == -$a-1, $a);
9b0e499b
GS
79
80$a = 2147483648;
81$b = -$a;
82$c=--$b;
3510b4a1 83ok ($b == -$a-1, $a);
9b0e499b
GS
84
85$a = 2147483648;
86$b = -$a;
87$b=$b-1;
3510b4a1
NC
88ok ($b == -(++$a), $a);
89
90# Verify that shared hash keys become unshared.
91
92sub check_same {
93 my ($orig, $suspect) = @_;
94 my $fail;
95 while (my ($key, $value) = each %$suspect) {
96 if (exists $orig->{$key}) {
97 if ($orig->{$key} ne $value) {
98 print "# key '$key' was '$orig->{$key}' now '$value'\n";
99 $fail = 1;
100 }
101 } else {
102 print "# key '$key' is '$orig->{$key}', unexpect.\n";
103 $fail = 1;
104 }
105 }
106 foreach (keys %$orig) {
107 next if (exists $suspect->{$_});
108 print "# key '$_' was '$orig->{$_}' now missing\n";
109 $fail = 1;
110 }
111 ok (!$fail);
112}
113
114my (%orig) = my (%inc) = my (%dec) = my (%postinc) = my (%postdec)
115 = (1 => 1, ab => "ab");
116my %up = (1=>2, ab => 'ac');
117my %down = (1=>0, ab => -1);
118
119foreach (keys %inc) {
120 my $ans = $up{$_};
121 my $up;
122 eval {$up = ++$_};
123 ok ((defined $up and $up eq $ans), $up, $@);
124}
125
126check_same (\%orig, \%inc);
127
128foreach (keys %dec) {
129 my $ans = $down{$_};
130 my $down;
131 eval {$down = --$_};
132 ok ((defined $down and $down eq $ans), $down, $@);
133}
134
135check_same (\%orig, \%dec);
136
137foreach (keys %postinc) {
138 my $ans = $postinc{$_};
139 my $up;
140 eval {$up = $_++};
141 ok ((defined $up and $up eq $ans), $up, $@);
142}
143
144check_same (\%orig, \%postinc);
145
146foreach (keys %postdec) {
147 my $ans = $postdec{$_};
148 my $down;
149 eval {$down = $_--};
150 ok ((defined $down and $down eq $ans), $down, $@);
151}
152
153check_same (\%orig, \%postdec);