@@ -252,6 +252,66 @@ public async Task Test_MemoryStream_ReadWriteAsync_Memory()
252252 Assert . IsTrue ( data . Span . SequenceEqual ( result . Span ) ) ;
253253 }
254254
255+ [ TestMethod ]
256+ public void Test_MemoryStream_CopyTo ( )
257+ {
258+ Stream source = new byte [ 100 ] . AsMemory ( ) . AsStream ( ) ;
259+
260+ Memory < byte > data = CreateRandomData ( 64 ) ;
261+
262+ source . Write ( data . Span ) ;
263+
264+ Assert . AreEqual ( source . Position , data . Length ) ;
265+
266+ source . Position = 0 ;
267+
268+ Stream destination = new byte [ 100 ] . AsMemory ( ) . AsStream ( ) ;
269+
270+ source . CopyTo ( destination ) ;
271+
272+ Assert . AreEqual ( source . Position , destination . Position ) ;
273+
274+ destination . Position = 0 ;
275+
276+ Memory < byte > result = new byte [ data . Length ] ;
277+
278+ int bytesRead = destination . Read ( result . Span ) ;
279+
280+ Assert . AreEqual ( bytesRead , result . Length ) ;
281+ Assert . AreEqual ( destination . Position , data . Length ) ;
282+ Assert . IsTrue ( data . Span . SequenceEqual ( result . Span ) ) ;
283+ }
284+
285+ [ TestMethod ]
286+ public async Task Test_MemoryStream_CopyToAsync ( )
287+ {
288+ Stream source = new byte [ 100 ] . AsMemory ( ) . AsStream ( ) ;
289+
290+ Memory < byte > data = CreateRandomData ( 64 ) ;
291+
292+ await source . WriteAsync ( data ) ;
293+
294+ Assert . AreEqual ( source . Position , data . Length ) ;
295+
296+ source . Position = 0 ;
297+
298+ Stream destination = new byte [ 100 ] . AsMemory ( ) . AsStream ( ) ;
299+
300+ await source . CopyToAsync ( destination ) ;
301+
302+ Assert . AreEqual ( source . Position , destination . Position ) ;
303+
304+ destination . Position = 0 ;
305+
306+ Memory < byte > result = new byte [ data . Length ] ;
307+
308+ int bytesRead = await destination . ReadAsync ( result ) ;
309+
310+ Assert . AreEqual ( bytesRead , result . Length ) ;
311+ Assert . AreEqual ( destination . Position , data . Length ) ;
312+ Assert . IsTrue ( data . Span . SequenceEqual ( result . Span ) ) ;
313+ }
314+
255315 /// <summary>
256316 /// Creates a random <see cref="byte"/> array filled with random data.
257317 /// </summary>
0 commit comments