PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/YEDNodeTest.j

http://github.com/rheimbuch/YED
Unknown | 164 lines | 142 code | 22 blank | 0 comment | 0 complexity | e647dabc1d03345c9454b573c8384137 MD5 | raw file
  1@import "../YEDNode.j"
  2
  3// CPLogRegister(CPLogPrint);
  4
  5@implementation YEDNodeTest : OJTestCase
  6
  7- (void)testYEDNodeCanConnectToAnotherYEDNode
  8{
  9    var node1 = [YEDNode nodeWithName:"node1"];
 10    var node2 = [YEDNode nodeWithName:"node2"];
 11    [self assertTrue:([node1 canConnectTo:node2])
 12          message:"node1 should be able to connect to node2"];
 13    
 14    [self assertTrue:([node2 canConnectTo:node2])
 15          message:"node2 should be able to connect to node1"];
 16}
 17
 18- (void)testYEDNodeAllDescendents
 19{
 20    // Create nodes
 21    var n1 = [YEDNode nodeWithName:"n1"],
 22        n2 = [YEDNode nodeWithName:"n2"],
 23        n3 = [YEDNode nodeWithName:"n3"],
 24        n4 = [YEDNode nodeWithName:"n4"],
 25        n5 = [YEDNode nodeWithName:"n5"],
 26        n6 = [YEDNode nodeWithName:"n6"],
 27        n7 = [YEDNode nodeWithName:"n7"];
 28        
 29    //Construct Graph (See ./YEDNodeTestGraph.dot)
 30                            // digraph YEDNodeTest
 31                            // {
 32    [n1 directedEdgeTo:n2]; //  n1 -> n2
 33    [n1 directedEdgeTo:n3]; //  n1 -> n3
 34    [n2 directedEdgeTo:n3]; //  n2 -> n3
 35    [n2 directedEdgeTo:n4]; //  n2 -> n4
 36    [n3 directedEdgeTo:n5]; //  n3 -> n5
 37    [n5 directedEdgeTo:n6]; //  n5 -> n6
 38    [n5 directedEdgeTo:n7]; //  n5 -> n7
 39    [n4 directedEdgeTo:n7]; //  n4 -> n7
 40                            // }
 41    
 42    // Setup what the descendents should be
 43    var n1Descendents = [CPSet setWithArray:[n2,n3,n4,n5,n6,n7]],
 44        n2Descendents = [CPSet setWithArray:[n3,n4,n5,n6,n7]],
 45        n3Descendents = [CPSet setWithArray:[n5,n6,n7]],
 46        n4Descendents = [CPSet setWithArray:[n7]],
 47        n5Descendents = [CPSet setWithArray:[n6,n7]],
 48        n6Descendents = [CPSet set],
 49        n7Descendents = [CPSet set];
 50        
 51    [self assertTrue:([[n1 allDescendents] isEqualToSet:n1Descendents])
 52          message:"n1 should have all other nodes as it's descendents"];
 53    [self assertTrue:([[n2 allDescendents] isEqualToSet:n2Descendents])
 54          message:"n2 should have n3-n7 as descendents"];
 55    [self assertTrue:([[n3 allDescendents] isEqualToSet:n3Descendents])
 56          message:"n3 should have n5-n7 as descendents"];
 57    [self assertTrue:([[n4 allDescendents] isEqualToSet:n4Descendents])
 58          message:"n4 should have n7 as its descendent"];
 59    [self assertTrue:([[n5 allDescendents] isEqualToSet:n5Descendents])
 60          message:"n5 should have n6 & n7 as descendents"];
 61    [self assertTrue:([[n6 allDescendents] isEqualToSet:n6Descendents])
 62          message:"n6 should have no nodes as descendents"];
 63    [self assertTrue:([[n7 allDescendents] isEqualToSet:n7Descendents])
 64          message:"n7 should have no nodes as descendents"];
 65}
 66
 67- (void)testYEDNodeGraphHasCyclesFunctionDetectsSimpleCycles
 68{
 69    var n1 = [YEDNode nodeWithName:"n1"],
 70        n2 = [YEDNode nodeWithName:"n2"];
 71    
 72    // simplest acyclical graph
 73    [n1 directedEdgeTo:n2];
 74    [self assertFalse:YEDNodeGraphHasCycles(n1)
 75            message:"Starting at n1, cycle between n1, n2 SHOULD NOT be detected"];
 76    [self assertFalse:YEDNodeGraphHasCycles(n2)
 77            message:"Starting at n2, cycle between n1, n2 SHOULD NOT be detected"];
 78    
 79    [self assertFalse:YEDNodeGraphHasCycles(n1,YES) //Reverse search for cycles through inEdges
 80            message:"Starting at n1, cycle between n1, n2 SHOULD NOT be detected"];
 81    [self assertFalse:YEDNodeGraphHasCycles(n2,YES) //Reverse search for cycles through inEdges
 82            message:"Starting at n2, cycle between n1, n2 SHOULD NOT be detected"];
 83    
 84    // Now create a cycle
 85    [n2 directedEdgeTo:n1];
 86    
 87    [self assertTrue:YEDNodeGraphHasCycles(n1)
 88            message:"Starting at n1, cycle between n1, n2 SHOULD be detected"];
 89    [self assertTrue:YEDNodeGraphHasCycles(n2)
 90            message:"Starting at n2, cycle between n1, n2 SHOULD be detected"];
 91            
 92    [self assertTrue:YEDNodeGraphHasCycles(n1,YES) //Search for cycles through inEdges
 93            message:"Starting at n1, cycle between n1, n2 SHOULD be detected"];
 94    [self assertTrue:YEDNodeGraphHasCycles(n2,YES) //Search for cycles through inEdges
 95            message:"Starting at n2, cycle between n1, n2 SHOULD be detected"];
 96}
 97
 98- (void)testYEDNodeGraphHasCyclesFunctionDetectsComplexCycles
 99{
100    // Create nodes
101    var n1 = [YEDNode nodeWithName:"n1"],
102        n2 = [YEDNode nodeWithName:"n2"],
103        n3 = [YEDNode nodeWithName:"n3"],
104        n4 = [YEDNode nodeWithName:"n4"],
105        n5 = [YEDNode nodeWithName:"n5"],
106        n6 = [YEDNode nodeWithName:"n6"],
107        n7 = [YEDNode nodeWithName:"n7"];
108        
109    //Construct Graph with cycles
110                            // digraph YEDNodeTestCycles
111                            // {
112    [n1 directedEdgeTo:n2]; //  n1 -> n2
113    [n1 directedEdgeTo:n3]; //  n1 -> n3
114    [n2 directedEdgeTo:n3]; //  n2 -> n3
115    [n2 directedEdgeTo:n4]; //  n2 -> n4
116    [n3 directedEdgeTo:n5]; //  n3 -> n5
117    [n5 directedEdgeTo:n6]; //  n5 -> n6
118    [n6 directedEdgeTo:n4]; //  n6 -> n4
119    [n5 directedEdgeTo:n7]; //  n5 -> n7
120    [n4 directedEdgeTo:n7]; //  n4 -> n7
121    [n7 directedEdgeTo:n2]; //  n7 -> n2
122                            // }
123    
124    //Starting at n1 we should detect a cycle
125    [self assertTrue:YEDNodeGraphHasCycles(n1)
126            message:"Starting at n1, cycle should be detected"];
127    [self assertTrue:YEDNodeGraphHasCycles(n7)
128            message:"Starting at n7, cycle should be detected"];
129}
130
131- (void)testYEDNodeCyclePrevention
132{
133    var n1 = [YEDNode acyclicNodeWithName:"n1"],
134        n2 = [YEDNode acyclicNodeWithName:"n2"],
135        n3 = [YEDNode acyclicNodeWithName:"n3"];
136    
137    // Setup acyclic graph
138    
139    [n1 directedEdgeTo:n2];
140    [n2 directedEdgeTo:n3];
141    
142    [self assertFalse:[n1 cycleInDescendents]
143            message:"n1 should not have any cycles in its descendents"];
144    
145    // Try to introduce a cycle
146    try
147    {
148        CPLog.info("Introducing a cycle: n3 -> n1");
149        [n3 directedEdgeTo:n1];
150    }
151    catch(err)
152    {
153        CPLog.info("Caught Error: %s", err);
154    }
155    // The edge should not have been introduced
156    [self assertFalse:([[n3 outEdges] containsObject:n1])
157            message:"n1 should not be in n3's outgoing edges"];
158    [self assertFalse:([[n1 inEdges] containsObject:n3])
159            message:"n3 should not be in n1's incoming edges"];
160    // Therefore there should not be a cycle
161    [self assertFalse:[n1 cycleInDescendents]
162            message:"n1 should not have any cycles in its descendents"];
163}
164@end