PHPUnit Extensions (custom assert for Complex numbers)

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@85691 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2012-01-23 23:42:32 +00:00
parent 166311bf69
commit 7429a98174
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,70 @@
<?php
class Complex {
private $realPart = 0;
private $imaginaryPart = 0;
private $suffix = 'i';
public static function _parseComplex($complexNumber) {
$complexNumber = (string) $complexNumber;
$validComplex = preg_match('/^([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)?(([-+]?)([ij]?))$/ui',$complexNumber,$complexParts);
if (!$validComplex) {
return array( $complexNumber, 0, '' );
}
if (($complexParts[4] === '') && ($complexParts[9] !== '')) {
$complexParts[4] = $complexParts[8] . 1;
}
return array( (float) $complexParts[1],
(float) $complexParts[4],
$complexParts[9]
);
} // function _parseComplex()
public function __construct($realPart, $imaginaryPart = null, $suffix = null)
{
if ($imaginaryPart === null) {
if (is_array($realPart)) {
list ($realPart, $imaginaryPart, $suffix) = $realPart;
} elseif((is_string($realPart)) || (is_numeric($realPart))) {
list ($realPart, $imaginaryPart, $suffix) = self::_parseComplex($realPart);
}
}
$this->realPart = $realPart;
$this->imaginaryPart = $imaginaryPart;
$this->suffix = strtolower($suffix);
}
public function getReal()
{
return $this->realPart;
}
public function getImaginary()
{
return $this->imaginaryPart;
}
public function getSuffix()
{
return $this->suffix;
}
public function __toString() {
$str = "";
if ($this->imaginaryPart != 0.0) $str .= $this->imaginaryPart . $this->suffix;
if ($this->realPart != 0.0) {
if ($str) $str = "+" . $str;
$str = $this->realPart . $str;
}
if (!$str) $str = "0";
return $str;
}
}

View File

@ -0,0 +1,52 @@
<?php
include_once __DIR__.'/Complex.php';
class complexAssert {
private $_errorMessage = '';
public function assertComplexEquals($expected, $actual, $delta = 0)
{
$expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual);
if (!is_numeric($actualComplex->getReal()) || !is_numeric($expectedComplex->getReal())) {
if ($actualComplex->getReal() !== $expectedComplex->getReal()) {
$this->_errorMessage = 'Mismatched String: ' .
$actualComplex->getReal() . ' !== ' . $expectedComplex->getReal();
return FALSE;
}
return TRUE;
}
if ($actualComplex->getReal() < ($expectedComplex->getReal() - $delta) ||
$actualComplex->getReal() > ($expectedComplex->getReal() + $delta)) {
$this->_errorMessage = 'Mismatched Real part: ' .
$actualComplex->getReal() . ' != ' . $expectedComplex->getReal();
return FALSE;
}
if ($actualComplex->getImaginary() < ($expectedComplex->getImaginary() - $delta) ||
$actualComplex->getImaginary() > ($expectedComplex->getImaginary() + $delta)) {
$this->_errorMessage = 'Mismatched Imaginary part: ' .
$actualComplex->getImaginary() . ' != ' . $expectedComplex->getImaginary();
return FALSE;
}
if ($actualComplex->getSuffix() !== $actualComplex->getSuffix()) {
$this->_errorMessage = 'Mismatched Suffix: ' .
$actualComplex->getSuffix() . ' != ' . $expectedComplex->getSuffix();
return FALSE;
}
return TRUE;
}
public function getErrorMessage() {
return $this->_errorMessage;
}
}