public void TryGetOutEdges() { var graph = new TransitionFactoryImplicitGraph <CloneableTestVertex, Edge <CloneableTestVertex> >(); var vertex0 = new CloneableTestVertex("0"); var vertex1 = new CloneableTestVertex("1"); var vertex2 = new CloneableTestVertex("2"); var vertex3 = new CloneableTestVertex("3"); var vertex4 = new CloneableTestVertex("4"); var vertex5 = new CloneableTestVertex("5"); var edge1 = new Edge <CloneableTestVertex>(vertex1, vertex2); var edge2 = new Edge <CloneableTestVertex>(vertex1, vertex2); var edge3 = new Edge <CloneableTestVertex>(vertex1, vertex3); var edge4 = new Edge <CloneableTestVertex>(vertex2, vertex2); var edge5 = new Edge <CloneableTestVertex>(vertex2, vertex4); var edge6 = new Edge <CloneableTestVertex>(vertex3, vertex1); var edge7 = new Edge <CloneableTestVertex>(vertex4, vertex5); graph.AddTransitionFactory( new TestTransitionFactory <CloneableTestVertex>(vertex1, Enumerable.Empty <Edge <CloneableTestVertex> >())); AssertNoOutEdge(graph, vertex1); graph.ClearTransitionFactories(); graph.AddTransitionFactory( new TestTransitionFactory <CloneableTestVertex>(new[] { new TestTransitionFactory <CloneableTestVertex> .VertexEdgesSet(vertex1, new[] { edge1, edge2, edge3 }), new TestTransitionFactory <CloneableTestVertex> .VertexEdgesSet(vertex2, new[] { edge4 }), new TestTransitionFactory <CloneableTestVertex> .VertexEdgesSet(vertex3, new[] { edge6 }), })); graph.AddTransitionFactory( new TestTransitionFactory <CloneableTestVertex>(vertex2, new[] { edge5 })); graph.AddTransitionFactory( new TestTransitionFactory <CloneableTestVertex>(vertex4, new[] { edge7 })); Assert.IsFalse(graph.TryGetOutEdges(vertex0, out _)); Assert.IsFalse(graph.TryGetOutEdges(vertex5, out _)); // Vertex5 was not discovered Assert.IsTrue(graph.TryGetOutEdges(vertex3, out IEnumerable <Edge <CloneableTestVertex> > gotEdges)); CollectionAssert.AreEqual(new[] { edge6 }, gotEdges); Assert.IsTrue(graph.TryGetOutEdges(vertex1, out gotEdges)); CollectionAssert.AreEqual(new[] { edge1, edge2, edge3 }, gotEdges); // Trigger discover of vertex5 // ReSharper disable once ReturnValueOfPureMethodIsNotUsed graph.OutEdges(vertex4); Assert.IsTrue(graph.TryGetOutEdges(vertex5, out gotEdges)); CollectionAssert.IsEmpty(gotEdges); }
public void ClearTransitionFactories() { var graph = new TransitionFactoryImplicitGraph <CloneableTestVertex, Edge <CloneableTestVertex> >(); var vertex1 = new CloneableTestVertex("1"); var vertex2 = new CloneableTestVertex("2"); var vertex3 = new CloneableTestVertex("3"); var edge11 = new Edge <CloneableTestVertex>(vertex1, vertex1); var edge12 = new Edge <CloneableTestVertex>(vertex1, vertex2); var edge13 = new Edge <CloneableTestVertex>(vertex1, vertex3); var edge23 = new Edge <CloneableTestVertex>(vertex2, vertex3); var edge33 = new Edge <CloneableTestVertex>(vertex3, vertex3); graph.AddTransitionFactory( new TestTransitionFactory <CloneableTestVertex>(new[] { new TestTransitionFactory <CloneableTestVertex> .VertexEdgesSet(vertex1, new[] { edge11, edge12, edge13 }), new TestTransitionFactory <CloneableTestVertex> .VertexEdgesSet(vertex2, new[] { edge23 }) })); graph.AddTransitionFactory( new TestTransitionFactory <CloneableTestVertex>(vertex3, new[] { edge33 })); // ReSharper disable ReturnValueOfPureMethodIsNotUsed => trigger caching of edges graph.OutEdges(vertex1); graph.OutEdges(vertex2); graph.OutEdges(vertex3); // ReSharper restore ReturnValueOfPureMethodIsNotUsed AssertHasVertices(graph, new[] { vertex1, vertex2, vertex3 }); graph.ClearTransitionFactories(); AssertNoVertices(graph, new[] { vertex1, vertex2, vertex3 }); }
public void RemoveTransitionFactories() { var graph = new TransitionFactoryImplicitGraph <CloneableTestVertex, Edge <CloneableTestVertex> >(); Assert.IsFalse(graph.RemoveTransitionFactory(null)); var vertex1 = new CloneableTestVertex("1"); var vertex2 = new CloneableTestVertex("2"); var vertex3 = new CloneableTestVertex("3"); var factory1 = new TestTransitionFactory <CloneableTestVertex>(vertex1, Enumerable.Empty <Edge <CloneableTestVertex> >()); var factory2 = new TestTransitionFactory <CloneableTestVertex>(vertex2, Enumerable.Empty <Edge <CloneableTestVertex> >()); var factory3 = new TestTransitionFactory <CloneableTestVertex>(vertex3, Enumerable.Empty <Edge <CloneableTestVertex> >()); graph.AddTransitionFactories(new[] { factory1, factory2 }); Assert.IsFalse(graph.ContainsTransitionFactory(null)); Assert.IsTrue(graph.ContainsTransitionFactory(factory1)); Assert.IsTrue(graph.ContainsTransitionFactory(factory2)); Assert.IsFalse(graph.RemoveTransitionFactory(factory3)); Assert.IsTrue(graph.RemoveTransitionFactory(factory1)); Assert.IsFalse(graph.RemoveTransitionFactory(factory1)); Assert.IsTrue(graph.RemoveTransitionFactory(factory2)); var factory4 = new TestTransitionFactory <CloneableTestVertex>( vertex1, new[] { new Edge <CloneableTestVertex>(vertex1, vertex2), new Edge <CloneableTestVertex>(vertex1, vertex3) }); graph.AddTransitionFactory(factory4); Assert.IsTrue(graph.ContainsTransitionFactory(factory4)); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed graph.OutEdges(vertex1); // Force exploration from vertex1 Assert.IsTrue(graph.RemoveTransitionFactory(factory4)); }
public void ContainsVertex() { var graph = new TransitionFactoryImplicitGraph <CloneableTestVertex, Edge <CloneableTestVertex> >(); var vertex1 = new CloneableTestVertex("1"); var vertex2 = new CloneableTestVertex("2"); var otherVertex1 = new CloneableTestVertex("1"); var vertex3 = new CloneableTestVertex("3"); var vertex4 = new CloneableTestVertex("4"); var edge34 = new Edge <CloneableTestVertex>(vertex3, vertex4); Assert.IsFalse(graph.ContainsVertex(vertex1)); Assert.IsFalse(graph.ContainsVertex(vertex2)); Assert.IsFalse(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); var factory1 = new TestTransitionFactory <CloneableTestVertex>(vertex1, Enumerable.Empty <Edge <CloneableTestVertex> >()); graph.AddTransitionFactory(factory1); Assert.IsFalse(graph.ContainsVertex(vertex1)); // Not explored yet Assert.IsFalse(graph.ContainsVertex(vertex2)); Assert.IsFalse(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed graph.OutEdges(vertex1); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsFalse(graph.ContainsVertex(vertex2)); Assert.IsFalse(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); var factory2 = new TestTransitionFactory <CloneableTestVertex>(vertex2, Enumerable.Empty <Edge <CloneableTestVertex> >()); graph.AddTransitionFactory(factory2); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsFalse(graph.ContainsVertex(vertex2)); // Not explored yet Assert.IsFalse(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed graph.OutEdges(vertex2); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsTrue(graph.ContainsVertex(vertex2)); Assert.IsFalse(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); var factoryOther1 = new TestTransitionFactory <CloneableTestVertex>(otherVertex1, Enumerable.Empty <Edge <CloneableTestVertex> >()); graph.AddTransitionFactory(factoryOther1); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsTrue(graph.ContainsVertex(vertex2)); Assert.IsFalse(graph.ContainsVertex(otherVertex1)); // Not explored yet Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed graph.OutEdges(otherVertex1); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsTrue(graph.ContainsVertex(vertex2)); Assert.IsTrue(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); Assert.IsFalse(graph.ContainsVertex(vertex4)); var factory3 = new TestTransitionFactory <CloneableTestVertex>(vertex3, new[] { edge34 }); graph.AddTransitionFactory(factory3); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsTrue(graph.ContainsVertex(vertex2)); Assert.IsTrue(graph.ContainsVertex(otherVertex1)); Assert.IsFalse(graph.ContainsVertex(vertex3)); // Not explored yet Assert.IsFalse(graph.ContainsVertex(vertex4)); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed graph.OutEdges(vertex3); Assert.IsTrue(graph.ContainsVertex(vertex1)); Assert.IsTrue(graph.ContainsVertex(vertex2)); Assert.IsTrue(graph.ContainsVertex(otherVertex1)); Assert.IsTrue(graph.ContainsVertex(vertex3)); Assert.IsTrue(graph.ContainsVertex(vertex4)); // Discovered when requesting vertex3 }