Skip to content

Commit

Permalink
Merge pull request #8 from FaronBracy/FixSpelling
Browse files Browse the repository at this point in the history
Code cleanup and additional unit tests
  • Loading branch information
FaronBracy authored Jan 1, 2019
2 parents 4cac168 + 9ea3a90 commit d20f757
Show file tree
Hide file tree
Showing 84 changed files with 985 additions and 127 deletions.
95 changes: 95 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Rules in this file were initially inferred by Visual Studio IntelliCode from the RogueSharp codebase based on best match to current usage at 12/9/2018
# You can modify the rules from these initially generated values to suit your own policies
# You can learn more about editorconfig here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
[*.cs]

#Core editorconfig formatting - indentation

#use soft tabs (spaces) for indentation
indent_style = space
#use 3 spaces for indentation
indent_size = 3
#remove whitespace characters preceding newline characters
trim_trailing_whitespace = true

#Formatting - indentation options

#do not indent switch labels
csharp_indent_switch_labels = false

#Formatting - new line options

#require braces to be on a new line for properties, control_blocks, methods, and types (also known as "Allman" style)
csharp_new_line_before_open_brace = properties, control_blocks, methods, types

#Formatting - organize using options

#sort System.* using directives alphabetically, and place them before other usings
dotnet_sort_system_directives_first = true

#Formatting - spacing options

#require a space between a cast and the value
csharp_space_after_cast = true
#require a space before the colon for bases or interfaces in a type declaration
csharp_space_after_colon_in_inheritance_clause = true
#require a space after a keyword in a control flow statement such as a for loop
csharp_space_after_keywords_in_control_flow_statements = true
#require a space before the colon for bases or interfaces in a type declaration
csharp_space_before_colon_in_inheritance_clause = true
#remove space within empty argument list parentheses
csharp_space_between_method_call_empty_parameter_list_parentheses = false
#remove space between method call name and opening parenthesis
csharp_space_between_method_call_name_and_opening_parenthesis = false
#place a space character after the opening parenthesis and before the closing parenthesis of a method call
csharp_space_between_method_call_parameter_list_parentheses = true
#remove space within empty parameter list parentheses for a method declaration
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
#do not place a space character after the opening parenthesis and before the closing parenthesis of a method declaration parameter list.
csharp_space_between_method_declaration_parameter_list_parentheses = true

#Formatting - wrapping options

#leave code block on single line
csharp_preserve_single_line_blocks = true

#Style - expression bodied member options

#prefer block bodies for accessors
csharp_style_expression_bodied_accessors = false:suggestion
#prefer block bodies for constructors
csharp_style_expression_bodied_constructors = false:suggestion
#prefer block bodies for methods
csharp_style_expression_bodied_methods = false:suggestion
#prefer block bodies for operators
csharp_style_expression_bodied_operators = false:suggestion
#prefer block bodies for properties
csharp_style_expression_bodied_properties = false:suggestion

#Style - expression level options

#prefer out variables to be declared before the method call
csharp_style_inlined_variable_declaration = false:suggestion
#prefer the language keyword for member access expressions, instead of the type name, for types that have a keyword to represent them
dotnet_style_predefined_type_for_member_access = true:suggestion

#Style - implicit and explicit types

#prefer explicit type over var to declare variables with built-in system types such as int
csharp_style_var_for_built_in_types = false:suggestion
#prefer explicit type over var when the type is already mentioned on the right-hand side of a declaration
csharp_style_var_when_type_is_apparent = false:suggestion

#Style - language keyword and framework type options

#prefer the language keyword for local variables, method parameters, and class members, instead of the type name, for types that have a keyword to represent them
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion

#Style - qualification options

#prefer fields not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_field = false:suggestion
#prefer methods not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_method = false:suggestion
#prefer properties not to be prefaced with this. or Me. in Visual Basic
dotnet_style_qualification_for_property = false:suggestion
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 - 2017 Faron Bracy
https://bitbucket.org/FaronBracy/roguesharp
Copyright (c) 2014 - 2019 Faron Bracy
https://github.com/FaronBracy/RogueSharp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
[![Build status](https://ci.appveyor.com/api/projects/status/mx09mla59wsgrkkj?svg=true)](https://ci.appveyor.com/project/FaronBracy/roguesharp-20n28)
[![Nuget](https://buildstats.info/nuget/roguesharp)](https://www.nuget.org/packages/RogueSharp)

[![Test status](https://img.shields.io/azure-devops/tests/dreamersdesign/RogueSharp/1.svg)](https://dreamersdesign.visualstudio.com/RogueSharp/_build/latest?definitionId=1)
[![Code coverage](https://img.shields.io/azure-devops/coverage/dreamersdesign/RogueSharp/1.svg)](https://dreamersdesign.visualstudio.com/RogueSharp/_build/latest?definitionId=1)

[![Build History](https://buildstats.info/appveyor/chart/FaronBracy/roguesharp-20n28)](https://ci.appveyor.com/project/FaronBracy/roguesharp-20n28/history)

# What is RogueSharp? #
Expand Down Expand Up @@ -103,7 +106,7 @@ A more interesting way to create a map is to use the `Map` class's static method

The MIT License (MIT)

Copyright (c) 2014 - 2018 Faron Bracy
Copyright (c) 2014 - 2019 Faron Bracy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
74 changes: 74 additions & 0 deletions RogueSharp.Test/Algorithms/DepthFirstPathsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RogueSharp.Algorithms;

namespace RogueSharp.Test.Algorithms
{
[TestClass]
public class DepthFirstPathsTest
{
[TestMethod]
public void Constructor_WhenGraphIsNull_WillThrowArgumentException()
{
Assert.ThrowsException<ArgumentException>( () => new DepthFirstPaths( null, 3 ) );
}

[TestMethod]
public void HasPathTo_WhenPathExistsBetweenVertices_WillReturnTrue()
{
Graph graph = new Graph( 5 );
graph.AddEdge( 0, 1 );
graph.AddEdge( 1, 2 );
graph.AddEdge( 1, 4 );
graph.AddEdge( 2, 3 );
graph.AddEdge( 3, 4 );
DepthFirstPaths paths = new DepthFirstPaths( graph, 3 );

Assert.IsTrue( paths.HasPathTo( 0 ) );
}

[TestMethod]
public void HasPathTo_WhenPathDoesNotExistBetweenVertices_WillReturnFalse()
{
Graph graph = new Graph( 5 );
graph.AddEdge( 0, 1 );
graph.AddEdge( 1, 2 );
graph.AddEdge( 1, 4 );
DepthFirstPaths paths = new DepthFirstPaths( graph, 0 );

Assert.IsFalse( paths.HasPathTo( 3 ) );
}

[TestMethod]
public void PathTo_WhenPathExistsBetweenVertices_WillReturnVerticesInPath()
{
Graph graph = new Graph( 5 );
graph.AddEdge( 0, 1 );
graph.AddEdge( 1, 2 );
graph.AddEdge( 1, 4 );
graph.AddEdge( 2, 3 );
graph.AddEdge( 3, 4 );
DepthFirstPaths paths = new DepthFirstPaths( graph, 3 );

int[] pathVertices = paths.PathTo( 0 ).ToArray();

Assert.AreEqual( pathVertices[0], 2 );
Assert.AreEqual( pathVertices[1], 1 );
Assert.AreEqual( pathVertices[2], 0 );
Assert.AreEqual( pathVertices.Length, 3 );
}

[TestMethod]
public void PathTo_WhenPathDoesNotExistBetweenVertices_WillReturnNull()
{
Graph graph = new Graph( 5 );
graph.AddEdge( 0, 1 );
graph.AddEdge( 1, 2 );
graph.AddEdge( 1, 4 );
DepthFirstPaths paths = new DepthFirstPaths( graph, 0 );

Assert.IsNull( paths.PathTo( 3 ) );
}
}
}
174 changes: 174 additions & 0 deletions RogueSharp.Test/Algorithms/DijkstraShortestPathTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RogueSharp.Algorithms;

namespace RogueSharp.Test.Algorithms
{
[TestClass]
public class DijkstraShortestPathTest
{
[TestMethod]
public void Constructor_WhenGraphIsNull_WillThrowArgumentNullException()
{
Assert.ThrowsException<ArgumentNullException>( () => new DijkstraShortestPath( null, 0 ) );
}

[TestMethod]
public void Constructor_WhenGraphHasEdgesWithNegativeWeights_WillThrowArgumentOutOfRangeException()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 2 );
digraph.AddEdge( new DirectedEdge( 0, 1, -1.5 ) );

Assert.ThrowsException<ArgumentOutOfRangeException>( () => new DijkstraShortestPath( digraph, 0 ) );
}

[TestMethod]
public void HasPathTo_WhenPathExistsBetween0And4_WillBeTrue()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 0 );

Assert.IsTrue( dijkstra.HasPathTo( 4 ) );
}

[TestMethod]
public void HasPathTo_WhenPathDoesNotExistBetween4And0_WillBeFalse()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 4 );

Assert.IsFalse( dijkstra.HasPathTo( 0 ) );
}

[TestMethod]
public void DistanceTo_WhenPathExistsBetween0And4_WillBeSumOfWeightsInPath()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 0 );

Assert.AreEqual( 9, dijkstra.DistanceTo( 4 ) );
}

[TestMethod]
public void DistanceTo_WhenMultiplePathsExistBetween0And4_WillBeSumOfWeightsInShortestPath()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 0 );

Assert.AreEqual( 7.25, dijkstra.DistanceTo( 4 ) );
}

[TestMethod]
public void DistanceTo_WhenNoPathExists_WillBePositiveInfinity()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 4 );

Assert.AreEqual( double.PositiveInfinity, dijkstra.DistanceTo( 0 ) );
}

[TestMethod]
public void PathTo_WhenMultiplePathsExistBetween0And4_WillBeShortestPath()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 0 );

DirectedEdge[] path = dijkstra.PathTo( 4 ).ToArray();

Assert.AreEqual( "From: 0, To: 1, Weight: 2.5", path[0].ToString() );
Assert.AreEqual( "From: 1, To: 4, Weight: 4.75", path[1].ToString() );
Assert.AreEqual( 2, path.Length );
}

[TestMethod]
public void PathTo_WhenNoPathExists_WillBeNull()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 4 );

IEnumerable<DirectedEdge> path = dijkstra.PathTo( 0 );

Assert.IsNull( path );
}

[TestMethod]
public void FindPath_WhenMultiplePathsExistBetween0And4_WillBeShortestPath()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );

DirectedEdge[] path = DijkstraShortestPath.FindPath( digraph, 0, 4 ).ToArray();

Assert.AreEqual( "From: 0, To: 1, Weight: 2.5", path[0].ToString() );
Assert.AreEqual( "From: 1, To: 4, Weight: 4.75", path[1].ToString() );
Assert.AreEqual( 2, path.Length );
}

[TestMethod]
public void FindPath_WhenNoPathExists_WillBeNull()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );

IEnumerable<DirectedEdge> path = DijkstraShortestPath.FindPath( digraph, 4, 0 );

Assert.IsNull( path );
}

[TestMethod]
public void Check_WhenGivenValidGraph_WillBeTrue()
{
EdgeWeightedDigraph digraph = new EdgeWeightedDigraph( 5 );
digraph.AddEdge( new DirectedEdge( 0, 1, 2.5 ) );
digraph.AddEdge( new DirectedEdge( 1, 2, 3.25 ) );
digraph.AddEdge( new DirectedEdge( 1, 4, 4.75 ) );
digraph.AddEdge( new DirectedEdge( 2, 3, 1.25 ) );
digraph.AddEdge( new DirectedEdge( 3, 4, 2 ) );
DijkstraShortestPath dijkstra = new DijkstraShortestPath( digraph, 0 );

Assert.IsTrue( dijkstra.Check( digraph, 0 ) );
}
}
}
Loading

0 comments on commit d20f757

Please sign in to comment.