Example #1
0
        public void GetEdge_Throws()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var graph2 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var random = new Random();

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, random));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge(graph2, null));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, null));

            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, 1, random));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), 1, null));
            Assert.Throws <ArgumentNullException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(null, 1, null));
            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(() => RandomGraphFactory.GetVertex(graph, random));

            Assert.Throws <ArgumentOutOfRangeException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), -1, random));
            Assert.Throws <ArgumentOutOfRangeException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), 0, random));
            Assert.Throws <InvalidOperationException>(() => RandomGraphFactory.GetEdge <int, Edge <int> >(Enumerable.Empty <Edge <int> >(), 1, random));
            Assert.Throws <InvalidOperationException>(
                () => RandomGraphFactory.GetEdge <int, Edge <int> >(
                    new[] { new Edge <int>(1, 2), new Edge <int>(1, 3) },
                    10,
                    new Random(123456)));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
Example #2
0
        public void GraphWithSelfEdges(
            [UsingLinear(2, 9)] int i,
            [UsingLinear(0, 10)] int j
            )
        {
            if (i == 0 && j == 0)
            {
                return;
            }

            Random rnd = new Random();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         new IntVertexFactory(),
                                                         FactoryCompiler.GetEdgeFactory <int, Edge <int> >(),
                                                         rnd, i, j, true);

            algo = new BreadthFirstSearchAlgorithm <int, Edge <int> >(g);
            try
            {
                algo.InitializeVertex += new VertexEventHandler <int>(this.InitializeVertex);
                algo.DiscoverVertex   += new VertexEventHandler <int>(this.DiscoverVertex);
                algo.ExamineEdge      += new EdgeEventHandler <int, Edge <int> >(this.ExamineEdge);
                algo.ExamineVertex    += new VertexEventHandler <int>(this.ExamineVertex);
                algo.TreeEdge         += new EdgeEventHandler <int, Edge <int> >(this.TreeEdge);
                algo.NonTreeEdge      += new EdgeEventHandler <int, Edge <int> >(this.NonTreeEdge);
                algo.GrayTarget       += new EdgeEventHandler <int, Edge <int> >(this.GrayTarget);
                algo.BlackTarget      += new EdgeEventHandler <int, Edge <int> >(this.BlackTarget);
                algo.FinishVertex     += new VertexEventHandler <int>(this.FinishVertex);

                parents.Clear();
                distances.Clear();
                currentDistance = 0;
                sourceVertex    = RandomGraphFactory.GetVertex(g, rnd);

                foreach (int v in g.Vertices)
                {
                    distances[v] = int.MaxValue;
                    parents[v]   = v;
                }
                distances[sourceVertex] = 0;
                algo.Compute(sourceVertex);

                CheckBfs();
            }
            finally
            {
                algo.InitializeVertex -= new VertexEventHandler <int>(this.InitializeVertex);
                algo.DiscoverVertex   -= new VertexEventHandler <int>(this.DiscoverVertex);
                algo.ExamineEdge      -= new EdgeEventHandler <int, Edge <int> >(this.ExamineEdge);
                algo.ExamineVertex    -= new VertexEventHandler <int>(this.ExamineVertex);
                algo.TreeEdge         -= new EdgeEventHandler <int, Edge <int> >(this.TreeEdge);
                algo.NonTreeEdge      -= new EdgeEventHandler <int, Edge <int> >(this.NonTreeEdge);
                algo.GrayTarget       -= new EdgeEventHandler <int, Edge <int> >(this.GrayTarget);
                algo.BlackTarget      -= new EdgeEventHandler <int, Edge <int> >(this.BlackTarget);
                algo.FinishVertex     -= new VertexEventHandler <int>(this.FinishVertex);
            }
        }
        public void GetVertex_Throws()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var graph2 = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var random = new Random();

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, random)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex(graph2, null)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, null)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, 1, random)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), 1, null)
                );

            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.GetVertex <int>(null, 1, null)
                );

            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.GetVertex(graph, random)
                );

            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), -1, random)
                );

            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), 0, random)
                );

            Assert.Throws <InvalidOperationException>(
                () => RandomGraphFactory.GetVertex(Enumerable.Empty <int>(), 1, random)
                );

            Assert.Throws <InvalidOperationException>(
                () => RandomGraphFactory.GetVertex(
                    new[] { 1, 2 },
                    10,
                    new Random(123456)
                    )
                );
        }
        public void GetVertex()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertexRange(new[] { 1, 2, 3, 4, 5 });

            int vertex = RandomGraphFactory.GetVertex(
                graph,
                new Random(123456)
                );

            Assert.AreEqual(2, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph,
                new Random(456789)
                );
            Assert.AreEqual(5, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph.Vertices,
                graph.VertexCount,
                new Random(123456)
                );
            Assert.AreEqual(2, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph.Vertices,
                graph.VertexCount,
                new Random(456789)
                );
            Assert.AreEqual(5, vertex);

            vertex = RandomGraphFactory.GetVertex(
                graph.Vertices,
                3,
                new Random(123)
                );
            Assert.AreEqual(3, vertex);
        }