| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- //
- // System.IO.BufferedStream Unit Tests
- //
- // Authors:
- // Ville Palo ([email protected])
- // Sebastien Pouliot <[email protected]>
- //
- // (C) 2003 Ville Palo
- // Copyright (C) 2004 Novell (http://www.novell.com)
- //
- using NUnit.Framework;
- using System.IO;
- using System.Text;
- using System;
- namespace MonoTests.System.IO {
- [TestFixture]
- public class BufferedStreamTest : Assertion {
-
- private MemoryStream mem;
-
- [SetUp]
- protected void SetUp ()
- {
- mem = new MemoryStream ();
- }
- [TearDown]
- protected void TearDown ()
- {
- //Some tests might mess with mem, so let's check it first
- if (mem != null)
- mem.Close ();
- }
- [Test]
- public void Ctor ()
- {
- MemoryStream str = new MemoryStream ();
- str.Write (new byte [] {1, 2, 3, 4, 5, 6}, 0, 6);
- BufferedStream stream = new BufferedStream (str);
-
- AssertEquals ("test#01", true, stream.CanRead);
- AssertEquals ("test#02", true, stream.CanSeek);
- AssertEquals ("test#03", true, stream.CanWrite);
- AssertEquals ("test#04", 6, stream.Length);
- AssertEquals ("test#05", 6, stream.Position);
-
- string path = Path.GetTempFileName ();
- if (File.Exists (path))
- File.Delete (path);
-
- FileStream file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
- stream = new BufferedStream (file);
- AssertEquals ("test#06", false, stream.CanRead);
- AssertEquals ("test#07", true, stream.CanSeek);
- AssertEquals ("test#08", true, stream.CanWrite);
- AssertEquals ("test#09", 0, stream.Length);
- AssertEquals ("test#10", 0, stream.Position);
- file.Close ();
-
- if (File.Exists (path))
- File.Delete (path);
-
- file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
- stream = new BufferedStream (file, 12);
- AssertEquals ("test#11", false, stream.CanRead);
- AssertEquals ("test#12", true, stream.CanSeek);
- AssertEquals ("test#13", true, stream.CanWrite);
- AssertEquals ("test#14", 0, stream.Length);
- AssertEquals ("test#15", 0, stream.Position);
- file.Close ();
- if (File.Exists (path))
- File.Delete (path);
- }
- /// <summary>
- /// Throws an exception if stream is null
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void CtorNullExceptionStream ()
- {
- BufferedStream stream = new BufferedStream (null);
- }
- /// <summary>
- /// Throws an exception if stream is null
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void CtorNullExceptionStream1 ()
- {
- BufferedStream stream = new BufferedStream (null, 12);
- }
-
- /// <summary>
- /// Throws an exception if stream is null
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void CtorOutOfRangeExceptionStream1 ()
- {
- MemoryStream str = new MemoryStream ();
- BufferedStream stream = new BufferedStream (str, -12);
- }
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void CtorOutOfRangeException2 ()
- {
- MemoryStream str = new MemoryStream ();
- str.Close ();
- BufferedStream stream = new BufferedStream (str);
- }
-
- [Test]
- public void Close1 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Close ();
- stream.Close ();
- }
- [Test]
- public void Close2 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Close ();
- AssertEquals ("test#01", false, stream.CanRead);
- AssertEquals ("test#02", false, stream.CanSeek);
- AssertEquals ("test#03", false, stream.CanWrite);
- }
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void Close3 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Close ();
- long l = stream.Position;
- }
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void Close4 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Close ();
- long l = stream.Length;
- }
-
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void Close5 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Close ();
- stream.WriteByte (1);
- }
-
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void Close6 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Close ();
- stream.ReadByte ();
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException))]
- public void Close7 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- mem.Close ();
- stream.WriteByte (1);
- }
-
- [Test]
- public void Read ()
- {
- mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
- BufferedStream stream = new BufferedStream (mem);
- byte [] bytes = new byte [10];
- stream.Read (bytes, 0, 3);
- AssertEquals ("test#01", 0, bytes [0]);
- AssertEquals ("test#02", 0, bytes [1]);
- AssertEquals ("test#03", 0, bytes [2]);
- stream.Seek (0, SeekOrigin.Begin);
- stream.Read (bytes, 0, 3);
- AssertEquals ("test#04", 0, bytes [0]);
- AssertEquals ("test#05", 1, bytes [1]);
- AssertEquals ("test#06", 2, bytes [2]);
- AssertEquals ("test#07", 0, bytes [0]);
- stream.Read (bytes, 5, 3);
- AssertEquals ("test#08", 3, bytes [5]);
- AssertEquals ("test#09", 4, bytes [6]);
- AssertEquals ("test#10", 5, bytes [7]);
- AssertEquals ("test#11", 0, bytes [8]);
- stream.Read (bytes, 0, 10);
- AssertEquals ("test#12", 3, bytes [5]);
- AssertEquals ("test#13", 4, bytes [6]);
- AssertEquals ("test#14", 5, bytes [7]);
- AssertEquals ("test#15", 0, bytes [9]);
- }
-
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void ReadException ()
- {
- mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
- BufferedStream stream = new BufferedStream (mem);
- byte [] bytes = new byte [10];
- stream.Read (bytes, 0, 30);
- }
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void Read_Null ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Read (null, 0, 0);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException))]
- public void Read_CantRead ()
- {
- WriteOnlyStream wo = new WriteOnlyStream ();
- BufferedStream stream = new BufferedStream (wo);
- stream.Read (new byte [1], 0, 1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Read_OffsetNegative ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Read (new byte [1], -1, 1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void Read_OffsetOverflow ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Read (new byte [1], Int32.MaxValue, 1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Read_CountNegative ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Read (new byte [1], 1, -1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void Read_CountOverflow ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Read (new byte [1], 1, Int32.MaxValue);
- }
-
- [Test]
- public void ReadByte ()
- {
- mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
- BufferedStream stream = new BufferedStream (mem);
- AssertEquals ("test#01", -1, stream.ReadByte ());
- AssertEquals ("test#02", -1, stream.ReadByte ());
- AssertEquals ("test#03", -1, stream.ReadByte ());
- stream.Seek (0, SeekOrigin.Begin);
- AssertEquals ("test#04", 0, stream.ReadByte ());
- AssertEquals ("test#05", 1, stream.ReadByte ());
- AssertEquals ("test#06", 2, stream.ReadByte ());
- AssertEquals ("test#07", 3, stream.ReadByte ());
- }
-
- [Test]
- public void Write ()
- {
- BufferedStream stream = new BufferedStream (mem);
-
- stream.Write (new byte [] {0, 1, 2, 3, 4}, 0, 4);
- AssertEquals ("test#01", 4, stream.Length);
- byte [] bytes = mem.GetBuffer ();
- AssertEquals ("test#02", 0, bytes [0]);
- AssertEquals ("test#03", 1, bytes [1]);
- AssertEquals ("test#04", 2, bytes [2]);
- AssertEquals ("test#05", 3, bytes [3]);
- bytes = new byte [] {1, 4, 3};
- stream.Write (bytes, 0, 3);
- stream.Flush ();
- bytes = mem.GetBuffer ();
- AssertEquals ("test#06", 0, bytes [0]);
- AssertEquals ("test#07", 1, bytes [1]);
- AssertEquals ("test#08", 2, bytes [2]);
- AssertEquals ("test#09", 3, bytes [3]);
- AssertEquals ("test#10", 1, bytes [4]);
- AssertEquals ("test#11", 4, bytes [5]);
- AssertEquals ("test#10", 3, bytes [6]);
- AssertEquals ("test#11", 0, bytes [7]);
- AssertEquals ("test#12", 7, stream.Length);
- }
-
- [Test]
- [ExpectedException(typeof (ArgumentException))]
- public void WriteException ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [] {0,1,2,3}, 0, 10);
- }
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void Write_Null ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (null, 0, 0);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException))]
- public void Write_CantWrite ()
- {
- ReadOnlyStream ro = new ReadOnlyStream ();
- BufferedStream stream = new BufferedStream (ro);
- stream.Write (new byte [1], 0, 1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Write_OffsetNegative ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [1], -1, 1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void Write_OffsetOverflow ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [1], Int32.MaxValue, 1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void Write_CountNegative ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [1], 1, -1);
- }
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void Write_CountOverflow ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [1], 1, Int32.MaxValue);
- }
-
- [Test]
- public void WriteByte ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.WriteByte (1);
- stream.WriteByte (2);
- stream.WriteByte (3);
- stream.Flush ();
- AssertEquals ("test#01", 256, mem.GetBuffer ().Length);
- AssertEquals ("test#02", 3, stream.Length);
- AssertEquals ("test#03", 1, mem.GetBuffer () [0]);
- AssertEquals ("test#04", 2, mem.GetBuffer () [1]);
- AssertEquals ("test#05", 3, mem.GetBuffer () [2]);
- }
-
- [Test]
- public void Flush ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.WriteByte (1);
- stream.WriteByte (2);
-
- byte [] bytes = mem.GetBuffer ();
- AssertEquals ("test#01", 0, bytes.Length);
- stream.Flush ();
-
- bytes = mem.GetBuffer ();
- AssertEquals ("test#02", 256, bytes.Length);
- AssertEquals ("test#03", 1, bytes [0]);
- AssertEquals ("test#04", 2, bytes [1]);
- mem.Close ();
- mem = new MemoryStream ();
- bytes = new byte [] {0, 1, 2, 3, 4, 5};
- stream = new BufferedStream (mem);
- stream.Write (bytes, 0, 2);
- AssertEquals ("test#05", 2, stream.Length);
- bytes = mem.GetBuffer ();
- AssertEquals ("test#06", 256, bytes.Length);
- AssertEquals ("test#07", 0, bytes [0]);
- AssertEquals ("test#08", 1, bytes [1]);
-
- stream.Write (bytes, 0, 2);
-
- bytes = mem.GetBuffer ();
- AssertEquals ("test#09", 0, bytes [0]);
- AssertEquals ("test#10", 1, bytes [1]);
- AssertEquals ("test#11", 0, bytes [2]);
- AssertEquals ("test#12", 0, bytes [3]);
- stream.Flush ();
- bytes = mem.GetBuffer ();
- AssertEquals ("test#13", 0, bytes [2]);
- AssertEquals ("test#14", 1, bytes [3]);
- }
-
- [Test]
- public void Seek ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [] {0, 1, 2, 3, 4, 5}, 0, 6);
-
- AssertEquals ("test#01", 6, stream.Position);
-
- stream.Seek (-5, SeekOrigin.End);
- AssertEquals ("test#02", 1, stream.Position);
-
- stream.Seek (3, SeekOrigin.Current);
- AssertEquals ("test#03", 4, stream.Position);
-
- stream.Seek (300, SeekOrigin.Current);
- AssertEquals ("test#04", 304, stream.Position);
- }
-
- [Test]
- [ExpectedException(typeof (IOException))]
- public void SeekException ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Seek (-1, SeekOrigin.Begin);
- }
-
- [Test]
- public void SetLength ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6);
-
- AssertEquals ("test#01", 6, stream.Length);
- stream.SetLength (60);
- AssertEquals ("test#02", 60, stream.Length);
-
- stream.SetLength (2);
- AssertEquals ("test#03", 2, stream.Length);
- }
-
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void SetLengthException ()
- {
- BufferedStream stream = new BufferedStream (mem);
- stream.SetLength (-1);
- }
- [Test]
- [ExpectedException(typeof(NotSupportedException))]
- public void SetLengthException2 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- mem.Close ();
- stream.SetLength (1);
- }
- [Test]
- public void SetLengthException3 ()
- {
- BufferedStream stream = new BufferedStream (mem);
- mem = null;
- // Strangely, this does not throw an exception on .NET 1.1
- stream.SetLength (1);
- }
-
- [Test]
- public void Position ()
- {
- mem = new MemoryStream ();
- BufferedStream stream = new BufferedStream (mem,5);
- stream.Write (new byte [] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, 0, 16);
-
- stream.Position = 0;
- AssertEquals ("test#01", 0, stream.Position);
- AssertEquals ("test#02", 0, stream.ReadByte ());
-
- stream.Position = 5;
- AssertEquals ("test#01", 5, stream.Position);
- AssertEquals ("test#02", 5, stream.ReadByte ());
-
- // Should not need to read from the underlying stream:
- stream.Position = 7;
- AssertEquals ("test#01", 7, stream.Position);
- AssertEquals ("test#02", 7, stream.ReadByte ());
-
- // Should not need to read from the underlying stream:
- stream.Position = 5;
- AssertEquals ("test#01", 5, stream.Position);
- AssertEquals ("test#02", 5, stream.ReadByte ());
-
- // Should not need to read from the underlying stream:
- stream.Position = 9;
- AssertEquals ("test#01", 9, stream.Position);
- AssertEquals ("test#02", 9, stream.ReadByte ());
-
- stream.Position = 10;
- AssertEquals ("test#01", 10, stream.Position);
- AssertEquals ("test#02", 10, stream.ReadByte ());
-
- stream.Position = 9;
- AssertEquals ("test#01", 9, stream.Position);
- AssertEquals ("test#02", 9, stream.ReadByte ());
- }
- [Test]
- public void PositionAfterSetLength ()
- {
- BufferedStream stream = new BufferedStream (new MemoryStream ());
- stream.SetLength (32);
- stream.Position = 32;
- stream.SetLength (16);
- AssertEquals ("Position==16", 16, stream.Position);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void SetLength_Disposed ()
- {
- BufferedStream stream = new BufferedStream (new MemoryStream ());
- stream.Close ();
- stream.SetLength (16);
- }
- [Test]
- [ExpectedException (typeof (NotSupportedException))]
- public void Seek_ClosedMemoryStream ()
- {
- MemoryStream ms = new MemoryStream ();
- BufferedStream stream = new BufferedStream (ms);
- ms.Close ();
- stream.Seek (0, SeekOrigin.Begin);
- }
- [Test]
- [ExpectedException (typeof (ObjectDisposedException))]
- public void Seek_ClosedBufferedStream ()
- {
- BufferedStream stream = new BufferedStream (new MemoryStream ());
- stream.Close ();
- stream.Seek (0, SeekOrigin.Begin);
- }
- }
- }
|