Example #1
0
        private void PerformMutationOp_Seek()
        {
            int currPos = (int)_strmA.Position;
            int currLen = (int)_strmA.Length;

            double dice = _rng.NextDouble();

            if (dice < 0.33)
            {
                // Begin.
                int offset = (int)(_rng.NextDouble() * currLen);
                _strmA.Seek(offset, SeekOrigin.Begin);
                _strmB.Seek(offset, SeekOrigin.Begin);
                Debug.WriteLine(string.Format("Seek({0}, SeekOrigin.Begin) (pos was {1})", offset, currPos));
            }
            else if (dice >= 0.33 || dice < 0.66)
            {
                // Current.
                int offset = (int)(_rng.NextDouble() * (currLen - currPos));
                _strmA.Seek(offset, SeekOrigin.Current);
                _strmB.Seek(offset, SeekOrigin.Current);
                Debug.WriteLine(string.Format("Seek({0}, SeekOrigin.Current) (pos was {1})", offset, currPos));
            }
            else
            {
                // End.
                int offset = -(int)(_rng.NextDouble() * currLen);
                _strmA.Seek(offset, SeekOrigin.End);
                _strmB.Seek(offset, SeekOrigin.End);
                Debug.WriteLine(string.Format("Seek({0}, SeekOrigin.End) (pos was {1})", offset, currPos));
            }
        }
        public void Setup()
        {
            var bytes    = new byte[9 * 1024];
            int position = 0;

            random = new Random();
            random.NextBytes(bytes);

            // this is our master stream, all operations on the chained stream
            // should match the results on this stream
            master = new MemoryStream(bytes);
            mbuf   = new byte[4096];
            buf    = new byte[4096];

            // write the content into the memory block stream in random chunks
            blocks = new MemoryBlockStream();

            Assert.IsTrue(blocks.CanRead, "Expected to be able to read from the memory block stream.");
            Assert.IsTrue(blocks.CanWrite, "Expected to be able to write to the memory block stream.");
            Assert.IsTrue(blocks.CanSeek, "Expected to be able to seek in the memory block stream.");
            Assert.IsFalse(blocks.CanTimeout, "Did not expect to be able to set timeouts in the memory block stream.");

            while (position < bytes.Length)
            {
                int n = Math.Min(bytes.Length - position, random.Next() % 4096);
                blocks.Write(bytes, position, n);
                position += n;
            }

            blocks.Seek(0, SeekOrigin.Begin);
        }
Example #3
0
        async Task <BoundaryType> ConstructMimePartAsync(MimePart part, CancellationToken cancellationToken)
        {
            ScanContentResults results;
            Stream             content;

            if (persistent)
            {
                long begin = GetOffset(inputIndex);
                long end;

                using (var measured = new MeasuringStream()) {
                    results = await ScanContentAsync(measured, true, cancellationToken).ConfigureAwait(false);

                    end = begin + measured.Length;
                }

                content = new BoundStream(stream, begin, end, true);
            }
            else
            {
                content = new MemoryBlockStream();
                results = await ScanContentAsync(content, true, cancellationToken).ConfigureAwait(false);

                content.Seek(0, SeekOrigin.Begin);
            }

            if (!results.IsEmpty)
            {
                part.ContentObject = new ContentObject(content, part.ContentTransferEncoding);
            }

            return(results.Boundary);
        }
Example #4
0
        public MemoryBlockStreamTests()
        {
            var bytes    = new byte[9 * 1024];
            int position = 0;

            random = new Random();
            random.NextBytes(bytes);

            // this is our master stream, all operations on the chained stream
            // should match the results on this stream
            master = new MemoryStream(bytes);
            mbuf   = new byte[4096];
            buf    = new byte[4096];

            // write the content into the memory block stream in random chunks
            blocks = new MemoryBlockStream();

            while (position < bytes.Length)
            {
                int n = Math.Min(bytes.Length - position, random.Next() % 4096);
                blocks.Write(bytes, position, n);
                position += n;
            }

            blocks.Seek(0, SeekOrigin.Begin);
        }
Example #5
0
        async Task ConstructMimePartAsync(MimePart part, CancellationToken cancellationToken)
        {
            Stream content;
            bool   isEmpty;

            if (persistent)
            {
                long begin = GetOffset(inputIndex);
                long end;

                using (var measured = new MeasuringStream()) {
                    isEmpty = await ScanContentAsync(measured, true, cancellationToken).ConfigureAwait(false);

                    end = begin + measured.Length;
                }

                content = new BoundStream(stream, begin, end, true);
            }
            else
            {
                content = new MemoryBlockStream();
                isEmpty = await ScanContentAsync(content, true, cancellationToken).ConfigureAwait(false);

                content.Seek(0, SeekOrigin.Begin);
            }

            if (!isEmpty)
            {
                part.Content = new MimeContent(content, part.ContentTransferEncoding);
            }
            else
            {
                content.Dispose();
            }
        }
Example #6
0
        async Task ConstructMimePartAsync(MimePart part, MimeEntityEndEventArgs args, CancellationToken cancellationToken)
        {
            long endOffset, beginOffset = GetOffset(inputIndex);
            var  beginLineNumber = lineNumber;
            ScanContentResult result;
            Stream            content;

            if (persistent)
            {
                using (var measured = new MeasuringStream()) {
                    result = await ScanContentAsync(measured, true, cancellationToken).ConfigureAwait(false);

                    endOffset = beginOffset + measured.Length;
                }

                content = new BoundStream(stream, beginOffset, endOffset, true);
            }
            else
            {
                content = new MemoryBlockStream();

                try {
                    result = await ScanContentAsync(content, true, cancellationToken).ConfigureAwait(false);

                    content.Seek(0, SeekOrigin.Begin);
                } catch {
                    content.Dispose();
                    throw;
                }

                endOffset = beginOffset + content.Length;
            }

            args.Lines = GetLineCount(beginLineNumber, beginOffset, endOffset);

            if (!result.IsEmpty)
            {
                part.Content = new MimeContent(content, part.ContentTransferEncoding)
                {
                    NewLineFormat = result.Format
                }
            }
            ;
            else
            {
                content.Dispose();
            }
        }
Example #7
0
        public void TestSeek()
        {
            for (int attempt = 0; attempt < 10; attempt++)
            {
                long offset = random.Next() % master.Length;

                long expected = master.Seek(offset, SeekOrigin.Begin);
                long actual   = blocks.Seek(offset, SeekOrigin.Begin);

                Assert.AreEqual(expected, actual, "SeekOrigin.Begin");

                AssertSeekResults();
                master.Seek(actual, SeekOrigin.Begin);
                blocks.Seek(actual, SeekOrigin.Begin);

                // seek backwards from current position
                offset = -1 * (random.Next() % offset);

                expected = master.Seek(offset, SeekOrigin.Current);
                actual   = blocks.Seek(offset, SeekOrigin.Current);

                Assert.AreEqual(expected, actual, "SeekOrigin.Current (-)");

                AssertSeekResults();
                master.Seek(actual, SeekOrigin.Begin);
                blocks.Seek(actual, SeekOrigin.Begin);

                // seek forwards from current position
                offset = random.Next() % (master.Length - actual);

                expected = master.Seek(offset, SeekOrigin.Current);
                actual   = blocks.Seek(offset, SeekOrigin.Current);

                Assert.AreEqual(expected, actual, "SeekOrigin.Current (+)");

                AssertSeekResults();

                // seek backwards from the end of the stream
                offset = -1 * (random.Next() % master.Length);

                expected = master.Seek(offset, SeekOrigin.End);
                actual   = blocks.Seek(offset, SeekOrigin.End);

                Assert.AreEqual(expected, actual, "SeekOrigin.End");

                AssertSeekResults();
            }

            Assert.Throws <IOException> (() => blocks.Seek(-1, SeekOrigin.Begin));
        }
        public void TestSeeking()
        {
            for (int attempt = 0; attempt < 10; attempt++)
            {
                long offset = random.Next() % master.Length;

                long expected = master.Seek(offset, SeekOrigin.Begin);
                long actual   = blocks.Seek(offset, SeekOrigin.Begin);

                Assert.AreEqual(expected, actual, "Seeking the memory block stream did not return the expected position");

                int n     = (int)Math.Min(master.Length - master.Position, mbuf.Length);
                int mread = master.Read(mbuf, 0, n);
                int nread = blocks.Read(buf, 0, n);

                Assert.AreEqual(mread, nread, "Did not read the expected number of bytes from the memory block stream");
                Assert.AreEqual(master.Position, blocks.Position, "The memory block stream's position did not match");

                for (int i = 0; i < n; i++)
                {
                    Assert.AreEqual(mbuf[i], buf[i], "The bytes read do not match");
                }
            }
        }