2015-05-17 16:10:35 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Custom;
|
2015-05-17 16:10:35 +00:00
|
|
|
|
2018-07-25 13:38:44 +00:00
|
|
|
use Complex\Complex;
|
|
|
|
|
2016-05-18 11:37:00 +00:00
|
|
|
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
|
|
|
|
2018-07-25 13:38:44 +00:00
|
|
|
private function testExpectedExceptions($expected, $actual)
|
2015-05-17 16:10:35 +00:00
|
|
|
{
|
2018-07-25 13:38:44 +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;
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2018-07-25 13:38:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function adjustDelta($expected, $actual, $delta)
|
|
|
|
{
|
|
|
|
$adjustedDelta = $delta;
|
|
|
|
|
|
|
|
if (abs($actual) > 10 && abs($expected) > 10) {
|
|
|
|
$variance = floor(log10(abs($expected)));
|
|
|
|
$adjustedDelta *= pow(10, $variance);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $adjustedDelta > 1.0 ? 1.0 : $adjustedDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function assertComplexEquals($expected, $actual, $delta = 0)
|
|
|
|
{
|
|
|
|
if ($expected === INF || $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();
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 16:10:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 16:10:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-25 13:38:44 +00:00
|
|
|
$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();
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 16:10:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-25 13:38:44 +00:00
|
|
|
$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();
|
2016-08-16 15:33:57 +00:00
|
|
|
|
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();
|
2016-08-16 15:33:57 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|