public void TestRandomSeeking() { for (int attempt = 0; attempt < 10; attempt++) { long offset = random.Next() % master.Length; long expected = master.Seek(offset, SeekOrigin.Begin); long actual = chained.Seek(offset, SeekOrigin.Begin); Assert.AreEqual(expected, actual, "Seeking the chained stream did not return the expected position"); AssertSeekResults("seeking to random position"); } }
public void TestRandomSeeking() { Assert.IsTrue(chained.CanSeek, "Expected to be able to seek in the chained stream."); for (int attempt = 0; attempt < 10; attempt++) { long offset = random.Next() % master.Length; var origin = (SeekOrigin)(attempt % 3); long expected, actual; switch (origin) { case SeekOrigin.Current: offset = offset - master.Position; break; case SeekOrigin.End: offset = offset - master.Length; break; } if (origin == SeekOrigin.Begin) { chained.Position = offset; master.Position = offset; expected = master.Position; actual = chained.Position; } else { expected = master.Seek(offset, origin); actual = chained.Seek(offset, origin); } Assert.AreEqual(expected, actual, "Seeking the chained stream did not return the expected position"); AssertSeekResults("seeking to random position"); } }
public void TestCanReadWriteSeek() { var buffer = new byte[1024]; using (var chained = new ChainedStream()) { chained.Add(new CanReadWriteSeekStream(true, false, false, false)); Assert.IsTrue(chained.CanRead); Assert.IsFalse(chained.CanWrite); Assert.IsFalse(chained.CanSeek); Assert.IsFalse(chained.CanTimeout); Assert.Throws <NotImplementedException> (() => chained.Read(buffer, 0, buffer.Length)); Assert.Throws <NotSupportedException> (() => chained.Write(buffer, 0, buffer.Length)); Assert.Throws <NotSupportedException> (() => chained.Seek(0, SeekOrigin.End)); } using (var chained = new ChainedStream()) { chained.Add(new CanReadWriteSeekStream(false, true, false, false)); Assert.IsFalse(chained.CanRead); Assert.IsTrue(chained.CanWrite); Assert.IsFalse(chained.CanSeek); Assert.IsFalse(chained.CanTimeout); Assert.Throws <NotSupportedException> (() => chained.Read(buffer, 0, buffer.Length)); Assert.Throws <NotImplementedException> (() => chained.Write(buffer, 0, buffer.Length)); Assert.Throws <NotSupportedException> (() => chained.Seek(0, SeekOrigin.End)); } using (var chained = new ChainedStream()) { chained.Add(new CanReadWriteSeekStream(false, false, true, false)); Assert.IsFalse(chained.CanRead); Assert.IsFalse(chained.CanWrite); Assert.IsTrue(chained.CanSeek); Assert.IsFalse(chained.CanTimeout); Assert.Throws <NotSupportedException> (() => chained.Read(buffer, 0, buffer.Length)); Assert.Throws <NotSupportedException> (() => chained.Write(buffer, 0, buffer.Length)); Assert.Throws <NotImplementedException> (() => chained.Seek(0, SeekOrigin.End)); } }
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 = chained.Seek(offset, SeekOrigin.Begin); Assert.AreEqual(expected, actual, "Seeking the chained stream did not return the expected position"); int n = (int)Math.Min(master.Length - master.Position, mbuf.Length); int nread = chained.Read(cbuf, 0, n); int mread = master.Read(mbuf, 0, n); Assert.AreEqual(mread, nread, "Did not read the expected number of bytes from the chained stream"); Assert.AreEqual(master.Position, chained.Position, "The chained stream's position did not match"); for (int i = 0; i < n; i++) { Assert.AreEqual(mbuf[i], cbuf[i], "The bytes read do not match"); } } }