Skip to content

Commit a42c731

Browse files
author
Pete Sramek
committed
added test project for abstraction library
1 parent 2693411 commit a42c731

File tree

7 files changed

+321
-0
lines changed

7 files changed

+321
-0
lines changed

PolylineAlgorithm.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Project Path="src/PolylineAlgorithm/PolylineAlgorithm.csproj" />
1111
</Folder>
1212
<Folder Name="/tests/">
13+
<Project Path="tests/PolylineAlgorithm.Abstraction.Tests/PolylineAlgorithm.Abstraction.Tests.csproj" Id="8df45c12-7ca3-4be5-8928-4bc22c641700" />
1314
<Project Path="tests/PolylineAlgorithm.Tests/PolylineAlgorithm.Tests.csproj" />
1415
</Folder>
1516
<Folder Name="/utilities/" Id="02ea681e-c7d8-13c7-8484-4ac65e1b71e8">
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>13.0</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<InvariantGlobalization>true</InvariantGlobalization>
9+
</PropertyGroup>
10+
11+
<PropertyGroup>
12+
<OutputType>Exe</OutputType>
13+
<IsTestProject>true</IsTestProject>
14+
<EnableMSTestRunner>true</EnableMSTestRunner>
15+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
16+
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
17+
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
18+
<TestingExtensionsProfile>None</TestingExtensionsProfile>
19+
</PropertyGroup>
20+
21+
<PropertyGroup>
22+
<IsPackable>false</IsPackable>
23+
</PropertyGroup>
24+
25+
<PropertyGroup>
26+
<AnalysisMode>All</AnalysisMode>
27+
<AnalysisLevel>latest</AnalysisLevel>
28+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
29+
<EnableNETAnalyzers>false</EnableNETAnalyzers>
30+
</PropertyGroup>
31+
32+
<ItemGroup>
33+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
34+
<PackageReference Include="MSTest" Version="3.*" />
35+
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="1.*" />
36+
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.*" />
37+
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.*" />
38+
</ItemGroup>
39+
40+
<ItemGroup>
41+
<ProjectReference Include="..\..\src\PolylineAlgorithm.Abstraction\PolylineAlgorithm.Abstraction.csproj" />
42+
</ItemGroup>
43+
44+
</Project>
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
namespace PolylineAlgorithm.Abstraction.Tests;
2+
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
[TestClass]
8+
public class PolylineEncodingTests {
9+
public static IEnumerable<(int variance, string polyline)> VariancePolylinePairs => [
10+
(0,"?"),
11+
(1,"A"),
12+
(-1,"@"),
13+
(16,"_@"),
14+
(-16,"^"),
15+
(511,"}^"),
16+
(-511,"|^"),
17+
(512,"__@"),
18+
(-512,"~^"),
19+
(16383,"}~^"),
20+
(-16383,"|~^"),
21+
(16384,"___@"),
22+
(-16384,"~~^"),
23+
(524287,"}~~^"),
24+
(-524287,"|~~^"),
25+
(524288,"____@"),
26+
(-524288,"~~~^"),
27+
(16777215,"}~~~^"),
28+
(-16777215,"|~~~^")
29+
];
30+
31+
public static IEnumerable<(double denormalized, int normalized, PolylineEncoding.ValueType)> DenormalizedNormalizedPairs => [
32+
(0,0, PolylineEncoding.ValueType.Latitude),
33+
(0,0, PolylineEncoding.ValueType.Longitude),
34+
(1.23456,123456, PolylineEncoding.ValueType.Latitude),
35+
(-1.23456,-123456, PolylineEncoding.ValueType.Latitude),
36+
(1.23456,123456, PolylineEncoding.ValueType.Longitude),
37+
(-1.23456,-123456, PolylineEncoding.ValueType.Longitude),
38+
(90,9000000, PolylineEncoding.ValueType.Latitude),
39+
(-90,-9000000, PolylineEncoding.ValueType.Latitude),
40+
(90,9000000, PolylineEncoding.ValueType.Longitude),
41+
(-90,-9000000, PolylineEncoding.ValueType.Longitude),
42+
(180,18000000, PolylineEncoding.ValueType.Longitude),
43+
(-180,-18000000, PolylineEncoding.ValueType.Longitude)
44+
];
45+
46+
public static IEnumerable<(double denormalized, PolylineEncoding.ValueType)> DenormalizedOutOfRangeValues => [
47+
(90.00001,PolylineEncoding.ValueType.Latitude),
48+
(-90.00001,PolylineEncoding.ValueType.Latitude),
49+
(180.00001,PolylineEncoding.ValueType.Longitude),
50+
(-180.00001,PolylineEncoding.ValueType.Longitude),
51+
(double.NaN,PolylineEncoding.ValueType.Latitude),
52+
(double.NaN,PolylineEncoding.ValueType.Longitude),
53+
(double.MinValue,PolylineEncoding.ValueType.Latitude),
54+
(double.MaxValue,PolylineEncoding.ValueType.Latitude),
55+
(double.MinValue,PolylineEncoding.ValueType.Longitude),
56+
(double.MaxValue,PolylineEncoding.ValueType.Longitude),
57+
(double.NegativeInfinity,PolylineEncoding.ValueType.Latitude),
58+
(double.PositiveInfinity,PolylineEncoding.ValueType.Latitude),
59+
(double.NegativeInfinity,PolylineEncoding.ValueType.Longitude),
60+
(double.PositiveInfinity,PolylineEncoding.ValueType.Longitude),
61+
];
62+
63+
public static IEnumerable<(int normalized, PolylineEncoding.ValueType)> NormalizedOutOfRangeValues => [
64+
(9000001,PolylineEncoding.ValueType.Latitude),
65+
(-9000001,PolylineEncoding.ValueType.Latitude),
66+
(18000001,PolylineEncoding.ValueType.Longitude),
67+
(-18000001,PolylineEncoding.ValueType.Longitude),
68+
(int.MinValue,PolylineEncoding.ValueType.Latitude),
69+
(int.MaxValue,PolylineEncoding.ValueType.Latitude),
70+
(int.MinValue,PolylineEncoding.ValueType.Longitude),
71+
(int.MaxValue,PolylineEncoding.ValueType.Longitude),
72+
];
73+
74+
[TestMethod]
75+
[DynamicData(nameof(DenormalizedNormalizedPairs), DynamicDataSourceType.Property)]
76+
public void Normalize_Ok(double denormalized, int expected, PolylineEncoding.ValueType type) {
77+
// Arrange & Act
78+
int result = PolylineEncoding.Normalize(denormalized, type);
79+
80+
// Assert
81+
Assert.AreEqual(expected, result);
82+
}
83+
84+
85+
[TestMethod]
86+
[DynamicData(nameof(DenormalizedNormalizedPairs), DynamicDataSourceType.Property)]
87+
public void Denormalize_Ok(double expected, int normalized, PolylineEncoding.ValueType type) {
88+
// Arrange & Act
89+
double result = PolylineEncoding.Denormalize(normalized, type);
90+
91+
// Assert
92+
Assert.AreEqual(expected, result);
93+
}
94+
95+
[TestMethod]
96+
[DynamicData(nameof(VariancePolylinePairs), DynamicDataSourceType.Property)]
97+
public void TryWriteValue_Ok(int variance, string expected) {
98+
// Arrange
99+
int position = 0;
100+
Span<char> buffer = stackalloc char[6];
101+
102+
// Act
103+
bool result = PolylineEncoding.TryWriteValue(variance, ref buffer, ref position);
104+
105+
// Assert
106+
Assert.IsTrue(result);
107+
Assert.AreEqual(expected.Length, position);
108+
Assert.AreEqual(expected, buffer[..position].ToString());
109+
}
110+
111+
[TestMethod]
112+
[DynamicData(nameof(VariancePolylinePairs), DynamicDataSourceType.Property)]
113+
public void TryReadValue_Ok(int expected, string polyline) {
114+
// Arrange
115+
int position = 0;
116+
int variance = 0;
117+
var buffer = polyline.AsMemory();
118+
119+
// Act
120+
bool result = PolylineEncoding.TryReadValue(ref variance, ref buffer, ref position);
121+
122+
// Assert
123+
Assert.IsTrue(result);
124+
Assert.AreEqual(buffer.Length, position);
125+
Assert.AreEqual(expected, variance);
126+
}
127+
128+
[TestMethod]
129+
[DynamicData(nameof(VariancePolylinePairs), DynamicDataSourceType.Property)]
130+
public void TryWriteValue_BufferExactlyRightSize_Ok(int variance, string _) {
131+
// Arrange
132+
int position = 0;
133+
int required = PolylineEncoding.GetCharCount(variance);
134+
Span<char> buffer = stackalloc char[required];
135+
136+
// Act
137+
bool result = PolylineEncoding.TryWriteValue(variance, ref buffer, ref position);
138+
139+
// Assert
140+
Assert.IsTrue(result);
141+
Assert.AreEqual(required, position);
142+
}
143+
144+
[TestMethod]
145+
[DynamicData(nameof(VariancePolylinePairs), DynamicDataSourceType.Property)]
146+
public void TryWriteValue_BufferTooSmall_ReturnsFalse(int variance, string _) {
147+
// Arrange
148+
int position = 0;
149+
int required = PolylineEncoding.GetCharCount(variance);
150+
Span<char> buffer = stackalloc char[required - 1];
151+
152+
// Act
153+
bool result = PolylineEncoding.TryWriteValue(variance, ref buffer, ref position);
154+
155+
// Assert
156+
Assert.IsFalse(result);
157+
}
158+
159+
[TestMethod]
160+
public void TryReadValue_EmptyBuffer_ReturnsFalse() {
161+
// Arrange
162+
int variance = 0;
163+
int position = 0;
164+
ReadOnlyMemory<char> buffer = Memory<char>.Empty;
165+
166+
// Act
167+
bool result = PolylineEncoding.TryReadValue(ref variance, ref buffer, ref position);
168+
169+
// Assert
170+
Assert.IsFalse(result);
171+
}
172+
173+
[TestMethod]
174+
public void TryReadValue_MalformedBuffer_ReturnsFalseAndDoesNotChangeVariance() {
175+
//Arrange
176+
int position = 0;
177+
int variance = 42;
178+
int expected = variance;
179+
// Buffer with a char that will never finish a value (simulate incomplete encoding)
180+
char[] chars = [(char)(127)]; // 127 - 63 = 64, which is >= 32, so loop never breaks
181+
ReadOnlyMemory<char> buffer = chars.AsMemory();
182+
183+
// Act
184+
bool result = PolylineEncoding.TryReadValue(ref variance, ref buffer, ref position);
185+
186+
// Assert
187+
Assert.IsFalse(result);
188+
Assert.AreEqual(expected, variance);
189+
}
190+
191+
[TestMethod]
192+
[DynamicData(nameof(DenormalizedOutOfRangeValues), DynamicDataSourceType.Property)]
193+
public void Normalize_Throws_ArgumentOutOfRangeException(double value, PolylineEncoding.ValueType type) {
194+
// Arrange
195+
static int Normalize(double value, PolylineEncoding.ValueType type) => PolylineEncoding.Normalize(value, type);
196+
197+
// Act & Assert
198+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Normalize(value, type));
199+
}
200+
201+
[TestMethod]
202+
[DynamicData(nameof(NormalizedOutOfRangeValues), DynamicDataSourceType.Property)]
203+
public void Denormalize_Throws_ArgumentOutOfRangeException(int value, PolylineEncoding.ValueType type) {
204+
// Arrange
205+
static double Denormalize(int value, PolylineEncoding.ValueType type) => PolylineEncoding.Denormalize(value, type);
206+
207+
// Act & Assert
208+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Denormalize(value, type));
209+
}
210+
211+
[TestMethod]
212+
public void GetCharCount_Covers_All_Cases() {
213+
Assert.AreEqual(1, PolylineEncoding.GetCharCount(0));
214+
Assert.AreEqual(1, PolylineEncoding.GetCharCount(15));
215+
Assert.AreEqual(1, PolylineEncoding.GetCharCount(-16));
216+
Assert.AreEqual(2, PolylineEncoding.GetCharCount(16));
217+
Assert.AreEqual(2, PolylineEncoding.GetCharCount(-17));
218+
Assert.AreEqual(2, PolylineEncoding.GetCharCount(511));
219+
Assert.AreEqual(2, PolylineEncoding.GetCharCount(-512));
220+
Assert.AreEqual(3, PolylineEncoding.GetCharCount(512));
221+
Assert.AreEqual(3, PolylineEncoding.GetCharCount(-513));
222+
Assert.AreEqual(3, PolylineEncoding.GetCharCount(16383));
223+
Assert.AreEqual(3, PolylineEncoding.GetCharCount(-16384));
224+
Assert.AreEqual(4, PolylineEncoding.GetCharCount(16384));
225+
Assert.AreEqual(4, PolylineEncoding.GetCharCount(-16385));
226+
Assert.AreEqual(4, PolylineEncoding.GetCharCount(524287));
227+
Assert.AreEqual(4, PolylineEncoding.GetCharCount(-524288));
228+
Assert.AreEqual(5, PolylineEncoding.GetCharCount(524288));
229+
Assert.AreEqual(5, PolylineEncoding.GetCharCount(-524289));
230+
Assert.AreEqual(5, PolylineEncoding.GetCharCount(16777215));
231+
Assert.AreEqual(5, PolylineEncoding.GetCharCount(-16777216));
232+
Assert.AreEqual(6, PolylineEncoding.GetCharCount(16777216));
233+
Assert.AreEqual(6, PolylineEncoding.GetCharCount(-16777217));
234+
Assert.AreEqual(6, PolylineEncoding.GetCharCount(int.MaxValue));
235+
Assert.AreEqual(6, PolylineEncoding.GetCharCount(int.MinValue));
236+
}
237+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace PolylineAlgorithm.Tests.Properties;
7+
8+
/// <summary>
9+
/// Provides constants for test categories.
10+
/// </summary>
11+
internal static class Category {
12+
/// <summary>
13+
/// Represents the unit test category.
14+
/// </summary>
15+
public const string Unit = nameof(Unit);
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[assembly: SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Ignored in test asemblies.")]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
global using Microsoft.VisualStudio.TestTools.UnitTesting;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using PolylineAlgorithm.Tests.Properties;
7+
8+
[assembly: Parallelize(Scope = ExecutionScope.ClassLevel)]
9+
[assembly: TestCategory(Category.Unit)]

0 commit comments

Comments
 (0)