public void BufferedSourceExtensions_ReadAll()
        {
            int testSampleSize = 1163;
            long skipSize = long.MaxValue - (long)testSampleSize;

            // чтение больше буфера
            int srcBufSize = testSampleSize / 3;
            var src = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
            src.TrySkip (skipSize);
            var result = BufferedSourceExtensions.ReadAllBytes (src);
            for (int i = 0; i < testSampleSize; i++)
            {
                Assert.AreEqual (FillFunction ((long)(skipSize + i)), result[i]);
            }

            // чтение меньше буфера
            srcBufSize = testSampleSize * 9;
            src = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
            src.TrySkip (skipSize);
            result = BufferedSourceExtensions.ReadAllBytes (src);
            for (int i = 0; i < testSampleSize; i++)
            {
                Assert.AreEqual (FillFunction ((long)(skipSize + i)), result[i]);
            }
        }
        public void TemplateSeparatedBufferedSource_RequestSkipPart()
        {
            byte templPos = 162;
            var separator = new byte[]
            {
                FillFunction (templPos),
                FillFunction (templPos + 1),
                FillFunction (templPos + 2),
                FillFunction (templPos + 3),
                FillFunction (templPos + 4)
            };
            long skipBeforeLimitingSize = 0xfffffffd;
            long secondPartPos = (skipBeforeLimitingSize | 0xffL) + 1L + (long)templPos + separator.Length;
            int skipBufferSize = 93;
            int srcBufSize = 32768;

            // части в середине источника
            var subSrc = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
            subSrc.TrySkip (skipBeforeLimitingSize);
            var src = new TemplateSeparatedBufferedSource (subSrc, separator);
            src.EnsureBuffer (skipBufferSize + 3);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + 2), src.Buffer[src.Offset + 2]);
            src.SkipBuffer (skipBufferSize);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + 2), src.Buffer[src.Offset + 2]);
            Assert.IsTrue (src.TrySkipPart ());
            src.EnsureBuffer (3);
            Assert.AreEqual (FillFunction (secondPartPos), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (secondPartPos + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (secondPartPos + 2), src.Buffer[src.Offset + 2]);
            Assert.AreEqual (256 - separator.Length, src.TrySkip (long.MaxValue));

            // части в конце источника
            long size = 4611686018427387904L;
            subSrc = new BigBufferedSourceMock (size, srcBufSize, FillFunction);
            subSrc.TrySkip (size - 256 - 256 - 20); // отступаем так чтобы осталось две части с хвостиком
            src = new TemplateSeparatedBufferedSource (subSrc, separator);
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsFalse (src.TrySkipPart ());

            // разделитель в конце источника
            separator = new byte[]
            {
                FillFunction (253),
                FillFunction (254),
                FillFunction (255)
            };
            subSrc = new BigBufferedSourceMock (768, srcBufSize, FillFunction);
            src = new TemplateSeparatedBufferedSource (subSrc, separator);
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsFalse (src.TrySkipPart ());
        }
        public void SizeLimitedBufferedSource_RequestSkip()
        {
            long skipBeforeLimitingSize = int.MaxValue;
            int skipBufferSize = 123;
            long skipInsideLimitingSize = 562945658454016;

            // ограничение больше буфера
            int srcBufSize = 32768;
            long limitingSize = srcBufSize + 4611686018427387904L;
            var subSrc = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
            subSrc.TrySkip (skipBeforeLimitingSize);

            var src = new SizeLimitedBufferedSource (subSrc, limitingSize);
            src.EnsureBuffer (skipBufferSize + 3);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize+1), src.Buffer[src.Offset+1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize+2), src.Buffer[src.Offset+2]);
            src.SkipBuffer (skipBufferSize);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + 2), src.Buffer[src.Offset + 2]);
            Assert.AreEqual (skipInsideLimitingSize, src.TrySkip (skipInsideLimitingSize));
            src.EnsureBuffer (3);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + skipInsideLimitingSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + skipInsideLimitingSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + skipInsideLimitingSize + 2), src.Buffer[src.Offset + 2]);
            Assert.AreEqual (limitingSize - skipBufferSize - skipInsideLimitingSize, src.TrySkip (long.MaxValue));

            // ограничение меньше буфера
            srcBufSize = 32767;
            limitingSize = 1293L;
            subSrc = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
            subSrc.TrySkip (skipBeforeLimitingSize);

            src = new SizeLimitedBufferedSource (subSrc, limitingSize);
            src.EnsureBuffer (skipBufferSize + 3);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + 2), src.Buffer[src.Offset + 2]);
            src.SkipBuffer (skipBufferSize);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + 2), src.Buffer[src.Offset + 2]);
            Assert.AreEqual (skipBufferSize, src.TrySkip (skipBufferSize));
            src.EnsureBuffer (3);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + skipBufferSize), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + skipBufferSize + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (skipBeforeLimitingSize + skipBufferSize + skipBufferSize + 2), src.Buffer[src.Offset + 2]);
            Assert.AreEqual (limitingSize - skipBufferSize - skipBufferSize, src.TrySkip (long.MaxValue));
        }
 public void BufferedSourceExtensions_Read()
 {
     int srcBufSize = 32768;
     int testSampleSize = 68;
     int readBufSize = 1000;
     int readBufOffset = 512;
     var src = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
     src.FillBuffer ();
     var skip = srcBufSize - testSampleSize;
     src.SkipBuffer (skip);
     var buf = new byte[readBufSize];
     Assert.AreEqual (testSampleSize, BufferedSourceExtensions.Read (src, buf, readBufOffset, testSampleSize));
     for (int i = 0; i < testSampleSize; i++)
     {
         Assert.AreEqual (FillFunction ((long)(skip + i)), buf[readBufOffset + i]);
     }
     src.TrySkip (long.MaxValue - (long)srcBufSize - 3);
     Assert.AreEqual (3, BufferedSourceExtensions.Read (src, buf, 0, buf.Length));
 }
        public void BufferedSourceExtensions_IsEmpty()
        {
            var src = new BigBufferedSourceMock (0, 1, FillFunction);
            src.FillBuffer ();
            Assert.IsTrue (BufferedSourceExtensions.IsEmpty (src));

            src = new BigBufferedSourceMock (1, 1, FillFunction);
            src.FillBuffer ();
            Assert.IsFalse (BufferedSourceExtensions.IsEmpty (src));
            src.SkipBuffer (1);
            Assert.IsTrue (BufferedSourceExtensions.IsEmpty (src));

            src = new BigBufferedSourceMock (long.MaxValue, 32768, FillFunction);
            src.FillBuffer ();
            Assert.IsFalse (BufferedSourceExtensions.IsEmpty (src));
            src.TrySkip (long.MaxValue - 1);
            src.FillBuffer ();
            Assert.IsFalse (BufferedSourceExtensions.IsEmpty (src));
            src.SkipBuffer (1);
            Assert.IsTrue (BufferedSourceExtensions.IsEmpty (src));
        }
        public void EvaluatorPartitionedBufferedSourceBase_ReadAndSkipPart()
        {
            long skipBeforeLimitingSize = 0x408c5f081052008cL;

            long firstPartPos = 0x408c5f08105200c0L;
            long secondPartPos = 0x408c5f08105200ccL;
            long thirdPartPos = 0x408c5f0810520100L;
            int srcBufSize = 32768;

            // части в середине источника
            var subSrc = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
            subSrc.TrySkip (skipBeforeLimitingSize);
            var src = new OneHundredEvaluatorBufferedSource (subSrc);
            Assert.AreEqual (0, src.FillBuffer ());
            Assert.IsTrue (src.TrySkipPart ());
            src.EnsureBuffer (3);
            Assert.AreEqual (FillFunction (firstPartPos), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (firstPartPos + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (firstPartPos + 2), src.Buffer[src.Offset + 2]);
            Assert.IsTrue (src.TrySkipPart ());
            src.EnsureBuffer (3);
            Assert.AreEqual (FillFunction (secondPartPos), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (secondPartPos + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (secondPartPos + 2), src.Buffer[src.Offset + 2]);
            Assert.IsTrue (src.TrySkipPart ());
            src.EnsureBuffer (3);
            Assert.AreEqual (FillFunction (thirdPartPos), src.Buffer[src.Offset]);
            Assert.AreEqual (FillFunction (thirdPartPos + 1), src.Buffer[src.Offset + 1]);
            Assert.AreEqual (FillFunction (thirdPartPos + 2), src.Buffer[src.Offset + 2]);

            // части в конце источника
            long size = 0x4000000000000000L;
            subSrc = new BigBufferedSourceMock (size, srcBufSize, FillFunction);
            subSrc.TrySkip (0x3fffffffffffffc0L); // отступаем так чтобы осталось две части с хвостиком
            src = new OneHundredEvaluatorBufferedSource (subSrc);
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsTrue (src.TrySkipPart ());
            Assert.IsFalse (src.TrySkipPart ());
        }
 public void BufferedSourceExtensions_WriteTo()
 {
     int testSampleSize = 1163;
     long skipSize = long.MaxValue - (long)testSampleSize;
     int srcBufSize = testSampleSize / 3;
     var src = new BigBufferedSourceMock (long.MaxValue, srcBufSize, FillFunction);
     src.TrySkip (skipSize);
     byte[] result;
     using (var dst = new MemoryStream ())
     {
         Assert.AreEqual (testSampleSize, BufferedSourceExtensions.WriteTo (src, dst));
         result = dst.GetBuffer ();
     }
     for (int i = 0; i < testSampleSize; i++)
     {
         Assert.AreEqual (FillFunction ((long)(skipSize + i)), result[i]);
     }
 }
        private static void TransformAllTest(
			int inBlockSize,
			int outBlockSize,
			int inputCacheBlocks,
			int dataSize,
			int bufSize,
			bool canTransformMultipleBlocks,
			int transformBufferSize)
        {
            var src = new BigBufferedSourceMock (dataSize, bufSize, FillFunction);
            var mock = new CryptoTransformMock (inBlockSize, outBlockSize, inputCacheBlocks, canTransformMultipleBlocks);
            var transform = new CryptoTransformingBufferedSource (src, mock, new byte[transformBufferSize]);

            var fullBlocks = dataSize / inBlockSize;
            var inReminder = dataSize - fullBlocks * inBlockSize;
            var outReminder = Math.Min (inReminder, outBlockSize);
            var resultSize = fullBlocks * outBlockSize + outReminder;

            // считываем весь результат преобразования
            int len = 0;
            var result = new byte[resultSize];
            int available;
            while ((available = transform.FillBuffer ()) > 0)
            {
                Array.Copy (transform.Buffer, transform.Offset, result, len, available);
                len += available;
                transform.SkipBuffer (available);
            }
            Assert.IsTrue (transform.IsExhausted);
            Assert.AreEqual (resultSize, len);

            // проверяем что результат преобразования совпадает с тестовым массивом
            for (var i = 0; i < fullBlocks; i++)
            {
                for (int j = 0; j < outBlockSize; j++)
                {
                    var inIndex = i * inBlockSize + (j % inBlockSize);
                    var outIndex = i * outBlockSize + j;
                    Assert.AreEqual (FillFunction (inIndex), (byte)~result[outIndex]);
                }
            }
            for (int j = 0; j < outReminder; j++)
            {
                var inIndex = fullBlocks * inBlockSize + (j % inBlockSize);
                var outIndex = fullBlocks * outBlockSize + j;
                Assert.AreEqual (FillFunction (inIndex), (byte)~result[outIndex]);
            }
        }
        private static void SkipTransformChunkTest(
			int inBlockSize,
			int outBlockSize,
			int inputCacheBlocks,
			long dataSize,
			int bufSize,
			bool canTransformMultipleBlocks,
			int transformBufferSize,
			long totalSkip,
			int bufferSkip)
        {
            var src = new BigBufferedSourceMock (dataSize, bufSize, FillFunction);
            var mock = new CryptoTransformMock (inBlockSize, outBlockSize, inputCacheBlocks, canTransformMultipleBlocks);
            var transform = new CryptoTransformingBufferedSource (src, mock, new byte[transformBufferSize]);

            Assert.AreEqual (totalSkip, transform.TrySkip (totalSkip));
            transform.EnsureBuffer (3);
            Assert.AreEqual ((byte)~FillFunction (MapTransformIndexBackToOriginal (totalSkip, mock)), transform.Buffer[transform.Offset]);
            Assert.AreEqual ((byte)~FillFunction (MapTransformIndexBackToOriginal (totalSkip + 1, mock)), transform.Buffer[transform.Offset + 1]);
            Assert.AreEqual ((byte)~FillFunction (MapTransformIndexBackToOriginal (totalSkip + 2, mock)), transform.Buffer[transform.Offset + 2]);
            transform.EnsureBuffer (bufferSkip);
            transform.SkipBuffer (bufferSkip);
            transform.EnsureBuffer (3);
            Assert.AreEqual ((byte)~FillFunction (MapTransformIndexBackToOriginal (totalSkip + bufferSkip, mock)), transform.Buffer[transform.Offset]);
            Assert.AreEqual ((byte)~FillFunction (MapTransformIndexBackToOriginal (totalSkip + bufferSkip + 1, mock)), transform.Buffer[transform.Offset + 1]);
            Assert.AreEqual ((byte)~FillFunction (MapTransformIndexBackToOriginal (totalSkip + bufferSkip + 2, mock)), transform.Buffer[transform.Offset + 2]);
        }