Esempio n. 1
0
        public void ContainsSelfLoops()
        {
            var net =new BasicAdjList(Guid.NewGuid());
            BasicNode nodeA = net.CreateNode() as BasicNode;
            BasicNode nodeB = net.CreateNode() as BasicNode;
            BasicNode nodeC = net.CreateNode() as BasicNode;
            BasicNode nodeD = net.CreateNode() as BasicNode;

            net.CreateEdge(nodeA, nodeB);
            net.CreateEdge(nodeA, nodeA);
            net.CreateEdge(nodeA, nodeD);
            net.CreateEdge(nodeD, nodeD);
            net.CreateEdge(nodeD, nodeD);

            bool result = nodeA.ContainsSelfLoops();
            Assert.True(result);

            result = nodeB.ContainsSelfLoops();
            Assert.False(result);

            result = nodeC.ContainsSelfLoops();
            Assert.False(result);

            result = nodeD.ContainsSelfLoops();
            Assert.True(result);
        }
Esempio n. 2
0
 public void CreateEdge_Assigns_Edge_Index()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     INode nodeC = net.CreateNode();
     BasicEdge edgeA = net.CreateEdge(nodeA, nodeB) as BasicEdge;
     BasicEdge edgeB = net.CreateEdge(nodeC, nodeB) as BasicEdge;
     Assert.Equal(0, edgeA.Index);
     Assert.Equal(1, edgeB.Index);
 }
        public void AddingEdges_Adds_Row_With_Matching_Key_In_EdgeData(int nodeCount, int edgeCount)
        {
            Random rand = new Random();
            var net = new BasicAdjList(Guid.NewGuid());
            var table = new DataAttributeTable<IEdge>();
            table.Network = net;
            net.EdgeData = table;

            for (int i = 0; i < nodeCount; i++)
            {
                var node = net.CreateNode();
            }

            INode nodeA = null;
            INode nodeB = null;
            IEdge edge = null;
            for (int i = 0; i < edgeCount; i++)
            {
                nodeA = net.Nodes[rand.Next(nodeCount)];
                nodeB = net.Nodes[rand.Next(nodeCount)];
                edge = net.CreateEdge(nodeA, nodeB);
                Assert.Equal((i + 1), net.EdgeCount);
                Assert.Equal((i + 1), table.RowCount);
                Assert.True(table.RowOwnerMap.ContainsKey(edge));
            }
        }
        public void ChangeEventsEnabled_When_False_Prevents_NetworkChanged_Fired_When_RemoveEdge_Is_Called(int nodeCount, int edgeCount, int edgesToRemove)
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            Random rand = new Random();
            for (int i = 0; i < nodeCount; i++)
                net.CreateNode();

            INode nodeA = null;
            INode nodeB = null;

            IEdge[] edges = new IEdge[edgeCount];
            for (int i = 0; i < edgeCount; i++)
            {
                nodeA = net.Nodes[rand.Next(nodeCount)];
                nodeB = net.Nodes[rand.Next(nodeCount)];
                edges[i] = net.CreateEdge(nodeA, nodeB);
            }
            //---------------------

            var helper = new NetworkChangedEventsTestHelper();
            net.NetworkChanged += new NetworkChangedEventHandler<INetwork, NetworkChangedEventArgs>(helper.NetworkChanged_DummyHandler);

            net.ChangeEventsEnabled = false;
            for (int i = 0; i < edges.Length; i++)
            {
                net.RemoveEdge(edges[i]);
            }

            Assert.Equal(0, helper.NetworkChanged_TimesCalled);
        }
Esempio n. 5
0
 public void Contains_ReturnsTrueWhenAnEdgeExists()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     bool result = net.Contains(edge);
     Assert.True(result);
 }
Esempio n. 6
0
        /// <summary>
        /// Parses a particular string format to a network.  Format is
        /// "n,e,d|pair;pair;pair" where n = node count, e = edge count, d = true/false for directedness and
        /// pair is a number pair of node indices indicating an edges.  Ex:  "5,3,false|0,1;1,2;3,5"  *note that the
        /// number of pairs should match the number of edges
        /// </summary>
        public static IBasicAdjList ParseToNetwork(string input, bool enableNodeData, bool enableEdgeData)
        {
            // n,e,directed|pair;
            // 7,23,false|0,3;0,2;0,4;1,4;1,2;1,3;2,5;2,4;2,4;3,5;3,1;3,6;3,0;4,4;4,5;4,3;5,1;5,1;5,4;5,6;5,0;6,3;6,1;
            IBasicAdjList network = null;
            var split1 = input.Split(new[] {'|'});
            var split2 = split1[0].Split(new[] { ',' });

            int n = int.Parse(split2[0]);
            int m = int.Parse(split2[1]);
            bool directed = bool.Parse(split2[2]);

            var split3 = split1[1].Split(new[] { ';' }); // pairs

            if(split3.Length != m)
                throw new ArgumentException(string.Format("The number of pairs {0} did not match the number of edges {1} in {2}", split3.Length, m, input));
            INode a = null;
            INode b = null;

            using (var fac = new BasicAdjListFactory())
            {
                network = new BasicAdjList(Guid.NewGuid(), directed);
                for(int i=0; i<n; i++)
                    network.CreateNode();
            }

            string[] pair = null;
            int indexA = -1;
            int indexB = -1;
            for (int i = 0; i < split3.Length; i++ )
            {
                pair = split3[i].Split(new[] { ',' });
                indexA = int.Parse(pair[0]);
                indexB = int.Parse(pair[1]);
                a = network.Nodes[indexA];
                b = network.Nodes[indexB];
                network.CreateEdge(a, b);
            }

            using (var fac = new DataAttributeTableFactory(network))
            {
                if (enableNodeData)
                    fac.CreateNodeDataTable();
                if (enableEdgeData)
                    fac.CreateEdgeDataTable();
            }
            return network;
        }
Esempio n. 7
0
        public void CreateEdge_Calls_Add_Edge_For_Source_And_Targ_Nodes()
        {
            //Arrange
            var mockNodeA = new Mock<IBasicNode>();
            var mockNodeB = new Mock<IBasicNode>();

            var net = new BasicAdjList(Guid.NewGuid());

            mockNodeA.Setup(foo => foo.Network).Returns(net);
            mockNodeB.Setup(foo => foo.Network).Returns(net);

            mockNodeA.Setup(foo => foo.Index).Returns(0);
            mockNodeB.Setup(foo => foo.Index).Returns(1);

            net.AddNewNode(mockNodeA.Object);
            net.AddNewNode(mockNodeB.Object);

            var edge = net.CreateEdge(mockNodeA.Object, mockNodeB.Object);
            mockNodeA.Verify(foo => foo.AddEdge(edge), Times.AtLeastOnce());
            mockNodeB.Verify(foo => foo.AddEdge(edge), Times.AtLeastOnce());
        }
Esempio n. 8
0
 public void IsEdge_ReturnsTrueIfEdgeExistsBetweenTwoNodes()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     bool result = net.IsEdge(nodeA, nodeB);
     Assert.True(result);
 }
Esempio n. 9
0
 public void IsEdge_ReturnsTrueFor_Self_Looped_Edges()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeB, nodeB);
     bool result = net.IsEdge(nodeB, nodeB);
     Assert.True(result);
 }
Esempio n. 10
0
 public void IsEdge_ReturnsFalseIf_Nodes_AreReversedButEdgeExists()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     bool result = net.IsEdge(nodeB, nodeA);
     Assert.False(result);
 }
Esempio n. 11
0
 public void IsEdge_ReturnsFalseIfEdgeDNE()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     INode nodeC = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     bool result = net.IsEdge(nodeC, nodeA);
     Assert.False(result);
 }
Esempio n. 12
0
        public void EdgeCount(int nodeCount, int edgeCount)
        {
            var net = new BasicAdjList(Guid.NewGuid());
            INode a = null, b = null;
            a = net.CreateNode();
            b = net.CreateNode();

            for (int i = 0; i < edgeCount; i++)
                net.CreateEdge(a, b);

            Assert.Equal(net.EdgeCount, edgeCount);
        }
Esempio n. 13
0
 public void GetEdge_With_Ignore_Direction_Param_Returns_Null_If_EdgeDNE(bool ignoreDirection)
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     INode nodeC = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     IEdge result = net.GetEdge(nodeC, nodeA, ignoreDirection);
     Assert.Null(result);
 }
        public void ItemGetter_Returns_Cell_Val_By_RowIndex_And_ColIndex()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());

            var table = new DataAttributeTable<IEdge>();
            table.Network = net;

            table.Columns.Add(new DataColumn("X", typeof(string)));

            var nodeA = net.CreateNode();
            var nodeB = net.CreateNode();
            var nodeC = net.CreateNode();

            var edgeA = net.CreateEdge(nodeB, nodeC);
            var edgeB = net.CreateEdge(nodeA, nodeC);

            table.AddRow(edgeA);
            table.AddRow(edgeB);

            table.SetValue<string>(edgeA, 0, "valA");
            table.SetValue<string>(edgeB, 0, "valB");

            //Act
            string val0 = (string)table[0, 0];
            string val1 = (string)table[1, 0];
            // verify primary keys

            //Assert
            Assert.Equal(val0, "valA");
            Assert.Equal(val1, "valB");
        }
        public void SetValue_Of_T_Assigns_Object_To_Cell_Val_By_Owner_And_ColIndex()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());

            var table = new DataAttributeTable<IEdge>();
            table.Network = net;

            table.Columns.Add(new DataColumn("X", typeof (string)));

            var nodeA = net.CreateNode();
            var nodeB = net.CreateNode();
            var nodeC = net.CreateNode();

            var edgeA = net.CreateEdge(nodeB, nodeC);
            var edgeB = net.CreateEdge(nodeA, nodeC);

            table.AddRow(edgeA);
            table.AddRow(edgeB);

            //Act
            table.SetValue<string>(edgeA, 0, "valA");
            table.SetValue<string>(edgeB, 0, "valB");

            string val0 = (string)table.Rows[0][0];
            string val1 = (string)table.Rows[1][0];
            // verify primary keys

            //Assert
            Assert.Equal(val0, "valA");
            Assert.Equal(val1, "valB");
        }
Esempio n. 16
0
        public void ReverseEdge_DoesNotAffectNodeAccessorAndPredecessorLists()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            Type expectedType = typeof(BasicEdge);
            INode nodeA = net.CreateNode();
            INode nodeB = net.CreateNode();
            IEdge edge = net.CreateEdge(nodeA, nodeB);

            bool result = nodeA.SuccessorList.Contains(nodeB);
            Assert.True(result);
            result = nodeA.PredecessorList.Contains(nodeB);
            Assert.False(result);

            result = nodeB.PredecessorList.Contains(nodeA);
            Assert.True(result);
            result = nodeB.SuccessorList.Contains(nodeA);
            Assert.False(result);

            net.ReverseEdge(edge);

            result = nodeA.SuccessorList.Contains(nodeB);
            Assert.False(result);
            result = nodeA.PredecessorList.Contains(nodeB);
            Assert.True(result);

            result = nodeB.PredecessorList.Contains(nodeA);
            Assert.False(result);
            result = nodeB.SuccessorList.Contains(nodeA);
            Assert.True(result);
        }
Esempio n. 17
0
        public static IBasicAdjList GenerateAdjListWithDataAttribs(List<Point> edgeList, bool directed, bool enableNodeDataAttribs, bool enableEdgeDataAttribs)
        {
            var q = ((from p in edgeList
                      select p.X)
                .Union((from p in edgeList
                        select p.Y))).Distinct();

            var nodeIndices = q.ToList();
            nodeIndices.Sort();

            var net = new BasicAdjList(Guid.NewGuid(), directed);
            for (int i = 0; i < nodeIndices.Count; i++)
                net.CreateNode();

            INode srcNode = null;
            INode targNode = null;
            for (int i = 0; i < edgeList.Count; i++)
            {
                srcNode = net.Nodes[edgeList[i].X];
                targNode = net.Nodes[edgeList[i].Y];
                net.CreateEdge(srcNode, targNode);
            }

            using (var fac = new DataAttributeTableFactory(net))
            {
                if (enableNodeDataAttribs)
                    fac.CreateNodeDataTable();
                if (enableEdgeDataAttribs)
                    fac.CreateEdgeDataTable();
            }

            return net;
        }
Esempio n. 18
0
        public void GetEdgeCount_For_NodeB_to_NodeB(int AtoBEdges, int BtoAEdges, int AtoAEdges, int BtoBEdges, bool ignoreDirection, int expectedCount)
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            INode nodeA = net.CreateNode();
            INode nodeB = net.CreateNode();

            for (int i = 0; i < AtoBEdges; i++)
            {
                net.CreateEdge(nodeA, nodeB);
            }
            for (int i = 0; i < BtoAEdges; i++)
            {
                net.CreateEdge(nodeB, nodeA);
            }
            for (int i = 0; i < AtoAEdges; i++)
            {
                net.CreateEdge(nodeA, nodeA);
            }
            for (int i = 0; i < BtoBEdges; i++)
            {
                net.CreateEdge(nodeB, nodeB);
            }

            int result = net.GetEdgeCount(nodeB, nodeB, ignoreDirection);

            Assert.Equal(expectedCount, result);
        }
Esempio n. 19
0
 public void Edges_Count_Returns_The_Number_Of_Edges(int nodeCount)
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     INode nodeC = net.CreateNode();
     Assert.Equal(0, net.Edges.Count);
     IEdge edgeA = net.CreateEdge(nodeA, nodeB);
     Assert.Equal(1, net.Edges.Count);
     IEdge edgeB = net.CreateEdge(nodeC, nodeB);
     Assert.Equal(2, net.Edges.Count);
 }
Esempio n. 20
0
        public void ReverseEdge_Throws_Ex_If_Edge_DNE_In_Network()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            INode nodeA = net.CreateNode();
            INode nodeB = net.CreateNode();
            IEdge edge0 = net.CreateEdge(nodeA, nodeB);

            var net2 = new BasicAdjList(Guid.NewGuid());
            INode node0 = net2.CreateNode();
            INode node1 = net2.CreateNode();
            IEdge edge1 = net2.CreateEdge(node0, node1);

            var ex = Assert.Throws<ArgumentException>(() => net.ReverseEdge(edge1));
        }
Esempio n. 21
0
 public void CreateEdge_Increments_Edge_Count_By_1_For_Self_Looped_Edge()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     Assert.Equal(0, net.EdgeCount);
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeB, nodeB);
     Assert.Equal(1, net.EdgeCount);
 }
Esempio n. 22
0
        public void ReverseEdge()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            Type expectedType = typeof(BasicEdge);
            INode nodeA = net.CreateNode();
            INode nodeB = net.CreateNode();
            IEdge edge = net.CreateEdge(nodeA, nodeB);

            Assert.Same(nodeA, edge.SourceNode);
            Assert.Same(nodeB, edge.DestinationNode);

            net.ReverseEdge(edge);

            Assert.NotNull(edge.SourceNode);
            Assert.NotNull(edge.DestinationNode);

            Assert.Same(nodeB, edge.SourceNode);
            Assert.Same(nodeA, edge.DestinationNode);
        }
Esempio n. 23
0
 public void GetEdge_Returns_Null_If_Edge_DNE()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     INode nodeC = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     IEdge result = net.GetEdge(nodeC, nodeA);
     Assert.Null(result);
 }
Esempio n. 24
0
        public void ReverseEdge_WithSelfLoop()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            Type expectedType = typeof(BasicEdge);
            INode nodeA = net.CreateNode();
            IEdge edge = net.CreateEdge(nodeA, nodeA);

            bool result = nodeA.SuccessorList.Contains(nodeA);
            Assert.True(result);
            result = nodeA.PredecessorList.Contains(nodeA);
            Assert.True(result);

            Assert.Equal(1, nodeA.SuccessorList.Count);
            Assert.Equal(1, nodeA.PredecessorList.Count);

            net.ReverseEdge(edge);

            result = nodeA.SuccessorList.Contains(nodeA);
            Assert.True(result);
            result = nodeA.PredecessorList.Contains(nodeA);
            Assert.True(result);

            Assert.Equal(1, nodeA.SuccessorList.Count);
            Assert.Equal(1, nodeA.PredecessorList.Count);
        }
        public void AddRow_Appends_New_Row_And_Updates_Map()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            var table = new DataAttributeTable<IEdge>();
            table.Network = net;

            var nodeA = net.CreateNode();
            var nodeB = net.CreateNode();
            var edge = net.CreateEdge(nodeA, nodeB);

            //Act
            table.AddRow(edge);

            //Assert
            Assert.Equal(table.Rows.Count, 1);

            Assert.Equal(table.RowOwnerMap.Count, 1);
            Assert.True(table.RowOwnerMap.ContainsKey(edge));
        }
Esempio n. 26
0
 public void GetEdge_Returns_Null_If_Nodes_Are_Reversed_But_Edge_Exists()
 {
     //Arrange
     var net = new BasicAdjList(Guid.NewGuid());
     INode nodeA = net.CreateNode();
     INode nodeB = net.CreateNode();
     IEdge edge = net.CreateEdge(nodeA, nodeB);
     IEdge result = net.GetEdge(nodeB, nodeA);
     Assert.Null(result);
 }
        public void RemoveRow_Deletes_Row_For_Specified_Node_And_Updates_Map()
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());

            var nodeA = net.CreateNode();
            var nodeB = net.CreateNode();
            var nodeC = net.CreateNode();

            var edgeA = net.CreateEdge(nodeB, nodeC);
            var edgeB = net.CreateEdge(nodeA, nodeC);

            var table = new DataAttributeTable<IEdge>();
            table.Network = net;

            table.AddRow(edgeA);
            table.AddRow(edgeB);

            Assert.Equal(table.Rows.Count, 2);
            Assert.Equal(table.RowOwnerMap.Count, 2);

            //Act
            table.RemoveRow(edgeB);

            //Assert
            Assert.Equal(table.Rows.Count, 1);
            Assert.Equal(table.RowOwnerMap.Count, 1);

            Assert.False(table.RowOwnerMap.ContainsKey(edgeB));
        }
Esempio n. 28
0
        public void GetEdge_With_Ignore_Direction_Param_If_Nodes_AreReversed(bool ignoreDirection, bool nullExpected)
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            INode nodeA = net.CreateNode();
            INode nodeB = net.CreateNode();
            IEdge edge = net.CreateEdge(nodeA, nodeB);
            IEdge result = net.GetEdge(nodeB, nodeA, ignoreDirection);

            if (nullExpected)
            {
                Assert.Null(result);
            }
            else
            {
                Assert.NotNull(result);
                Assert.Same(result, edge);
            }
        }
        public void Copy_For_Nodes_For_Empty_Network(int numNodes, int numEdges, bool isDirected, bool selfLoops, bool multiEdges, int[] intVals, double[] doubleVals, bool[] boolVals, string[] stringVals)
        {
            //Arrange
            var copier = new DataAttributeTableCopier();

            #region Create src and target networks

            IBasicAdjList src = BasicAdjListGenerator.GenerateAdjListWithDataAttribs(numNodes, numEdges, isDirected, true, false);
            var builder = new DataAttributesBuilder();
            var colIndex = builder.AddColumn((INodeAttributes)src, typeof(int));
            builder.PopulateCol<int, INode>(src.NodeData, colIndex, intVals);
            colIndex = builder.AddColumn((INodeAttributes)src, typeof(double));
            builder.PopulateCol<double, INode>(src.NodeData, colIndex, doubleVals);
            colIndex = builder.AddColumn((INodeAttributes)src, typeof(bool));
            builder.PopulateCol<bool, INode>(src.NodeData, colIndex, boolVals);
            colIndex = builder.AddColumn((INodeAttributes)src, typeof(string));
            builder.PopulateCol<string, INode>(src.NodeData, colIndex, stringVals);

            // create a copy of src network and enable node data attribs
            IBasicAdjList targ = new BasicAdjList(Guid.NewGuid(), isDirected);
            foreach (var srcNode in src.Nodes)
            {
                targ.CreateNode();
            }
            INode a;
            INode b;
            foreach (var srcEdge in src.Edges)
            {
                a = targ.Nodes[srcEdge.SourceNode.Index];
                b = targ.Nodes[srcEdge.DestinationNode.Index];
                targ.CreateEdge(a, b);
            }
            using (var fac = new DataAttributeTableFactory(targ))
            {
                fac.CreateNodeDataTable();
            }

            #endregion

            //Act
            copier.Copy(src.NodeData, targ.NodeData);

            //Assert
            Assert.NotNull(targ.NodeData);
            Assert.Equal(src.NodeData.ColumnCount, targ.NodeData.ColumnCount);
            Assert.Equal(numNodes, targ.NodeData.RowCount);

            for (int i = 0; i < targ.NodeDataAttributeCount; i++)
            {
                Assert.Equal(src.NodeData.DataTypes[i], targ.NodeData.DataTypes[i]);
                Assert.Equal(src.NodeData.ColumnNames[i], targ.NodeData.ColumnNames[i]);
                Assert.Equal(src.NodeData.ColumnDefaultValues[i], targ.NodeData.ColumnDefaultValues[i]);
            }
        }
Esempio n. 30
0
        public void GetEdge_With_Ignore_Direction_Param_ReturnsEdge(bool ignoreDirection)
        {
            //Arrange
            var net = new BasicAdjList(Guid.NewGuid());
            INode nodeA = net.CreateNode();
            INode nodeB = net.CreateNode();
            IEdge edge0 = net.CreateEdge(nodeA, nodeB);

            IEdge result = net.GetEdge(nodeA, nodeB, ignoreDirection);
            Assert.NotNull(result);
            Assert.Same(result, edge0);
            Assert.Same(nodeA, edge0.SourceNode);
            Assert.Same(nodeB, edge0.DestinationNode);
        }