Skip to content

Commit f624a23

Browse files
author
Petr Sramek
committed
fixes
1 parent e527ed7 commit f624a23

13 files changed

+69
-63
lines changed

src/PolylineAlgorithm/Internal/PolylineReader.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public PolylineReader(ref readonly Polyline polyline) {
2222

2323
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2424
public Coordinate Read() {
25-
EnsureRead();
25+
InvalidReaderStateException.ThrowIfCannotRead(CanRead, Position, _polyline.Length);
2626

2727
GetCurrent(out int currentLatitude, out int currentLongitude);
2828

@@ -63,26 +63,20 @@ int ReadNext(ref readonly int value) {
6363
int shifter = 0;
6464

6565
do {
66-
EnsureRead();
66+
InvalidReaderStateException.ThrowIfCannotRead(CanRead, Position, _polyline.Length);
6767
chunk = _polyline[Position] - Defaults.Algorithm.QuestionMark;
6868
sum |= (chunk & Defaults.Algorithm.UnitSeparator) << shifter;
6969
shifter += Defaults.Algorithm.ShiftLength;
7070
Advance();
7171
} while (chunk >= Defaults.Algorithm.Space && CanRead);
7272

7373
if (!CanRead && chunk >= Defaults.Algorithm.Space) {
74-
throw new PolylineMalformedException(Position);
74+
PolylineMalformedException.Throw(Position);
7575
}
7676

7777
return value + ((sum & 1) == 1 ? ~(sum >> 1) : sum >> 1);
7878
}
7979

80-
readonly void EnsureRead() {
81-
if (!CanRead) {
82-
throw new InvalidOperationException(ExceptionMessageResource.PolylineReaderCannotRead, innerException: new InvalidReaderStateException(Position, _polyline.Length));
83-
}
84-
}
85-
8680
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 12)]
8781
private ref struct ReaderState {
8882
public ReaderState() {

src/PolylineAlgorithm/Internal/PolylineWriter.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public PolylineWriter(ref readonly Memory<char> buffer) {
1919

2020
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2121
public void Write(ref readonly Coordinate coordinate) {
22-
EnsureState();
22+
InvalidWriterStateException.ThrowIfCannotWrite(CanWrite, Position, _buffer.Length);
2323

2424
Imprecise(coordinate.Latitude, out int latitude);
2525
Imprecise(coordinate.Longitude, out int longitude);
@@ -35,12 +35,6 @@ static void Imprecise(double value, out int result) {
3535
}
3636
}
3737

38-
readonly void EnsureState() {
39-
if (!CanWrite) {
40-
throw new InvalidOperationException(ExceptionMessageResource.PolylineWriterCannotWrite, new InvalidWriterStateException(Position, _buffer.Length));
41-
}
42-
}
43-
4438
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4539
void WriteNext(ref readonly int value) {
4640
int rem = value << 1;
@@ -59,7 +53,7 @@ void WriteNext(ref readonly int value) {
5953

6054
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6155
void WriteChar(char value) {
62-
EnsureState();
56+
InvalidWriterStateException.ThrowIfCannotWrite(CanWrite, Position, _buffer.Length);
6357

6458
_buffer.Span[Position] = value;
6559
_state.Position += 1;

src/PolylineAlgorithm/InvalidCoordinateException.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace PolylineAlgorithm;
1313
/// </summary>
1414
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Main purpose is to report coordinate that is invalid, thus we have to have only one construtor.")]
1515
public sealed class InvalidCoordinateException : Exception {
16-
private InvalidCoordinateException(Coordinate coordinate, string message, Exception? innerException = null)
17-
: base(message, innerException) {
16+
private InvalidCoordinateException(Coordinate coordinate, string message)
17+
: base(message) {
1818
Coordinate = coordinate;
1919
}
2020

@@ -23,7 +23,11 @@ private InvalidCoordinateException(Coordinate coordinate, string message, Except
2323
/// </summary>
2424
public Coordinate Coordinate { get; }
2525

26-
public static void Throw(Coordinate coordinate, Exception? innerException = null) {
27-
throw new InvalidCoordinateException(coordinate, string.Format(ExceptionMessageResource.CoordinateIsOutOfRangeMessageFormat, coordinate.Latitude, coordinate.Longitude), innerException);
26+
public static void ThrowIfNotValid(Coordinate coordinate) {
27+
if(coordinate.IsValid) {
28+
return;
29+
}
30+
31+
throw new InvalidCoordinateException(coordinate, string.Format(ExceptionMessageResource.CoordinateIsOutOfRangeMessageFormat, coordinate.Latitude, coordinate.Longitude));
2832
}
2933
}

src/PolylineAlgorithm/InvalidReaderStateException.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ namespace PolylineAlgorithm;
1616
public sealed class InvalidReaderStateException(string message)
1717
: Exception(message) {
1818

19-
public static void Throw(int readerPosition, int polylineLength) {
19+
public static void ThrowIfCannotRead(bool canRead, int readerPosition, int polylineLength) {
20+
if(canRead) {
21+
return;
22+
}
23+
2024
if (polylineLength == 0) {
2125
throw new InvalidReaderStateException(ExceptionMessageResource.PolylineStringIsEmptyMessage);
2226
} else {

src/PolylineAlgorithm/InvalidWriterStateException.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ namespace PolylineAlgorithm;
1616
public sealed class InvalidWriterStateException(string message)
1717
: Exception(message) {
1818

19-
public static void Throw(int readerPosition, int polylineLength) {
20-
if (polylineLength == 0) {
19+
public static void ThrowIfCannotWrite(bool canWrite, int readerPosition, int bufferSize) {
20+
if (canWrite) {
21+
return;
22+
}
23+
24+
if (bufferSize == 0) {
2125
throw new InvalidWriterStateException(ExceptionMessageResource.PolylineBufferIsEmptyMessage);
2226
} else {
2327
throw new InvalidWriterStateException(string.Format(ExceptionMessageResource.UnableToWritePolylineBufferAtPositionMessageFormat, readerPosition, bufferSize));

src/PolylineAlgorithm/PolylineDecoder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public IEnumerable<Coordinate> Decode(ref readonly Polyline polyline) {
3232
while (reader.CanRead) {
3333
var coordinate = reader.Read();
3434

35-
if(!coordinate.IsValid) {
36-
throw new InvalidCoordinateException(coordinate);
37-
}
35+
InvalidCoordinateException.ThrowIfNotValid(coordinate);
3836

3937
result.Add(coordinate);
4038
}

src/PolylineAlgorithm/PolylineEncoder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public Polyline Encode(IEnumerable<Coordinate> coordinates) {
3838
PolylineWriter writer = new(in buffer);
3939

4040
foreach (var coordinate in coordinates) {
41-
if (!coordinate.IsValid) {
42-
throw new InvalidCoordinateException(coordinate);
43-
}
44-
41+
InvalidCoordinateException.ThrowIfNotValid(coordinate);
4542
writer.Write(in coordinate);
4643
}
4744

src/PolylineAlgorithm/PolylineMalformedException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public sealed class PolylineMalformedException : Exception {
1919
/// </summary>
2020
/// <param name="position"></param>
2121
/// <param name="innerException"></param>
22-
private PolylineMalformedException(int position, string message, Exception? innerException = null)
23-
: base(message, innerException) {
22+
private PolylineMalformedException(int position, string message)
23+
: base(message) {
2424
Position = position;
2525
}
2626

@@ -29,7 +29,7 @@ private PolylineMalformedException(int position, string message, Exception? inne
2929
/// </summary>
3030
public int Position { get; }
3131

32-
public static void Throw(int position, Exception? innerException = null) {
33-
throw new PolylineMalformedException(position, string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position), innerException);
32+
public static void Throw(int position) {
33+
throw new PolylineMalformedException(position, string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position));
3434
}
3535
}

tests/PolylineAlgorithm.Tests/Data/Values.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace PolylineAlgorithm.Tests.Data;
77

8+
using PolylineAlgorithm.Validation;
89
using System.Collections.Generic;
910

1011
/// <summary>
@@ -73,6 +74,8 @@ public static class MalformedPolylineException {
7374
}
7475

7576
public static class InvalidCoordinateException {
76-
public static readonly Coordinate Coordinate = new(_random.NextDouble(), _random.NextDouble());
77+
public static readonly Coordinate Valid = new(_random.NextDouble(), _random.NextDouble());
78+
79+
public static readonly Coordinate Invalid = new(double.MinValue, double.MaxValue);
7780
}
7881
}

tests/PolylineAlgorithm.Tests/InvalidCoordinateExceptionTest.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,30 @@ public class InvalidCoordinateExceptionTest {
2020
/// <see cref="Exception.InnerException" /> equals passed argument,
2121
/// and <see cref="Exception.Message"/> in not <see langword="null"/>, empty -or- whitespace.</remarks>
2222
[TestMethod]
23-
public void Constructor_Ok() {
23+
public void ThrowIfNotValid_Valid_Parameter_Ok() {
2424
// Arrange
25-
var coordinate = Values.InvalidCoordinateException.Coordinate;
26-
var innerException = new Exception();
25+
var coordinate = Values.InvalidCoordinateException.Valid;
2726

2827
// Act
29-
InvalidCoordinateException result = new(coordinate, innerException);
28+
InvalidCoordinateException.ThrowIfNotValid(coordinate);
3029

3130
// Assert
32-
Assert.AreEqual(coordinate, result.Coordinate);
33-
Assert.IsNotNull(innerException);
34-
Assert.IsFalse(string.IsNullOrWhiteSpace(result.Message));
31+
// We are assering exception was not thrown, if it was test won't pass
32+
}
33+
34+
[TestMethod]
35+
public void ThrowIfNotValid_Invalid_Parameter_InvalidCoordinateException_Thrown() {
36+
// Arrange
37+
var coordinate = Values.InvalidCoordinateException.Invalid;
38+
39+
// Act
40+
void ThrowIfNotValid(Coordinate coordinate) {
41+
InvalidCoordinateException.ThrowIfNotValid(coordinate);
42+
}
43+
44+
// Assert
45+
var exception = Assert.ThrowsException<InvalidCoordinateException>(() => ThrowIfNotValid(coordinate));
46+
Assert.AreEqual(coordinate, exception.Coordinate);
47+
Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
3548
}
3649
}

0 commit comments

Comments
 (0)