示例#1
0
        public void SeekTest()
        {
            using (MemoryStream memstream = new MemoryStream(this.content))
                using (OffsetStream offsetstream = new OffsetStream(memstream, this.offset))
                {
                    Assert.True(offsetstream.CanSeek);
                    Assert.AreEqual(memstream.CanSeek, offsetstream.CanSeek);
                    Assert.AreEqual(memstream.Position + this.offset, offsetstream.Position);
                    long pos = offsetstream.Seek(10, SeekOrigin.Begin);
                    Assert.AreEqual(110, pos);
                    Assert.AreEqual(10, memstream.Position);
                    pos = offsetstream.Seek(0, SeekOrigin.End);
                    Assert.AreEqual(offsetstream.Length, pos);
                    Assert.AreEqual(memstream.Length, memstream.Position);
                    pos = offsetstream.Seek(0, SeekOrigin.Current);
                    Assert.AreEqual(offsetstream.Length, pos);
                    Assert.AreEqual(memstream.Length, memstream.Position);
                    offsetstream.Seek(10, SeekOrigin.Begin);
                    pos = offsetstream.Seek(10, SeekOrigin.Current);
                    Assert.AreEqual(this.offset + 20, pos);
                    Assert.AreEqual(20, memstream.Position);

                    // negative seek
                    pos = offsetstream.Seek(-10, SeekOrigin.Current);
                    Assert.AreEqual(this.offset + 10, pos);
                    Assert.AreEqual(10, memstream.Position);
                    pos = offsetstream.Seek(-10, SeekOrigin.Current);
                    Assert.AreEqual(this.offset, pos);
                    Assert.AreEqual(0, memstream.Position);

                    // seek into illegal areas
                    try
                    {
                        pos = offsetstream.Seek(-10, SeekOrigin.Current);
                        Assert.Fail();
                    }
                    catch (IOException)
                    {
                    }

                    Assert.AreEqual(this.offset, pos);
                    Assert.AreEqual(0, memstream.Position);
                }

            // Using an unseekable stream should return CanSeek = false
            var mockstream = new Mock <Stream>();

            mockstream.SetupGet(s => s.CanSeek).Returns(false);
            using (OffsetStream offsetstream = new OffsetStream(mockstream.Object)) {
                Assert.False(offsetstream.CanSeek);
            }
        }