Utility methods for testing graph components.
All methods are static.
        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;
            }
        }
Ejemplo n.º 2
0
        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(

                    "Smrf.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;
            }
        }
Ejemplo n.º 3
0
        CreateGraph
        (
            GraphDirectedness eDirectedness,
            Int32 iVertices
        )
        {
            Debug.Assert(iVertices >= 0);

            m_oGraph = new Graph(eDirectedness);

            m_aoVertices = TestGraphUtil.AddVertices(m_oGraph, iVertices);
        }
Ejemplo n.º 4
0
        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++;
            }
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
        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));
            }
        }
Ejemplo n.º 7
0
        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));
            }
        }
Ejemplo n.º 8
0
        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++;
            }
        }
Ejemplo n.º 10
0
        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++;
            }
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        TestOnLayoutBegin()
        {
            // Draw rectangles, hide intergroup edges.

            const Double GroupRectanglePenWidth = 1.23;

            const IntergroupEdgeStyle IntergroupEdgeStyle =
                IntergroupEdgeStyle.Hide;

            IGraph oGraph = new Graph(GraphDirectedness.Undirected);

            IVertex[] aoVertices = TestGraphUtil.AddVertices(oGraph, 4);

            IEdgeCollection oEdges = oGraph.Edges;
            IEdge           oEdge1 = oEdges.Add(aoVertices[0], aoVertices[1], false);
            IEdge           oEdge2 = oEdges.Add(aoVertices[0], aoVertices[2], false);
            IEdge           oEdge3 = oEdges.Add(aoVertices[2], aoVertices[3], false);
            IEdge           oEdge4 = oEdges.Add(aoVertices[1], aoVertices[3], false);

            // oEdge2 is intergroup.  Give it an initial visibility.

            oEdge2.SetValue(ReservedMetadataKeys.Visibility,
                            VisibilityKeyValue.Filtered);

            // (oEdge4 is intergroup.  Don't give it an initial visibility.)

            GroupInfo oGroup1 = new GroupInfo();

            oGroup1.Vertices.AddLast(aoVertices[0]);
            oGroup1.Vertices.AddLast(aoVertices[1]);
            oGroup1.Rectangle = Rectangle.FromLTRB(1, 2, 3, 4);

            GroupInfo oGroup2 = new GroupInfo();

            oGroup2.Vertices.AddLast(aoVertices[2]);
            oGroup2.Vertices.AddLast(aoVertices[3]);
            oGroup2.Rectangle = Rectangle.FromLTRB(5, 6, 7, 8);

            GroupMetadataManager.OnLayoutBegin(oGraph);

            GroupMetadataManager.OnLayoutUsingGroupsEnd(oGraph,
                                                        new GroupInfo[] { oGroup1, oGroup2 },
                                                        GroupRectanglePenWidth, IntergroupEdgeStyle);

            GroupLayoutDrawingInfo oGroupLayoutDrawingInfo;

            Assert.IsTrue(GroupMetadataManager.TryGetGroupLayoutDrawingInfo(
                              oGraph, out oGroupLayoutDrawingInfo));

            Assert.AreEqual(GroupRectanglePenWidth,
                            oGroupLayoutDrawingInfo.PenWidth);

            IList <GroupInfo> oGroupInfo = oGroupLayoutDrawingInfo.GroupsToDraw;

            Assert.AreEqual(2, oGroupInfo.Count);
            Assert.AreEqual(oGroup1, oGroupInfo[0]);
            Assert.AreEqual(oGroup2, oGroupInfo[1]);

            Assert.IsTrue(oGraph.ContainsKey(
                              ReservedMetadataKeys.IntergroupEdgesHidden));

            Assert.IsFalse(oEdge1.ContainsKey(
                               ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge2.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.AreEqual(VisibilityKeyValue.Filtered,
                            oEdge2.GetValue(ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge2.ContainsKey(
                              ReservedMetadataKeys.Visibility));

            Assert.AreEqual(VisibilityKeyValue.Hidden,
                            oEdge2.GetValue(ReservedMetadataKeys.Visibility));

            Assert.IsFalse(oEdge3.ContainsKey(
                               ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge4.ContainsKey(
                              ReservedMetadataKeys.Visibility));

            Assert.AreEqual(VisibilityKeyValue.Hidden,
                            oEdge4.GetValue(ReservedMetadataKeys.Visibility));

            // Verify that OnLayoutBegin() reverses the metadata changes.

            GroupMetadataManager.OnLayoutBegin(oGraph);

            VerifyOriginalMetadata(oGraph, oEdge1, oEdge2, oEdge3, oEdge4);
        }
Ejemplo n.º 13
0
        TestOnLayoutBegin6()
        {
            // Request that intergroup edges be combined, and there are some.

            const Double GroupRectanglePenWidth = 4.567;

            const IntergroupEdgeStyle IntergroupEdgeStyle =
                IntergroupEdgeStyle.Combine;

            IGraph oGraph = new Graph(GraphDirectedness.Undirected);

            IVertex[] aoVertices = TestGraphUtil.AddVertices(oGraph, 5);

            IEdgeCollection oEdges = oGraph.Edges;

            // Not intergroup.

            IEdge oEdge1 = oEdges.Add(aoVertices[0], aoVertices[1], false);
            IEdge oEdge2 = oEdges.Add(aoVertices[2], aoVertices[3], false);

            // Intergroup, between groups 1 and 2.

            IEdge oEdge3 = oEdges.Add(aoVertices[0], aoVertices[2], false);
            IEdge oEdge4 = oEdges.Add(aoVertices[1], aoVertices[2], false);
            IEdge oEdge5 = oEdges.Add(aoVertices[1], aoVertices[3], false);

            // Intergroup, between groups 2 and 3.

            IEdge oEdge6 = oEdges.Add(aoVertices[2], aoVertices[4], false);
            IEdge oEdge7 = oEdges.Add(aoVertices[3], aoVertices[4], false);

            // Intergroup, between groups 1 and 3.

            IEdge oEdge8 = oEdges.Add(aoVertices[1], aoVertices[4], false);

            GroupInfo oGroup1 = new GroupInfo();

            oGroup1.Vertices.AddLast(aoVertices[0]);
            oGroup1.Vertices.AddLast(aoVertices[1]);
            oGroup1.Rectangle = Rectangle.FromLTRB(1, 2, 3, 4);

            GroupInfo oGroup2 = new GroupInfo();

            oGroup2.Vertices.AddLast(aoVertices[2]);
            oGroup2.Vertices.AddLast(aoVertices[3]);
            oGroup2.Rectangle = Rectangle.FromLTRB(5, 6, 7, 8);

            GroupInfo oGroup3 = new GroupInfo();

            oGroup3.Vertices.AddLast(aoVertices[4]);
            oGroup3.Rectangle = Rectangle.FromLTRB(9, 10, 11, 12);

            GroupMetadataManager.OnLayoutBegin(oGraph);

            GroupMetadataManager.OnLayoutUsingGroupsEnd(oGraph,
                                                        new GroupInfo[] { oGroup1, oGroup2, oGroup3 },
                                                        GroupRectanglePenWidth, IntergroupEdgeStyle);

            GroupLayoutDrawingInfo oGroupLayoutDrawingInfo;

            Assert.IsTrue(GroupMetadataManager.TryGetGroupLayoutDrawingInfo(
                              oGraph, out oGroupLayoutDrawingInfo));

            Assert.AreEqual(GroupRectanglePenWidth,
                            oGroupLayoutDrawingInfo.PenWidth);

            IList <GroupInfo> oGroupInfo = oGroupLayoutDrawingInfo.GroupsToDraw;

            Assert.AreEqual(3, oGroupInfo.Count);
            Assert.AreEqual(oGroup1, oGroupInfo[0]);
            Assert.AreEqual(oGroup2, oGroupInfo[1]);
            Assert.AreEqual(oGroup3, oGroupInfo[2]);

            Assert.IsTrue(oGraph.ContainsKey(
                              ReservedMetadataKeys.IntergroupEdgesHidden));

            Assert.IsFalse(oEdge1.ContainsKey(
                               ReservedMetadataKeys.SavedVisibility));

            Assert.IsFalse(oEdge2.ContainsKey(
                               ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge3.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge4.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge5.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge6.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge7.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.IsTrue(oEdge8.ContainsKey(
                              ReservedMetadataKeys.SavedVisibility));

            Assert.AreEqual(3,
                            oGroupLayoutDrawingInfo.CombinedIntergroupEdges.Count());

            IntergroupEdgeInfo oCombinedIntergroupEdge;

            oCombinedIntergroupEdge =
                oGroupLayoutDrawingInfo.CombinedIntergroupEdges.Single(
                    intergroupEdgeInfo =>
                    intergroupEdgeInfo.Group1Index == 0
                    &&
                    intergroupEdgeInfo.Group2Index == 1
                    );

            Assert.AreEqual(0, oCombinedIntergroupEdge.Group1Index);
            Assert.AreEqual(1, oCombinedIntergroupEdge.Group2Index);
            Assert.AreEqual(3, oCombinedIntergroupEdge.Edges);

            oCombinedIntergroupEdge =
                oGroupLayoutDrawingInfo.CombinedIntergroupEdges.Single(
                    intergroupEdgeInfo =>
                    intergroupEdgeInfo.Group1Index == 0
                    &&
                    intergroupEdgeInfo.Group2Index == 2
                    );

            Assert.AreEqual(0, oCombinedIntergroupEdge.Group1Index);
            Assert.AreEqual(2, oCombinedIntergroupEdge.Group2Index);
            Assert.AreEqual(1, oCombinedIntergroupEdge.Edges);

            oCombinedIntergroupEdge =
                oGroupLayoutDrawingInfo.CombinedIntergroupEdges.Single(
                    intergroupEdgeInfo =>
                    intergroupEdgeInfo.Group1Index == 1
                    &&
                    intergroupEdgeInfo.Group2Index == 2
                    );

            Assert.AreEqual(1, oCombinedIntergroupEdge.Group1Index);
            Assert.AreEqual(2, oCombinedIntergroupEdge.Group2Index);
            Assert.AreEqual(2, oCombinedIntergroupEdge.Edges);
        }
Ejemplo n.º 14
0
        TestOnLayoutBegin5()
        {
            // Request that intergroup edges be combined, although there are none.

            const Double GroupRectanglePenWidth = 4.567;

            const IntergroupEdgeStyle IntergroupEdgeStyle =
                IntergroupEdgeStyle.Combine;

            IGraph oGraph = new Graph(GraphDirectedness.Undirected);

            IVertex[] aoVertices = TestGraphUtil.AddVertices(oGraph, 5);

            IEdgeCollection oEdges = oGraph.Edges;
            IEdge           oEdge1 = oEdges.Add(aoVertices[0], aoVertices[1], false);
            IEdge           oEdge2 = oEdges.Add(aoVertices[2], aoVertices[3], false);

            GroupInfo oGroup1 = new GroupInfo();

            oGroup1.Vertices.AddLast(aoVertices[0]);
            oGroup1.Vertices.AddLast(aoVertices[1]);
            oGroup1.Rectangle = Rectangle.FromLTRB(1, 2, 3, 4);

            GroupInfo oGroup2 = new GroupInfo();

            oGroup2.Vertices.AddLast(aoVertices[2]);
            oGroup2.Vertices.AddLast(aoVertices[3]);
            oGroup2.Rectangle = Rectangle.FromLTRB(5, 6, 7, 8);

            GroupInfo oGroup3 = new GroupInfo();

            oGroup3.Vertices.AddLast(aoVertices[4]);
            oGroup3.Rectangle = Rectangle.FromLTRB(9, 10, 11, 12);

            GroupMetadataManager.OnLayoutBegin(oGraph);

            GroupMetadataManager.OnLayoutUsingGroupsEnd(oGraph,
                                                        new GroupInfo[] { oGroup1, oGroup2, oGroup3 },
                                                        GroupRectanglePenWidth, IntergroupEdgeStyle);

            GroupLayoutDrawingInfo oGroupLayoutDrawingInfo;

            Assert.IsTrue(GroupMetadataManager.TryGetGroupLayoutDrawingInfo(
                              oGraph, out oGroupLayoutDrawingInfo));

            Assert.AreEqual(GroupRectanglePenWidth,
                            oGroupLayoutDrawingInfo.PenWidth);

            IList <GroupInfo> oGroupInfo = oGroupLayoutDrawingInfo.GroupsToDraw;

            Assert.AreEqual(3, oGroupInfo.Count);
            Assert.AreEqual(oGroup1, oGroupInfo[0]);
            Assert.AreEqual(oGroup2, oGroupInfo[1]);
            Assert.AreEqual(oGroup3, oGroupInfo[2]);

            Assert.IsFalse(oGraph.ContainsKey(
                               ReservedMetadataKeys.IntergroupEdgesHidden));

            Assert.IsFalse(oEdge1.ContainsKey(
                               ReservedMetadataKeys.SavedVisibility));

            Assert.IsFalse(oEdge2.ContainsKey(
                               ReservedMetadataKeys.SavedVisibility));

            Assert.AreEqual(0,
                            oGroupLayoutDrawingInfo.CombinedIntergroupEdges.Count());
        }
Ejemplo n.º 15
0
        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);
                }
            }
        }
Ejemplo n.º 16
0
        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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 17
0
        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]);
                }
            }
        }