Skip to content

Commit

Permalink
Add less than comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeboer committed Aug 16, 2019
1 parent 90fc488 commit b7f2650
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Common/Interfaces/LinearDim.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public function unit (): LinearUnit;
public function times (float $multiplier): LinearDim;
public function minus (LinearDim $dim): LinearDim;
public function max (float $value): LinearDim;
public function lessThan (LinearDim $dim): bool;

/**
* @param \Dbt\Volumes\Common\Interfaces\LinearUnit|null $unit
Expand Down
2 changes: 1 addition & 1 deletion src/ConicalFrustumWithAngle.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function __construct (
$opposite = new Line(tan($angle->value()), $this->baseLinearUnit());

/** @var RadialDim $bottom */
$this->bottom = $top->minus($opposite);
$this->bottom = $this->top->minus($opposite);
}
}
29 changes: 27 additions & 2 deletions src/Dimensions/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dbt\Volumes\Dimensions;

use Dbt\Volumes\Common\Exceptions\WrongUnit;
use Dbt\Volumes\Common\Interfaces\Dim;
use Dbt\Volumes\Common\Interfaces\LinearDim;
use Dbt\Volumes\Common\Interfaces\LinearUnit;
Expand Down Expand Up @@ -52,13 +53,37 @@ public function times (float $multiplier): LinearDim
return $this->of($this->value() * $multiplier);
}

public function max (float $value): LinearDim
{
return $this->of(max($value, $this->value()));
}

public function minus (LinearDim $dim): LinearDim
{
$this->assertSameUnit($dim);

return $this->of($this->value() - $dim->value());
}

public function max (float $value): LinearDim
/*
* Comparison
*/

public function lessThan (LinearDim $dim): bool
{
return $this->of(max($value, $this->value()));
$this->assertSameUnit($dim);

return $this->value() < $dim->value();
}

/*
* Assertions
*/

private function assertSameUnit (LinearDim $dim): void
{
if (!$this->hasSameUnit($dim)) {
throw new WrongUnit();
}
}
}
29 changes: 29 additions & 0 deletions tests/Dimensions/LineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dbt\Volumes\Tests\Dimensions;

use Dbt\Volumes\Common\Exceptions\WrongUnit;
use Dbt\Volumes\Dimensions\Line;
use Dbt\Volumes\Tests\UnitTestCase;
use Dbt\Volumes\Units\Inch;
Expand Down Expand Up @@ -85,4 +86,32 @@ public function maxing_immutably ()
$voNeg->max($max)->value()
);
}

/** @test */
public function comparing ()
{
$lower = (float) rand(1, 99);
$upper = (float) rand(100, 199);
$unit = new None();

$vo1 = new Line($lower, $unit);
$vo2 = new Line($upper, $unit);

$this->assertTrue($vo1->lessThan($vo2));
$this->assertFalse($vo2->lessThan($vo1));
}

/** @test */
public function failing_to_compare ()
{
$this->expectException(WrongUnit::class);

$lower = (float) rand(1, 99);
$upper = (float) rand(100, 199);

$vo1 = new Line($lower, new Inch());
$vo2 = new Line($upper, new Millimeter());

$vo1->lessThan($vo2);
}
}

0 comments on commit b7f2650

Please sign in to comment.