public void TryExpandingTheStreamViaSetLengthAndThenWritingReadingFromIt() { DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream(); const int testArrayLength = 500000; dataStream.SetLength(testArrayLength); Assert.AreEqual(testArrayLength, dataStream.Length); Assert.AreEqual(0, dataStream.Position); const int halfTestArray = testArrayLength / 2; //на половине пути запишем что-нибудь интересное. dataStream.SetPosition(halfTestArray); var bytesToWrite = new byte[halfTestArray]; for (int i = 0; i < bytesToWrite.Length; i++) { bytesToWrite[i] = 231; } dataStream.Write(bytesToWrite, 0, bytesToWrite.Length); dataStream.SetPosition(halfTestArray); var bytesRead = new byte[bytesToWrite.Length]; dataStream.Read(bytesRead, 0, bytesRead.Length); MakeSureEachByteInArrayIsRight(bytesRead, 231); // и проверим, что записалось что нужно, - новый объем как будто проработан. }
public void TryChangingPositionCombinedWithWritingAndReading() { DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream(); byte[] bytes = ByteBufferFactory.CreateByteBufferWithAllBytesSet(123, 123); dataStream.Write(bytes, 0, bytes.Length); dataStream.SetPosition(0); byte[] bytes2 = ByteBufferFactory.CreateByteBufferWithAllBytesSet(14, 77); dataStream.Write(bytes2, 0, bytes2.Length); var bytesRead = new byte[dataStream.Length]; dataStream.SetPosition(0); dataStream.Read(bytesRead, 0, dataStream.Length); for (int i = 0; i < dataStream.Length; i++) { if (i <= 13) { Assert.AreEqual(77, bytesRead[i]); } else { Assert.AreEqual(123, bytesRead[i]); } } }
public void MakeSureReadDoesNotThrowIfReadingBeyondTheStream() { DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream(); byte[] bytes = ByteBufferFactory.CreateByteBufferWithAllBytesSet(55000, 123); dataStream.Write(bytes, 0, bytes.Length); dataStream.SetPosition(55000 - 35); var largeArray = new byte[2000]; Assert.AreEqual(35, dataStream.Read(largeArray, 0, 2000)); }
private void ShrinkAndMakeSureDataIsIntact(DataStreamReadableWritable dataStream, int newLength) { dataStream.SetLength(newLength); Assert.AreEqual(newLength, dataStream.Length); dataStream.SetPosition(0); byte[] bytesRead = new byte[newLength]; dataStream.Read(bytesRead, 0, bytesRead.Length); for (int i = 0; i < newLength; i++) { Assert.AreEqual(bytesRead[i], (byte)(i / 10000)); } }
public void MakeSureShrinkingFreesDiskSpace() { var testCollaborators = TestCollaboratorsFactory.CreateCollaboratorsForTestingDataStreams(); DataStreamReadableWritable dataStream = testCollaborators.VirtualFileSystem.OpenFileForWriting(testCollaborators.FileInfo.FullPath); int valueCloseToDiskSize = (int)testCollaborators.Disk.DiskSizeInBytes - 25 * testCollaborators.Disk.BlockSizeInBytes; dataStream.SetLength(valueCloseToDiskSize); var fileSystem = testCollaborators.VirtualFileSystem; FileInfo newFileNode = fileSystem.CreateFile(@"\testFile2"); DataStreamReadableWritable newFileNodeStream = fileSystem.OpenFileForWriting(newFileNode.FullPath); ExceptionAssert.MakeSureExceptionIsRaisedBy <InsufficientSpaceException>( delegate { newFileNodeStream.SetLength(valueCloseToDiskSize); }); dataStream.SetLength(0); newFileNodeStream.SetLength(valueCloseToDiskSize); dataStream.SetPosition(0); const int answeringToAllQuestionsByte = 42; byte[] bytesToWrite = ByteBufferFactory.CreateByteBufferWithAllBytesSet(valueCloseToDiskSize, answeringToAllQuestionsByte); newFileNodeStream.Write(bytesToWrite, 0, bytesToWrite.Length); newFileNodeStream.SetPosition(0); var buffer = new byte[valueCloseToDiskSize]; newFileNodeStream.Read(buffer, 0, buffer.Length); MakeSureEachByteInArrayIsRight(buffer, answeringToAllQuestionsByte); }
private void TryWritingAndReadingBackSpecifiedByteArrays(byte[] testArray) { DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream(); Assert.AreEqual(0, dataStream.Position); Assert.AreEqual(0, dataStream.Length); dataStream.Write(testArray, 0, testArray.Length); Assert.AreEqual(testArray.Length, dataStream.Position); Assert.AreEqual(testArray.Length, dataStream.Length); var bytesRead = new byte[testArray.Length]; Assert.AreEqual(0, dataStream.Read(bytesRead, 0, testArray.Length)); // позиция в DataStream = testArray.Length; Прочитать ничего не получится. dataStream.SetPosition(0); dataStream.Read(bytesRead, 0, testArray.Length); CollectionAssert.AreEqual(testArray, bytesRead); }
public void TryReadingAndWritingPortionsOfDataByteByByte() { DataStreamReadableWritable dataStream = TestCollaboratorsFactory.CreateEmptyDataStream(); var stream = new MemoryStream(); var binaryWriter = new BinaryWriter(stream); const int numberOfIntegersToTest = 2561; for (int i = 0; i < numberOfIntegersToTest; i++) { binaryWriter.Write(i); } byte[] bytes = stream.ToArray(); for (int i = 0; i < bytes.Length; i++) { dataStream.Write(new byte[] { bytes[i] }, 0, 1); } dataStream.SetPosition(0); var bytesRead = new byte[dataStream.Length]; dataStream.Read(bytesRead, 0, dataStream.Length); var readingStream = new MemoryStream(bytesRead, false); var reader = new BinaryReader(readingStream); for (int i = 0; i < numberOfIntegersToTest; i++) { int number = reader.ReadInt32(); Assert.AreEqual(i, number); } }
public override void SetPosition(int newPosition) { _dataStream.SetPosition(newPosition); }