PhpSpreadsheet/tests/PhpSpreadsheetTests/Custom/ComplexAssert.php

83 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2015-05-17 16:10:35 +00:00
<?php
namespace PhpOffice\PhpSpreadsheetTests\Custom;
2015-05-17 16:10:35 +00:00
use Complex\Complex;
class ComplexAssert
2015-05-17 16:10:35 +00:00
{
2017-10-08 05:37:11 +00:00
private $errorMessage = '';
2015-05-17 16:10:35 +00:00
private function testExpectedExceptions($expected, $actual)
2015-05-17 16:10:35 +00:00
{
// Expecting an error, so we do a straight string comparison
if ($expected === $actual) {
return true;
} elseif ($expected === INF && $actual === 'INF') {
return true;
}
$this->errorMessage = 'Expected Error: ' . $actual . ' !== ' . $expected;
return false;
}
private function adjustDelta($expected, $actual, $delta)
{
$adjustedDelta = $delta;
if (abs($actual) > 10 && abs($expected) > 10) {
$variance = floor(log10(abs($expected)));
2020-05-18 04:49:57 +00:00
$adjustedDelta *= 10 ** $variance;
}
return $adjustedDelta > 1.0 ? 1.0 : $adjustedDelta;
}
public function assertComplexEquals($expected, $actual, $delta = 0)
{
if ($expected === INF || (is_string($expected) && $expected[0] === '#')) {
return $this->testExpectedExceptions($expected, $actual);
2015-05-17 16:10:35 +00:00
}
$expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual);
if (!is_numeric($actualComplex->getReal()) || !is_numeric($expectedComplex->getReal())) {
if ($actualComplex->getReal() !== $expectedComplex->getReal()) {
2017-10-08 05:37:11 +00:00
$this->errorMessage = 'Mismatched String: ' . $actualComplex->getReal() . ' !== ' . $expectedComplex->getReal();
2015-05-17 16:10:35 +00:00
return false;
}
2015-05-17 16:10:35 +00:00
return true;
}
$adjustedDelta = $this->adjustDelta($expectedComplex->getReal(), $actualComplex->getReal(), $delta);
if (abs($actualComplex->getReal() - $expectedComplex->getReal()) > $adjustedDelta) {
2017-10-08 05:37:11 +00:00
$this->errorMessage = 'Mismatched Real part: ' . $actualComplex->getReal() . ' != ' . $expectedComplex->getReal();
2015-05-17 16:10:35 +00:00
return false;
}
$adjustedDelta = $this->adjustDelta($expectedComplex->getImaginary(), $actualComplex->getImaginary(), $delta);
if (abs($actualComplex->getImaginary() - $expectedComplex->getImaginary()) > $adjustedDelta) {
2017-10-08 05:37:11 +00:00
$this->errorMessage = 'Mismatched Imaginary part: ' . $actualComplex->getImaginary() . ' != ' . $expectedComplex->getImaginary();
2015-05-17 16:10:35 +00:00
return false;
}
if ($actualComplex->getSuffix() !== $actualComplex->getSuffix()) {
2017-10-08 05:37:11 +00:00
$this->errorMessage = 'Mismatched Suffix: ' . $actualComplex->getSuffix() . ' != ' . $expectedComplex->getSuffix();
2015-05-17 16:10:35 +00:00
return false;
}
return true;
}
public function getErrorMessage()
{
2017-10-08 05:37:11 +00:00
return $this->errorMessage;
2015-05-17 16:10:35 +00:00
}
}