コード例 #1
0
ファイル: TreesAndGraphsTests.cs プロジェクト: travalc/CTCI
        public void TestBuildOrderException()
        {
            // Arrange
            List <char> case1Projects = new List <char> {
                'a', 'b', 'c', 'd', 'e', 'f'
            };
            List <Tuple <char, char> > case1Dependencies = new List <Tuple <char, char> > {
                new Tuple <char, char> ('a', 'd'),
                new Tuple <char, char> ('f', 'b'),
                new Tuple <char, char> ('b', 'd'),
                new Tuple <char, char> ('f', 'a'),
                new Tuple <char, char> ('d', 'c'),
                new Tuple <char, char> ('c', 'a'),
            };

            // Act
            List <char> case1Result = TreesAndGraphs.BuildOrder(case1Projects, case1Dependencies);
        }
コード例 #2
0
        public void BuildOrder()
        {
            DirectedGraph g = new DirectedGraph();

            g.AddNode(1);
            g.AddNode(2);
            g.AddNode(3);
            g.AddNode(4);
            g.AddNode(5);
            g.AddNode(6);
            g.AddNode(7);
            g.AddNode(8);

            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            g.AddEdge(2, 4);
            g.AddEdge(3, 5);
            g.AddEdge(6, 7);
            g.AddEdge(7, 8);

            var result = TreesAndGraphs.BuildOrder(g);

            Assert.IsNotNull(result);
            foreach (var x in result)
            {
                Trace.Write(x.Data.ToString() + "->");
            }


            g = new DirectedGraph();
            g.AddNode(1);
            g.AddNode(2);
            g.AddNode(3);
            g.AddNode(4);

            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            g.AddEdge(4, 2);
            g.AddEdge(3, 4);

            result = TreesAndGraphs.BuildOrder(g);
            Assert.IsNull(result);
        }
コード例 #3
0
ファイル: TreesAndGraphsTests.cs プロジェクト: travalc/CTCI
        public void TestBuildOrder()
        {
            // Arrange
            List <char> case1Projects = new List <char> {
                'a', 'b', 'c', 'd', 'e', 'f'
            };
            List <Tuple <char, char> > case1Dependencies = new List <Tuple <char, char> > {
                new Tuple <char, char> ('a', 'd'),
                new Tuple <char, char> ('f', 'b'),
                new Tuple <char, char> ('b', 'd'),
                new Tuple <char, char> ('f', 'a'),
                new Tuple <char, char> ('d', 'c')
            };

            List <char> case2Projects = new List <char> {
                'a', 'b', 'c', 'd', 'e', 'f', 'g'
            };
            List <Tuple <char, char> > case2Dependencies = new List <Tuple <char, char> > {
                new Tuple <char, char> ('a', 'd'),
                new Tuple <char, char> ('f', 'b'),
                new Tuple <char, char> ('b', 'd'),
                new Tuple <char, char> ('f', 'a'),
                new Tuple <char, char> ('d', 'c'),
                new Tuple <char, char> ('g', 'b')
            };

            // Act
            List <char> case1Result = TreesAndGraphs.BuildOrder(case1Projects, case1Dependencies);
            List <char> case2Result = TreesAndGraphs.BuildOrder(case2Projects, case2Dependencies);

            // Assert
            List <char> case1Expected = new List <char> {
                'f', 'e', 'b', 'a', 'd', 'c'
            };

            CollectionAssert.AreEqual(case1Expected, case1Result);
            List <char> case2Expected = new List <char> {
                'f', 'g', 'e', 'b', 'a', 'd', 'c'
            };

            CollectionAssert.AreEqual(case2Expected, case2Result);
        }