Example #1
0
 /**
  * This method tests a set of choices.
  */
 public function testChoice()
 {
     //$this->markTestIncomplete();
     $p0 = IInt32\Type::make(0);
     $e0 = 0;
     $p1 = IInt32\Type::make(1);
     $e1 = 1;
     Control\Type::choice($p0)->when($p0, function (IInt32\Type $x) use($e0) {
         $this->assertSame($e0, $x->unbox());
     })->otherwise(function (IInt32\Type $x) use($e1) {
         $this->assertSame($e1, $x->unbox());
     })->end();
     Control\Type::choice($p0)->when($p1, function (IInt32\Type $x) use($e1) {
         $this->assertSame($e1, $x->unbox());
     })->when($p0, function (IInt32\Type $x) use($e0) {
         $this->assertSame($e0, $x->unbox());
     })->end();
     Control\Type::choice($p0)->when($p1, function (IInt32\Type $x) use($e1) {
         $this->assertSame($e1, $x->unbox());
     })->otherwise(function (IInt32\Type $x) use($e0) {
         $this->assertSame($e0, $x->unbox());
     })->end();
 }
Example #2
0
 /**
  * This method returns the value as an IInt32. Note: Using this method may result in
  * lost of precision.
  *
  * @access public
  * @static
  * @param ITrit\Type $x                                     the object to be converted
  * @return IInt32\Type                                      the value as an IInt32
  */
 public static function toInt32(ITrit\Type $x) : IInt32\Type
 {
     return IInt32\Type::make($x->unbox());
 }
Example #3
0
 /**
  * This method returns a value as a boxed object.  A value is typically a PHP typed
  * primitive or object.  It is considered type-safe.
  *
  * @access public
  * @static
  * @param mixed $value                                      the value(s) to be boxed
  * @return IRatio\Type                                      the boxed object
  */
 public static function make($value) : IRatio\Type
 {
     $values = is_array($value) ? $value : func_get_args();
     $values = array_map(function ($value) {
         return $value instanceof IInt32\Type ? $value : IInt32\Type::make($value);
     }, $values);
     $denominator = $values[1];
     $signum = IInt32\Module::signum($denominator)->unbox();
     if ($signum == 0) {
         throw new Throwable\InvalidArgument\Exception('Unable to create ratio. Denominator must not be zero.');
     }
     $numerator = $values[0];
     if (IInt32\Module::signum($numerator)->unbox() == 0) {
         return IRatio\Type::zero();
     }
     if ($signum < 0) {
         $numerator = IInt32\Module::negate($numerator);
         $denominator = IInt32\Module::negate($denominator);
     }
     $gcd = IInt32\Module::gcd($numerator, $denominator);
     if (!IInt32\Module::eq($gcd, IInt32\Type::one())->unbox()) {
         $numerator = IInt32\Module::divide($numerator, $gcd);
         $denominator = IInt32\Module::divide($denominator, $gcd);
     }
     return new IRatio\Type($numerator, $denominator);
 }
Example #4
0
 /**
  * This method tests the "make" method.
  *
  * @dataProvider data_make
  */
 public function test_make(array $provided, array $expected)
 {
     $p0 = IInt32\Type::make($provided[0]);
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p0);
     $p1 = $p0->unbox();
     $e1 = $expected[0];
     $this->assertInternalType('integer', $p1);
     $this->assertSame($e1, $p1);
 }