コード例 #1
0
        public void NoExceptionThrown_WhenVertexIsAccessibleFromRoot()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph = new[] { new Edge <int>(1, 2) },
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = true
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            Assert.DoesNotThrow(() => { double _ = algorithm.GetDistance(2); });
        }
コード例 #2
0
        public void ExceptionThrown_WhenAlgorithmHasNotYetBeenComputed()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph               = new[] { new Edge <int>(1, 2) },
                SingleVerticesInGraph      = new int[0],
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = false
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            Assert.Throws <InvalidOperationException>(() => algorithm.TryGetDistance(2, out _));
        }
コード例 #3
0
        public void NoExceptionThrown_WhenVertexExistsButIsInaccessibleFromRoot()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph               = new[] { new Edge <int>(1, 2) },
                SingleVerticesInGraph      = new[] { 3 },
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = true
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            double _ = algorithm.GetDistance(3);
        }
コード例 #4
0
        public void DistanceReturned_WhenVertexIsAccessibleFromRoot()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph = new[] { new Edge <int>(1, 2) },
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = true
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            bool distanceFound = algorithm.TryGetDistance(2, out _);

            Assert.True(distanceFound, "Distance should have been found since the vertex is accessible from root.");
        }
コード例 #5
0
        public void NoDistanceFound_WhenVertexDoesNotExistInGraph()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph = new[] { new Edge <int>(1, 2) },
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = true
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            bool distanceFound = algorithm.TryGetDistance(3, out _);

            Assert.False(distanceFound, "No distance should have been found since the vertex does not exist.");
        }
コード例 #6
0
        public void ExceptionThrown_WhenTargetVertexIsNull()
        {
            var scenario = new ContractScenario <string>
            {
                EdgesInGraph               = new[] { new Edge <string>("1", "2") },
                SingleVerticesInGraph      = new string[0],
                AccessibleVerticesFromRoot = new[] { "2" },
                Root          = "1",
                DoComputation = false
            };

            IDistancesCollection <string> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            // ReSharper disable once AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => { double _ = algorithm.GetDistance(null); });
        }
コード例 #7
0
        public void EmptyCollectionReturned_WhenAlgorithmHasNotYetBeenRun()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph               = new[] { new Edge <int>(1, 2) },
                SingleVerticesInGraph      = new[] { 3 },
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = false
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            IEnumerable <KeyValuePair <int, double> > distances = algorithm.GetDistances();

            CollectionAssert.IsEmpty(distances);
        }
コード例 #8
0
        public void DistancesForAllVerticesInGraphReturnedWhenAlgorithmHasBeenRun()
        {
            var scenario = new ContractScenario <int>
            {
                EdgesInGraph               = new[] { new Edge <int>(1, 2) },
                SingleVerticesInGraph      = new[] { 3 },
                AccessibleVerticesFromRoot = new[] { 2 },
                Root          = 1,
                DoComputation = true
            };

            IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario);

            IEnumerable <KeyValuePair <int, double> > distances = algorithm.GetDistances();

            CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, distances.Select(pair => pair.Key));
        }