コード例 #1
0
        public void SortThrowsTopologicalSortExceptionExceptionWhenGraphContainsLoops()
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            graph.AddSequence(new[] { 61, 11 });
            graph.Sort().ToArray();
        }
コード例 #2
0
        public void GenerateGraphGeneratesTriangleGraphStructure1_BackSequencesTest()
        {
            var target = GraphGenerator.GenerateGraph(3, 2, 4);

            GraphTestHelper.AssertConsistsOfBackSequences(target, new[]
            {
                new[] { 8, 0 },
                new[] { 8, 1 },
                new[] { 8, 2 },
                new[] { 8, 3 },
                new[] { 9, 0 },
                new[] { 9, 1 },
                new[] { 9, 2 },
                new[] { 9, 3 },
                new[] { 10, 4 },
                new[] { 10, 5 },
                new[] { 10, 6 },
                new[] { 10, 7 },
                new[] { 11, 4 },
                new[] { 11, 5 },
                new[] { 11, 6 },
                new[] { 11, 7 },
                new[] { 12, 8 },
                new[] { 12, 9 },
                new[] { 12, 10 },
                new[] { 12, 11 },
                new[] { 13, 8 },
                new[] { 13, 9 },
                new[] { 13, 10 },
                new[] { 13, 11 },
            });
        }
コード例 #3
0
        public void GenerateGraphGeneratesTriangleGraphStructure2()
        {
            var target = GraphGenerator.GenerateGraph(3, 2, 4);

            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 0, 8 },
                new[] { 1, 8 },
                new[] { 2, 8 },
                new[] { 3, 8 },
                new[] { 0, 9 },
                new[] { 1, 9 },
                new[] { 2, 9 },
                new[] { 3, 9 },
                new[] { 4, 10 },
                new[] { 5, 10 },
                new[] { 6, 10 },
                new[] { 7, 10 },
                new[] { 4, 11 },
                new[] { 5, 11 },
                new[] { 6, 11 },
                new[] { 7, 11 },
                new[] { 8, 12 },
                new[] { 9, 12 },
                new[] { 10, 12 },
                new[] { 11, 12 },
                new[] { 8, 13 },
                new[] { 9, 13 },
                new[] { 10, 13 },
                new[] { 11, 13 },
            });
        }
コード例 #4
0
        public void GenerateGraphGeneratesBackTriangleGraphStructure2()
        {
            var target = GraphGenerator.GenerateGraph(3, 4, 2);

            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 0, 2 },
                new[] { 0, 3 },
                new[] { 0, 4 },
                new[] { 0, 5 },
                new[] { 1, 2 },
                new[] { 1, 3 },
                new[] { 1, 4 },
                new[] { 1, 5 },
                new[] { 2, 6 },
                new[] { 2, 7 },
                new[] { 2, 8 },
                new[] { 2, 9 },
                new[] { 3, 6 },
                new[] { 3, 7 },
                new[] { 3, 8 },
                new[] { 3, 9 },
                new[] { 4, 10 },
                new[] { 4, 11 },
                new[] { 4, 12 },
                new[] { 4, 13 },
                new[] { 5, 10 },
                new[] { 5, 11 },
                new[] { 5, 12 },
                new[] { 5, 13 },
            });
        }
コード例 #5
0
        public void GetNodesReturnsNodesOnTheLevel(int level, int[] expectedNodes)
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            GraphTestHelper.AssertCollectionsConsistsOfNodes(expectedNodes,
                                                             graph.GetNodes(level));
            GraphTestHelper.AssertNodesAreOrderedByLevel(graph.GetNodes(level));
        }
コード例 #6
0
        public void GetNodesWithBetweenReturnsNodesOnTheLevelsInTheRange(int levelFrom, int levelTo, int[] expectedNodes)
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            GraphTestHelper.AssertCollectionsConsistsOfNodes(expectedNodes,
                                                             graph.GetNodesBetween(levelFrom, levelTo));
            GraphTestHelper.AssertNodesAreOrderedByLevel(graph.GetNodesBetween(levelFrom, levelTo));
        }
コード例 #7
0
        public void SortReturnsNodesInTopologicalOrder()
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);
            var nodes = graph.Sort();

            Assert.AreEqual(20, nodes.Count());
            AssertNodesAreInTopologicalOrder(nodes);
        }
コード例 #8
0
        public void DescendantsReturnsDescendantsOfTheNode(int node, int[] expectedDescendants)
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            GraphTestHelper.AssertCollectionsConsistsOfNodes(expectedDescendants,
                                                             graph.Find(node).Descendants);
            GraphTestHelper.AssertNodesAreOrderedByLevel(graph.Find(node).Descendants);
        }
コード例 #9
0
        public void GetNeighboursReturnsNeighboursOfTheNodeInTheRangeOfLevels(int node, int[] expectedNeighbours)
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            GraphTestHelper.AssertCollectionsConsistsOfNodes(expectedNeighbours,
                                                             graph.Find(node).GetNeighbours(-1, 2));
            GraphTestHelper.AssertNodesAreOrderedByLevel(graph.Find(node).GetNeighbours(-1, 2));
        }
コード例 #10
0
        public void TerminatingPrecedentsReturnsPrecedentsOfTheNodeOnTheZeroLevel(int node, int[] expectedTerminatingPrecedents)
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            GraphTestHelper.AssertCollectionsConsistsOfNodes(expectedTerminatingPrecedents,
                                                             graph.Find(node).TerminatingPrecedents);
            GraphTestHelper.AssertNodesAreOrderedByLevel(graph.Find(node).TerminatingPrecedents);
        }
コード例 #11
0
        public void LevelCalculatesCorrectly(int node, int expectedLevel)
        {
            var graph       = GraphTestHelper.CreateExampleGraph(TargetGraph);
            var countLevels = graph.CountLevels; // force graph to rebuild. fix this later
            var target      = graph.Find(node);

            Assert.AreEqual(expectedLevel, target.Level,
                            "Level of node {0} expected to be {1}, but is {2}", target.Value, expectedLevel, target.Level);
        }
コード例 #12
0
        public void CountLevelsReturnsNumberOfLevelsInGraph2()
        {
            var graph = GraphTestHelper.CreateEmptyGraph(TargetGraph);

            graph.AddSequences(new[]
            {
                new[] { 1, 2, 6 },
                new[] { 2, 3, 4, 5 },
                new[] { 9, 8, 7, 6 },
            });
            Assert.AreEqual(6, graph.CountLevels);
        }
コード例 #13
0
        public void CanSortReturnsWhetherGraphCanBeSorted2()
        {
            // {41, 51, 61, 100},
            // {42, 52, 62, 100},
            var graph = GraphTestHelper.CreateSimpleGraph(TargetGraph);

            Assert.True(graph.CanSort());
            graph.AddSequences(new[]
            {
                new[] { 100, 41 },
            });
            Assert.False(graph.CanSort());
        }
コード例 #14
0
        public void ConstructorWithInitialSequenceArgumentInitializesGraph()
        {
            var target = GraphTestHelper.CreateEmptyGraph(TargetGraph);

            target.AddSequence(new[] { 1, 2, 3, 4, 5 });
            Assert.AreEqual(5, target.CountNodes);
            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 1, 2 },
                new[] { 2, 3 },
                new[] { 3, 4 },
                new[] { 4, 5 },
                new[] { 1, 2, 3, 4, 5 },
            });
        }
コード例 #15
0
        public void GenerateGraphGeneratesSquareGraphStructure()
        {
            var target = GraphGenerator.GenerateGraph(3, 2, 2);

            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 0, 2 },
                new[] { 0, 3 },
                new[] { 1, 2 },
                new[] { 1, 3 },
                new[] { 2, 4 },
                new[] { 2, 5 },
                new[] { 3, 4 },
                new[] { 3, 5 },
            });
        }
コード例 #16
0
        public void GenerateGraphGeneratesVerticalGraphStructure()
        {
            var target = GraphGenerator.GenerateGraph(10, 1, 1);

            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 0, 1 },
                new[] { 1, 2 },
                new[] { 2, 3 },
                new[] { 3, 4 },
                new[] { 4, 5 },
                new[] { 5, 6 },
                new[] { 6, 7 },
                new[] { 7, 8 },
                new[] { 8, 9 },
            });
        }
コード例 #17
0
        public void GenerateGraphGeneratesTriangleGraphStructure1()
        {
            var target = GraphGenerator.GenerateGraph(3, 3, 1);

            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 0, 1 },
                new[] { 0, 2 },
                new[] { 0, 3 },
                new[] { 1, 4 },
                new[] { 1, 5 },
                new[] { 1, 6 },
                new[] { 2, 7 },
                new[] { 2, 8 },
                new[] { 2, 9 },
                new[] { 3, 10 },
                new[] { 3, 11 },
                new[] { 3, 12 },
            });
        }
コード例 #18
0
        public void AddAddsSequence()
        {
            var target = GraphTestHelper.CreateEmptyGraph(TargetGraph);

            target.AddSequence(new [] { 0, 1 });
            target.AddSequence(new [] { 1, 2 });
            target.AddSequence(new [] { 2, 3 });
            target.AddSequence(new [] { 3, 4 });
            target.AddSequence(new [] { 4, 5 });
            target.AddSequence(new [] { 5, 6 });
            target.AddSequence(new [] { 6, 7 });
            target.AddSequence(new [] { 7, 8 });
            target.AddSequence(new [] { 8, 9 });
            Assert.AreEqual(10, target.CountNodes);
            GraphTestHelper.AssertConsistsOfSequences(target, new[]
            {
                new[] { 0, 1 },
                new[] { 1, 2 },
                new[] { 2, 3 },
                new[] { 3, 4 },
                new[] { 4, 5 },
                new[] { 5, 6 },
                new[] { 6, 7 },
                new[] { 7, 8 },
                new[] { 8, 9 },
                new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
            });
            GraphTestHelper.AssertConsistsOfBackSequences(target, new[]
            {
                new[] { 1, 0 },
                new[] { 2, 1 },
                new[] { 3, 2 },
                new[] { 4, 3 },
                new[] { 5, 4 },
                new[] { 6, 5 },
                new[] { 7, 6 },
                new[] { 8, 7 },
                new[] { 9, 8 },
                new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
            });
        }
コード例 #19
0
        public void InitialLevelCalculatesCorrectly()
        {
            var graph = GraphTestHelper.CreateEmptyGraph(TargetGraph);

            graph.AddSequences(new[]
            {
                new[] { 2, 4, 3, 9 },
                new[] { 1, 3 },
                new[] { 4, 5, 7, 8, 3 },
                new[] { 4, 6, 8 },
                new[] { 2, 9 },
            });

            var countLevels = graph.CountLevels;

            for (var level = 0; level < countLevels; level++)
            {
                var nodesOnLevel = graph.GetNodes(level).Count();
                Assert.IsTrue(nodesOnLevel > 0);
            }
        }
コード例 #20
0
        public void AddRangeAddsSequences()
        {
            var graph = GraphTestHelper.CreateEmptyGraph(TargetGraph);

            graph.AddSequences(new []
            {
                new[] { 41, 51, 61, 100 },
                new[] { 42, 52, 62, 100 },
            });
            Assert.AreEqual(7, graph.CountNodes);
            GraphTestHelper.AssertConsistsOfSequences(graph, new[]
            {
                new[] { 41, 51 },
                new[] { 51, 61 },
                new[] { 61, 100 },
                new[] { 42, 52 },
                new[] { 52, 62 },
                new[] { 62, 100 },
                new[] { 41, 51, 61, 100 },
                new[] { 42, 52, 62, 100 },
            });
        }
コード例 #21
0
        public void AddAddsExistingSequence()
        {
            // {41, 51, 61, 100},
            // {42, 52, 62, 100},
            var graph = GraphTestHelper.CreateSimpleGraph(TargetGraph);

            Assert.AreEqual(7, graph.CountNodes);
            graph.AddSequence(new[] { 51, 61 });
            graph.AddSequence(new[] { 42, 52 });
            Assert.AreEqual(7, graph.CountNodes);

            GraphTestHelper.AssertConsistsOfSequences(graph, new[]
            {
                new[] { 41, 51 },
                new[] { 51, 61 },
                new[] { 61, 100 },
                new[] { 42, 52 },
                new[] { 52, 62 },
                new[] { 62, 100 },
                new[] { 41, 51, 61, 100 },
                new[] { 42, 52, 62, 100 },
            });
        }
コード例 #22
0
        public void SingleSequenceCannotBeAdded()
        {
            var graph = GraphTestHelper.CreateEmptyGraph(TargetGraph);

            graph.AddSequence(new[] { 1 });
        }
コード例 #23
0
        public void FindReturnsCorrectValue(int node)
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            Assert.AreEqual(node, graph.Find(node).Value);
        }
コード例 #24
0
        public void CanSortReturnsWhetherGraphCanBeSorted1()
        {
            var graph = GraphTestHelper.CreateSimpleGraph(TargetGraph);

            Assert.True(graph.CanSort());
        }
コード例 #25
0
        public void CanSortReturnsWhetherGraphWithTheSequenceCanBeSorted1()
        {
            var graph = GraphTestHelper.CreateSimpleGraph(TargetGraph);

            Assert.True(graph.CanSort(new[] { 43, 52, 63, 100 }));
        }
コード例 #26
0
        public void CanSortReturnsWhetherGraphWithTheSequenceCanBeSorted2()
        {
            var graph = GraphTestHelper.CreateSimpleGraph(TargetGraph);

            Assert.False(graph.CanSort(new[] { 100, 41 }));
        }
コード例 #27
0
        public void CountLevelsReturnsNumberOfLevelsInGraph1()
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            Assert.AreEqual(6, graph.CountLevels);
        }
コード例 #28
0
        public void CountReturnsCountOfGraph()
        {
            var graph = GraphTestHelper.CreateExampleGraph(TargetGraph);

            Assert.AreEqual(20, graph.CountNodes);
        }