protected static void InEdges_NullThrows_Test <TVertex, TEdge>( [NotNull] IBidirectionalIncidenceGraph <TVertex, TEdge> graph) where TVertex : class where TEdge : IEdge <TVertex> { // ReSharper disable ReturnValueOfPureMethodIsNotUsed // ReSharper disable AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.IsInEdgesEmpty(null)); Assert.Throws <ArgumentNullException>(() => graph.InDegree(null)); Assert.Throws <ArgumentNullException>(() => graph.InEdges(null).ToArray()); // ReSharper restore AssignNullToNotNullAttribute // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
protected static void InEdges_Throws_Test <TVertex, TEdge>( [NotNull] IBidirectionalIncidenceGraph <TVertex, TEdge> graph) where TVertex : IEquatable <TVertex>, new() where TEdge : IEdge <TVertex> { // ReSharper disable ReturnValueOfPureMethodIsNotUsed var vertex = new TVertex(); Assert.Throws <VertexNotFoundException>(() => graph.IsInEdgesEmpty(vertex)); Assert.Throws <VertexNotFoundException>(() => graph.InDegree(vertex)); Assert.Throws <VertexNotFoundException>(() => graph.InEdges(vertex).ToArray()); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
IEnumerable <TEdge> IBidirectionalIncidenceGraph <TVertex, TEdge> .InEdges(TVertex v) { IBidirectionalIncidenceGraph <TVertex, TEdge> ithis = this; Contract.Requires(v != null); Contract.Requires(ithis.ContainsVertex(v)); Contract.Ensures(Contract.Result <IEnumerable <TEdge> >() != null); Contract.Ensures(Contract.Result <IEnumerable <TEdge> >().All(edge => edge != null && edge.Target.Equals(v) ) ); return(default(IEnumerable <TEdge>)); }
public static void AssertHasInEdges <TVertex, TEdge>( [NotNull] IBidirectionalIncidenceGraph <TVertex, TEdge> graph, [NotNull] TVertex vertex, [NotNull, ItemNotNull] IEnumerable <TEdge> edges) where TEdge : IEdge <TVertex> { TEdge[] edgeArray = edges.ToArray(); CollectionAssert.IsNotEmpty(edgeArray); Assert.IsFalse(graph.IsInEdgesEmpty(vertex)); Assert.AreEqual(edgeArray.Length, graph.InDegree(vertex)); CollectionAssert.AreEquivalent(edgeArray, graph.InEdges(vertex)); }
public BestFirstFrontierSearchAlgorithm( IAlgorithmComponent host, IBidirectionalIncidenceGraph <TVertex, TEdge> visitedGraph, Func <TEdge, double> edgeWeights, IDistanceRelaxer distanceRelaxer) : base(host, visitedGraph) { Contract.Requires(edgeWeights != null); Contract.Requires(distanceRelaxer != null); this.edgeWeights = edgeWeights; this.distanceRelaxer = distanceRelaxer; }
protected static void Degree_Test( [NotNull] GraphData <int, Edge <int> > data1, [NotNull] GraphData <int, Edge <int> > data2, [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph) { // ReSharper disable ReturnValueOfPureMethodIsNotUsed data1.CheckCalls(0); data2.CheckCalls(0); data1.ShouldReturnValue = false; data2.ShouldReturnValue = false; Assert.Throws <VertexNotFoundException>(() => graph.Degree(1)); data1.CheckCalls(0); data2.CheckCalls(1); data1.ShouldReturnValue = true; data2.ShouldReturnValue = false; Assert.Throws <VertexNotFoundException>(() => graph.Degree(1)); data1.CheckCalls(0); data2.CheckCalls(1); data1.ShouldReturnValue = false; data2.ShouldReturnValue = true; Assert.Throws <VertexNotFoundException>(() => graph.Degree(1)); data1.CheckCalls(1); data2.CheckCalls(1); // ReSharper restore ReturnValueOfPureMethodIsNotUsed data1.ShouldReturnValue = true; data2.ShouldReturnValue = true; Assert.AreEqual(0, graph.Degree(1)); data1.ShouldReturnEdges = new[] { new Edge <int>(1, 2) }; data2.ShouldReturnEdges = null; Assert.AreEqual(1, graph.Degree(1)); data1.ShouldReturnEdges = null; data2.ShouldReturnEdges = new[] { new Edge <int>(3, 1) }; Assert.AreEqual(1, graph.Degree(1)); data1.ShouldReturnEdges = new[] { new Edge <int>(1, 2), new Edge <int>(1, 3) }; data2.ShouldReturnEdges = new[] { new Edge <int>(4, 1) }; Assert.AreEqual(3, graph.Degree(1)); // Self edge data1.ShouldReturnEdges = new[] { new Edge <int>(1, 2), new Edge <int>(1, 3), new Edge <int>(1, 1) }; data2.ShouldReturnEdges = new[] { new Edge <int>(4, 1), new Edge <int>(1, 1) }; Assert.AreEqual(5, graph.Degree(1)); }
public static void AssertHasReversedInEdges <TVertex, TEdge>( IBidirectionalIncidenceGraph <TVertex, SReversedEdge <TVertex, TEdge> > graph, TVertex vertex, IEnumerable <TEdge> edges) where TEdge : IEdge <TVertex> { TEdge[] edgeArray = edges.ToArray(); CollectionAssert.IsNotEmpty(edgeArray); Assert.IsFalse(graph.IsInEdgesEmpty(vertex)); Assert.AreEqual(edgeArray.Length, graph.InDegree(vertex)); CollectionAssert.AreEquivalent( edgeArray.Select(edge => new SReversedEdge <TVertex, TEdge>(edge)), graph.InEdges(vertex)); }
protected static void InEdge_ImmutableGraph_Test( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IBidirectionalIncidenceGraph <int, Edge <int> > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge13 = new Edge <int>(1, 3); var edge21 = new Edge <int>(2, 1); var edge41 = new Edge <int>(4, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge13, edge21, edge41 }); IBidirectionalIncidenceGraph <int, Edge <int> > graph = createGraph(); Assert.AreSame(edge11, graph.InEdge(1, 0)); Assert.AreSame(edge41, graph.InEdge(1, 2)); Assert.AreSame(edge13, graph.InEdge(3, 0)); }
bool IBidirectionalIncidenceGraph <TVertex, TEdge> .TryGetInEdges(TVertex v, out IEnumerable <TEdge> edges) { IBidirectionalIncidenceGraph <TVertex, TEdge> ithis = this; Contract.Requires(v != null); Contract.Requires(ithis.ContainsVertex(v)); Contract.Ensures(Contract.Result <bool>() == ithis.ContainsVertex(v)); Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out edges) != null); Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out edges).All(edge => edge != null && edge.Target.Equals(v) ) ); edges = null; return(default(bool)); }
protected static void InEdge_ImmutableGraph_ReversedTest( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge31 = new Edge <int>(3, 1); var edge32 = new Edge <int>(3, 2); var edge34 = new Edge <int>(3, 4); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge31, edge32, edge34 }); IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); AssertSameReversedEdge(edge11, graph.InEdge(1, 0)); AssertSameReversedEdge(edge31, graph.InEdge(3, 0)); AssertSameReversedEdge(edge34, graph.InEdge(3, 2)); }
protected static void InEdges_Throws_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph) { // ReSharper disable ReturnValueOfPureMethodIsNotUsed data.CheckCalls(0); data.ShouldReturnValue = false; Assert.Throws <VertexNotFoundException>(() => graph.IsInEdgesEmpty(1)); data.CheckCalls(1); Assert.Throws <VertexNotFoundException>(() => graph.InDegree(1)); data.CheckCalls(1); Assert.Throws <VertexNotFoundException>(() => graph.InEdges(1)); data.CheckCalls(1); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
protected static void InEdge_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph) { var edge11 = new Edge <int>(1, 1); var edge21 = new Edge <int>(2, 1); var edge31 = new Edge <int>(3, 1); data.CheckCalls(0); data.ShouldReturnValue = true; data.ShouldReturnEdges = new[] { edge11, edge21, edge31 }; Assert.AreSame(edge11, graph.InEdge(1, 0)); data.CheckCalls(1); Assert.AreSame(edge31, graph.InEdge(1, 2)); data.CheckCalls(1); }
protected static void InEdges_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph) { data.CheckCalls(0); data.ShouldReturnValue = true; AssertNoInEdge(graph, 1); data.CheckCalls(3); Edge <int>[] edges = { new Edge <int>(1, 2), new Edge <int>(1, 3) }; data.ShouldReturnEdges = edges; AssertHasInEdges(graph, 1, edges); data.CheckCalls(3); }
protected static void TryGetInEdges_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph) { data.CheckCalls(0); data.ShouldReturnValue = false; Assert.IsFalse(graph.TryGetInEdges(1, out _)); data.CheckCalls(1); data.ShouldReturnValue = true; Assert.IsTrue(graph.TryGetInEdges(1, out IEnumerable <Edge <int> > edges)); CollectionAssert.IsEmpty(edges); data.CheckCalls(1); data.ShouldReturnEdges = new[] { new Edge <int>(4, 1), new Edge <int>(2, 1) }; Assert.IsTrue(graph.TryGetInEdges(1, out edges)); CollectionAssert.AreEqual(data.ShouldReturnEdges, edges); data.CheckCalls(1); }
protected static void InEdge_Throws_Test( [NotNull] GraphData <int, Edge <int> > data, [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph) { // ReSharper disable ReturnValueOfPureMethodIsNotUsed data.CheckCalls(0); data.ShouldReturnValue = false; Assert.Throws <VertexNotFoundException>(() => graph.InEdge(1, 0)); data.CheckCalls(1); data.ShouldReturnValue = true; AssertIndexOutOfRange(() => graph.InEdge(1, 0)); data.CheckCalls(1); data.ShouldReturnEdges = new[] { new Edge <int>(1, 2) }; AssertIndexOutOfRange(() => graph.InEdge(1, 1)); data.CheckCalls(1); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
protected static void Degree_ImmutableGraph_ReversedTest( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); var edge3 = new Edge <int>(1, 4); var edge4 = new Edge <int>(2, 4); var edge5 = new Edge <int>(3, 2); var edge6 = new Edge <int>(3, 3); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6 }); wrappedGraph.AddVertex(5); IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); Assert.AreEqual(3, graph.Degree(1)); Assert.AreEqual(3, graph.Degree(2)); Assert.AreEqual(4, graph.Degree(3)); // Self edge Assert.AreEqual(2, graph.Degree(4)); Assert.AreEqual(0, graph.Degree(5)); }
protected static void InEdge_Throws_ImmutableGraph_ReversedTest( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { const int vertex1 = 1; const int vertex2 = 2; // ReSharper disable ReturnValueOfPureMethodIsNotUsed IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); Assert.Throws <VertexNotFoundException>(() => graph.InEdge(vertex1, 0)); wrappedGraph.AddVertex(vertex1); wrappedGraph.AddVertex(vertex2); graph = createGraph(); AssertIndexOutOfRange(() => graph.InEdge(vertex1, 0)); wrappedGraph.AddEdge(new Edge <int>(1, 2)); graph = createGraph(); AssertIndexOutOfRange(() => graph.InEdge(vertex1, 5)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }