TestCloneBad4() { // Vertices not in same graph. try { IVertex oVertex1 = m_aoVertices[0]; IVertex oVertex2 = m_aoVertices[1]; IEdge oEdge = CreateEdge(oVertex1, oVertex2, true); IGraph oGraph2 = new Graph(); IVertex [] aoVertices2 = TestGraphUtil.AddVertices(oGraph2, 2); oEdge.Clone(true, true, oVertex1, aoVertices2[0], true); } catch (ArgumentException oArgumentException) { Assert.AreEqual( "Microsoft.NodeXL.Core." + "Edge.Constructor: vertex1 and vertex2 have been added to" + " different graphs. An edge can't connect vertices from" + " different graphs.\r\n" + "Parameter name: vertex2" , oArgumentException.Message ); throw oArgumentException; } }
TestSort() { // Ascending sort using default comparer, which sorts by ID. const Int32 Vertices = 100; IGraph oGraph = new Graph(); IVertex [] aoUnsortedVertices = TestGraphUtil.AddVertices(oGraph, Vertices); IVertexCollection oVertexCollection = oGraph.Vertices; ICollection <IVertex> oSortedVertices = m_oByDelegateVertexSorter.Sort(oVertexCollection); Assert.AreEqual(Vertices, oSortedVertices.Count); Int32 iPreviousID = -1; foreach (IVertex oSortedVertex in oSortedVertices) { Int32 iID = oSortedVertex.ID; if (iPreviousID != -1) { Assert.AreEqual(iPreviousID + 1, iID); } iPreviousID = iID; } }
AddVertices ( Int32 iVerticesToAdd ) { Debug.Assert(iVerticesToAdd >= 0); return(TestGraphUtil.AddVertices(m_oGraph, iVerticesToAdd)); }
CreateGraph ( GraphDirectedness eDirectedness, Int32 iVertices ) { Debug.Assert(iVertices >= 0); m_oGraph = new Graph(eDirectedness); m_aoVertices = TestGraphUtil.AddVertices(m_oGraph, iVertices); }
TestSort4() { // Descending sort on Double. ByMetadataVertexSorter <Double> oByMetadataVertexSorter = new ByMetadataVertexSorter <Double>(SortKey); const Int32 Vertices = 100; IGraph oGraph = new Graph(); IVertex [] aoUnsortedVertices = TestGraphUtil.AddVertices(oGraph, Vertices); IVertexCollection oVertexCollection = oGraph.Vertices; Int32 i; for (i = 0; i < Vertices; i++) { aoUnsortedVertices[i].SetValue(SortKey, (Double)(Vertices - i)); } oByMetadataVertexSorter.SortAscending = false; for (i = 0; i < Vertices; i++) { aoUnsortedVertices[i].SetValue(SortKey, (Double)(Vertices - i)); } ICollection <IVertex> oSortedVertices = oByMetadataVertexSorter.Sort(oVertexCollection); Assert.AreEqual(Vertices, oSortedVertices.Count); i = 0; foreach (IVertex oSortedVertex in oSortedVertices) { Assert.AreEqual( (Double)(Vertices - i), (Double)oSortedVertex.GetRequiredValue( SortKey, typeof(Double)) ); i++; } }
TestVertices() { Assert.IsNotNull(m_oGraph.Vertices); Assert.IsInstanceOfType(m_oGraph.Vertices, typeof(VertexCollection)); Assert.AreEqual(0, m_oGraph.Vertices.Count); const Int32 Vertices = 1000; IVertex [] aoVertices = TestGraphUtil.AddVertices(m_oGraph, Vertices); Assert.AreEqual(Vertices, m_oGraph.Vertices.Count); foreach (IVertex oVertex in aoVertices) { Assert.IsTrue(m_oGraph.Vertices.Contains(oVertex)); } }
TestLayOut() { const Int32 Vertices = 100; IGraph oGraph = new Graph(); IVertex [] aoVertices = TestGraphUtil.AddVertices(oGraph, Vertices); TestGraphUtil.MakeGraphComplete(oGraph, aoVertices, false); // Initialize the vertex locations to impossible values. const Int32 ImpossibleCoordinate = Int32.MinValue; foreach (IVertex oVertex in aoVertices) { oVertex.Location = new Point( ImpossibleCoordinate, ImpossibleCoordinate); } const Int32 Width = 1000; const Int32 Height = 600; Rectangle oRectangle = new Rectangle(0, 0, Width, Height); LayoutContext oLayoutContext = new LayoutContext(oRectangle); m_oFruchtermanReingoldLayout.LayOutGraph(oGraph, oLayoutContext); foreach (IVertex oVertex in aoVertices) { PointF oLocation = oVertex.Location; Single fX = oLocation.X; Assert.AreNotEqual(fX, ImpossibleCoordinate); Assert.IsTrue(fX >= 0); Assert.IsTrue(fX <= Width); Single fY = oLocation.Y; Assert.AreNotEqual(fY, ImpossibleCoordinate); Assert.IsTrue(fY >= 0); Assert.IsTrue(fY <= Height); } }
TestEdges() { Assert.IsNotNull(m_oGraph.Edges); Assert.IsInstanceOfType(m_oGraph.Edges, typeof(EdgeCollection)); Assert.AreEqual(0, m_oGraph.Edges.Count); const Int32 Vertices = 100; IVertex [] aoVertices = TestGraphUtil.AddVertices(m_oGraph, Vertices); IEdge [] aoEdges = TestGraphUtil.MakeGraphComplete(m_oGraph, aoVertices, false); Assert.AreEqual(aoEdges.Length, m_oGraph.Edges.Count); foreach (IEdge oEdge in aoEdges) { Assert.IsTrue(m_oGraph.Edges.Contains(oEdge)); } }
TestEdgeAdded() { const Int32 Vertices = 2000; IVertex [] aoVertices = TestGraphUtil.AddVertices(m_oGraph, Vertices); for (Int32 i = 1; i < Vertices; i++) { IEdge oEdge = new Edge(aoVertices[0], aoVertices[i], false); m_bEdgeAdded = false; m_oAddedEdge = null; m_oGraph.Edges.Add(oEdge); Assert.IsTrue(m_bEdgeAdded); Assert.AreEqual(oEdge, m_oAddedEdge); } }
TestSort2() { // Ascending sort on IVertex.Name. m_oByDelegateVertexSorter.VertexComparer = this.CompareVerticesByName; const Int32 Vertices = 100; IGraph oGraph = new Graph(); IVertex [] aoUnsortedVertices = TestGraphUtil.AddVertices(oGraph, Vertices); IVertexCollection oVertexCollection = oGraph.Vertices; Int32 i; for (i = 0; i < Vertices; i++) { aoUnsortedVertices[i].Name = (Vertices - i).ToString("D3"); } ICollection <IVertex> oSortedVertices = m_oByDelegateVertexSorter.Sort(oVertexCollection); Assert.AreEqual(Vertices, oSortedVertices.Count); i = 0; foreach (IVertex oSortedVertex in oSortedVertices) { Assert.AreEqual( (i + 1).ToString("D3"), oSortedVertex.Name ); i++; } }
TestSort2() { // Descending sort on Int32. const Int32 Vertices = 100; IGraph oGraph = new Graph(); IVertex [] aoUnsortedVertices = TestGraphUtil.AddVertices(oGraph, Vertices); IVertexCollection oVertexCollection = oGraph.Vertices; Int32 i; for (i = 0; i < Vertices; i++) { aoUnsortedVertices[i].SetValue(SortKey, Vertices - i); } m_oByMetadataVertexSorter.SortAscending = false; ICollection <IVertex> oSortedVertices = m_oByMetadataVertexSorter.Sort(oVertexCollection); Assert.AreEqual(Vertices, oSortedVertices.Count); i = 0; foreach (IVertex oSortedVertex in oSortedVertices) { Assert.AreEqual( Vertices - i, (Int32)oSortedVertex.GetRequiredValue(SortKey, typeof(Int32)) ); i++; } }
TestLargeGraph() { // Create a large graph, make sure there are no exceptions or // assertions. const Int32 Vertices = 1000000; const Int32 Edges = 100000; IVertex [] aoVertices = TestGraphUtil.AddVertices(m_oGraph, Vertices); Random oRandom = new Random(1); for (Int32 i = 0; i < Edges; i++) { IVertex oVertex1 = aoVertices[oRandom.Next(Vertices)]; IVertex oVertex2 = aoVertices[oRandom.Next(Vertices)]; IEdgeCollection oEdgeCollection = m_oGraph.Edges; oEdgeCollection.Add(oVertex1, oVertex2); } Assert.AreEqual(Vertices, m_oGraph.Vertices.Count); Assert.AreEqual(Edges, m_oGraph.Edges.Count); m_oGraph.Edges.Clear(); Assert.AreEqual(Vertices, m_oGraph.Vertices.Count); Assert.AreEqual(0, m_oGraph.Edges.Count); m_oGraph.Vertices.Clear(); Assert.AreEqual(0, m_oGraph.Vertices.Count); Assert.AreEqual(0, m_oGraph.Edges.Count); }
TestRandomGraph ( Int32 iVertices, Random oRandom ) { Debug.Assert(iVertices >= 0); Debug.Assert(oRandom != null); // Stores the edges actually added to the graph. List <IEdge> oActualEdges = new List <IEdge>(); // Create a graph with random directedness. GraphDirectedness eDirectedness = TestGraphUtil.AllGraphDirectedness[ oRandom.Next(TestGraphUtil.AllGraphDirectedness.Length)]; InitializeGraph(eDirectedness); // Add random vertices. IVertex [] aoVertices = TestGraphUtil.AddVertices(m_oGraph, iVertices); Assert.AreEqual(iVertices, m_oGraph.Vertices.Count); Assert.AreEqual(0, m_oGraph.Edges.Count); // Add random edges. Int32 iAttemptedEdges = oRandom.Next(iVertices); IEdgeCollection oEdgeCollection = m_oGraph.Edges; for (Int32 i = 0; i < iAttemptedEdges; i++) { Boolean bIsDirected = false; switch (eDirectedness) { case GraphDirectedness.Undirected: bIsDirected = false; break; case GraphDirectedness.Directed: bIsDirected = true; break; case GraphDirectedness.Mixed: bIsDirected = (oRandom.Next(2) % 2 == 0); break; default: Debug.Assert(false); break; } IVertex oVertex1 = aoVertices[oRandom.Next(iVertices)]; IVertex oVertex2 = aoVertices[oRandom.Next(iVertices)]; IEdge oEdge = oEdgeCollection.Add(oVertex1, oVertex2, bIsDirected); oActualEdges.Add(oEdge); } Assert.AreEqual(iVertices, m_oGraph.Vertices.Count); Assert.AreEqual(oActualEdges.Count, m_oGraph.Edges.Count); // Set random metadata. foreach (IVertex oVertex in m_oGraph.Vertices) { String sName = null; if (oRandom.Next(3) % 3 == 0) { MetadataUtil.SetRandomMetadata( oVertex, true, true, oVertex.ID); // Mark the vertex as having metadata. sName = MetadataMarker; } oVertex.Name = sName; } foreach (IEdge oEdge in m_oGraph.Edges) { String sName = null; if (oRandom.Next(4) % 4 == 0) { MetadataUtil.SetRandomMetadata(oEdge, true, true, oEdge.ID); sName = MetadataMarker; } oEdge.Name = sName; } MetadataUtil.SetRandomMetadata(m_oGraph, true, true, m_oGraph.ID); // Check the random metadata. CheckRandomMetadataOnRandomGraph(); // Remove random edges. Int32 iRemovedEdges = 0; if (oRandom.Next(2) % 2 == 0) { foreach (IEdge oEdge in oActualEdges) { if (oRandom.Next(5) % 5 == 0) { m_oGraph.Edges.Remove(oEdge); iRemovedEdges++; } } } Assert.AreEqual(iVertices, m_oGraph.Vertices.Count); Assert.AreEqual(oActualEdges.Count - iRemovedEdges, m_oGraph.Edges.Count); // Remove random vertices. Int32 iRemovedVertices = 0; if (oRandom.Next(2) % 2 == 0) { foreach (IVertex oVertex in aoVertices) { if (oRandom.Next(3) % 3 == 0) { m_oGraph.Vertices.Remove(oVertex); iRemovedVertices++; } } } Assert.AreEqual(iVertices - iRemovedVertices, m_oGraph.Vertices.Count); // Note: Can't test m_oGraph.Edges.Count here, because removing // vertices probably removed some edges as well. // Check the random metadata on the remaining objects. CheckRandomMetadataOnRandomGraph(); // Check all the vertices, including the ones that were removed. // First, store all the non-removed vertex IDs in a dictionary to avoid // having to repeatedly call Vertices.Contains(), which is slow. Dictionary <Int32, Byte> oContainedVertexIDs = new Dictionary <Int32, Byte>(); foreach (IVertex oVertex in m_oGraph.Vertices) { oContainedVertexIDs.Add(oVertex.ID, 0); } foreach (IVertex oVertex in aoVertices) { Boolean bContainedInGraph = oContainedVertexIDs.ContainsKey(oVertex.ID); Assert.AreEqual(bContainedInGraph, oVertex.ParentGraph != null); if (oVertex.Name == MetadataMarker) { MetadataUtil.CheckRandomMetadata( oVertex, true, true, oVertex.ID); } else { Assert.IsNull(oVertex.Tag); } } oContainedVertexIDs.Clear(); // Remove all edges. m_oGraph.Edges.Clear(); Assert.AreEqual(iVertices - iRemovedVertices, m_oGraph.Vertices.Count); Assert.AreEqual(0, m_oGraph.Edges.Count); // Remove all vertices. m_oGraph.Vertices.Clear(); Assert.AreEqual(0, m_oGraph.Vertices.Count); Assert.AreEqual(0, m_oGraph.Edges.Count); // Check all the vertices. foreach (IVertex oVertex in aoVertices) { Boolean bContainedInGraph = m_oGraph.Vertices.Contains(oVertex); Assert.IsFalse(bContainedInGraph); if (oVertex.Name == MetadataMarker) { MetadataUtil.CheckRandomMetadata( oVertex, true, true, oVertex.ID); } else { Assert.IsNull(oVertex.Tag); } } }
TestClone() { // Loop through multiple scenarios. Int32 [] aiVertices = new Int32 [] { 0, 6 }; foreach (Boolean bCopyMetadataValues in TestGraphUtil.AllBoolean) { foreach (Boolean bCopyTag in TestGraphUtil.AllBoolean) { foreach (GraphDirectedness eDirectedness in TestGraphUtil.AllGraphDirectedness) { foreach (Int32 iVertices in aiVertices) { foreach (Boolean bAddEdges in TestGraphUtil.AllBoolean) { const String Name = "fdjkerwuio"; // Prepare the graph to be cloned. InitializeGraph(eDirectedness); m_oGraph.Name = Name; MetadataUtil.SetRandomMetadata(m_oGraph, true, true, m_oGraph.ID); // Add the vertices and set metadata on them. For the seed, use // the vertex ID. IVertex [] aoVertices = TestGraphUtil.AddVertices(m_oGraph, iVertices); for (Int32 i = 0; i < iVertices; i++) { IVertex oVertex = aoVertices[i]; MetadataUtil.SetRandomMetadata( oVertex, true, true, oVertex.ID); } if (bAddEdges) { // Add the edges and set metadata on them. For the seed, use // the edge ID. IEdge [] aoEdges = TestGraphUtil.MakeGraphComplete( m_oGraph, aoVertices, (eDirectedness == GraphDirectedness.Directed) ); foreach (IEdge oEdge in m_oGraph.Edges) { MetadataUtil.SetRandomMetadata( oEdge, true, true, oEdge.ID); } } // Clone the graph. IGraph oNewGraph = m_oGraph.Clone(bCopyMetadataValues, bCopyTag); // Check the metadata on the new graph. MetadataUtil.CheckRandomMetadata( oNewGraph, bCopyMetadataValues, bCopyTag, m_oGraph.ID); // Check the vertices on the new graph. IVertexCollection oNewVertexCollection = oNewGraph.Vertices; Assert.IsNotNull(oNewVertexCollection); Assert.AreEqual(iVertices, oNewVertexCollection.Count); // Loop through the original vertices. foreach (IVertex oVertex in m_oGraph.Vertices) { // Find the corresponding new vertex, by name. IVertex oNewVertex; Assert.IsTrue(oNewVertexCollection.Find( oVertex.Name, out oNewVertex)); Assert.AreNotEqual(oVertex.ID, oNewVertex.ID); // Check the vertex's metadata. Use the original vertex ID as // a seed. MetadataUtil.CheckRandomMetadata(oNewVertex, bCopyMetadataValues, bCopyTag, oVertex.ID); } // Check the edges on the new graph. IEdgeCollection oNewEdgeCollection = oNewGraph.Edges; Assert.IsNotNull(oNewEdgeCollection); Assert.AreEqual( bAddEdges ? TestGraphUtil.GetEdgeCountForCompleteGraph(iVertices) : 0, oNewEdgeCollection.Count ); // Loop through the original edges. foreach (IEdge oEdge in m_oGraph.Edges) { // Find the corresponding new edge, by name. IEdge oNewEdge; Assert.IsTrue(oNewEdgeCollection.Find( oEdge.Name, out oNewEdge)); Assert.AreNotEqual(oEdge.ID, oNewEdge.ID); // Check the edge's metadata. Use the original edge ID as a // seed. MetadataUtil.CheckRandomMetadata(oNewEdge, bCopyMetadataValues, bCopyTag, oEdge.ID); // Verify that the new edge and original edge connect vertices // that correspond. Assert.IsNotNull(oNewEdge.Vertices[0]); Assert.IsNotNull(oNewEdge.Vertices[1]); Assert.AreEqual(oNewEdge.Vertices[0].Name, oEdge.Vertices[0].Name); Assert.AreEqual(oNewEdge.Vertices[1].Name, oEdge.Vertices[1].Name); } // Check the other properties on the new graph. Assert.AreEqual(Name, oNewGraph.Name); Assert.AreEqual(eDirectedness, oNewGraph.Directedness); Assert.AreNotEqual(m_oGraph.ID, oNewGraph.ID); } } } } } }
TestClone ( Boolean bCopyMetadataValues, Boolean bCopyTag, CloneOverload eCloneOverload ) { // Create N objects, set random metadata and Tag on each object, clone // each object, check new object. const Int32 Vertices = 1000; CreateGraph(GraphDirectedness.Directed, Vertices); // Connect the first vertex to each of the other vertices. IEdge [] aoEdges = new Edge[Vertices - 1]; for (Int32 i = 0; i < Vertices - 1; i++) { IEdge oEdge = aoEdges[i] = CreateEdge(m_aoVertices[0], m_aoVertices[i + 1], true); MetadataUtil.SetRandomMetadata(oEdge, true, true, i); oEdge.Name = oEdge.ID.ToString(); } // Create a second graph with 2 vertices for the // CloneOverload.SpecifiedVertices case. IGraph oGraph2 = new Graph(); IVertex [] aoVertices2 = TestGraphUtil.AddVertices(oGraph2, 2); for (Int32 i = 0; i < Vertices - 1; i++) { // Clone the object. IEdge oEdge = aoEdges[i]; IEdge oNewEdge = null; switch (eCloneOverload) { case CloneOverload.SameType: oNewEdge = oEdge.Clone(bCopyMetadataValues, bCopyTag); break; case CloneOverload.SpecifiedVertices: oNewEdge = oEdge.Clone( bCopyMetadataValues, bCopyTag, aoVertices2[0], aoVertices2[1], true); break; default: Debug.Assert(false); break; } // Check the metadata on the new object. MetadataUtil.CheckRandomMetadata( oNewEdge, bCopyMetadataValues, bCopyTag, i); // Check the name and ID on the new object. Assert.AreEqual(oEdge.Name, oNewEdge.Name); Assert.AreNotEqual(oEdge.ID, oNewEdge.ID); // Check the vertices on the new object. Assert.IsNotNull(oNewEdge.Vertices); Assert.AreEqual(2, oNewEdge.Vertices.Length); if (eCloneOverload == CloneOverload.SpecifiedVertices) { Assert.AreEqual(aoVertices2[0], oNewEdge.Vertices[0]); Assert.AreEqual(aoVertices2[1], oNewEdge.Vertices[1]); // Make sure the cloned edge can be added to the second graph. oGraph2.Edges.Add(oNewEdge); } else { Assert.AreEqual(oEdge.Vertices[0], oNewEdge.Vertices[0]); Assert.AreEqual(oEdge.Vertices[1], oNewEdge.Vertices[1]); } } }