This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
SYN SYN
[perl5.git] / t / op / arith.t
1 #!./perl -w
2
3 print "1..109\n";
4
5 sub try ($$) {
6    print +($_[1] ? "ok" : "not ok"), " $_[0]\n";
7 }
8 sub tryeq ($$$) {
9   if ($_[1] == $_[2]) {
10     print "ok $_[0]\n";
11   } else {
12     print "not ok $_[0] # $_[1] != $_[2]\n";
13   }
14 }
15
16 tryeq 1,  13 %  4, 1;
17 tryeq 2, -13 %  4, 3;
18 tryeq 3,  13 % -4, -3;
19 tryeq 4, -13 % -4, -1;
20
21 my $limit = 1e6;
22
23 # Division (and modulo) of floating point numbers
24 # seem to be rather sloppy in Cray.
25 $limit = 1e8 if $^O eq 'unicos';
26
27 try 5, abs( 13e21 %  4e21 -  1e21) < $limit;
28 try 6, abs(-13e21 %  4e21 -  3e21) < $limit;
29 try 7, abs( 13e21 % -4e21 - -3e21) < $limit;
30 try 8, abs(-13e21 % -4e21 - -1e21) < $limit;
31
32 # UVs should behave properly
33
34 tryeq 9, 4063328477 % 65535, 27407;
35 tryeq 10, 4063328477 % 4063328476, 1;
36 tryeq 11, 4063328477 % 2031664238, 1;
37 tryeq 12, 2031664238 % 4063328477, 2031664238;
38
39 # These should trigger wrapping on 32 bit IVs and UVs
40
41 tryeq 13, 2147483647 + 0, 2147483647;
42
43 # IV + IV promote to UV
44 tryeq 14, 2147483647 + 1, 2147483648;
45 tryeq 15, 2147483640 + 10, 2147483650;
46 tryeq 16, 2147483647 + 2147483647, 4294967294;
47 # IV + UV promote to NV
48 tryeq 17, 2147483647 + 2147483649, 4294967296;
49 # UV + IV promote to NV
50 tryeq 18, 4294967294 + 2, 4294967296;
51 # UV + UV promote to NV
52 tryeq 19, 4294967295 + 4294967295, 8589934590;
53
54 # UV + IV to IV
55 tryeq 20, 2147483648 + -1, 2147483647;
56 tryeq 21, 2147483650 + -10, 2147483640;
57 # IV + UV to IV
58 tryeq 22, -1 + 2147483648, 2147483647;
59 tryeq 23, -10 + 4294967294, 4294967284;
60 # IV + IV to NV
61 tryeq 24, -2147483648 + -2147483648, -4294967296;
62 tryeq 25, -2147483640 + -10, -2147483650;
63
64 # Hmm. Don't forget the simple stuff
65 tryeq 26, 1 + 1, 2;
66 tryeq 27, 4 + -2, 2;
67 tryeq 28, -10 + 100, 90;
68 tryeq 29, -7 + -9, -16;
69 tryeq 30, -63 + +2, -61;
70 tryeq 31, 4 + -1, 3;
71 tryeq 32, -1 + 1, 0;
72 tryeq 33, +29 + -29, 0;
73 tryeq 34, -1 + 4, 3;
74 tryeq 35, +4 + -17, -13;
75
76 # subtraction
77 tryeq 36, 3 - 1, 2;
78 tryeq 37, 3 - 15, -12;
79 tryeq 38, 3 - -7, 10;
80 tryeq 39, -156 - 5, -161;
81 tryeq 40, -156 - -5, -151;
82 tryeq 41, -5 - -12, 7;
83 tryeq 42, -3 - -3, 0;
84 tryeq 43, 15 - 15, 0;
85
86 tryeq 44, 2147483647 - 0, 2147483647;
87 tryeq 45, 2147483648 - 0, 2147483648;
88 tryeq 46, -2147483648 - 0, -2147483648;
89
90 tryeq 47, 0 - -2147483647, 2147483647;
91 tryeq 48, -1 - -2147483648, 2147483647;
92 tryeq 49, 2 - -2147483648, 2147483650;
93
94 tryeq 50, 4294967294 - 3, 4294967291;
95 tryeq 51, -2147483648 - -1, -2147483647;
96
97 # IV - IV promote to UV
98 tryeq 52, 2147483647 - -1, 2147483648;
99 tryeq 53, 2147483647 - -2147483648, 4294967295;
100 # UV - IV promote to NV
101 tryeq 54, 4294967294 - -3, 4294967297;
102 # IV - IV promote to NV
103 tryeq 55, -2147483648 - +1, -2147483649;
104 # UV - UV promote to IV
105 tryeq 56, 2147483648 - 2147483650, -2;
106 # IV - UV promote to IV
107 tryeq 57, 2000000000 - 4000000000, -2000000000;
108
109 # No warnings should appear;
110 my $a;
111 $a += 1;
112 tryeq 58, $a, 1;
113 undef $a;
114 $a += -1;
115 tryeq 59, $a, -1;
116 undef $a;
117 $a += 4294967290;
118 tryeq 60, $a, 4294967290;
119 undef $a;
120 $a += -4294967290;
121 tryeq 61, $a, -4294967290;
122 undef $a;
123 $a += 4294967297;
124 tryeq 62, $a, 4294967297;
125 undef $a;
126 $a += -4294967297;
127 tryeq 63, $a, -4294967297;
128
129 my $s;
130 $s -= 1;
131 tryeq 64, $s, -1;
132 undef $s;
133 $s -= -1;
134 tryeq 65, $s, +1;
135 undef $s;
136 $s -= -4294967290;
137 tryeq 66, $s, +4294967290;
138 undef $s;
139 $s -= 4294967290;
140 tryeq 67, $s, -4294967290;
141 undef $s;
142 $s -= 4294967297;
143 tryeq 68, $s, -4294967297;
144 undef $s;
145 $s -= -4294967297;
146 tryeq 69, $s, +4294967297;
147
148 # Multiplication
149
150 tryeq 70, 1 * 3, 3;
151 tryeq 71, -2 * 3, -6;
152 tryeq 72, 3 * -3, -9;
153 tryeq 73, -4 * -3, 12;
154
155 # check with 0xFFFF and 0xFFFF
156 tryeq 74, 65535 * 65535, 4294836225;
157 tryeq 75, 65535 * -65535, -4294836225;
158 tryeq 76, -65535 * 65535, -4294836225;
159 tryeq 77, -65535 * -65535, 4294836225;
160
161 # check with 0xFFFF and 0x10001
162 tryeq 78, 65535 * 65537, 4294967295;
163 tryeq 79, 65535 * -65537, -4294967295;
164 tryeq 80, -65535 * 65537, -4294967295;
165 tryeq 81, -65535 * -65537, 4294967295;
166
167 # check with 0x10001 and 0xFFFF
168 tryeq 82, 65537 * 65535, 4294967295;
169 tryeq 83, 65537 * -65535, -4294967295;
170 tryeq 84, -65537 * 65535, -4294967295;
171 tryeq 85, -65537 * -65535, 4294967295;
172
173 # These should all be dones as NVs
174 tryeq 86, 65537 * 65537, 4295098369;
175 tryeq 87, 65537 * -65537, -4295098369;
176 tryeq 88, -65537 * 65537, -4295098369;
177 tryeq 89, -65537 * -65537, 4295098369;
178
179 # will overflow an IV (in 32-bit)
180 tryeq 90, 46340 * 46342, 0x80001218;
181 tryeq 91, 46340 * -46342, -0x80001218;
182 tryeq 92, -46340 * 46342, -0x80001218;
183 tryeq 93, -46340 * -46342, 0x80001218;
184
185 tryeq 94, 46342 * 46340, 0x80001218;
186 tryeq 95, 46342 * -46340, -0x80001218;
187 tryeq 96, -46342 * 46340, -0x80001218;
188 tryeq 97, -46342 * -46340, 0x80001218;
189
190 # will overflow a positive IV (in 32-bit)
191 tryeq 98, 65536 * 32768, 0x80000000;
192 tryeq 99, 65536 * -32768, -0x80000000;
193 tryeq 100, -65536 * 32768, -0x80000000;
194 tryeq 101, -65536 * -32768, 0x80000000;
195
196 tryeq 102, 32768 * 65536, 0x80000000;
197 tryeq 103, 32768 * -65536, -0x80000000;
198 tryeq 104, -32768 * 65536, -0x80000000;
199 tryeq 105, -32768 * -65536, 0x80000000;
200
201 # 2147483647 is prime. bah.
202
203 tryeq 106, 46339 * 46341, 0x7ffea80f;
204 tryeq 107, 46339 * -46341, -0x7ffea80f;
205 tryeq 108, -46339 * 46341, -0x7ffea80f;
206 tryeq 109, -46339 * -46341, 0x7ffea80f;