|
|
|
|
@ -85,4 +85,81 @@ class OperatorTest extends \BaseTest |
|
|
|
|
{ |
|
|
|
|
$this->exec('Cannot divide or modulo by zero', 'assign-modulo-by-zero.php'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testFloatLiteralSpecialValuesAndWholeNumbers(): void |
|
|
|
|
{ |
|
|
|
|
$previous = ini_set('precision', '14'); |
|
|
|
|
try { |
|
|
|
|
global $translator; |
|
|
|
|
$compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); |
|
|
|
|
$translator = $compiler; |
|
|
|
|
$testFile = __DIR__ . '/../code/float-literal-special.php'; |
|
|
|
|
$compiler->addFiles([$testFile]); |
|
|
|
|
$compiler->prepareFile($testFile); |
|
|
|
|
$cppFile = $compiler->convertFile($testFile); |
|
|
|
|
$cpp = file_get_contents($cppFile); |
|
|
|
|
} finally { |
|
|
|
|
if ($previous !== false) { |
|
|
|
|
ini_set('precision', $previous); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString('1.0', $cpp); |
|
|
|
|
$this->assertStringContainsString('0.0', $cpp); |
|
|
|
|
$this->assertStringContainsString('std::numeric_limits<double>::infinity()', $cpp); |
|
|
|
|
$this->assertStringContainsString('-std::numeric_limits<double>::infinity()', $cpp); |
|
|
|
|
$this->assertStringContainsString('std::numeric_limits<double>::quiet_NaN()', $cpp); |
|
|
|
|
$this->assertStringContainsString('2.7182818284590451', $cpp); |
|
|
|
|
$this->assertStringNotContainsString('2.718281828459)', $cpp); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testFloatDeclarationMetadataIgnoresHostPrecisionAndHandlesSpecialValues(): void |
|
|
|
|
{ |
|
|
|
|
$previous = ini_set('precision', '14'); |
|
|
|
|
try { |
|
|
|
|
global $translator; |
|
|
|
|
$compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); |
|
|
|
|
$translator = $compiler; |
|
|
|
|
$testFile = TYPEPHP_ROOT_PATH . '/phpunit/code/float-declaration-metadata.php'; |
|
|
|
|
$compiler->addFiles([$testFile]); |
|
|
|
|
$compiler->prepareFile($testFile); |
|
|
|
|
$compiler->convertFile($testFile); |
|
|
|
|
$arginfoHeader = $compiler->getArgInfoHeaderFile($testFile); |
|
|
|
|
$arginfo = file_get_contents($arginfoHeader); |
|
|
|
|
$extension = file_get_contents($compiler->genExtension()); |
|
|
|
|
} finally { |
|
|
|
|
if ($previous !== false) { |
|
|
|
|
ini_set('precision', $previous); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString('ZVAL_DOUBLE(&const_POSITIVE_INF_value, std::numeric_limits<double>::infinity());', $arginfo); |
|
|
|
|
$this->assertStringContainsString('ZVAL_DOUBLE(&const_NEGATIVE_INF_value, -std::numeric_limits<double>::infinity());', $arginfo); |
|
|
|
|
$this->assertStringContainsString('ZVAL_DOUBLE(&const_NOT_A_NUMBER_value, std::numeric_limits<double>::quiet_NaN());', $arginfo); |
|
|
|
|
$this->assertStringContainsString('ZVAL_DOUBLE(&const_CONST_E_value, 2.7182818284590451);', $arginfo); |
|
|
|
|
$this->assertStringContainsString('ZVAL_DOUBLE(&const_CONST_ONE_POINT_FIVE_value, 1.5);', $arginfo); |
|
|
|
|
$this->assertStringNotContainsString('2.718281828459);', $arginfo); |
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString('php::toFloat(2.7182818284590451)', $extension); |
|
|
|
|
$this->assertStringContainsString('php::toFloat(std::numeric_limits<double>::infinity())', $extension); |
|
|
|
|
$this->assertStringContainsString('php::toFloat(std::numeric_limits<double>::quiet_NaN())', $extension); |
|
|
|
|
$this->assertStringNotContainsString('2.718281828459)', $extension); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testFloatLiteralEmissionIsLocaleIndependent(): void |
|
|
|
|
{ |
|
|
|
|
$previousLocale = setlocale(LC_ALL, 'de_DE.UTF-8', 'da_DK.UTF-8', 'en_DK.utf8'); |
|
|
|
|
try { |
|
|
|
|
global $translator; |
|
|
|
|
$compiler = \TypePhp\CompilerTest::create(TYPEPHP_ROOT_PATH); |
|
|
|
|
$translator = $compiler; |
|
|
|
|
$result = $compiler->genFloatLiteral(1.5); |
|
|
|
|
$this->assertSame('1.5', $result); |
|
|
|
|
$this->assertStringNotContainsString(',', $result); |
|
|
|
|
} finally { |
|
|
|
|
if ($previousLocale !== false) { |
|
|
|
|
setlocale(LC_ALL, $previousLocale); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|