public void TestSetLength() { using (var stream = new MeasuringStream()) { Assert.Throws <ArgumentOutOfRangeException> (() => stream.SetLength(-1)); stream.SetLength(1024); Assert.AreEqual(1024, stream.Length); } }
public void TestSeek() { using (var stream = new MeasuringStream()) { stream.SetLength(1024); for (int attempt = 0; attempt < 10; attempt++) { long offset = random.Next() % stream.Length; stream.Position = offset; long actual = stream.Position; long expected = offset; Assert.AreEqual(expected, actual, "SeekOrigin.Begin"); Assert.AreEqual(expected, stream.Position, "Position"); if (offset > 0) { // seek backwards from current position offset = -1 * (random.Next() % offset); expected += offset; actual = stream.Seek(offset, SeekOrigin.Current); Assert.AreEqual(expected, actual, "SeekOrigin.Current (-)"); Assert.AreEqual(expected, stream.Position, "Position"); } if (actual < stream.Length) { // seek forwards from current position offset = random.Next() % (stream.Length - actual); expected += offset; actual = stream.Seek(offset, SeekOrigin.Current); Assert.AreEqual(expected, actual, "SeekOrigin.Current (+)"); Assert.AreEqual(expected, stream.Position, "Position"); } // seek backwards from the end of the stream offset = -1 * (random.Next() % stream.Length); expected = stream.Length + offset; actual = stream.Seek(offset, SeekOrigin.End); Assert.AreEqual(expected, actual, "SeekOrigin.End"); Assert.AreEqual(expected, stream.Position, "Position"); } Assert.Throws <IOException> (() => stream.Seek(-1, SeekOrigin.Begin)); } }