xref: /PHP-8.0/ext/gmp/tests/overloading.phpt (revision 08777387)
1--TEST--
2GMP operator overloading
3--SKIPIF--
4<?php if (!extension_loaded("gmp")) print "skip"; ?>
5--FILE--
6<?php
7
8$a = gmp_init(42);
9$b = gmp_init(17);
10
11var_dump($a + $b);
12var_dump($a + 17);
13var_dump(42 + $b);
14
15var_dump($a - $b);
16var_dump($a - 17);
17var_dump(42 - $b);
18
19var_dump($a * $b);
20var_dump($a * 17);
21var_dump(42 * $b);
22
23var_dump($a / $b);
24var_dump($a / 17);
25var_dump(42 / $b);
26try {
27    var_dump($a / 0);
28} catch (\DivisionByZeroError $e) {
29    echo $e->getMessage() . \PHP_EOL;
30}
31
32var_dump($a % $b);
33var_dump($a % 17);
34var_dump(42 % $b);
35try {
36    var_dump($a % 0);
37} catch (\DivisionByZeroError $e) {
38    echo $e->getMessage() . \PHP_EOL;
39}
40
41var_dump($a ** $b);
42var_dump($a ** 17);
43var_dump(42 ** $b);
44
45var_dump($a | $b);
46var_dump($a | 17);
47var_dump(42 | $b);
48
49var_dump($a & $b);
50var_dump($a & 17);
51var_dump(42 & $b);
52
53var_dump($a ^ $b);
54var_dump($a ^ 17);
55var_dump(42 ^ $b);
56
57var_dump($a << $b);
58var_dump($a << 17);
59var_dump(42 << $b);
60
61var_dump($a >> 2);
62var_dump(-$a >> 2);
63
64try {
65    $a << -1;
66} catch (ValueError $exception) {
67    echo $exception->getMessage() . "\n";
68}
69
70try {
71    $a >> -1;
72} catch (ValueError $exception) {
73    echo $exception->getMessage() . "\n";
74}
75
76var_dump(~$a);
77var_dump(-$a);
78var_dump(+$a);
79
80var_dump($a == $b);
81var_dump($a != $b);
82var_dump($a < $b);
83var_dump($a <= $b);
84var_dump($a > $b);
85var_dump($a >= $b);
86
87var_dump($a == $a);
88var_dump($a != $a);
89
90var_dump($a == 42);
91var_dump($a != 42);
92var_dump($a < 42);
93var_dump($a <= 42);
94var_dump($a > 42);
95var_dump($a >= 42);
96
97try {
98    var_dump($a == new stdClass);
99} catch (\TypeError $e) {
100    echo $e->getMessage() . \PHP_EOL;
101}
102
103$a += 1;
104var_dump($a);
105$a -= 1;
106var_dump($a);
107
108var_dump(++$a);
109var_dump($a++);
110var_dump($a);
111
112var_dump(--$a);
113var_dump($a--);
114var_dump($a);
115
116// Test operator that was not overloaded
117
118var_dump($a . $b);
119var_dump($a . '17');
120var_dump('42' . $b);
121
122$a .= '17';
123var_dump($a);
124
125?>
126--EXPECT--
127object(GMP)#3 (1) {
128  ["num"]=>
129  string(2) "59"
130}
131object(GMP)#3 (1) {
132  ["num"]=>
133  string(2) "59"
134}
135object(GMP)#3 (1) {
136  ["num"]=>
137  string(2) "59"
138}
139object(GMP)#3 (1) {
140  ["num"]=>
141  string(2) "25"
142}
143object(GMP)#3 (1) {
144  ["num"]=>
145  string(2) "25"
146}
147object(GMP)#3 (1) {
148  ["num"]=>
149  string(2) "25"
150}
151object(GMP)#3 (1) {
152  ["num"]=>
153  string(3) "714"
154}
155object(GMP)#3 (1) {
156  ["num"]=>
157  string(3) "714"
158}
159object(GMP)#3 (1) {
160  ["num"]=>
161  string(3) "714"
162}
163object(GMP)#3 (1) {
164  ["num"]=>
165  string(1) "2"
166}
167object(GMP)#3 (1) {
168  ["num"]=>
169  string(1) "2"
170}
171object(GMP)#3 (1) {
172  ["num"]=>
173  string(1) "2"
174}
175Division by zero
176object(GMP)#4 (1) {
177  ["num"]=>
178  string(1) "8"
179}
180object(GMP)#4 (1) {
181  ["num"]=>
182  string(1) "8"
183}
184object(GMP)#4 (1) {
185  ["num"]=>
186  string(1) "8"
187}
188Modulo by zero
189object(GMP)#3 (1) {
190  ["num"]=>
191  string(28) "3937657486715347520027492352"
192}
193object(GMP)#3 (1) {
194  ["num"]=>
195  string(28) "3937657486715347520027492352"
196}
197object(GMP)#3 (1) {
198  ["num"]=>
199  string(28) "3937657486715347520027492352"
200}
201object(GMP)#3 (1) {
202  ["num"]=>
203  string(2) "59"
204}
205object(GMP)#3 (1) {
206  ["num"]=>
207  string(2) "59"
208}
209object(GMP)#3 (1) {
210  ["num"]=>
211  string(2) "59"
212}
213object(GMP)#3 (1) {
214  ["num"]=>
215  string(1) "0"
216}
217object(GMP)#3 (1) {
218  ["num"]=>
219  string(1) "0"
220}
221object(GMP)#3 (1) {
222  ["num"]=>
223  string(1) "0"
224}
225object(GMP)#3 (1) {
226  ["num"]=>
227  string(2) "59"
228}
229object(GMP)#3 (1) {
230  ["num"]=>
231  string(2) "59"
232}
233object(GMP)#3 (1) {
234  ["num"]=>
235  string(2) "59"
236}
237object(GMP)#3 (1) {
238  ["num"]=>
239  string(7) "5505024"
240}
241object(GMP)#3 (1) {
242  ["num"]=>
243  string(7) "5505024"
244}
245object(GMP)#3 (1) {
246  ["num"]=>
247  string(7) "5505024"
248}
249object(GMP)#3 (1) {
250  ["num"]=>
251  string(2) "10"
252}
253object(GMP)#5 (1) {
254  ["num"]=>
255  string(3) "-11"
256}
257Shift must be greater than or equal to 0
258Shift must be greater than or equal to 0
259object(GMP)#5 (1) {
260  ["num"]=>
261  string(3) "-43"
262}
263object(GMP)#5 (1) {
264  ["num"]=>
265  string(3) "-42"
266}
267object(GMP)#5 (1) {
268  ["num"]=>
269  string(2) "42"
270}
271bool(false)
272bool(true)
273bool(false)
274bool(false)
275bool(true)
276bool(true)
277bool(true)
278bool(false)
279bool(true)
280bool(false)
281bool(false)
282bool(true)
283bool(false)
284bool(true)
285Number must be of type GMP|string|int, stdClass given
286object(GMP)#4 (1) {
287  ["num"]=>
288  string(2) "43"
289}
290object(GMP)#1 (1) {
291  ["num"]=>
292  string(2) "42"
293}
294object(GMP)#4 (1) {
295  ["num"]=>
296  string(2) "43"
297}
298object(GMP)#4 (1) {
299  ["num"]=>
300  string(2) "43"
301}
302object(GMP)#1 (1) {
303  ["num"]=>
304  string(2) "44"
305}
306object(GMP)#4 (1) {
307  ["num"]=>
308  string(2) "43"
309}
310object(GMP)#4 (1) {
311  ["num"]=>
312  string(2) "43"
313}
314object(GMP)#1 (1) {
315  ["num"]=>
316  string(2) "42"
317}
318string(4) "4217"
319string(4) "4217"
320string(4) "4217"
321string(4) "4217"
322