public void MemoryStream_CopyTo_Invalid() { ChunkedMemoryStream memoryStream; const string BufferSize = "bufferSize"; using (memoryStream = new ChunkedMemoryStream(this.allocator)) { const string Destination = "destination"; Assert.Throws <ArgumentNullException>(Destination, () => memoryStream.CopyTo(destination: null)); // Validate the destination parameter first. Assert.Throws <ArgumentNullException>(Destination, () => memoryStream.CopyTo(destination: null, bufferSize: 0)); Assert.Throws <ArgumentNullException>(Destination, () => memoryStream.CopyTo(destination: null, bufferSize: -1)); // Then bufferSize. Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // 0-length buffer doesn't make sense. Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: -1)); } // After the Stream is disposed, we should fail on all CopyTos. Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // Not before bufferSize is validated. Assert.Throws <ArgumentOutOfRangeException>(BufferSize, () => memoryStream.CopyTo(Stream.Null, bufferSize: -1)); ChunkedMemoryStream disposedStream = memoryStream; // We should throw first for the source being disposed... Assert.Throws <ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1)); // Then for the destination being disposed. memoryStream = new ChunkedMemoryStream(this.allocator); Assert.Throws <ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1)); memoryStream.Dispose(); }