protected static void AddVerticesAndEdgeRange_Throws_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > graph) { int vertexAdded = 0; int edgeAdded = 0; AssertEmptyGraph(graph); // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.VertexAdded += v => { Assert.IsNotNull(v); ++vertexAdded; }; // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => graph.AddVerticesAndEdgeRange(null)); // Edge 1, 2, 3 var edge1 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); Assert.Throws <ArgumentNullException>(() => graph.AddVerticesAndEdgeRange(new[] { edge1, null, edge3 })); Assert.AreEqual(0, vertexAdded); Assert.AreEqual(0, edgeAdded); AssertEmptyGraph(graph); }
protected static void InEdges_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IBidirectionalIncidenceGraph <int, Edge <int> > > createGraph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge32 = new Edge <int>(3, 2); var edge33 = new Edge <int>(3, 3); wrappedGraph.AddVertex(1); IBidirectionalIncidenceGraph <int, Edge <int> > graph = createGraph(); AssertNoInEdge(graph, 1); AssertNoOutEdge(graph, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge32, edge33 }); graph = createGraph(); AssertHasOutEdges(graph, 1, new[] { edge12, edge13, edge14 }); AssertHasOutEdges(graph, 2, new[] { edge24 }); AssertHasOutEdges(graph, 3, new[] { edge32, edge33 }); AssertNoOutEdge(graph, 4); AssertNoInEdge(graph, 1); AssertHasInEdges(graph, 2, new[] { edge12, edge32 }); AssertHasInEdges(graph, 3, new[] { edge13, edge33 }); AssertHasInEdges(graph, 4, new[] { edge14, edge24 }); }
protected static void TryGetOutEdges_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitGraph <int, SEquatableEdge <int> > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(2, 2); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); var edge7 = new Edge <int>(4, 5); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6, edge7 }); IImplicitGraph <int, SEquatableEdge <int> > graph = createGraph(); Assert.IsFalse(graph.TryGetOutEdges(0, out _)); Assert.IsTrue(graph.TryGetOutEdges(5, out IEnumerable <SEquatableEdge <int> > gotEdges)); CollectionAssert.IsEmpty(gotEdges); Assert.IsTrue(graph.TryGetOutEdges(3, out gotEdges)); CollectionAssert.AreEqual( new[] { new SEquatableEdge <int>(3, 1) }, gotEdges); Assert.IsTrue(graph.TryGetOutEdges(1, out gotEdges)); CollectionAssert.AreEqual( new[] { new SEquatableEdge <int>(1, 2), new SEquatableEdge <int>(1, 2), new SEquatableEdge <int>(1, 3) }, gotEdges); }
protected static void TryGetInEdges_ImmutableGraph_Test( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IBidirectionalIncidenceGraph <int, Edge <int> > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(2, 2); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); var edge7 = new Edge <int>(5, 3); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6, edge7 }); IBidirectionalIncidenceGraph <int, Edge <int> > graph = createGraph(); Assert.IsFalse(graph.TryGetInEdges(0, out _)); Assert.IsTrue(graph.TryGetInEdges(5, out IEnumerable <Edge <int> > gotEdges)); CollectionAssert.IsEmpty(gotEdges); Assert.IsTrue(graph.TryGetInEdges(4, out gotEdges)); CollectionAssert.AreEqual(new[] { edge5 }, gotEdges); Assert.IsTrue(graph.TryGetInEdges(2, out gotEdges)); CollectionAssert.AreEqual(new[] { edge1, edge2, edge4 }, gotEdges); }
protected static void TryGetInEdges_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, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(1, 1); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); var edge7 = new Edge <int>(4, 5); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6, edge7 }); IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); Assert.IsFalse(graph.TryGetInEdges(0, out _)); Assert.IsTrue(graph.TryGetInEdges(5, out IEnumerable <SReversedEdge <int, Edge <int> > > gotEdges)); CollectionAssert.IsEmpty(gotEdges); Assert.IsTrue(graph.TryGetInEdges(4, out gotEdges)); AssertSameReversedEdges(new[] { edge7 }, gotEdges); Assert.IsTrue(graph.TryGetInEdges(1, out gotEdges)); AssertSameReversedEdges(new[] { edge1, edge2, edge3, edge4 }, gotEdges); }
protected static void TryGetOutEdges_ImmutableGraph_ReversedTest( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IImplicitGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(2, 2); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); var edge7 = new Edge <int>(5, 4); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6, edge7 }); IImplicitGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); Assert.IsFalse(graph.TryGetOutEdges(0, out _)); Assert.IsTrue(graph.TryGetOutEdges(5, out IEnumerable <SReversedEdge <int, Edge <int> > > gotEdges)); CollectionAssert.IsEmpty(gotEdges); Assert.IsTrue(graph.TryGetOutEdges(3, out gotEdges)); AssertSameReversedEdges(new[] { edge3 }, gotEdges); Assert.IsTrue(graph.TryGetOutEdges(2, out gotEdges)); AssertSameReversedEdges(new[] { edge1, edge2, edge4 }, gotEdges); }
protected static void TryGetEdge_ImmutableGraph_ReversedTest( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(2, 2); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6 }); IIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); Assert.IsFalse(graph.TryGetEdge(0, 10, out _)); Assert.IsFalse(graph.TryGetEdge(0, 1, out _)); Assert.IsTrue(graph.TryGetEdge(4, 2, out SReversedEdge <int, Edge <int> > gotEdge)); AssertSameReversedEdge(edge5, gotEdge); Assert.IsTrue(graph.TryGetEdge(2, 2, out gotEdge)); AssertSameReversedEdge(edge4, gotEdge); Assert.IsTrue(graph.TryGetEdge(2, 1, out gotEdge)); AssertSameReversedEdge(edge1, gotEdge); Assert.IsFalse(graph.TryGetEdge(1, 2, out _)); }
protected static void TryGetEdge_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IIncidenceGraph <int, SEquatableEdge <int> > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(2, 2); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6 }); IIncidenceGraph <int, SEquatableEdge <int> > graph = createGraph(); Assert.IsFalse(graph.TryGetEdge(0, 10, out _)); Assert.IsFalse(graph.TryGetEdge(0, 1, out _)); Assert.IsTrue(graph.TryGetEdge(2, 4, out SEquatableEdge <int> gotEdge)); Assert.AreEqual(new SEquatableEdge <int>(2, 4), gotEdge); Assert.IsTrue(graph.TryGetEdge(2, 2, out gotEdge)); Assert.AreEqual(new SEquatableEdge <int>(2, 2), gotEdge); Assert.IsTrue(graph.TryGetEdge(1, 2, out gotEdge)); Assert.AreEqual(new SEquatableEdge <int>(1, 2), gotEdge); Assert.IsFalse(graph.TryGetEdge(2, 1, out _)); }
protected static void InEdges_ImmutableGraph_ReversedTest( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge33 = new Edge <int>(3, 3); var edge34 = new Edge <int>(3, 4); var edge43 = new Edge <int>(4, 3); wrappedGraph.AddVertex(1); IBidirectionalIncidenceGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); AssertNoInEdge(graph, 1); AssertNoOutEdge(graph, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge33, edge34, edge43 }); graph = createGraph(); AssertNoOutEdge(graph, 1); AssertHasReversedOutEdges(graph, 2, new[] { edge12 }); AssertHasReversedOutEdges(graph, 3, new[] { edge13, edge33, edge43 }); AssertHasReversedOutEdges(graph, 4, new[] { edge14, edge34 }); AssertHasReversedInEdges(graph, 1, new[] { edge12, edge13, edge14 }); AssertNoInEdge(graph, 2); AssertHasReversedInEdges(graph, 3, new[] { edge33, edge34 }); AssertHasReversedInEdges(graph, 4, new[] { edge43 }); }
protected static void AdjacentEdges_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); wrappedGraph.AddVertex(1); IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph(); AssertNoAdjacentEdge(graph, 1); wrappedGraph.AddVertex(5); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 }); graph = createGraph(); AssertHasAdjacentEdges(graph, 1, new[] { edge12, edge13, edge14, edge31 }); AssertHasAdjacentEdges(graph, 2, new[] { edge12, edge24 }); AssertHasAdjacentEdges(graph, 3, new[] { edge13, edge31, edge33 }, 4); // Has self edge counting twice AssertHasAdjacentEdges(graph, 4, new[] { edge14, edge24 }); AssertNoAdjacentEdge(graph, 5); }
protected static void RemoveVertexIf_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > graph) { int verticesRemoved = 0; int edgesRemoved = 0; // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.VertexRemoved += v => { Assert.IsNotNull(v); // ReSharper disable once AccessToModifiedClosure ++verticesRemoved; }; // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.EdgeRemoved += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesRemoved; }; var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 }); Assert.AreEqual(0, graph.RemoveVertexIf(vertex => vertex > 10)); CheckCounters(0, 0); Assert.AreEqual(2, graph.RemoveVertexIf(vertex => vertex > 2)); CheckCounters(2, 5); AssertHasVertices(graph, new[] { 1, 2 }); AssertHasEdges(graph, new[] { edge12 }); Assert.AreEqual(2, graph.RemoveVertexIf(_ => true)); CheckCounters(2, 1); AssertEmptyGraph(graph); #region Local function void CheckCounters(int expectedRemovedVertices, int expectedRemovedEdges) { Assert.AreEqual(expectedRemovedVertices, verticesRemoved); Assert.AreEqual(expectedRemovedEdges, edgesRemoved); verticesRemoved = 0; edgesRemoved = 0; } #endregion }
protected static void AddVerticesAndEdgeRange_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > graph) { int vertexAdded = 0; int edgeAdded = 0; AssertEmptyGraph(graph); // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.VertexAdded += v => { Assert.IsNotNull(v); ++vertexAdded; }; // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // Edge 1, 2 var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); Assert.AreEqual(2, graph.AddVerticesAndEdgeRange(new[] { edge1, edge2 })); Assert.AreEqual(3, vertexAdded); Assert.AreEqual(2, edgeAdded); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertHasEdges(graph, new[] { edge1, edge2 }); // Edge 1, 3 var edge3 = new Edge <int>(2, 3); Assert.AreEqual(1, graph.AddVerticesAndEdgeRange(new[] { edge1, edge3 })); // Showcase the add of only one edge Assert.AreEqual(3, vertexAdded); Assert.AreEqual(3, edgeAdded); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); }
protected static void AddVerticesAndEdgeRange_Test( IMutableVertexAndEdgeSet <int, Edge <int> > graph) { int vertexAdded = 0; int edgeAdded = 0; AssertEmptyGraph(graph); graph.VertexAdded += v => { Assert.IsNotNull(v); ++vertexAdded; }; graph.EdgeAdded += e => { Assert.IsNotNull(e); ++edgeAdded; }; // Edge 1, 2 var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 3); Assert.AreEqual(2, graph.AddVerticesAndEdgeRange(new[] { edge1, edge2 })); Assert.AreEqual(3, vertexAdded); Assert.AreEqual(2, edgeAdded); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertHasEdges(graph, new[] { edge1, edge2 }); // Edge 1, 3 var edge3 = new Edge <int>(2, 3); Assert.AreEqual(1, graph.AddVerticesAndEdgeRange(new[] { edge1, edge3 })); // Showcase the add of only one edge Assert.AreEqual(3, vertexAdded); Assert.AreEqual(3, edgeAdded); AssertHasVertices(graph, new[] { 1, 2, 3 }); AssertHasEdges(graph, new[] { edge1, edge2, edge3 }); }
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)); }
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 OutEdges_ImmutableGraph_Test( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IImplicitGraph <int, SEquatableEdge <int> > > createGraph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); wrappedGraph.AddVertex(1); IImplicitGraph <int, SEquatableEdge <int> > graph = createGraph(); AssertNoOutEdge(graph, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 }); graph = createGraph(); AssertHasOutEdges( graph, 1, new[] { new SEquatableEdge <int>(1, 2), new SEquatableEdge <int>(1, 3), new SEquatableEdge <int>(1, 4) }); AssertHasOutEdges( graph, 2, new[] { new SEquatableEdge <int>(2, 4) }); AssertHasOutEdges( graph, 3, new[] { new SEquatableEdge <int>(3, 1), new SEquatableEdge <int>(3, 3) }); AssertNoOutEdge(graph, 4); }
protected static void OutEdge_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitGraph <int, Edge <int> > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); IImplicitGraph <int, Edge <int> > graph = createGraph(); Assert.AreSame(edge11, graph.OutEdge(1, 0)); Assert.AreSame(edge13, graph.OutEdge(1, 2)); Assert.AreSame(edge24, graph.OutEdge(2, 0)); Assert.AreSame(edge33, graph.OutEdge(3, 0)); Assert.AreSame(edge41, graph.OutEdge(4, 0)); }
protected static void OutEdge_ImmutableGraph_Test( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IImplicitGraph <int, SEquatableEdge <int> > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); IImplicitGraph <int, SEquatableEdge <int> > graph = createGraph(); Assert.AreEqual(new SEquatableEdge <int>(1, 1), graph.OutEdge(1, 0)); Assert.AreEqual(new SEquatableEdge <int>(1, 3), graph.OutEdge(1, 2)); Assert.AreEqual(new SEquatableEdge <int>(2, 4), graph.OutEdge(2, 0)); Assert.AreEqual(new SEquatableEdge <int>(3, 3), graph.OutEdge(3, 0)); Assert.AreEqual(new SEquatableEdge <int>(4, 1), graph.OutEdge(4, 0)); }
protected static void AdjacentEdge_ImmutableGraph_Test( IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph) { var edge11 = new Edge <int>(1, 1); var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge41 = new Edge <int>(4, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge11, edge12, edge13, edge24, edge33, edge41 }); IImplicitUndirectedGraph <int, Edge <int> > graph = createGraph(); Assert.AreSame(edge11, graph.AdjacentEdge(1, 0)); Assert.AreSame(edge13, graph.AdjacentEdge(1, 2)); Assert.AreSame(edge41, graph.AdjacentEdge(1, 3)); Assert.AreSame(edge13, graph.AdjacentEdge(3, 0)); Assert.AreSame(edge33, graph.AdjacentEdge(3, 1)); Assert.AreSame(edge24, graph.AdjacentEdge(4, 0)); }
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 TryGetEdge_ImmutableGraph_UndirectedGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitUndirectedGraph <int, Edge <int> > > createGraph) { var edge1 = new Edge <int>(1, 2); var edge2 = new Edge <int>(1, 2); var edge3 = new Edge <int>(1, 3); var edge4 = new Edge <int>(2, 2); var edge5 = new Edge <int>(2, 4); var edge6 = new Edge <int>(3, 1); var edge7 = new Edge <int>(5, 2); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6, edge7 }); var graph = createGraph(); Assert.IsFalse(graph.TryGetEdge(0, 10, out _)); Assert.IsFalse(graph.TryGetEdge(0, 1, out _)); Assert.IsTrue(graph.TryGetEdge(2, 4, out Edge <int> gotEdge)); Assert.AreSame(edge5, gotEdge); Assert.IsTrue(graph.TryGetEdge(1, 2, out gotEdge)); Assert.AreSame(edge1, gotEdge); Assert.IsTrue(graph.TryGetEdge(2, 2, out gotEdge)); Assert.AreSame(edge4, gotEdge); // 1 -> 2 is present in this undirected graph Assert.IsTrue(graph.TryGetEdge(2, 1, out gotEdge)); Assert.AreSame(edge1, gotEdge); Assert.IsTrue(graph.TryGetEdge(5, 2, out gotEdge)); Assert.AreSame(edge7, gotEdge); // 5 -> 2 is present in this undirected graph Assert.IsTrue(graph.TryGetEdge(2, 5, out gotEdge)); Assert.AreSame(edge7, gotEdge); }
protected static void OutEdges_ImmutableGraph_ReversedTest( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IImplicitGraph <int, SReversedEdge <int, Edge <int> > > > createGraph) { var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge33 = new Edge <int>(3, 3); var edge34 = new Edge <int>(3, 4); wrappedGraph.AddVertex(1); IImplicitGraph <int, SReversedEdge <int, Edge <int> > > graph = createGraph(); AssertNoOutEdge(graph, 1); wrappedGraph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge33, edge34 }); graph = createGraph(); AssertNoOutEdge(graph, 1); AssertHasReversedOutEdges(graph, 2, new[] { edge12 }); AssertHasReversedOutEdges(graph, 3, new[] { edge13, edge33 }); AssertHasReversedOutEdges(graph, 4, new[] { edge14, edge24, edge34 }); }
protected static void RemoveVertex_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > graph) { int verticesRemoved = 0; int edgesRemoved = 0; graph.VertexRemoved += v => { Assert.IsNotNull(v); // ReSharper disable once AccessToModifiedClosure ++verticesRemoved; }; graph.EdgeRemoved += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesRemoved; }; var edge12 = new Edge <int>(1, 2); var edge13 = new Edge <int>(1, 3); var edge14 = new Edge <int>(1, 4); var edge24 = new Edge <int>(2, 4); var edge31 = new Edge <int>(3, 1); var edge33 = new Edge <int>(3, 3); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge31, edge33 }); Assert.IsFalse(graph.RemoveVertex(5)); CheckCounters(0, 0); Assert.IsTrue(graph.RemoveVertex(3)); CheckCounters(1, 3); AssertHasVertices(graph, new[] { 1, 2, 4 }); AssertHasEdges(graph, new[] { edge12, edge14, edge24 }); Assert.IsTrue(graph.RemoveVertex(1)); CheckCounters(1, 2); AssertHasVertices(graph, new[] { 2, 4 }); AssertHasEdges(graph, new[] { edge24 }); Assert.IsTrue(graph.RemoveVertex(2)); CheckCounters(1, 1); AssertHasVertices(graph, new[] { 4 }); AssertNoEdge(graph); Assert.IsTrue(graph.RemoveVertex(4)); CheckCounters(1, 0); AssertEmptyGraph(graph); #region Local function void CheckCounters(int expectedRemovedVertices, int expectedRemovedEdges) { Assert.AreEqual(expectedRemovedVertices, verticesRemoved); Assert.AreEqual(expectedRemovedEdges, edgesRemoved); verticesRemoved = 0; edgesRemoved = 0; } #endregion }
protected static void RemoveEdge_EquatableEdge_Test( [NotNull] IMutableVertexAndEdgeSet <int, EquatableEdge <int> > graph) { int verticesRemoved = 0; int edgesRemoved = 0; graph.VertexRemoved += v => { Assert.IsNotNull(v); ++verticesRemoved; }; graph.EdgeRemoved += e => { Assert.IsNotNull(e); // ReSharper disable once AccessToModifiedClosure ++edgesRemoved; }; var edge12 = new EquatableEdge <int>(1, 2); var edge13 = new EquatableEdge <int>(1, 3); var edge13Bis = new EquatableEdge <int>(1, 3); var edge14 = new EquatableEdge <int>(1, 4); var edge24 = new EquatableEdge <int>(2, 4); var edge31 = new EquatableEdge <int>(3, 1); var edge33 = new EquatableEdge <int>(3, 3); var edgeNotInGraph = new EquatableEdge <int>(3, 4); var edgeWithVertexNotInGraph1 = new EquatableEdge <int>(2, 10); var edgeWithVertexNotInGraph2 = new EquatableEdge <int>(10, 2); var edgeWithVerticesNotInGraph = new EquatableEdge <int>(10, 11); var edgeEquatable = new EquatableEdge <int>(1, 2); graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge13Bis, edge14, edge24, edge31, edge33 }); Assert.IsFalse(graph.RemoveEdge(edgeNotInGraph)); CheckCounters(0); Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph1)); CheckCounters(0); Assert.IsFalse(graph.RemoveEdge(edgeWithVertexNotInGraph2)); CheckCounters(0); Assert.IsFalse(graph.RemoveEdge(edgeWithVerticesNotInGraph)); CheckCounters(0); Assert.IsTrue(graph.RemoveEdge(edgeEquatable)); CheckCounters(1); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge13, edge13Bis, edge14, edge24, edge31, edge33 }); Assert.IsTrue(graph.RemoveEdge(edge13Bis)); CheckCounters(1); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge13, edge14, edge24, edge31, edge33 }); Assert.IsTrue(graph.RemoveEdge(edge31)); CheckCounters(1); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertHasEdges(graph, new[] { edge13, edge14, edge24, edge33 }); Assert.IsTrue(graph.RemoveEdge(edge13)); Assert.IsTrue(graph.RemoveEdge(edge14)); Assert.IsTrue(graph.RemoveEdge(edge24)); Assert.IsTrue(graph.RemoveEdge(edge33)); CheckCounters(4); AssertHasVertices(graph, new[] { 1, 2, 3, 4 }); AssertNoEdge(graph); #region Local function void CheckCounters(int expectedRemovedEdges) { Assert.AreEqual(0, verticesRemoved); Assert.AreEqual(expectedRemovedEdges, edgesRemoved); edgesRemoved = 0; } #endregion }