Skip to content

Commit

Permalink
add tests for new numeric methods
Browse files Browse the repository at this point in the history
  • Loading branch information
akitson-fu committed Aug 4, 2014
1 parent 2c2ba4b commit b1c053e
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 27 deletions.
11 changes: 8 additions & 3 deletions README.md
Expand Up @@ -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:
Expand Down Expand Up @@ -207,7 +210,7 @@ Install [Composer](https://getcomposer.org/)
add

<pre>
"chippyash/strong-type": ">=1.0.8"
"chippyash/strong-type": ">=1.0.9"
</pre>

to your composer.json "requires" section
Expand Down Expand Up @@ -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
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
54 changes: 52 additions & 2 deletions src/chippyash/Type/Number/Complex/ComplexType.php
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
35 changes: 33 additions & 2 deletions src/chippyash/Type/Number/FloatType.php
Expand Up @@ -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
*
Expand Down
41 changes: 36 additions & 5 deletions src/chippyash/Type/Number/IntType.php
Expand Up @@ -37,14 +37,45 @@ 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(
new RationalType($this, $one), new RationalType(new IntType(0), $one)
);
}

/**
* 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
*
Expand All @@ -62,7 +93,7 @@ protected function typeOf($value)

/**
* Return all factors of this number (sorted)
*
*
* @return array [factor,factor, ...]
*/
public function factors()
Expand All @@ -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()
Expand Down Expand Up @@ -120,7 +151,7 @@ public function primeFactors()
}
}
} while ($n > 1 && $d <= $dmax);

return $factors;
}

Expand Down
26 changes: 25 additions & 1 deletion src/chippyash/Type/Number/NumericTypeInterface.php
Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion src/chippyash/Type/Number/Rational/AbstractRationalType.php
Expand Up @@ -91,14 +91,46 @@ 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()),
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 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
*
Expand Down
32 changes: 30 additions & 2 deletions test/src/chippyash/Type/Number/AbstractRationalTypeTest.php
Expand Up @@ -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())
Expand All @@ -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);
}
}

0 comments on commit b1c053e

Please sign in to comment.