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));
        }
        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);
        }