Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 4488cec

Browse files
committed
Merge pull request #1976 from stephentoub/ums_copytests
Add test cases for UnmanagedMemoryStream.CopyTo/CopyToAsync
2 parents 344b712 + 9b4d1ff commit 4488cec

File tree

3 files changed

+269
-101
lines changed

3 files changed

+269
-101
lines changed

src/System.IO.UnmanagedMemoryStream/tests/UmsTests.cs

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
using Xunit;
55
using System.Threading;
6-
7-
// TODO: add CopyTo tests
6+
using System.Threading.Tasks;
87

98
namespace System.IO.Tests
109
{
@@ -189,5 +188,145 @@ public static void CannotUseStreamAfterDispose()
189188
Assert.Throws<ObjectDisposedException>(() => stream.WriteAsync(buffer, 0, buffer.Length).GetAwaiter().GetResult());
190189
}
191190
}
191+
192+
[Fact]
193+
public static void CopyToTest()
194+
{
195+
byte[] testData = ArrayHelpers.CreateByteArray(8192);
196+
197+
using (var manager = new UmsManager(FileAccess.Read, testData))
198+
{
199+
UnmanagedMemoryStream ums = manager.Stream;
200+
UmsTests.ReadUmsInvariants(ums);
201+
MemoryStream destination = new MemoryStream();
202+
203+
destination.Position = 0;
204+
ums.CopyTo(destination);
205+
Assert.Equal(testData, destination.ToArray());
206+
207+
destination.Position = 0;
208+
ums.CopyTo(destination, 1);
209+
Assert.Equal(testData, destination.ToArray());
210+
}
211+
212+
// copy to disposed stream should throw
213+
using (var manager = new UmsManager(FileAccess.Read, testData))
214+
{
215+
UnmanagedMemoryStream ums = manager.Stream;
216+
UmsTests.ReadUmsInvariants(ums);
217+
218+
MemoryStream destination = new MemoryStream();
219+
destination.Dispose();
220+
221+
Assert.Throws<ObjectDisposedException>(() => ums.CopyTo(destination));
222+
}
223+
224+
// copy from disposed stream should throw
225+
using (var manager = new UmsManager(FileAccess.Read, testData))
226+
{
227+
UnmanagedMemoryStream ums = manager.Stream;
228+
UmsTests.ReadUmsInvariants(ums);
229+
ums.Dispose();
230+
231+
MemoryStream destination = new MemoryStream();
232+
233+
Assert.Throws<ObjectDisposedException>(() => ums.CopyTo(destination));
234+
}
235+
236+
// copying to non-writeable stream should throw
237+
using (var manager = new UmsManager(FileAccess.Read, testData))
238+
{
239+
UnmanagedMemoryStream ums = manager.Stream;
240+
UmsTests.ReadUmsInvariants(ums);
241+
242+
MemoryStream destination = new MemoryStream(new byte[0], false);
243+
244+
Assert.Throws<NotSupportedException>(() => ums.CopyTo(destination));
245+
}
246+
247+
// copying from non-readable stream should throw
248+
using (var manager = new UmsManager(FileAccess.Write, testData))
249+
{
250+
UnmanagedMemoryStream ums = manager.Stream;
251+
UmsTests.WriteUmsInvariants(ums);
252+
253+
MemoryStream destination = new MemoryStream(new byte[0], false);
254+
255+
Assert.Throws<NotSupportedException>(() => ums.CopyTo(destination));
256+
}
257+
}
258+
259+
[Fact]
260+
public static async Task CopyToAsyncTest()
261+
{
262+
byte[] testData = ArrayHelpers.CreateByteArray(8192);
263+
264+
using (var manager = new UmsManager(FileAccess.Read, testData))
265+
{
266+
UnmanagedMemoryStream ums = manager.Stream;
267+
UmsTests.ReadUmsInvariants(ums);
268+
MemoryStream destination = new MemoryStream();
269+
270+
destination.Position = 0;
271+
await ums.CopyToAsync(destination);
272+
Assert.Equal(testData, destination.ToArray());
273+
274+
destination.Position = 0;
275+
await ums.CopyToAsync(destination, 2);
276+
Assert.Equal(testData, destination.ToArray());
277+
278+
destination.Position = 0;
279+
await ums.CopyToAsync(destination, 0x1000, new CancellationTokenSource().Token);
280+
Assert.Equal(testData, destination.ToArray());
281+
282+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => ums.CopyToAsync(destination, 0x1000, new CancellationToken(true)));
283+
}
284+
285+
// copy to disposed stream should throw
286+
using (var manager = new UmsManager(FileAccess.Read, testData))
287+
{
288+
UnmanagedMemoryStream ums = manager.Stream;
289+
UmsTests.ReadUmsInvariants(ums);
290+
291+
MemoryStream destination = new MemoryStream();
292+
destination.Dispose();
293+
294+
await Assert.ThrowsAsync<ObjectDisposedException>(() => ums.CopyToAsync(destination));
295+
}
296+
297+
// copy from disposed stream should throw
298+
using (var manager = new UmsManager(FileAccess.Read, testData))
299+
{
300+
UnmanagedMemoryStream ums = manager.Stream;
301+
UmsTests.ReadUmsInvariants(ums);
302+
ums.Dispose();
303+
304+
MemoryStream destination = new MemoryStream();
305+
306+
await Assert.ThrowsAsync<ObjectDisposedException>(() => ums.CopyToAsync(destination));
307+
}
308+
309+
// cpoying to non-writeable stream should throw
310+
using (var manager = new UmsManager(FileAccess.Read, testData))
311+
{
312+
UnmanagedMemoryStream ums = manager.Stream;
313+
UmsTests.ReadUmsInvariants(ums);
314+
315+
MemoryStream destination = new MemoryStream(new byte[0], false);
316+
317+
await Assert.ThrowsAsync<NotSupportedException>(() => ums.CopyToAsync(destination));
318+
}
319+
320+
// copying from non-readable stream should throw
321+
using (var manager = new UmsManager(FileAccess.Write, testData))
322+
{
323+
UnmanagedMemoryStream ums = manager.Stream;
324+
UmsTests.WriteUmsInvariants(ums);
325+
326+
MemoryStream destination = new MemoryStream(new byte[0], false);
327+
328+
await Assert.ThrowsAsync<NotSupportedException>(() => ums.CopyToAsync(destination));
329+
}
330+
}
192331
}
193332
}

src/System.IO.UnmanagedMemoryStream/tests/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"System.Collections": "4.0.10-beta-*",
4+
"System.Diagnostics.Debug": "4.0.10-beta-*",
45
"System.IO": "4.0.10-beta-*",
56
"System.IO.FileSystem.Primitives": "4.0.0-beta-*",
67
"System.Reflection": "4.0.10-beta-*",

0 commit comments

Comments
 (0)