Skip to content

Commit 84c2a73

Browse files
author
Petr Sramek
committed
fixed occasional data loss during normalization and denormalization
1 parent c8fa680 commit 84c2a73

File tree

4 files changed

+11
-30
lines changed

4 files changed

+11
-30
lines changed

Diff for: src/PolylineAlgorithm/Internal/Defaults.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class Algorithm {
2020
/// <summary>
2121
/// The coordinate rounding precision.
2222
/// </summary>
23-
public const double Precision = 1E5;
23+
public const decimal Precision = 100_000;
2424

2525
/// <summary>
2626
/// The length of the shift used in the algorithm.

Diff for: src/PolylineAlgorithm/PolylineEncoding.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffer, ref
3333
}
3434

3535
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36-
public double Denormalize(int value) => value / Defaults.Algorithm.Precision;
36+
public double Denormalize(int value) => Convert.ToDouble(value / Defaults.Algorithm.Precision);
3737

3838

3939
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -59,7 +59,7 @@ public bool TryWriteValue(in int variance, ref Span<char> buffer, ref int positi
5959
}
6060

6161
[MethodImpl(MethodImplOptions.AggressiveInlining)]
62-
public int Normalize(double value) => (int)(value * Defaults.Algorithm.Precision);
62+
public int Normalize(double value) => Convert.ToInt32(Convert.ToDecimal(value) * Defaults.Algorithm.Precision);
6363

6464

6565
[MethodImpl(MethodImplOptions.AggressiveInlining)]

Diff for: tests/PolylineAlgorithm.Tests/PolylineEncoderTest.cs

-17
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,6 @@ void EncodeEmptyCoordinates() {
5353
Assert.ThrowsExactly<ArgumentException>(() => EncodeEmptyCoordinates());
5454
}
5555

56-
/// <summary>
57-
/// Tests the <see cref="PolylineEncoder.Encode(IEnumerable{Coordinate})"/> method with an invalid input, expecting an <see cref="InvalidCoordinateException"/>.
58-
/// </summary>
59-
[TestMethod]
60-
public void Encode_InvalidInput_ThrowsException() {
61-
// Arrange
62-
IEnumerable<Coordinate> invalid = Values.Coordinates.Invalid;
63-
64-
// Act
65-
void EncodeInvalidCoordinates() {
66-
Encoder.Encode(invalid);
67-
}
68-
69-
// Assert
70-
Assert.ThrowsExactly<InvalidOperationException>(() => EncodeInvalidCoordinates());
71-
}
72-
7356
/// <summary>
7457
/// Tests the <see cref="PolylineEncoder.Encode(IEnumerable{Coordinate})"/> method with a valid input.
7558
/// </summary>

Diff for: tests/PolylineAlgorithm.Tests/PolylineTest.cs

+8-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PolylineTest {
1616
/// <summary>
1717
/// Provides test data for the string parameter tests.
1818
/// </summary>
19-
public static IEnumerable<object[]> SizeParameters => new List<object[]> {
19+
public static IEnumerable<object[]> LengthParameters => new List<object[]> {
2020
new object[] { 1 },
2121
new object[] { 10 },
2222
new object[] { 100 },
@@ -63,20 +63,18 @@ public void Constructor_Null_String_ArgumentNullException() {
6363
/// </summary>
6464
/// <param name="value">The string value.</param>
6565
[TestMethod]
66-
[DynamicData(nameof(SizeParameters))]
67-
public void Constructor_String_Parameter_Ok(int size) {
66+
[DynamicData(nameof(LengthParameters))]
67+
public void Constructor_String_Parameter_Ok(int length) {
6868
// Arrange
69-
var polyline = ValueProvider.GetPolyline(size);
70-
bool empty = polyline.Length == 0;
71-
long length = polyline.Length;
69+
var value = ValueProvider.GetPolyline(length);
7270

7371
// Act
74-
Polyline result = Polyline.FromString(polyline.ToString());
72+
Polyline result = Polyline.FromString(value.ToString());
7573

7674
// Assert
77-
Assert.AreEqual(empty, polyline.Length == 0);
78-
Assert.AreEqual(length, polyline.Length);
79-
//Assert.IsTrue(span.Span.SequenceEqual(polyline.Span.Span));
75+
Assert.AreEqual(value.Length, result.Length);
76+
Assert.AreEqual(value.Length == 0, result.IsEmpty);
77+
Assert.IsTrue(value.Equals(result));
8078
}
8179

8280
///// <summary>

0 commit comments

Comments
 (0)