Skip to content

Commit

Permalink
#H5IS
Browse files Browse the repository at this point in the history
Fixed a bug in one of the "GetDistance" overloads in the new "DistanceUtils" class
  • Loading branch information
abjerner committed Apr 27, 2019
1 parent b833484 commit 1320675
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Skybrud.Essentials/Maps/DistanceUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static double GetDistance(IPoint loc1, IPoint loc2, double radius) {
/// <param name="lng2">The longitude of the second location.</param>
/// <returns>The distance in metres between the two locations.</returns>
public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
return GetDistance(lat1, lng2, lat2, lng2, EquatorialRadius);
return GetDistance(lat1, lng1, lat2, lng2, EquatorialRadius);
}

/// <summary>
Expand Down
7 changes: 3 additions & 4 deletions src/UnitTestProject1/Locations/LocationTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Skybrud.Essentials.Locations;

Expand Down Expand Up @@ -29,8 +28,8 @@ public void GetDistance() {

string format = "{0:0." + ("".PadLeft(sample.Decimals, '0')) + "}";

Assert.AreEqual(sample.Expected, String.Format(CultureInfo.InvariantCulture, format, LocationHelper.GetDistance(sample.From, sample.To) / 1000));
Assert.AreEqual(sample.Expected, String.Format(CultureInfo.InvariantCulture, format, LocationUtils.GetDistance(sample.From, sample.To) / 1000));
Assert.AreEqual(sample.Expected, string.Format(CultureInfo.InvariantCulture, format, LocationHelper.GetDistance(sample.From, sample.To) / 1000), "#1");
Assert.AreEqual(sample.Expected, string.Format(CultureInfo.InvariantCulture, format, LocationUtils.GetDistance(sample.From, sample.To) / 1000), "#2");

}

Expand Down
46 changes: 46 additions & 0 deletions src/UnitTestProject1/Maps/DistanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Skybrud.Essentials.Maps;
using Skybrud.Essentials.Maps.Shapes;

namespace UnitTestProject1.Maps {

[TestClass]
public class DistanceTests {

#pragma warning disable 618

[TestMethod]
public void GetDistance() {

// Values used for comparison comes from lines plotted into Google Maps. Google uses a varying number
// decimals depending on the distance, so we loose a bit precision when rounding. But the results of this
// unit test still shows that the method works (eg. the test will fail if using the mean radius rather than
// the equatorial radius).

var samples = new[] {
new { From = new Point(55.6946159, 10.0366974), To = new Point(55.6477614, 10.1589203), Expected = "9.28", Decimals = 2 },
new { From = new Point(54.8671242, 7.7124023), To = new Point(56.8159142, 12.2113037), Expected = "355", Decimals = 0 },
new { From = new Point(49.2104204, -6.1083984), To = new Point(59.578851, 22.9833984), Expected = "2184", Decimals = 0 },
new { From = new Point(12.3829283, -71.3671875), To = new Point(71.2443555, 25.4882813), Expected = "8958", Decimals = 0 }
};

foreach (var sample in samples) {

string format = "{0:0." + ("".PadLeft(sample.Decimals, '0')) + "}";

double result1 = DistanceUtils.GetDistance(sample.From, sample.To);
double result2 = DistanceUtils.GetDistance(sample.From.Latitude, sample.From.Longitude, sample.To.Latitude, sample.To.Longitude);

Assert.AreEqual(sample.Expected, string.Format(CultureInfo.InvariantCulture, format, result1 / 1000), "#1");
Assert.AreEqual(sample.Expected, string.Format(CultureInfo.InvariantCulture, format, result2 / 1000), "#2");

}

}

#pragma warning restore 618

}

}
1 change: 1 addition & 0 deletions src/UnitTestProject1/UnitTestProject1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Json\Converters\EssentialsPartialDateConverterTests.cs" />
<Compile Include="Json\JsonTests.cs" />
<Compile Include="Json\JObjectTests.cs" />
<Compile Include="Maps\DistanceTests.cs" />
<Compile Include="Locations\LocationTests.cs" />
<Compile Include="Security\SecurityTests.cs" />
<Compile Include="Strings\StringTests.cs" />
Expand Down

0 comments on commit 1320675

Please sign in to comment.