|
| 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 | +} |
0 commit comments