|
3 | 3 |
|
4 | 4 | using Xunit;
|
5 | 5 | using System.Threading;
|
6 |
| - |
7 |
| -// TODO: add CopyTo tests |
| 6 | +using System.Threading.Tasks; |
8 | 7 |
|
9 | 8 | namespace System.IO.Tests
|
10 | 9 | {
|
@@ -189,5 +188,145 @@ public static void CannotUseStreamAfterDispose()
|
189 | 188 | Assert.Throws<ObjectDisposedException>(() => stream.WriteAsync(buffer, 0, buffer.Length).GetAwaiter().GetResult());
|
190 | 189 | }
|
191 | 190 | }
|
| 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 | + } |
192 | 331 | }
|
193 | 332 | }
|
0 commit comments