From 7429a98174717de9541c3d130f2b28c9fcfc20b4 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 23 Jan 2012 23:42:32 +0000 Subject: [PATCH] PHPUnit Extensions (custom assert for Complex numbers) git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@85691 2327b42d-5241-43d6-9e2a-de5ac946f064 --- unitTests/custom/Complex.php | 70 ++++++++++++++++++++++++++++++ unitTests/custom/complexAssert.php | 52 ++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 unitTests/custom/Complex.php create mode 100644 unitTests/custom/complexAssert.php diff --git a/unitTests/custom/Complex.php b/unitTests/custom/Complex.php new file mode 100644 index 00000000..72938aa3 --- /dev/null +++ b/unitTests/custom/Complex.php @@ -0,0 +1,70 @@ +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; + } + +} diff --git a/unitTests/custom/complexAssert.php b/unitTests/custom/complexAssert.php new file mode 100644 index 00000000..680aa2ea --- /dev/null +++ b/unitTests/custom/complexAssert.php @@ -0,0 +1,52 @@ +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; + } + +}