test(native_types): 添加 Decimal 和 BigInt 类型的测试用例

- 添加 Decimal 类型的 ceil、floor、round、sqrt、toFloat 方法测试
- 添加 BigInt 类型的 divmod、gcd、powmod、sqrt 方法测试
- 添加 Decimal 和 BigInt 的 pow 和 divmod 运算测试
- 添加 BigInt 的 toInt 和 toFloat 类型转换测试
- 验证各种数学运算的边界情况和负数处理
- 确保所有测试用例都通过严格类型声明验证
pull/1/head
韩天峰 3 months ago
parent 706f4cc9ab
commit a4d5d86905
  1. 35
      tests/aot/bigint/divmod.phpt
  2. 29
      tests/aot/bigint/gcd.phpt
  3. 28
      tests/aot/bigint/powmod.phpt
  4. 32
      tests/aot/bigint/sqrt.phpt
  5. 20
      tests/aot/bigint/toInt_toFloat.phpt
  6. 26
      tests/aot/decimal/ceil.phpt
  7. 26
      tests/aot/decimal/divmod.phpt
  8. 26
      tests/aot/decimal/floor.phpt
  9. 22
      tests/aot/decimal/pow.phpt
  10. 22
      tests/aot/decimal/powmod.phpt
  11. 32
      tests/aot/decimal/round.phpt
  12. 26
      tests/aot/decimal/sqrt.phpt
  13. 18
      tests/aot/decimal/toFloat.phpt

@ -0,0 +1,35 @@
--TEST--
BigInt: divmod
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::bigInt(10);
$b = std::bigInt(3);
$r = $a->divmod($b);
var_dump(std::bigInt($r[0])->toString());
var_dump(std::bigInt($r[1])->toString());
$c = std::bigInt(100);
$d = std::bigInt(7);
$r2 = $c->divmod($d);
var_dump(std::bigInt($r2[0])->toString());
var_dump(std::bigInt($r2[1])->toString());
// negative dividend
$e = std::bigInt(-10);
$f = std::bigInt(3);
$r3 = $e->divmod($f);
var_dump(std::bigInt($r3[0])->toString());
var_dump(std::bigInt($r3[1])->toString());
}
?>
--EXPECT--
string(1) "3"
string(1) "1"
string(2) "14"
string(1) "2"
string(2) "-3"
string(2) "-1"

@ -0,0 +1,29 @@
--TEST--
BigInt: gcd
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::bigInt(12);
$b = std::bigInt(8);
echo $a->gcd($b)->toString(); echo "\n";
$c = std::bigInt(100);
$d = std::bigInt(25);
echo $c->gcd($d)->toString(); echo "\n";
$e = std::bigInt(17);
$f = std::bigInt(13);
echo $e->gcd($f)->toString(); echo "\n";
// gcd with Int argument
echo $a->gcd(4)->toString(); echo "\n";
}
?>
--EXPECT--
4
25
1
4

@ -0,0 +1,28 @@
--TEST--
BigInt: powmod
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
// 2^10 mod 1000 = 1024 mod 1000 = 24
$a = std::bigInt(2);
$r = $a->powmod(std::bigInt(10), std::bigInt(1000));
echo $r->toString(); echo "\n";
// 3^20 mod 100 = 3486784401 mod 100 = 1
$b = std::bigInt(3);
$r2 = $b->powmod(std::bigInt(20), std::bigInt(100));
echo $r2->toString(); echo "\n";
// 5^3 mod 13 = 125 mod 13 = 8
$c = std::bigInt(5);
$r3 = $c->powmod(std::bigInt(3), std::bigInt(13));
echo $r3->toString(); echo "\n";
}
?>
--EXPECT--
24
1
8

@ -0,0 +1,32 @@
--TEST--
BigInt: sqrt
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::bigInt(0);
echo $a->sqrt()->toString(); echo "\n";
$b = std::bigInt(1);
echo $b->sqrt()->toString(); echo "\n";
$c = std::bigInt(100);
echo $c->sqrt()->toString(); echo "\n";
// Integer sqrt: sqrt(2) = 1
$d = std::bigInt(2);
echo $d->sqrt()->toString(); echo "\n";
// Large perfect square: 10^20, sqrt = 10^10
$e = std::bigInt("100000000000000000000");
echo $e->sqrt()->toString(); echo "\n";
}
?>
--EXPECT--
0
1
10
1
10000000000

@ -0,0 +1,20 @@
--TEST--
BigInt: toInt / toFloat
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::bigInt(42);
var_dump($a->toInt());
var_dump($a->toFloat());
$b = std::bigInt(-100);
var_dump($b->toInt());
}
?>
--EXPECT--
int(42)
float(42)
int(-100)

@ -0,0 +1,26 @@
--TEST--
Decimal: ceil
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::decimal("3.2");
var_dump($a->ceil()->toString());
$b = std::decimal("-3.2");
var_dump($b->ceil()->toString());
$c = std::decimal("5.0");
var_dump($c->ceil()->toString());
$d = std::decimal("0.1");
var_dump($d->ceil()->toString());
}
?>
--EXPECT--
string(1) "4"
string(2) "-3"
string(1) "5"
string(1) "1"

@ -0,0 +1,26 @@
--TEST--
Decimal: divmod
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::decimal("10");
$b = std::decimal("3");
$r = $a->divmod($b);
var_dump(std::decimal($r[0])->toString());
var_dump(std::decimal($r[1])->toString());
$c = std::decimal("17.5");
$d = std::decimal("5.0");
$r2 = $c->divmod($d);
var_dump(std::decimal($r2[0])->toString());
var_dump(std::decimal($r2[1])->toString());
}
?>
--EXPECT--
string(1) "3"
string(1) "1"
string(1) "3"
string(3) "2.5"

@ -0,0 +1,26 @@
--TEST--
Decimal: floor
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::decimal("3.7");
var_dump($a->floor()->toString());
$b = std::decimal("-3.7");
var_dump($b->floor()->toString());
$c = std::decimal("5.0");
var_dump($c->floor()->toString());
$d = std::decimal("0.1");
var_dump($d->floor()->toString());
}
?>
--EXPECT--
string(1) "3"
string(2) "-4"
string(1) "5"
string(1) "0"

@ -0,0 +1,22 @@
--TEST--
Decimal: pow
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::decimal("2");
var_dump($a->pow(std::decimal("3"))->toString());
$b = std::decimal("10");
var_dump($b->pow(std::decimal("2"))->toString());
$c = std::decimal("3");
var_dump($c->pow(std::decimal("3"))->toString());
}
?>
--EXPECT--
string(1) "8"
string(3) "100"
string(2) "27"

@ -0,0 +1,22 @@
--TEST--
Decimal: powmod
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
// 2^10 mod 1000 = 24
$a = std::decimal("2");
$r = $a->powmod(std::decimal("10"), std::decimal("1000"));
var_dump($r->toString());
// 3^4 mod 5 = 81 mod 5 = 1
$b = std::decimal("3");
$r2 = $b->powmod(std::decimal("4"), std::decimal("5"));
var_dump($r2->toString());
}
?>
--EXPECT--
string(2) "24"
string(1) "1"

@ -0,0 +1,32 @@
--TEST--
Decimal: round
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
// round without precision (default 0, round to integer)
$a = std::decimal("3.4");
var_dump($a->round()->toString());
$b = std::decimal("3.5");
var_dump($b->round()->toString());
$c = std::decimal("-3.5");
var_dump($c->round()->toString());
// round with precision
$d = std::decimal("3.14159");
var_dump($d->round(2)->toString());
$e = std::decimal("2.71828");
var_dump($e->round(3)->toString());
}
?>
--EXPECT--
string(1) "3"
string(1) "4"
string(2) "-4"
string(4) "3.14"
string(5) "2.718"

@ -0,0 +1,26 @@
--TEST--
Decimal: sqrt
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::decimal("0");
var_dump($a->sqrt()->toString());
$b = std::decimal("1");
var_dump($b->sqrt()->toString());
$c = std::decimal("100");
var_dump($c->sqrt()->toString());
$d = std::decimal("4");
var_dump($d->sqrt()->toString());
}
?>
--EXPECT--
string(1) "0"
string(1) "1"
string(2) "10"
string(1) "2"

@ -0,0 +1,18 @@
--TEST--
Decimal: toFloat
--FILE--
<?php
declare(strict_types=1);
use native_types;
function main(): void {
$a = std::decimal("3.14");
var_dump($a->toFloat());
$b = std::decimal("100.5");
var_dump($b->toFloat());
}
?>
--EXPECT--
float(3.14)
float(100.5)
Loading…
Cancel
Save