Example #1
0
        public void TestConsistenceChecks()
        {
            var rand   = new Random();
            var buffer = new byte[2048];

            rand.NextBytes(buffer);

            // Test peek after read (should thow an InvalidOperationException)
            using (var sourceStream = new MemoryStream(buffer))
                using (var peekableStream = new ForwardOnlyPeekableStream(sourceStream))
                {
                    peekableStream.Read(new byte[1024], 0, 1024);

                    Assert.Throws <InvalidOperationException>(() =>
                    {
                        peekableStream.Peek(256);
                    });
                }

            // Test peek twice (should thow an InvalidOperationException)
            using (var sourceStream = new MemoryStream(buffer))
                using (var peekableStream = new ForwardOnlyPeekableStream(sourceStream))
                {
                    peekableStream.Peek(256);

                    Assert.Throws <InvalidOperationException>(() =>
                    {
                        peekableStream.Peek(256);
                    });
                }
        }
Example #2
0
        public void TestPeek(string data)
        {
            using (var sourceStream = new MemoryStream(Encoding.ASCII.GetBytes(data)))
                using (var peekableStream = new ForwardOnlyPeekableStream(sourceStream))
                {
                    int peekSize     = 256;
                    var peekedBytes  = peekableStream.Peek(peekSize);
                    var peekedString = Encoding.ASCII.GetString(peekedBytes);
                    var testData     = data.Length > peekSize?data.Substring(0, peekSize) : data;

                    Assert.Equal(testData, peekedString);
                }
        }
Example #3
0
        public void TestPartialRead(int dataLength, int peekSize, int bufferSize)
        {
            var data = new byte[dataLength];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)i;
            }

            using (var sourceStream = new MemoryStream(data))
                using (var peekableStream = new ForwardOnlyPeekableStream(sourceStream))
                {
                    var peekedBytes = peekableStream.Peek(peekSize);

                    for (int i = 0; i < peekedBytes.Length; i++)
                    {
                        Assert.Equal((byte)i, peekedBytes[i]);
                    }

                    var buffer = new byte[bufferSize];
                    int readBytes;
                    var testList = new List <byte>();

                    while ((readBytes = peekableStream.Read(buffer, 0, bufferSize)) > 0)
                    {
                        for (int i = 0; i < readBytes; i++)
                        {
                            testList.Add(buffer[i]);
                        }
                    }

                    for (int i = 0; i < dataLength; i++)
                    {
                        Assert.Equal(data[i], testList[i]);
                    }
                }
        }