Skip to content

Commit 74bb0f3

Browse files
committed
ext/gmp: Add behavioural tests for operator overloading
1 parent 0bdc4b8 commit 74bb0f3

6 files changed

+521
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
GMP comparison operator overloading supports null
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
8+
$num = gmp_init(42);
9+
10+
try {
11+
var_dump($num < null);
12+
} catch (Throwable $e) {
13+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
14+
}
15+
16+
try {
17+
var_dump($num > null);
18+
} catch (Throwable $e) {
19+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
20+
}
21+
22+
try {
23+
var_dump($num <= null);
24+
} catch (Throwable $e) {
25+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
26+
}
27+
28+
try {
29+
var_dump($num >= null);
30+
} catch (Throwable $e) {
31+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
32+
}
33+
34+
try {
35+
var_dump($num == null);
36+
} catch (Throwable $e) {
37+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
38+
}
39+
40+
try {
41+
var_dump($num <=> null);
42+
} catch (Throwable $e) {
43+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
44+
}
45+
46+
?>
47+
--EXPECT--
48+
bool(false)
49+
bool(true)
50+
bool(false)
51+
bool(true)
52+
bool(false)
53+
int(1)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
--TEST--
2+
GMP operator overloading does not support []
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
8+
$num = gmp_init(42);
9+
10+
try {
11+
var_dump($num + []);
12+
} catch (Throwable $e) {
13+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
14+
}
15+
16+
try {
17+
var_dump($num - []);
18+
} catch (Throwable $e) {
19+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
20+
}
21+
22+
try {
23+
var_dump($num * []);
24+
} catch (Throwable $e) {
25+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
26+
}
27+
28+
try {
29+
var_dump($num / []);
30+
} catch (Throwable $e) {
31+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
32+
}
33+
34+
try {
35+
var_dump($num % []);
36+
} catch (Throwable $e) {
37+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
38+
}
39+
40+
try {
41+
var_dump($num ** []);
42+
} catch (Throwable $e) {
43+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
44+
}
45+
46+
try {
47+
var_dump($num | []);
48+
} catch (Throwable $e) {
49+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
50+
}
51+
try {
52+
var_dump($num & []);
53+
} catch (Throwable $e) {
54+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
55+
}
56+
try {
57+
var_dump($num ^ []);
58+
} catch (Throwable $e) {
59+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
60+
}
61+
try {
62+
var_dump($num << []);
63+
} catch (Throwable $e) {
64+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
65+
}
66+
try {
67+
var_dump($num >> []);
68+
} catch (Throwable $e) {
69+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
70+
}
71+
72+
?>
73+
--EXPECT--
74+
TypeError: Number must be of type GMP|string|int, array given
75+
TypeError: Number must be of type GMP|string|int, array given
76+
TypeError: Number must be of type GMP|string|int, array given
77+
TypeError: Number must be of type GMP|string|int, array given
78+
TypeError: Number must be of type GMP|string|int, array given
79+
object(GMP)#3 (1) {
80+
["num"]=>
81+
string(1) "1"
82+
}
83+
TypeError: Number must be of type GMP|string|int, array given
84+
TypeError: Number must be of type GMP|string|int, array given
85+
TypeError: Number must be of type GMP|string|int, array given
86+
object(GMP)#2 (1) {
87+
["num"]=>
88+
string(2) "42"
89+
}
90+
object(GMP)#2 (1) {
91+
["num"]=>
92+
string(2) "42"
93+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
GMP operator overloading does not support null
3+
--EXTENSIONS--
4+
gmp
5+
--XFAIL--
6+
Test showcasing segfaulting behaviour
7+
--FILE--
8+
<?php
9+
10+
$num = gmp_init(42);
11+
12+
try {
13+
var_dump($num + null);
14+
} catch (Throwable $e) {
15+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
16+
}
17+
18+
try {
19+
var_dump($num - null);
20+
} catch (Throwable $e) {
21+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
22+
}
23+
24+
try {
25+
var_dump($num * null);
26+
} catch (Throwable $e) {
27+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
28+
}
29+
30+
try {
31+
var_dump($num / null);
32+
} catch (Throwable $e) {
33+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
34+
}
35+
36+
try {
37+
var_dump($num % null);
38+
} catch (Throwable $e) {
39+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
40+
}
41+
42+
try {
43+
var_dump($num ** null);
44+
} catch (Throwable $e) {
45+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
46+
}
47+
48+
try {
49+
var_dump($num | null);
50+
} catch (Throwable $e) {
51+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
52+
}
53+
try {
54+
var_dump($num & null);
55+
} catch (Throwable $e) {
56+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
57+
}
58+
try {
59+
var_dump($num ^ null);
60+
} catch (Throwable $e) {
61+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
62+
}
63+
try {
64+
var_dump($num << null);
65+
} catch (Throwable $e) {
66+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
67+
}
68+
try {
69+
var_dump($num >> null);
70+
} catch (Throwable $e) {
71+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
72+
}
73+
74+
?>
75+
--EXPECT--
76+
SEGFAULT
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
--TEST--
2+
GMP operator overloading does not support non-stringable objects
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
8+
$num = gmp_init(42);
9+
$o = new stdClass();
10+
11+
try {
12+
var_dump($num + $o);
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
15+
}
16+
17+
try {
18+
var_dump($num - $o);
19+
} catch (Throwable $e) {
20+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
21+
}
22+
23+
try {
24+
var_dump($num * $o);
25+
} catch (Throwable $e) {
26+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
27+
}
28+
29+
try {
30+
var_dump($num / $o);
31+
} catch (Throwable $e) {
32+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
33+
}
34+
35+
try {
36+
var_dump($num % $o);
37+
} catch (Throwable $e) {
38+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
39+
}
40+
41+
try {
42+
var_dump($num ** $o);
43+
} catch (Throwable $e) {
44+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
45+
}
46+
47+
try {
48+
var_dump($num | $o);
49+
} catch (Throwable $e) {
50+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
51+
}
52+
try {
53+
var_dump($num & $o);
54+
} catch (Throwable $e) {
55+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
56+
}
57+
try {
58+
var_dump($num ^ $o);
59+
} catch (Throwable $e) {
60+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
61+
}
62+
try {
63+
var_dump($num << $o);
64+
} catch (Throwable $e) {
65+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
66+
}
67+
try {
68+
var_dump($num >> $o);
69+
} catch (Throwable $e) {
70+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
71+
}
72+
73+
?>
74+
--EXPECTF--
75+
TypeError: Number must be of type GMP|string|int, stdClass given
76+
TypeError: Number must be of type GMP|string|int, stdClass given
77+
TypeError: Number must be of type GMP|string|int, stdClass given
78+
TypeError: Number must be of type GMP|string|int, stdClass given
79+
TypeError: Number must be of type GMP|string|int, stdClass given
80+
81+
Warning: Object of class stdClass could not be converted to int in %s on line %d
82+
object(GMP)#4 (1) {
83+
["num"]=>
84+
string(2) "42"
85+
}
86+
TypeError: Number must be of type GMP|string|int, stdClass given
87+
TypeError: Number must be of type GMP|string|int, stdClass given
88+
TypeError: Number must be of type GMP|string|int, stdClass given
89+
90+
Warning: Object of class stdClass could not be converted to int in %s on line %d
91+
object(GMP)#3 (1) {
92+
["num"]=>
93+
string(2) "84"
94+
}
95+
96+
Warning: Object of class stdClass could not be converted to int in %s on line %d
97+
object(GMP)#3 (1) {
98+
["num"]=>
99+
string(2) "21"
100+
}

0 commit comments

Comments
 (0)