CheckRandomMetadataOnRandomGraph() { foreach (IVertex oVertex in m_oGraph.Vertices) { if (oVertex.Name == MetadataMarker) { MetadataUtil.CheckRandomMetadata( oVertex, true, true, oVertex.ID); } else { Assert.IsNull(oVertex.Tag); } } foreach (IEdge oEdge in m_oGraph.Edges) { if (oEdge.Name == MetadataMarker) { MetadataUtil.CheckRandomMetadata( oEdge, true, true, oEdge.ID); } else { Assert.IsNull(oEdge.Tag); } } MetadataUtil.CheckRandomMetadata( m_oGraph, true, true, m_oGraph.ID); }
TestSetGetValue6() { // Add random keys and tags for each of N MockMetadataProvider objects. // Create an array of MockMetadataProvider objects and set random keys // and tags on them. const Int32 MockMetadataProviderObjects = 10000; MockMetadataProvider [] aoMockMetadataProvider = new MockMetadataProvider[MockMetadataProviderObjects]; for (Int32 i = 0; i < MockMetadataProviderObjects; i++) { MockMetadataProvider oMockMetadataProvider = aoMockMetadataProvider[i] = new MockMetadataProvider(); MetadataUtil.SetRandomMetadata( oMockMetadataProvider, true, true, i); } // Check the values, backwards. for (Int32 i = MockMetadataProviderObjects - 1; i >= 0; i--) { MetadataUtil.CheckRandomMetadata( aoMockMetadataProvider[i], true, true, i); } }
TestCopyTo8() { // Create N objects, set random metadata on each object, copy each // object's metadata to a new object, check metadata on new object. const Int32 Objects = 1976; MockMetadataProvider [] aoMockMetadataProvider = new MockMetadataProvider[Objects]; // Set random values on each object. for (Int32 i = 0; i < Objects; i++) { MockMetadataProvider oMockMetadataProvider = aoMockMetadataProvider[i] = new MockMetadataProvider(); MetadataUtil.SetRandomMetadata( oMockMetadataProvider, true, true, i); } for (Int32 i = 0; i < Objects; i++) { // Copy the object's metadata to a new object. MockMetadataProvider oMockMetadataProvider = aoMockMetadataProvider[i]; MockMetadataProvider oCopy = new MockMetadataProvider(); oMockMetadataProvider.CopyTo(oCopy, true, true); // Check the metadata on the new object. MetadataUtil.CheckRandomMetadata(oCopy, true, true, i); } }
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]); } } }