public void Test_HtmlStream4() { int bufsize = 10; MemoryStream baseStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyz")); HtmlStream htmlstream = new HtmlStream(baseStream, bufsize); byte[] buf = new byte[15]; // Test reading up to and beyond the cache size (we should still cache it all)... Assert.AreEqual(false, htmlstream.CanRewind); htmlstream.Read(buf, 0, buf.Length); Assert.AreEqual(true, htmlstream.CanRewind); // Seek to the beginning htmlstream.Rewind(); Assert.AreEqual(true, htmlstream.CanRewind); // Ensure the base stream is still in position (i.e. didn't get touched) Assert.AreEqual(15, baseStream.Position); // We should now reading fully from the cache byte[] buf2 = new byte[15]; htmlstream.Read(buf2, 0, 15); Assert.AreEqual("abcdefghijklmno", System.Text.Encoding.ASCII.GetString(buf2)); Assert.IsTrue(htmlstream.CanRewind, "CanRewind should be true, since we should have expanded our cache"); }
public void Test_HtmlStream3() { int bufsize = 10; MemoryStream baseStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyz")); HtmlStream htmlstream = new HtmlStream(baseStream, bufsize); byte[] buf = new byte[bufsize]; // Test reading up to cache size... Assert.AreEqual(false, htmlstream.CanRewind); htmlstream.Read(buf, 0, 10); Assert.AreEqual(true, htmlstream.CanRewind); // Seek to the beginning htmlstream.Rewind(); Assert.AreEqual(true, htmlstream.CanRewind); // Ensure the base stream is still in position (i.e. didn't get touched) Assert.AreEqual(10, baseStream.Position); // We should now reading from the cache, and in this case // a little bit of the real stream... byte[] buf2 = new byte[15]; htmlstream.Read(buf2, 0, 15); Assert.AreEqual("abcdefghijklmno", System.Text.Encoding.ASCII.GetString(buf2)); Assert.IsFalse(htmlstream.CanRewind, "CanRewind should be false, since we should be past the cache"); }
public void Test_HtmlStream2() { int bufsize = 10; MemoryStream baseStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyz")); HtmlStream htmlstream = new HtmlStream(baseStream, bufsize); byte[] buf = new byte[bufsize]; // Test reading up to cache size... Assert.AreEqual(false, htmlstream.CanRewind); htmlstream.Read(buf, 0, 10); Assert.AreEqual(true, htmlstream.CanRewind); // Seek to the beginning - after which we should no longer be able to seek htmlstream.Rewind(); Assert.AreEqual(true, htmlstream.CanRewind); // Ensure the base stream is still in position (i.e. didn't get touched) Assert.AreEqual(10, baseStream.Position); // We should now reading from the cache byte[] buf2 = new byte[bufsize]; htmlstream.Read(buf2, 0, 5); Assert.AreEqual("abcde", System.Text.Encoding.ASCII.GetString(buf2, 0, 5)); // Base stream is still in position Assert.AreEqual(10, baseStream.Position); // If we are reading from cache, we should be able to seek to beginning again Assert.AreEqual(true, htmlstream.CanRewind); htmlstream.Read(buf2, 5, 5); Assert.AreEqual("abcdefghij", System.Text.Encoding.ASCII.GetString(buf2)); Assert.AreEqual(true, htmlstream.CanRewind); // Now move just past the cache size... We should no longer be able to seek to origin htmlstream.Read(buf2, 0, 1); Assert.AreEqual(false, htmlstream.CanRewind); }