-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVisually_solving_network_navigation.php
More file actions
105 lines (81 loc) · 2.66 KB
/
Visually_solving_network_navigation.php
File metadata and controls
105 lines (81 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php declare(strict_types=1);
namespace Stratadox\PuzzleSolver\Test;
use PHPUnit\Framework\TestCase;
use Stratadox\PuzzleSolver\Find;
use Stratadox\PuzzleSolver\Puzzle\NetworkNavigation\NetworkNavigationPuzzle;
use Stratadox\PuzzleSolver\Renderer\MovesToFileRenderer;
use Stratadox\PuzzleSolver\Renderer\PuzzleStatesToFileRenderer;
use Stratadox\PuzzleSolver\UniversalSolver;
use function file_get_contents;
use function file_put_contents;
use function unlink;
/**
* @testdox Visually solving network navigation
*/
class Visually_solving_network_navigation extends TestCase
{
private const FILE = __DIR__ . '/temp.txt';
private const NETWORK = '[
{
"from": "A",
"to": "B",
"cost": 1.4
},
{
"from": "B",
"to": "C",
"cost": 1.34
},
{
"from": "A",
"to": "C",
"cost": 2.76
}
]';
protected function setUp(): void
{
file_put_contents(self::FILE, '');
}
public static function tearDownAfterClass(): void
{
unlink(self::FILE);
}
/** @test */
function solving_the_puzzle_with_debug_output()
{
$solver = UniversalSolver::aimingTo(Find::aBestSolution())
->withWeightedMoves()
->withLogging(self::FILE, '; ')
->select();
$puzzle = NetworkNavigationPuzzle::fromJsonAndStartAndGoal(self::NETWORK, 'A', 'C');
$solver->solve($puzzle)[0];
$output = file_get_contents(self::FILE);
self::assertEquals('A; B; C', $output);
}
/** @test */
function playing_back_the_solution()
{
$solver = UniversalSolver::aimingTo(Find::aBestSolution())
->withWeightedMoves()
->select();
$renderer = MovesToFileRenderer::fromFilenameAndSeparator(self::FILE, '; ');
$puzzle = NetworkNavigationPuzzle::fromJsonAndStartAndGoal(self::NETWORK, 'A', 'C');
$solution = $solver->solve($puzzle)[0];
$renderer->render($solution);
$output = file_get_contents(self::FILE);
self::assertEquals('Go to B; Go to C', $output);
}
/** @test */
function playing_back_the_solution_states()
{
$solver = UniversalSolver::aimingTo(Find::aBestSolution())
->withWeightedMoves()
->select();
$renderer = PuzzleStatesToFileRenderer::fromFilenameAndSeparator(self::FILE, '; ');
$puzzle = NetworkNavigationPuzzle::fromJsonAndStartAndGoal(self::NETWORK, 'A', 'C');
$solution = $solver->solve($puzzle)[0];
$renderer->render($solution);
$output = file_get_contents(self::FILE);
self::assertEquals('A; B; C', $output);
}
}