|
| 1 | +// <copyright file="GraphContractionTest.cs" company="Jonathan Hough"> |
| 2 | +// Copyright (C) 2023 Jonathan Hough. |
| 3 | +// Copyright Licensed under the MIT license. |
| 4 | +// See LICENSE file in the samples root for full license information. |
| 5 | +// </copyright> |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace SharpGraph |
| 12 | +{ |
| 13 | + public class GraphContractionTest |
| 14 | + { |
| 15 | + [Theory] |
| 16 | + [InlineData(6, "1", "2", 5, 10, 4)] |
| 17 | + [InlineData(10, "2", "6", 9, 36, 8)] |
| 18 | + public void TestContractEdgeFromComplete( |
| 19 | + uint totalNodeCount, |
| 20 | + string nodeToKeep, |
| 21 | + string nodeToRemove, |
| 22 | + int expectedNumberOfNodes, |
| 23 | + int expectedNumberOfEdges, |
| 24 | + int expectedAdjacentToKeptNode |
| 25 | + ) |
| 26 | + { |
| 27 | + var edgeToRemove = new Edge(nodeToKeep, nodeToRemove); |
| 28 | + var g = GraphGenerator.CreateComplete(totalNodeCount); |
| 29 | + var h = g.ContractEdge(edgeToRemove, new Node(nodeToKeep)); |
| 30 | + |
| 31 | + Assert.Equal(expectedNumberOfNodes, h.GetNodes().Count); |
| 32 | + Assert.Equal(expectedNumberOfEdges, h.GetEdges().Count); |
| 33 | + Assert.Equal(expectedAdjacentToKeptNode, h.GetAdjacent(new Node(nodeToKeep)).Count); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void TestContractionKeepsComponents() |
| 38 | + { |
| 39 | + var e1 = new Edge("1", "2"); |
| 40 | + var e2 = new Edge("2", "3"); |
| 41 | + var e3 = new Edge("2", "4"); |
| 42 | + var e4 = new Edge("5", "8"); |
| 43 | + var edges = new List<Edge> { e1, e2, e3, e4 }; |
| 44 | + var g = new Graph(edges); |
| 45 | + foreach (var e in g.GetEdges()) |
| 46 | + { |
| 47 | + g.AddComponent<EdgeDirection>(e); |
| 48 | + } |
| 49 | + |
| 50 | + var h = g.ContractEdge(e1, new Node("1")); |
| 51 | + |
| 52 | + Assert.Equal(5, h.GetNodes().Count); |
| 53 | + Assert.Equal(3, h.GetEdges().Count); |
| 54 | + Assert.Equal(2, h.GetAdjacent(new Node("1")).Count); |
| 55 | + |
| 56 | + // edge that was not updated should have kept the component |
| 57 | + Assert.True(h.HasComponent<EdgeDirection>(e4)); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments