Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extra test cases and some sanity checks
- Loading branch information
Showing
10 changed files
with
303 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\Pathfinder\Test\Sanity; | ||
|
||
use BadMethodCallException; | ||
use PHPUnit\Framework\TestCase; | ||
use function random_int; | ||
use Stratadox\Pathfinder\Graph\At; | ||
|
||
/** | ||
* @testdox Sanity check to assert that positions are infinite and immutable | ||
*/ | ||
class AtPositionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider dimensionAndCoordinates | ||
*/ | ||
function all_dimensions_are_accessible(int $dimension, float ...$coordinates) | ||
{ | ||
$position = At::position(...$coordinates); | ||
|
||
$this->assertTrue(isset($position[$dimension])); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider dimensionAndCoordinates | ||
*/ | ||
function cannot_write_to_positions(int $dimension, float ...$coordinates) | ||
{ | ||
$position = At::position(...$coordinates); | ||
$number = random_int(100, 1000) / random_int(10, 1000); | ||
|
||
$this->expectException(BadMethodCallException::class); | ||
|
||
$position[$dimension] = $number; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider dimensionAndCoordinates | ||
*/ | ||
function cannot_unset_positions(int $dimension, float ...$coordinates) | ||
{ | ||
$position = At::position(...$coordinates); | ||
|
||
$this->expectException(BadMethodCallException::class); | ||
|
||
unset($position[$dimension]); | ||
} | ||
|
||
public function dimensionAndCoordinates(): iterable | ||
{ | ||
$random = random_int(10, 90000); | ||
return [ | ||
'First dimension of [1,1]' => [0, 1, 1], | ||
'Third dimension of [1,1]' => [2, 1, 1], | ||
'Fifth dimension of [5,3]' => [4, 5, 3], | ||
'Fifth dimension of [5,3,4]' => [4, 5, 3, 4], | ||
$random.'th dimension of [5,3,4]' => [$random, 5, 3, 4], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\Pathfinder\Test\Sanity; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Stratadox\Pathfinder\Graph\Builder\WithoutEdges; | ||
|
||
/** | ||
* @testdox Sanity check to assert that edge creation behaves properly | ||
*/ | ||
class EdgeBuildingTest extends TestCase | ||
{ | ||
/** @test */ | ||
function zero_edges_plus_one_edge_is_one_edge() | ||
{ | ||
$edges = WithoutEdges::poorThing()->andTo('A'); | ||
|
||
$this->assertCount(1, $edges->gather()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\Pathfinder\Test\Sanity; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Stratadox\Pathfinder\Graph\Builder\GraphNetwork; | ||
use Stratadox\Pathfinder\Graph\Builder\WithEdge; | ||
use Stratadox\Pathfinder\Graph\GeometricView; | ||
use Stratadox\Pathfinder\Graph\Ids; | ||
|
||
/** | ||
* @testdox Sanity check to assert that the geometric view is a proper adapter | ||
*/ | ||
class GeometricViewTest extends TestCase | ||
{ | ||
/** @test */ | ||
function fetching_all_indices() | ||
{ | ||
$graph = GraphNetwork::create() | ||
->withVertex('A', WithEdge::to('B')) | ||
->withVertex('B', WithEdge::to('A')) | ||
->make(); | ||
|
||
$environment = GeometricView::of($graph); | ||
|
||
$this->assertEquals( | ||
Ids::consistingOf('A', 'B'), | ||
$environment->all() | ||
); | ||
} | ||
|
||
/** @test */ | ||
function detecting_negative_weights() | ||
{ | ||
$graph = GraphNetwork::create() | ||
->withVertex('A', WithEdge::to('B', -1.5)) | ||
->withVertex('B', WithEdge::to('A')) | ||
->make(); | ||
|
||
$environment = GeometricView::of($graph); | ||
|
||
$this->assertTrue($environment->hasNegativeEdgeCosts()); | ||
} | ||
|
||
/** @test */ | ||
function detecting_no_negative_weights() | ||
{ | ||
$graph = GraphNetwork::create() | ||
->withVertex('A', WithEdge::to('B')) | ||
->withVertex('B', WithEdge::to('A')) | ||
->make(); | ||
|
||
$environment = GeometricView::of($graph); | ||
|
||
$this->assertFalse($environment->hasNegativeEdgeCosts()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\Pathfinder\Test\Sanity; | ||
|
||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
use Stratadox\Pathfinder\Distance\Euclidean; | ||
use Stratadox\Pathfinder\Graph\At; | ||
use Stratadox\Pathfinder\Graph\Builder\GraphEnvironment; | ||
use Stratadox\Pathfinder\Graph\Builder\WithEdge; | ||
|
||
/** | ||
* @testdox Sanity check to assert that graphs behave properly | ||
*/ | ||
class GraphEnvironmentTest extends TestCase | ||
{ | ||
/** @test */ | ||
function cannot_auto_determine_the_cost_for_an_edge_that_goes_nowhere() | ||
{ | ||
$builder = GraphEnvironment::create() | ||
->withLocation('A', At::position(0, 0), WithEdge::to('B')) | ||
->determineEdgeCostsAs(Euclidean::distance()); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$builder->make(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\Pathfinder\Test\Sanity; | ||
|
||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
use Stratadox\Pathfinder\Graph\At; | ||
use Stratadox\Pathfinder\Graph\Builder\GraphEnvironment; | ||
use Stratadox\Pathfinder\Graph\Builder\GraphNetwork; | ||
use Stratadox\Pathfinder\Graph\Builder\WithEdge; | ||
|
||
/** | ||
* @testdox Sanity check to assert that graphs behave properly | ||
*/ | ||
class GraphTest extends TestCase | ||
{ | ||
/** @test */ | ||
function cannot_get_cost_between_non_neighbouring_nodes_in_a_network() | ||
{ | ||
$network = GraphNetwork::create() | ||
->withVertex('A', WithEdge::to('B')) | ||
->withVertex('B', WithEdge::to('C')) | ||
->withVertex('C', WithEdge::to('A')) | ||
->make(); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$network->movementCostBetween('A', 'C'); | ||
} | ||
|
||
/** @test */ | ||
function cannot_get_cost_between_non_neighbouring_nodes_in_an_environment() | ||
{ | ||
$network = GraphEnvironment::create() | ||
->withLocation('A', At::position(0, 0), WithEdge::to('B')) | ||
->withLocation('B', At::position(0, 1), WithEdge::to('C')) | ||
->withLocation('C', At::position(0, 2), WithEdge::to('A')) | ||
->make(); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
|
||
$network->movementCostBetween('A', 'C'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Stratadox\Pathfinder\Test\Sanity; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Stratadox\Pathfinder\Graph\Builder\Obstacle; | ||
|
||
/** | ||
* @testdox Sanity check to assert that obstacles have infinite costs | ||
*/ | ||
class ObstacleTest extends TestCase | ||
{ | ||
/** @test */ | ||
function obstacles_have_infinite_costs() | ||
{ | ||
$obstacle = Obstacle::here(); | ||
|
||
$this->assertInfinite($obstacle->price()); | ||
} | ||
|
||
/** @test */ | ||
function repriced_obstacles_still_have_infinite_costs() | ||
{ | ||
$obstacle = Obstacle::here()->costing(1.0); | ||
|
||
$this->assertInfinite($obstacle->price()); | ||
} | ||
|
||
/** @test */ | ||
function obstacles_do_not_have_labels() | ||
{ | ||
$obstacle = Obstacle::here(); | ||
|
||
$this->assertEmpty($obstacle->label()); | ||
} | ||
} |