1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Core
.Tests
19 using NUnit
.Framework
;
21 using Castle
.Core
.Internal
;
24 public class GraphTestCase
27 public void SimpleUsage()
29 GraphNode parent
= new GraphNode();
30 GraphNode child
= new GraphNode();
32 parent
.AddDependent(child
);
34 Assert
.AreSame( parent
, child
.Dependers
[0] );
35 Assert
.AreSame( child
, parent
.Dependents
[0] );
41 GraphNode parent
= new GraphNode();
42 GraphNode child
= new GraphNode();
44 parent
.AddDependent(child
);
46 child
.RemoveDepender(parent
);
48 Assert
.IsTrue( parent
.Dependents
.Length
== 0 );
49 Assert
.IsTrue( parent
.Dependers
.Length
== 0 );
50 Assert
.IsTrue( child
.Dependers
.Length
== 0 );
51 Assert
.IsTrue( child
.Dependents
.Length
== 0 );
55 public void TopologicalSortOneElement()
57 GraphNode alone
= new TestGraphNode("alone");
59 IVertex
[] nodes
= TopologicalSortAlgo
.Sort( new GraphNode
[] { alone }
);
61 Assert
.AreSame( alone
, nodes
[0] );
65 public void TopologicalSortSimple()
67 GraphNode alone
= new TestGraphNode("alone");
68 GraphNode first
= new TestGraphNode("first");
69 GraphNode second
= new TestGraphNode("second");
70 GraphNode third
= new TestGraphNode("third");
72 first
.AddDependent(second
);
73 second
.AddDependent(third
);
76 TopologicalSortAlgo
.Sort( new GraphNode
[] { alone, second, first, third }
);
78 Assert
.AreSame( first
, nodes
[0] );
79 Assert
.AreSame( second
, nodes
[1] );
80 Assert
.AreSame( third
, nodes
[2] );
81 Assert
.AreSame( alone
, nodes
[3] );
85 public void ComplexDag()
87 GraphNode shirt
= new TestGraphNode("shirt");
88 GraphNode tie
= new TestGraphNode("tie");
89 GraphNode jacket
= new TestGraphNode("jacket");
90 GraphNode belt
= new TestGraphNode("belt");
91 GraphNode watch
= new TestGraphNode("watch");
92 GraphNode undershorts
= new TestGraphNode("undershorts");
93 GraphNode pants
= new TestGraphNode("pants");
94 GraphNode shoes
= new TestGraphNode("shoes");
95 GraphNode socks
= new TestGraphNode("socks");
97 shirt
.AddDependent(belt
);
98 shirt
.AddDependent(tie
);
100 tie
.AddDependent(jacket
);
102 pants
.AddDependent(belt
);
103 pants
.AddDependent(shoes
);
105 undershorts
.AddDependent(pants
);
106 undershorts
.AddDependent(shoes
);
108 socks
.AddDependent(shoes
);
109 belt
.AddDependent(jacket
);
112 TopologicalSortAlgo
.Sort(
114 { shirt, tie, jacket, belt, watch, undershorts, pants, shoes, socks}
);
116 Assert
.AreSame( socks
, nodes
[0] );
117 Assert
.AreSame( undershorts
, nodes
[1] );
118 Assert
.AreSame( pants
, nodes
[2] );
119 Assert
.AreSame( shoes
, nodes
[3] );
120 Assert
.AreSame( watch
, nodes
[4] );
121 Assert
.AreSame( shirt
, nodes
[5] );
122 Assert
.AreSame( tie
, nodes
[6] );
123 Assert
.AreSame( belt
, nodes
[7] );
124 Assert
.AreSame( jacket
, nodes
[8] );
128 public class TestGraphNode
: GraphNode
130 private String _name
;
132 public TestGraphNode(String name
)
139 get { return _name; }