Skip to content

Commit d95f0bc

Browse files
committed
desing changes, code coverage
1 parent 19484c1 commit d95f0bc

11 files changed

Lines changed: 99 additions & 15 deletions

src/PolylineAlgorithm/Abstraction/AbstractPolylineEncoder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public TPolyline Encode(ReadOnlySpan<TCoordinate> coordinates) {
7878

7979
ValidateEmptyCoordinates(ref coordinates, _logger);
8080

81-
CoordinateVariance variance = new();
81+
CoordinateDelta variance = new();
8282

8383
int position = 0;
8484
int consumed = 0;
@@ -126,7 +126,7 @@ public TPolyline Encode(ReadOnlySpan<TCoordinate> coordinates) {
126126
return CreatePolyline(buffer[..position].ToString().AsMemory());
127127

128128
[MethodImpl(MethodImplOptions.AggressiveInlining)]
129-
static int GetRequiredLength(CoordinateVariance variance) =>
129+
static int GetRequiredLength(CoordinateDelta variance) =>
130130
PolylineEncoding.GetCharCount(variance.Latitude) + PolylineEncoding.GetCharCount(variance.Longitude);
131131

132132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -163,7 +163,7 @@ static void ValidateEmptyCoordinates(ref ReadOnlySpan<TCoordinate> coordinates,
163163
}
164164

165165
[MethodImpl(MethodImplOptions.AggressiveInlining)]
166-
static void ValidateBuffer(CoordinateVariance variance, int position, Span<char> buffer, ILogger logger) {
166+
static void ValidateBuffer(CoordinateDelta variance, int position, Span<char> buffer, ILogger logger) {
167167
if (GetRemainingBufferSize(position, buffer.Length) < GetRequiredLength(variance)) {
168168
var requiredLength = GetRequiredLength(variance);
169169

src/PolylineAlgorithm/Coordinate.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
namespace PolylineAlgorithm;
77

8+
using PolylineAlgorithm.Internal;
89
using PolylineAlgorithm.Properties;
910
using System;
1011
using System.Diagnostics;
1112
using System.Globalization;
1213
using System.Runtime.InteropServices;
14+
using System.Text.Json.Serialization;
1315
#if NET8_0_OR_GREATER
1416
using System.Text;
1517
#endif

src/PolylineAlgorithm/CoordinateValueType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public enum CoordinateValueType {
1212
/// <summary>
1313
/// Represents no specific type. This value is used when the type is not applicable or not specified.
1414
/// </summary>
15-
None = 0,
15+
Unspecified = 0,
1616
/// <summary>
1717
/// Represents a latitude value.
1818
/// </summary>

src/PolylineAlgorithm/Internal/CoordinateVariance.cs renamed to src/PolylineAlgorithm/Internal/CoordinateDelta.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ namespace PolylineAlgorithm.Internal;
1414
/// </summary>
1515
[DebuggerDisplay("{ToString(),nq}")]
1616
[StructLayout(LayoutKind.Auto)]
17-
internal struct CoordinateVariance {
17+
internal struct CoordinateDelta {
1818
private (int Latitude, int Longitude) _current;
1919

2020
/// <summary>
21-
/// Initializes a new instance of the <see cref="CoordinateVariance"/> struct with the default latitude and longitude deltas.
21+
/// Initializes a new instance of the <see cref="CoordinateDelta"/> struct with the default latitude and longitude deltas.
2222
/// </summary>
23-
public CoordinateVariance() {
23+
public CoordinateDelta() {
2424
_current = (default, default);
2525
}
2626

src/PolylineAlgorithm/PolylineAlgorithm.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
4545
</PackageReference>
4646
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="[6.0.0,11.0.0)" />
47+
<PackageReference Include="System.Text.Json" Version="10.0.5" />
4748
</ItemGroup>
4849

4950
<ItemGroup>

src/PolylineAlgorithm/PolylineEncoding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static bool TryReadValue(ref int variance, ReadOnlyMemory<char> buffer, r
102102

103103
public static double Denormalize(int value, CoordinateValueType type) {
104104
// Validate that the type is not None, as it does not represent a valid coordinate value type.
105-
if (type == CoordinateValueType.None) {
105+
if (type == CoordinateValueType.Unspecified) {
106106
throw new ArgumentOutOfRangeException(nameof(type), string.Format(CultureInfo.InvariantCulture, _argumentCannotBeCoordinateValueTypeMessageFormat, type.ToString()));
107107
}
108108

@@ -194,7 +194,7 @@ public static bool TryWriteValue(int variance, Span<char> buffer, ref int positi
194194

195195
public static int Normalize(double value, CoordinateValueType type) {
196196
// Validate that the type is not None, as it does not represent a valid coordinate value type.
197-
if (type == CoordinateValueType.None) {
197+
if (type == CoordinateValueType.Unspecified) {
198198
throw new ArgumentOutOfRangeException(nameof(type), string.Format(CultureInfo.InvariantCulture, _argumentCannotBeCoordinateValueTypeMessageFormat, type.ToString()));
199199
}
200200

tests/PolylineAlgorithm.Tests/CoordinateTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ public void Constructor_Boundary_Values_Ok() {
185185
Coordinate max = new(MaxLatitude, MaxLongitude);
186186

187187
// Assert
188+
Assert.IsFalse(min.IsDefault());
189+
Assert.IsFalse(max.IsDefault());
188190
Assert.AreEqual(MinLatitude, min.Latitude);
189191
Assert.AreEqual(-MaxLongitude, min.Longitude);
190192
Assert.AreEqual(MaxLatitude, max.Latitude);
@@ -250,4 +252,30 @@ public void Equality_Operators_Ok() {
250252
Assert.IsTrue(coordinate != notEqualCoordinate);
251253
Assert.IsFalse(coordinate == notEqualCoordinate);
252254
}
255+
256+
/// <summary>
257+
/// Tests the <see cref="Coordinate.GetHashCode"/> method for equal coordinates.
258+
/// </summary>
259+
[TestMethod]
260+
public void GetHashCode_EqualCoordinates_SameHash() {
261+
// Arrange
262+
var c1 = new Coordinate(90, 180);
263+
var c2 = new Coordinate(90, 180);
264+
265+
// Act & Assert
266+
Assert.AreEqual(c1.GetHashCode(), c2.GetHashCode());
267+
}
268+
269+
/// <summary>
270+
/// Tests the <see cref="Coordinate.GetHashCode"/> method for different coordinates.
271+
/// </summary>
272+
[TestMethod]
273+
public void GetHashCode_DifferentCoordinates_DifferentHash() {
274+
// Arrange
275+
var c1 = new Coordinate(90, 180);
276+
var c2 = new Coordinate(-90, -180);
277+
278+
// Act & Assert
279+
Assert.AreNotEqual(c1.GetHashCode(), c2.GetHashCode());
280+
}
253281
}

tests/PolylineAlgorithm.Tests/Internal/CoordinateVarianceTest.cs renamed to tests/PolylineAlgorithm.Tests/Internal/CoordinateDeltaTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PolylineAlgorithm.Tests.Internal;
99
using System.Globalization;
1010

1111
[TestClass]
12-
public class CoordinateVarianceTest {
12+
public class CoordinateDeltaTest {
1313
public static IEnumerable<(int Latitude, int Longitude)> Coordinates => [
1414
(0, 0),
1515
(-10, -10),
@@ -42,7 +42,7 @@ public class CoordinateVarianceTest {
4242
[TestMethod]
4343
public void Constructor_Sets_Defaults() {
4444
// Arrange & Act
45-
CoordinateVariance variance = new();
45+
CoordinateDelta variance = new();
4646
// Assert
4747
Assert.AreEqual(0, variance.Latitude);
4848
Assert.AreEqual(0, variance.Longitude);
@@ -52,7 +52,7 @@ public void Constructor_Sets_Defaults() {
5252
[DynamicData(nameof(Coordinates))]
5353
public void Next_Calculates_Correct_Variance_From_Default_Variance(int latitude, int longitude) {
5454
// Arrange
55-
CoordinateVariance variance = new();
55+
CoordinateDelta variance = new();
5656
var expected = (latitude, longitude);
5757

5858
// Act
@@ -67,7 +67,7 @@ public void Next_Calculates_Correct_Variance_From_Default_Variance(int latitude,
6767
[DynamicData(nameof(Variances))]
6868
public void Next_Calculates_Correct_Variance_From_Previous_Variance((int Latitude, int Longitude) initial, (int Latitude, int Longitude) next, (int Latitude, int Longitude) expected) {
6969
// Arrange
70-
CoordinateVariance variance = new();
70+
CoordinateDelta variance = new();
7171
variance.Next(initial.Latitude, initial.Longitude);
7272

7373
// Act
@@ -82,7 +82,7 @@ public void Next_Calculates_Correct_Variance_From_Previous_Variance((int Latitud
8282
[DynamicData(nameof(Coordinates))]
8383
public void ToString_Returns_Value_Containing_Variance(int latitude, int longitude) {
8484
// Arrange
85-
CoordinateVariance variance = new();
85+
CoordinateDelta variance = new();
8686
variance.Next(latitude, longitude);
8787

8888
// Act

tests/PolylineAlgorithm.Tests/PolylineEncoderExtensionsTest.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,20 @@ public void Encode_Null_Encoder_Empty_List_Throws_ArgumentNullException() {
3030
}
3131

3232
[TestMethod]
33-
public void Encode_Null_Coordinates_Throws_ArgumentNullException() {
33+
public void Encode_Null_Encoder_Empty_Array_Throws_ArgumentNullException() {
34+
// Arrange
35+
static void Encode() => PolylineEncoderExtensions.Encode<Coordinate, Polyline>(null!, Array.Empty<Coordinate>());
36+
37+
// Act
38+
var exception = Assert.ThrowsExactly<ArgumentNullException>(Encode);
39+
40+
// Assert
41+
Assert.AreEqual("encoder", exception.ParamName);
42+
Assert.IsTrue(exception.Message.Contains("Value cannot be null.", StringComparison.Ordinal));
43+
}
44+
45+
[TestMethod]
46+
public void Encode_Null_List_Throws_ArgumentNullException() {
3447
// Arrange
3548
void Encode() => PolylineEncoderExtensions.Encode(_encoder, (List<Coordinate>)null!);
3649

@@ -42,6 +55,19 @@ public void Encode_Null_Coordinates_Throws_ArgumentNullException() {
4255
Assert.IsTrue(exception.Message.Contains("Value cannot be null.", StringComparison.Ordinal));
4356
}
4457

58+
[TestMethod]
59+
public void Encode_Null_Array_Throws_ArgumentNullException() {
60+
// Arrange
61+
void Encode() => PolylineEncoderExtensions.Encode(_encoder, (Coordinate[])null!);
62+
63+
// Act
64+
var exception = Assert.ThrowsExactly<ArgumentNullException>(Encode);
65+
66+
// Assert
67+
Assert.AreEqual("coordinates", exception.ParamName);
68+
Assert.IsTrue(exception.Message.Contains("Value cannot be null.", StringComparison.Ordinal));
69+
}
70+
4571
[TestMethod]
4672
[DynamicData(nameof(CoordinateCount))]
4773
public void Encode_List_Returns_Expected_Coordinates(int count) {

tests/PolylineAlgorithm.Tests/PolylineEncoderTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ public class PolylineEncoderTest {
2020
/// </summary>
2121
private readonly PolylineEncoder _encoder = new();
2222

23+
public void Constructor_Options_Ok() {
24+
// Arrange
25+
var options = new PolylineEncodingOptions();
26+
27+
// Act
28+
var encoder = new PolylineEncoder(options);
29+
30+
// Assert
31+
Assert.IsNotNull(encoder);
32+
Assert.AreEqual(options, encoder.Options);
33+
}
34+
35+
public void Constructor_NullOptions_Throws_ArgumentNullException() {
36+
// Arrange
37+
PolylineEncodingOptions options = null!;
38+
39+
// Act
40+
PolylineEncoder New() => new(options);
41+
42+
// Assert
43+
Assert.ThrowsExactly<ArgumentNullException>(New);
44+
}
45+
2346
/// <summary>
2447
/// Tests the <see cref="PolylineEncoder.Encode(IEnumerable{Coordinate})"/> method with a null input, expecting an <see cref="ArgumentNullException"/>.
2548
/// </summary>

0 commit comments

Comments
 (0)