Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92594623
AbstractDirectedGraphTestCase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Nov 21, 19:58
Size
2 KB
Mime Type
text/x-php
Expires
Sat, Nov 23, 19:58 (2 d)
Engine
blob
Format
Raw Data
Handle
22465868
Attached To
rPHU libphutil
AbstractDirectedGraphTestCase.php
View Options
<?php
/**
* @group testcase
*/
final
class
AbstractDirectedGraphTestCase
extends
PhutilTestCase
{
public
function
testTrivialGraph
()
{
$graph
=
array
(
'A'
=>
array
(),
);
$cycle
=
$this
->
findGraphCycle
(
$graph
);
$this
->
assertEqual
(
null
,
$cycle
,
'Trivial Graph'
);
}
public
function
testNoncyclicGraph
()
{
$graph
=
array
(
'A'
=>
array
(
'B'
,
'C'
),
'B'
=>
array
(
'D'
),
'C'
=>
array
(),
'D'
=>
array
(),
);
$cycle
=
$this
->
findGraphCycle
(
$graph
);
$this
->
assertEqual
(
null
,
$cycle
,
'Noncyclic Graph'
);
}
public
function
testTrivialCyclicGraph
()
{
$graph
=
array
(
'A'
=>
array
(
'A'
),
);
$cycle
=
$this
->
findGraphCycle
(
$graph
);
$this
->
assertEqual
(
array
(
'A'
,
'A'
),
$cycle
,
'Trivial Cycle'
);
}
public
function
testCyclicGraph
()
{
$graph
=
array
(
'A'
=>
array
(
'B'
,
'C'
),
'B'
=>
array
(
'D'
),
'C'
=>
array
(
'E'
,
'F'
),
'D'
=>
array
(),
'E'
=>
array
(),
'F'
=>
array
(
'G'
,
'C'
),
'G'
=>
array
(),
);
$cycle
=
$this
->
findGraphCycle
(
$graph
);
$this
->
assertEqual
(
array
(
'A'
,
'C'
,
'F'
,
'C'
),
$cycle
,
'Cyclic Graph'
);
}
public
function
testNonTreeGraph
()
{
// This graph is non-cyclic, but C is both a child and a grandchild of A.
// This is permitted.
$graph
=
array
(
'A'
=>
array
(
'B'
,
'C'
),
'B'
=>
array
(
'C'
),
'C'
=>
array
(),
);
$cycle
=
$this
->
findGraphCycle
(
$graph
);
$this
->
assertEqual
(
null
,
$cycle
,
'NonTreeGraph'
);
}
public
function
testEdgeLoadFailure
()
{
$graph
=
array
(
'A'
=>
array
(
'B'
),
);
$raised
=
null
;
try
{
$this
->
findGraphCycle
(
$graph
);
}
catch
(
Exception
$ex
)
{
$raised
=
$ex
;
}
$this
->
assertEqual
(
true
,
(
bool
)
$raised
,
'Exception raised by unloadable edges.'
);
}
private
function
findGraphCycle
(
array
$graph
,
$seed
=
'A'
,
$search
=
'A'
)
{
$detector
=
new
TestAbstractDirectedGraph
();
$detector
->
setTestData
(
$graph
);
$detector
->
addNodes
(
array_select_keys
(
$graph
,
array
(
$seed
)));
$detector
->
loadGraph
();
return
$detector
->
detectCycles
(
$search
);
}
}
Event Timeline
Log In to Comment