66
77[ TestClass ]
88public class PolylineEncodingTests {
9+ #region Dynamic Data Properties
10+
911 public static IEnumerable < ( int variance , string polyline ) > VariancePolylinePairs => [
1012 ( 0 , "?" ) ,
1113 ( 1 , "A" ) ,
@@ -71,9 +73,37 @@ public class PolylineEncodingTests {
7173 ( int . MaxValue , PolylineEncoding . ValueType . Longitude ) ,
7274 ] ;
7375
76+ public static IEnumerable < ( int variance , int charCount ) > VarianceCharCountPairs => [
77+ ( 0 , 1 ) ,
78+ ( 15 , 1 ) ,
79+ ( - 16 , 1 ) ,
80+ ( 16 , 2 ) ,
81+ ( - 17 , 2 ) ,
82+ ( 511 , 2 ) ,
83+ ( - 512 , 2 ) ,
84+ ( 512 , 3 ) ,
85+ ( - 513 , 3 ) ,
86+ ( 16383 , 3 ) ,
87+ ( - 16384 , 3 ) ,
88+ ( 16384 , 4 ) ,
89+ ( - 16385 , 4 ) ,
90+ ( 524287 , 4 ) ,
91+ ( - 524288 , 4 ) ,
92+ ( 524288 , 5 ) ,
93+ ( - 524289 , 5 ) ,
94+ ( 16777215 , 5 ) ,
95+ ( - 16777216 , 5 ) ,
96+ ( 16777216 , 6 ) ,
97+ ( - 16777217 , 6 ) ,
98+ ( int . MaxValue , 6 ) ,
99+ ( int . MinValue , 6 ) ,
100+ ] ;
101+
102+ #endregion
103+
74104 [ TestMethod ]
75105 [ DynamicData ( nameof ( DenormalizedNormalizedPairs ) , DynamicDataSourceType . Property ) ]
76- public void Normalize_Ok ( double denormalized , int expected , PolylineEncoding . ValueType type ) {
106+ public void Normalize_Equals_Expected ( double denormalized , int expected , PolylineEncoding . ValueType type ) {
77107 // Arrange & Act
78108 int result = PolylineEncoding . Normalize ( denormalized , type ) ;
79109
@@ -84,7 +114,7 @@ public void Normalize_Ok(double denormalized, int expected, PolylineEncoding.Val
84114
85115 [ TestMethod ]
86116 [ DynamicData ( nameof ( DenormalizedNormalizedPairs ) , DynamicDataSourceType . Property ) ]
87- public void Denormalize_Ok ( double expected , int normalized , PolylineEncoding . ValueType type ) {
117+ public void Denormalize_Equals_Expected ( double expected , int normalized , PolylineEncoding . ValueType type ) {
88118 // Arrange & Act
89119 double result = PolylineEncoding . Denormalize ( normalized , type ) ;
90120
@@ -94,7 +124,7 @@ public void Denormalize_Ok(double expected, int normalized, PolylineEncoding.Val
94124
95125 [ TestMethod ]
96126 [ DynamicData ( nameof ( VariancePolylinePairs ) , DynamicDataSourceType . Property ) ]
97- public void TryWriteValue_Ok ( int variance , string expected ) {
127+ public void TryWriteValue_StaticBuffer_Returns_True_Equals_Expected ( int variance , string expected ) {
98128 // Arrange
99129 int position = 0 ;
100130 Span < char > buffer = stackalloc char [ 6 ] ;
@@ -108,56 +138,59 @@ public void TryWriteValue_Ok(int variance, string expected) {
108138 Assert . AreEqual ( expected , buffer [ ..position ] . ToString ( ) ) ;
109139 }
110140
141+
111142 [ TestMethod ]
112143 [ DynamicData ( nameof ( VariancePolylinePairs ) , DynamicDataSourceType . Property ) ]
113- public void TryReadValue_Ok ( int expected , string polyline ) {
144+ public void TryWriteValue_DynamicBuffer_Returns_True_Equals_Expected ( int variance , string expected ) {
114145 // Arrange
115146 int position = 0 ;
116- int variance = 0 ;
117- var buffer = polyline . AsMemory ( ) ;
147+ int required = PolylineEncoding . GetCharCount ( variance ) ;
148+ Span < char > buffer = stackalloc char [ required ] ;
118149
119150 // Act
120- bool result = PolylineEncoding . TryReadValue ( ref variance , ref buffer , ref position ) ;
151+ bool result = PolylineEncoding . TryWriteValue ( variance , ref buffer , ref position ) ;
121152
122153 // Assert
123154 Assert . IsTrue ( result ) ;
124- Assert . AreEqual ( buffer . Length , position ) ;
125- Assert . AreEqual ( expected , variance ) ;
155+ Assert . AreEqual ( required , position ) ;
156+ Assert . AreEqual ( expected . Length , position ) ;
157+ Assert . AreEqual ( expected , buffer [ ..position ] . ToString ( ) ) ;
126158 }
127159
128160 [ TestMethod ]
129161 [ DynamicData ( nameof ( VariancePolylinePairs ) , DynamicDataSourceType . Property ) ]
130- public void TryWriteValue_BufferExactlyRightSize_Ok ( int variance , string _ ) {
162+ public void TryWriteValue_BufferTooSmall_Returns_False ( int variance , string _ ) {
131163 // Arrange
132164 int position = 0 ;
133165 int required = PolylineEncoding . GetCharCount ( variance ) ;
134- Span < char > buffer = stackalloc char [ required ] ;
166+ Span < char > buffer = stackalloc char [ required - 1 ] ;
135167
136168 // Act
137169 bool result = PolylineEncoding . TryWriteValue ( variance , ref buffer , ref position ) ;
138170
139171 // Assert
140- Assert . IsTrue ( result ) ;
141- Assert . AreEqual ( required , position ) ;
172+ Assert . IsFalse ( result ) ;
142173 }
143174
144175 [ TestMethod ]
145176 [ DynamicData ( nameof ( VariancePolylinePairs ) , DynamicDataSourceType . Property ) ]
146- public void TryWriteValue_BufferTooSmall_ReturnsFalse ( int variance , string _ ) {
177+ public void TryReadValue_Ok ( int expected , string polyline ) {
147178 // Arrange
148179 int position = 0 ;
149- int required = PolylineEncoding . GetCharCount ( variance ) ;
150- Span < char > buffer = stackalloc char [ required - 1 ] ;
180+ int variance = 0 ;
181+ var buffer = polyline . AsMemory ( ) ;
151182
152183 // Act
153- bool result = PolylineEncoding . TryWriteValue ( variance , ref buffer , ref position ) ;
184+ bool result = PolylineEncoding . TryReadValue ( ref variance , ref buffer , ref position ) ;
154185
155186 // Assert
156- Assert . IsFalse ( result ) ;
187+ Assert . IsTrue ( result ) ;
188+ Assert . AreEqual ( buffer . Length , position ) ;
189+ Assert . AreEqual ( expected , variance ) ;
157190 }
158191
159192 [ TestMethod ]
160- public void TryReadValue_EmptyBuffer_ReturnsFalse ( ) {
193+ public void TryReadValue_EmptyBuffer_Returns_False ( ) {
161194 // Arrange
162195 int variance = 0 ;
163196 int position = 0 ;
@@ -171,7 +204,7 @@ public void TryReadValue_EmptyBuffer_ReturnsFalse() {
171204 }
172205
173206 [ TestMethod ]
174- public void TryReadValue_MalformedBuffer_ReturnsFalseAndDoesNotChangeVariance ( ) {
207+ public void TryReadValue_MalformedBuffer_Returns_False ( ) {
175208 //Arrange
176209 int position = 0 ;
177210 int variance = 42 ;
@@ -193,9 +226,10 @@ public void TryReadValue_MalformedBuffer_ReturnsFalseAndDoesNotChangeVariance()
193226 public void Normalize_Throws_ArgumentOutOfRangeException ( double value , PolylineEncoding . ValueType type ) {
194227 // Arrange
195228 static int Normalize ( double value , PolylineEncoding . ValueType type ) => PolylineEncoding . Normalize ( value , type ) ;
196-
229+
197230 // Act & Assert
198- Assert . ThrowsExactly < ArgumentOutOfRangeException > ( ( ) => Normalize ( value , type ) ) ;
231+ var exception = Assert . ThrowsExactly < ArgumentOutOfRangeException > ( ( ) => Normalize ( value , type ) ) ;
232+ Assert . IsFalse ( string . IsNullOrWhiteSpace ( exception . Message ) ) ;
199233 }
200234
201235 [ TestMethod ]
@@ -205,33 +239,17 @@ public void Denormalize_Throws_ArgumentOutOfRangeException(int value, PolylineEn
205239 static double Denormalize ( int value , PolylineEncoding . ValueType type ) => PolylineEncoding . Denormalize ( value , type ) ;
206240
207241 // Act & Assert
208- Assert . ThrowsExactly < ArgumentOutOfRangeException > ( ( ) => Denormalize ( value , type ) ) ;
242+ var exception = Assert . ThrowsExactly < ArgumentOutOfRangeException > ( ( ) => Denormalize ( value , type ) ) ;
243+ Assert . IsFalse ( string . IsNullOrWhiteSpace ( exception . Message ) ) ;
209244 }
210245
211246 [ 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 ) ) ;
247+ [ DynamicData ( nameof ( VarianceCharCountPairs ) , DynamicDataSourceType . Property ) ]
248+ public void GetCharCount_Equals_Expected ( int variance , int expected ) {
249+ // Arrange & Act
250+ var charCount = PolylineEncoding . GetCharCount ( variance ) ;
251+
252+ // Assert
253+ Assert . AreEqual ( expected , charCount ) ;
236254 }
237255}
0 commit comments