Beispiel #1
0
        public void RemoveNode_EmptyGraph_ReturnsFalse()
        {
            Graph graph = new Graph();
            var   node  = new ExampleNode();

            Assert.IsFalse(graph.RemoveNode(node), "RemoveNode returned true when the graph was empty");
        }
Beispiel #2
0
        public void FindDuplicateConnections_MultipleConnections_CorrectCount()
        {
            Graph graph = new Graph();

            var n1 = new ExampleNode();
            var n2 = new ExampleNode();
            var n3 = new ExampleNode();
            var n4 = new ExampleNode();

            var conn1 = new ExampleConnection();
            var conn2 = new ExampleConnection();
            var conn3 = new ExampleConnection();

            graph.Add(n1);
            graph.Add(n2);
            graph.Add(n3);
            graph.Add(n4);

            graph.AddConnection(n1, n2, conn1);
            graph.AddConnection(n1, n3, conn1);
            graph.AddConnection(n1, n4, conn1);

            graph.AddConnection(n2, n3, conn2);
            graph.AddConnection(n3, n2, conn2);

            graph.AddConnection(n3, n4, conn3);

            var dupList = graph.FindDuplicateConnections();

            Assert.AreEqual(2, dupList.Count);
        }
Beispiel #3
0
        public void RemoveNode_NonEmptyGraphNonexistentNode_ReturnsFalse()
        {
            Graph graph = new Graph();
            var   node1 = new ExampleNode();
            var   node2 = new ExampleNode();

            graph.Add(node1);

            Assert.IsFalse(graph.RemoveNode(node2), "RemoveNode returned true when it didn't contain the node");
        }
Beispiel #4
0
        public void RemoveNode_RemoveUnconnectedNode_Success()
        {
            Graph graph = new Graph();
            var   node  = new ExampleNode();

            graph.Add(node);
            Assert.IsTrue(graph.Nodes.Count > 0, "Graph was empty after adding");
            graph.RemoveNode(node);
            Assert.IsTrue(graph.Nodes.Count == 0, "Graph was non-empty after removing a node");
        }
Beispiel #5
0
        public void FindNode_WrongPredicate_ReturnsNull()
        {
            Graph graph = new Graph();
            var   n1    = new ExampleNode();
            var   n2    = new ExampleNode();

            graph.Add(n1);

            Assert.IsNull(graph.FindNode(node => node == n2));
        }
Beispiel #6
0
        public void RemoveNode_NonexistentNode_DoesNothing()
        {
            Graph graph = new Graph();
            var   node1 = new ExampleNode();
            var   node2 = new ExampleNode();

            graph.Add(node1);
            graph.RemoveNode(node2);

            Assert.IsTrue(graph.Nodes.Count > 0, "Graph is empty even though nothing was removed");
        }
Beispiel #7
0
        public void FindNode_Predicate_Success()
        {
            Graph graph = new Graph();
            var   n1    = new ExampleNode();
            var   n2    = new ExampleNode();

            graph.Add(n1);
            graph.Add(n2);

            Assert.AreSame(n1, graph.FindNode(node => node == n1));
        }
Beispiel #8
0
        public void RemoveNode_ReturnsTrue()
        {
            Graph graph = new Graph();
            var   node  = new ExampleNode();

            graph.Add(node);
            Assert.IsTrue(graph.Nodes.Count > 0, "Graph was empty after adding");
            var result = graph.RemoveNode(node);

            Assert.IsTrue(graph.Nodes.Count == 0 && result,
                          "RemoveNode return value was incorrect");
        }
Beispiel #9
0
        public void FindDuplicateConnections_MultipleDistinctDuplicates_ReturnsAll()
        {
            Graph graph = new Graph();

            var n1 = new ExampleNode();
            var n2 = new ExampleNode();
            var n3 = new ExampleNode();
            var n4 = new ExampleNode();

            var conn1 = new ExampleConnection();
            var conn2 = new ExampleConnection();
            var conn3 = new ExampleConnection();

            graph.Add(n1);
            graph.Add(n2);
            graph.Add(n3);
            graph.Add(n4);

            graph.AddConnection(n1, n2, conn1);
            graph.AddConnection(n1, n3, conn1);
            graph.AddConnection(n1, n4, conn1);

            graph.AddConnection(n2, n3, conn2);
            graph.AddConnection(n3, n2, conn2);

            var dupList = graph.FindDuplicateConnections();

            var conn1List = dupList.Find(tuple => ReferenceEquals(tuple.Item1, conn1)).Item2;
            var conn2List = dupList.Find(tuple => ReferenceEquals(tuple.Item1, conn2)).Item2;


            CollectionAssert.AreEquivalent(
                new List <(Node, Node)>()
            {
                (n1, n2),
                (n1, n3),
                (n1, n4),
            },
                conn1List
                );

            CollectionAssert.AreEquivalent(
                new List <(Node, Node)>()
            {
                (n2, n3),
                (n3, n2),
            },
                conn2List
                );
        }
Beispiel #10
0
        public void SanityCheck_Duplicates_ReturnsTrue()
        {
            Graph graph = new Graph();

            var n1 = new ExampleNode();
            var n2 = new ExampleNode();
            var n3 = new ExampleNode();

            var conn = new ExampleConnection();

            graph.Add(n1);
            graph.Add(n2);
            graph.Add(n3);

            graph.AddConnection(n1, n2, conn);
            graph.AddConnection(n1, n3, conn);

            Assert.IsTrue(graph.SanityCheckConnections());
        }
Beispiel #11
0
        public void RemoveNode_RemovesCorrectConnections()
        {
            Graph graph = new Graph();

            var n1 = new ExampleNode();
            var n2 = new ExampleNode();
            var n3 = new ExampleNode();

            graph.Add(n1);
            graph.Add(n2);
            graph.Add(n3);

            graph.AddConnection(n1, n2, new ExampleConnection());
            graph.AddConnection(n2, n1, new ExampleConnection());
            graph.AddConnection(n1, n3, new ExampleConnection());
            graph.AddConnection(n2, n3, new ExampleConnection());

            graph.RemoveNode(n1);
            Assert.AreEqual(new GraphCount(2, 1), graph.Count);
        }
Beispiel #12
0
        public void FindAllNodes_Predicate_Success()
        {
            Graph graph = new Graph();
            var   n1    = new ExampleNode();
            var   n2    = new ExampleNode();
            var   n3    = new ExampleNode();

            graph.Add(n1);
            graph.Add(n2);
            graph.Add(n3);

            CollectionAssert.AreEquivalent(new List <Node>()
            {
                n1, n2
            },
                                           graph.FindAllNodes(node => node == n1 || node == n2));
            CollectionAssert.AreEquivalent(new List <Node>()
            {
                n1, n3
            },
                                           graph.FindAllNodes(node => node == n1 || node == n3));
        }
Beispiel #13
0
        public void FindDuplicateConnections_NoDuplicates_ReturnsEmpty()
        {
            Graph graph = new Graph();

            var nodes = new List <Node>();

            for (int i = 0; i < 10; i++)
            {
                var node = new ExampleNode();
                nodes.Add(node);
                graph.Add(node);
            }

            for (int i = 0; i < nodes.Count - 1; i++)
            {
                graph.AddConnection(nodes[i], nodes[i + 1], new ExampleConnection());
            }

            var emptyList = new List <(Connection, List <(Node, Node)>)>();

            CollectionAssert.AreEquivalent(emptyList,
                                           graph.FindDuplicateConnections());
        }