Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Core / Castle.Core.Tests / GraphTestCase.cs
blobda6c1b3e6ba9d8438e4eaec6c1db658e892695a1
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
19 using NUnit.Framework;
21 using Castle.Core.Internal;
23 [TestFixture]
24 public class GraphTestCase
26 [Test]
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] );
38 [Test]
39 public void Removal()
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 );
54 [Test]
55 public void TopologicalSortOneElement()
57 GraphNode alone = new TestGraphNode("alone");
59 IVertex[] nodes = TopologicalSortAlgo.Sort( new GraphNode[] { alone } );
61 Assert.AreSame( alone, nodes[0] );
64 [Test]
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);
75 IVertex[] nodes =
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] );
84 [Test]
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);
111 IVertex[] nodes =
112 TopologicalSortAlgo.Sort(
113 new GraphNode[]
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)
134 _name = name;
137 public String Name
139 get { return _name; }