From b1c053e597300b8381dbcc0175a4bc92a59bd78f Mon Sep 17 00:00:00 2001 From: Ashley Kitson Date: Mon, 4 Aug 2014 08:50:50 +0100 Subject: [PATCH] add tests for new numeric methods --- README.md | 11 +++- .../Type/Number/Complex/ComplexType.php | 54 +++++++++++++++- src/chippyash/Type/Number/FloatType.php | 35 ++++++++++- src/chippyash/Type/Number/IntType.php | 41 ++++++++++-- .../Type/Number/NumericTypeInterface.php | 26 +++++++- .../Number/Rational/AbstractRationalType.php | 34 +++++++++- .../Type/Number/AbstractRationalTypeTest.php | 32 +++++++++- .../Type/Number/Complex/ComplexTypeTest.php | 62 ++++++++++++++++++- .../chippyash/Type/Number/FloatTypeTest.php | 30 ++++++++- .../src/chippyash/Type/Number/IntTypeTest.php | 38 ++++++++++-- .../Type/Number/Rational/RationalTypeTest.php | 1 + 11 files changed, 337 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5244d90..a82bee1 100644 --- a/README.md +++ b/README.md @@ -138,8 +138,11 @@ Numeric types, that is IntType, WholeIntType, NaturalIntType, FloatType, Rationa and ComplexType support the NumericTypeInterface which defines the methods * negate(): negate the number - NB Negation is will throw a \BadMethodCallException for WholeInt and NaturalInt types as they cannot be negative -* toComplex(): returns a complex real representation of the number (e.g. 2+0i). For +* asComplex(): returns a complex real representation of the number (e.g. 2+0i). For complex types, simply clones the existing object. +* asRational(): returns rational representation of the number. For rational types, simply clones the existing object. +* asIntType(): returns number caste as IntType. For IntType, simply clones the existing objeoct. +* asFloatType(): returns number caste as FloatType. For FloatType, simply clones the existing objeoct. * abs(): return the absolute value of the number Additionally, the RationalType supports the RationalTypeInterface: @@ -207,7 +210,7 @@ Install [Composer](https://getcomposer.org/) add
-    "chippyash/strong-type": ">=1.0.8"
+    "chippyash/strong-type": ">=1.0.9"
 
to your composer.json "requires" section @@ -255,4 +258,6 @@ V1.0.6 Add isReal() method for complex numbers V1.0.7 Add toComplex() method for numeric types -V1.0.8 Add abs() method for numeric types \ No newline at end of file +V1.0.8 Add abs() method for numeric types + +V1.0.9 Add asRational, asFloatType and asIntType methods for numeric types. Rename toComplex -> asComplex method \ No newline at end of file diff --git a/src/chippyash/Type/Number/Complex/ComplexType.php b/src/chippyash/Type/Number/Complex/ComplexType.php index 5723ca7..0b94dc6 100644 --- a/src/chippyash/Type/Number/Complex/ComplexType.php +++ b/src/chippyash/Type/Number/Complex/ComplexType.php @@ -16,6 +16,8 @@ use chippyash\Type\Number\Rational\RationalTypeFactory; use chippyash\Type\Number\NumericTypeInterface; use chippyash\Type\Exceptions\NotRealComplexException; +use chippyash\Type\Number\FloatType; +use chippyash\Type\Number\IntType; /** * A complex number - algabraic form @@ -267,12 +269,60 @@ public function toFloat() /** * Return the number as a Complex number i.e. a clone of this one * Required for NumericTypeInterface + * + * @return chippyash\Type\Number\Complex\ComplexType */ - public function toComplex() + public function asComplex() { return clone $this; } - + + /** + * Return number as Rational number. + * NB, numerator and denominator will be caste as IntTypes + * + * @returns chippyash\Type\Number\Rational\RationalType + * + * @throws NotRealComplexException + */ + public function asRational() + { + if ($this->isReal()) { + return clone $this->real; + } else { + throw new NotRealComplexException(); + } + } + + /** + * Return number as an IntType number. + * If number isReal() will return floor(r()) + * + * @returns chippyash\Type\Number\IntType + */ + public function asIntType() + { + if ($this->isReal()) { + return new IntType(floor($this->real->get())); + } else { + throw new NotRealComplexException(); + } + } + + /** + * Return number as a FloatType number. + * + * @returns chippyash\Type\Number\FloatType + */ + public function asFloatType() + { + if ($this->isReal()) { + return new FloatType($this->real->get()); + } else { + throw new NotRealComplexException(); + } + } + /** * Return Greatest Common Denominator of two numbers * diff --git a/src/chippyash/Type/Number/FloatType.php b/src/chippyash/Type/Number/FloatType.php index ed18a9b..d0c3005 100644 --- a/src/chippyash/Type/Number/FloatType.php +++ b/src/chippyash/Type/Number/FloatType.php @@ -39,14 +39,45 @@ public function negate() * * @returns chippyash\Type\Number\Complex\ComplexType */ - public function toComplex() + public function asComplex() { return new ComplexType( - RationalTypeFactory::fromFloat($this->value, 1E-17), + $this->asRational(), new RationalType(new IntType(0), new IntType(1)) ); } + /** + * Return number as Rational number. + * NB, numerator and denominator will be caste as IntTypes + * + * @returns chippyash\Type\Number\Rational\RationalType + */ + public function asRational() + { + return RationalTypeFactory::fromFloat($this->value, 1E-17); + } + + /** + * Return number as an IntType number. + * + * @returns chippyash\Type\Number\IntType + */ + public function asIntType() + { + return new IntType($this->value); + } + + /** + * Return number as a FloatType number. + * + * @returns chippyash\Type\Number\FloatType + */ + public function asFloatType() + { + return clone $this; + } + /** * Return the absolute value of the number * diff --git a/src/chippyash/Type/Number/IntType.php b/src/chippyash/Type/Number/IntType.php index e603672..940a23e 100644 --- a/src/chippyash/Type/Number/IntType.php +++ b/src/chippyash/Type/Number/IntType.php @@ -37,7 +37,7 @@ public function negate() /** * Return the number as a Complex number i.e. n+0i */ - public function toComplex() + public function asComplex() { $one = new self(1); return new ComplexType( @@ -45,6 +45,37 @@ public function toComplex() ); } + /** + * Return number as Rational number. + * NB, numerator and denominator will be caste as IntTypes + * + * @returns chippyash\Type\Number\Rational\RationalType + */ + public function asRational() + { + return new RationalType(new IntType($this->value), new IntType(1)); + } + + /** + * Return number as an IntType number. + * + * @returns chippyash\Type\Number\IntType + */ + public function asIntType() + { + return clone $this; + } + + /** + * Return number as a FloatType number. + * + * @returns chippyash\Type\Number\FloatType + */ + public function asFloatType() + { + return new FloatType($this->value); + } + /** * Return the absolute value of the number * @@ -62,7 +93,7 @@ protected function typeOf($value) /** * Return all factors of this number (sorted) - * + * * @return array [factor,factor, ...] */ public function factors() @@ -83,10 +114,10 @@ public function factors() /** * Return all prime factors of this number - * + * * Adapted from * @link http://www.thatsgeeky.com/2011/03/prime-factoring-with-php/ - * + * * @return array [primeFactor => exponent,...] */ public function primeFactors() @@ -120,7 +151,7 @@ public function primeFactors() } } } while ($n > 1 && $d <= $dmax); - + return $factors; } diff --git a/src/chippyash/Type/Number/NumericTypeInterface.php b/src/chippyash/Type/Number/NumericTypeInterface.php index f56869e..55c410c 100644 --- a/src/chippyash/Type/Number/NumericTypeInterface.php +++ b/src/chippyash/Type/Number/NumericTypeInterface.php @@ -23,8 +23,32 @@ public function negate(); /** * Return the number as a Complex number i.e. n+0i + * + * @return chippyash\Type\Number\Complex\ComplexType + */ + public function asComplex(); + + /** + * Return number as Rational number. + * NB, numerator and denominator will be caste as IntTypes + * + * @returns chippyash\Type\Number\Rational\RationalType + */ + public function asRational(); + + /** + * Return number as an IntType number. + * + * @returns chippyash\Type\Number\IntType + */ + public function asIntType(); + + /** + * Return number as a FloatType number. + * + * @returns chippyash\Type\Number\FloatType */ - public function toComplex(); + public function asFloatType(); /** * Return the absolute value of the number diff --git a/src/chippyash/Type/Number/Rational/AbstractRationalType.php b/src/chippyash/Type/Number/Rational/AbstractRationalType.php index 5ab12dd..1694e8d 100644 --- a/src/chippyash/Type/Number/Rational/AbstractRationalType.php +++ b/src/chippyash/Type/Number/Rational/AbstractRationalType.php @@ -91,7 +91,7 @@ public function set($value) /** * Return the number as a Complex number i.e. n+0i */ - public function toComplex() + public function asComplex() { return new ComplexType( new RationalType($this->numerator(), $this->denominator()), @@ -99,6 +99,38 @@ public function toComplex() ); } + /** + * Return number as Rational number. + * NB, numerator and denominator will be caste as IntTypes + * + * @returns chippyash\Type\Number\Rational\RationalType + */ + public function asRational() + { + return clone $this; + } + + /** + * Return number as an IntType number. + * Will return floor(n/d) + * + * @returns chippyash\Type\Number\IntType + */ + public function asIntType() + { + return new IntType(floor($this->get())); + } + + /** + * Return number as a FloatType number. + * + * @returns chippyash\Type\Number\FloatType + */ + public function asFloatType() + { + return new FloatType($this->get()); + } + /** * Set values for rational * diff --git a/test/src/chippyash/Type/Number/AbstractRationalTypeTest.php b/test/src/chippyash/Type/Number/AbstractRationalTypeTest.php index 5a619e1..05cd534 100644 --- a/test/src/chippyash/Type/Number/AbstractRationalTypeTest.php +++ b/test/src/chippyash/Type/Number/AbstractRationalTypeTest.php @@ -118,7 +118,7 @@ public function testMagicToStringReturnsValue() $this->assertEquals('foo', $o->__toString()); } - public function testToComplexReturnsComplexType() + public function testAsComplexReturnsComplexType() { $o = $this->object; $o->expects($this->any()) @@ -127,11 +127,39 @@ public function testToComplexReturnsComplexType() $o->expects($this->any()) ->method('denominator') ->will($this->returnValue(new IntType(1))); - $c = $o->toComplex(); + $c = $o->asComplex(); $this->assertInstanceOf('\chippyash\Type\Number\Complex\ComplexType', $c); $this->assertEquals('2', (string) $c); $this->assertInstanceOf('chippyash\Type\Number\Rational\RationalType', $c->r()); $this->assertInstanceOf('chippyash\Type\Number\Rational\RationalType', $c->i()); + } + + public function testAsRationalReturnsRationalType() + { + $o = $this->object; + $r = $o->AsRational(); + $this->assertEquals($o, $r); + } + public function testAsFloatTypeReturnsFloatType() + { + $o = $this->object; + $o->expects($this->any()) + ->method('get') + ->will($this->returnValue(2)); + $f = $o->asFloatType(); + $this->assertInstanceOf('\chippyash\Type\Number\FloatType', $f); + $this->assertEquals(2, (string) $f); + } + + public function testAsIntTypeReturnsIntType() + { + $o = $this->object; + $o->expects($this->any()) + ->method('get') + ->will($this->returnValue(2)); + $i = $o->AsIntType(); + $this->assertInstanceOf('\chippyash\Type\Number\IntType', $i); + $this->assertEquals(2, (string) $i); } } diff --git a/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php b/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php index 5126687..bda4668 100644 --- a/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php +++ b/test/src/chippyash/Type/Number/Complex/ComplexTypeTest.php @@ -346,13 +346,69 @@ public function testToFloatReturnsIntegerForIntegerFloatComplexNumber() $this->assertInternalType('int', $c->toFloat()); } - public function testToComplexReturnsCloneOfSelf() + public function testAsComplexReturnsCloneOfSelf() { $c = new ComplexType($this->createRationalType(1), $this->createRationalType(1)); - $c2 = $c->toComplex(); + $c2 = $c->asComplex(); $this->assertEquals($c, $c2); } + public function testAsRationalReturnsRationalType() + { + $t = new ComplexType($this->createRationalType(2), $this->createRationalType(0)); + $r = $t->AsRational(); + $this->assertInstanceOf('\chippyash\Type\Number\Rational\RationalType', $r); + $this->assertInstanceOf('\chippyash\Type\Number\IntType', $r->numerator()); + $this->assertInstanceOf('\chippyash\Type\Number\IntType', $r->denominator()); + $this->assertEquals(2, (string) $r); + } + + public function testAsFloatTypeReturnsFloatType() + { + $t = new ComplexType($this->createRationalType(2), $this->createRationalType(0)); + $f = $t->asFloatType(); + $this->assertInstanceOf('\chippyash\Type\Number\FloatType', $f); + $this->assertEquals(2, (string) $f); + } + + public function testAsIntTypeReturnsIntType() + { + $t = new ComplexType($this->createRationalType(2), $this->createRationalType(0)); + $i = $t->asIntType(); + $this->assertInstanceOf('\chippyash\Type\Number\IntType', $i); + $this->assertEquals(2, (string) $i); + } + + /** + * @expectedException chippyash\Type\Exceptions\NotRealComplexException + * @expectedExceptionMessage Not a Real complex type + */ + public function testAsRationalForNonRealThrowsException() + { + $t = new ComplexType($this->createRationalType(2), $this->createRationalType(1)); + $r = $t->AsRational(); + } + + /** + * @expectedException chippyash\Type\Exceptions\NotRealComplexException + * @expectedExceptionMessage Not a Real complex type + */ + public function testAsFloatTypeForNonRealThrowsException() + { + $t = new ComplexType($this->createRationalType(2), $this->createRationalType(1)); + $r = $t->asFloatType(); + } + + /** + * @expectedException chippyash\Type\Exceptions\NotRealComplexException + * @expectedExceptionMessage Not a Real complex type + */ + public function testAsIntTypeForNonRealThrowsException() + { + $t = new ComplexType($this->createRationalType(2), $this->createRationalType(1)); + $r = $t->asIntType(); + } + public function testAbsReturnsAbsoluteValue() { $c1 = new ComplexType($this->createRationalType(1), $this->createRationalType(2)); @@ -364,7 +420,7 @@ public function testAbsReturnsAbsoluteValue() $this->assertEquals($c1->modulus(), $c3->abs()); $this->assertEquals($c1->modulus(), $c4->abs()); } - + /** * Create a rational type * diff --git a/test/src/chippyash/Type/Number/FloatTypeTest.php b/test/src/chippyash/Type/Number/FloatTypeTest.php index 1996dca..3869f93 100644 --- a/test/src/chippyash/Type/Number/FloatTypeTest.php +++ b/test/src/chippyash/Type/Number/FloatTypeTest.php @@ -23,16 +23,42 @@ public function testCanNegateTheNumber() $this->assertEquals(-2.0, $t->negate()->get()); } - public function testToComplexReturnsComplexType() + public function testAsComplexReturnsComplexType() { $t = new FloatType(2.0); - $c = $t->toComplex(); + $c = $t->asComplex(); $this->assertInstanceOf('\chippyash\Type\Number\Complex\ComplexType', $c); $this->assertEquals('2', (string) $c); $this->assertInstanceOf('chippyash\Type\Number\Rational\RationalType', $c->r()); $this->assertInstanceOf('chippyash\Type\Number\Rational\RationalType', $c->i()); } + public function testAsRationalReturnsRationalType() + { + $t = new FloatType(2.0); + $r = $t->AsRational(); + $this->assertInstanceOf('\chippyash\Type\Number\Rational\RationalType', $r); + $this->assertEquals('2', (string) $r); + $this->assertInstanceOf('chippyash\Type\Number\IntType', $r->numerator()); + $this->assertInstanceOf('chippyash\Type\Number\IntType', $r->denominator()); + } + + public function testAsFloatTypeReturnsFloatType() + { + $t = new FloatType(2.0); + $f = $t->asFloatType(); + $this->assertInstanceOf('\chippyash\Type\Number\FloatType', $f); + $this->assertEquals($t, $f); + } + + public function testAsIntTypeReturnsIntType() + { + $t = new FloatType(2.0); + $i = $t->asIntType(); + $this->assertInstanceOf('\chippyash\Type\Number\IntType', $i); + $this->assertEquals(2, (string) $i); + } + public function testAbsReturnsAbsoluteValue() { $t1 = new FloatType(2.6); diff --git a/test/src/chippyash/Type/Number/IntTypeTest.php b/test/src/chippyash/Type/Number/IntTypeTest.php index 99de294..2cf9bac 100644 --- a/test/src/chippyash/Type/Number/IntTypeTest.php +++ b/test/src/chippyash/Type/Number/IntTypeTest.php @@ -29,16 +29,42 @@ public function testCanNegateTheNumber() $this->assertEquals(-2, $t->negate()->get()); } - public function testToComplexReturnsComplexType() + public function testAsComplexReturnsComplexType() { $t = new IntType(2); - $c = $t->toComplex(); + $c = $t->AsComplex(); $this->assertInstanceOf('\chippyash\Type\Number\Complex\ComplexType', $c); $this->assertEquals('2', (string) $c); $this->assertInstanceOf('chippyash\Type\Number\Rational\RationalType', $c->r()); $this->assertInstanceOf('chippyash\Type\Number\Rational\RationalType', $c->i()); } + public function testAsRationalReturnsRationalType() + { + $t = new IntType(2); + $r = $t->AsRational(); + $this->assertInstanceOf('\chippyash\Type\Number\Rational\RationalType', $r); + $this->assertEquals('2', (string) $r); + $this->assertInstanceOf('chippyash\Type\Number\IntType', $r->numerator()); + $this->assertInstanceOf('chippyash\Type\Number\IntType', $r->denominator()); + } + + public function testAsFloatTypeReturnsFloatType() + { + $t = new IntType(2); + $f = $t->asFloatType(); + $this->assertInstanceOf('\chippyash\Type\Number\FloatType', $f); + $this->assertEquals('2', (string) $f); + } + + public function testAsIntTypeReturnsIntType() + { + $t = new IntType(2); + $i = $t->asIntType(); + $this->assertInstanceOf('\chippyash\Type\Number\IntType', $i); + $this->assertEquals($t, $i); + } + public function testAbsReturnsAbsoluteValue() { $t1 = new IntType(2); @@ -46,7 +72,7 @@ public function testAbsReturnsAbsoluteValue() $this->assertEquals($t1, $t1->abs()); $this->assertEquals($t1, $t2->abs()); } - + /** * @dataProvider factors */ @@ -55,7 +81,7 @@ public function testFactorsReturnsAnArrayOfFactorsOfTheNumber($n, array $factors $i = new IntType($n); $this->assertEquals($factors, $i->factors()); } - + /** * @dataProvider factors */ @@ -70,10 +96,10 @@ public function testPrimeFactorsReturnsAnArrayOfFactorsOfTheNumber($n, array $ig } $this->assertEquals($pf, $i->primeFactors()); } - + /** * Example factorizations - * + * * @return array [[n, factors, primefactors],...] */ public function factors() diff --git a/test/src/chippyash/Type/Number/Rational/RationalTypeTest.php b/test/src/chippyash/Type/Number/Rational/RationalTypeTest.php index 43faf5f..14bd1bc 100644 --- a/test/src/chippyash/Type/Number/Rational/RationalTypeTest.php +++ b/test/src/chippyash/Type/Number/Rational/RationalTypeTest.php @@ -81,4 +81,5 @@ public function testAbsReturnsAbsoluteValue() $this->assertEquals($r1, $r2->abs()); $this->assertEquals($r1, $r3->abs()); } + }