2 // System.IO.BufferedStream Unit Tests
5 // Ville Palo (vi64pa@kolumbus.fi)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
17 namespace MonoTests.System.IO {
20 public class BufferedStreamTest : Assertion {
22 private MemoryStream mem;
25 protected void SetUp ()
27 mem = new MemoryStream ();
31 protected void TearDown ()
33 //Some tests might mess with mem, so let's check it first
42 MemoryStream str = new MemoryStream ();
43 str.Write (new byte [] {1, 2, 3, 4, 5, 6}, 0, 6);
44 BufferedStream stream = new BufferedStream (str);
46 AssertEquals ("test#01", true, stream.CanRead);
47 AssertEquals ("test#02", true, stream.CanSeek);
48 AssertEquals ("test#03", true, stream.CanWrite);
49 AssertEquals ("test#04", 6, stream.Length);
50 AssertEquals ("test#05", 6, stream.Position);
52 string path = Path.GetTempFileName ();
53 if (File.Exists (path))
56 FileStream file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
57 stream = new BufferedStream (file);
58 AssertEquals ("test#06", false, stream.CanRead);
59 AssertEquals ("test#07", true, stream.CanSeek);
60 AssertEquals ("test#08", true, stream.CanWrite);
61 AssertEquals ("test#09", 0, stream.Length);
62 AssertEquals ("test#10", 0, stream.Position);
65 if (File.Exists (path))
68 file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
69 stream = new BufferedStream (file, 12);
70 AssertEquals ("test#11", false, stream.CanRead);
71 AssertEquals ("test#12", true, stream.CanSeek);
72 AssertEquals ("test#13", true, stream.CanWrite);
73 AssertEquals ("test#14", 0, stream.Length);
74 AssertEquals ("test#15", 0, stream.Position);
77 if (File.Exists (path))
82 /// Throws an exception if stream is null
85 [ExpectedException(typeof(ArgumentNullException))]
86 public void CtorNullExceptionStream ()
88 BufferedStream stream = new BufferedStream (null);
92 /// Throws an exception if stream is null
95 [ExpectedException(typeof(ArgumentNullException))]
96 public void CtorNullExceptionStream1 ()
98 BufferedStream stream = new BufferedStream (null, 12);
102 /// Throws an exception if stream is null
105 [ExpectedException(typeof(ArgumentOutOfRangeException))]
106 public void CtorOutOfRangeExceptionStream1 ()
108 MemoryStream str = new MemoryStream ();
109 BufferedStream stream = new BufferedStream (str, -12);
114 [ExpectedException(typeof(ObjectDisposedException))]
115 public void CtorOutOfRangeException2 ()
117 MemoryStream str = new MemoryStream ();
119 BufferedStream stream = new BufferedStream (str);
123 public void Close1 ()
125 BufferedStream stream = new BufferedStream (mem);
131 public void Close2 ()
133 BufferedStream stream = new BufferedStream (mem);
135 AssertEquals ("test#01", false, stream.CanRead);
136 AssertEquals ("test#02", false, stream.CanSeek);
137 AssertEquals ("test#03", false, stream.CanWrite);
141 [ExpectedException(typeof(ObjectDisposedException))]
142 public void Close3 ()
144 BufferedStream stream = new BufferedStream (mem);
146 long l = stream.Position;
150 [ExpectedException(typeof(ObjectDisposedException))]
151 public void Close4 ()
153 BufferedStream stream = new BufferedStream (mem);
155 long l = stream.Length;
159 [ExpectedException(typeof(ObjectDisposedException))]
160 public void Close5 ()
162 BufferedStream stream = new BufferedStream (mem);
164 stream.WriteByte (1);
168 [ExpectedException(typeof(ObjectDisposedException))]
169 public void Close6 ()
171 BufferedStream stream = new BufferedStream (mem);
177 [ExpectedException(typeof(NotSupportedException))]
178 public void Close7 ()
180 BufferedStream stream = new BufferedStream (mem);
182 stream.WriteByte (1);
188 mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
189 BufferedStream stream = new BufferedStream (mem);
191 byte [] bytes = new byte [10];
192 stream.Read (bytes, 0, 3);
193 AssertEquals ("test#01", 0, bytes [0]);
194 AssertEquals ("test#02", 0, bytes [1]);
195 AssertEquals ("test#03", 0, bytes [2]);
197 stream.Seek (0, SeekOrigin.Begin);
198 stream.Read (bytes, 0, 3);
199 AssertEquals ("test#04", 0, bytes [0]);
200 AssertEquals ("test#05", 1, bytes [1]);
201 AssertEquals ("test#06", 2, bytes [2]);
202 AssertEquals ("test#07", 0, bytes [0]);
204 stream.Read (bytes, 5, 3);
205 AssertEquals ("test#08", 3, bytes [5]);
206 AssertEquals ("test#09", 4, bytes [6]);
207 AssertEquals ("test#10", 5, bytes [7]);
208 AssertEquals ("test#11", 0, bytes [8]);
210 stream.Read (bytes, 0, 10);
211 AssertEquals ("test#12", 3, bytes [5]);
212 AssertEquals ("test#13", 4, bytes [6]);
213 AssertEquals ("test#14", 5, bytes [7]);
214 AssertEquals ("test#15", 0, bytes [9]);
218 [ExpectedException(typeof(ArgumentException))]
219 public void ReadException ()
221 mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
222 BufferedStream stream = new BufferedStream (mem);
224 byte [] bytes = new byte [10];
225 stream.Read (bytes, 0, 30);
229 [ExpectedException(typeof(ArgumentNullException))]
230 public void Read_Null ()
232 BufferedStream stream = new BufferedStream (mem);
233 stream.Read (null, 0, 0);
237 [ExpectedException(typeof(NotSupportedException))]
238 public void Read_CantRead ()
240 WriteOnlyStream wo = new WriteOnlyStream ();
241 BufferedStream stream = new BufferedStream (wo);
242 stream.Read (new byte [1], 0, 1);
246 [ExpectedException(typeof(ArgumentOutOfRangeException))]
247 public void Read_OffsetNegative ()
249 BufferedStream stream = new BufferedStream (mem);
250 stream.Read (new byte [1], -1, 1);
254 [ExpectedException(typeof(ArgumentException))]
255 public void Read_OffsetOverflow ()
257 BufferedStream stream = new BufferedStream (mem);
258 stream.Read (new byte [1], Int32.MaxValue, 1);
262 [ExpectedException(typeof(ArgumentOutOfRangeException))]
263 public void Read_CountNegative ()
265 BufferedStream stream = new BufferedStream (mem);
266 stream.Read (new byte [1], 1, -1);
270 [ExpectedException(typeof(ArgumentException))]
271 public void Read_CountOverflow ()
273 BufferedStream stream = new BufferedStream (mem);
274 stream.Read (new byte [1], 1, Int32.MaxValue);
278 public void ReadByte ()
280 mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
281 BufferedStream stream = new BufferedStream (mem);
283 AssertEquals ("test#01", -1, stream.ReadByte ());
284 AssertEquals ("test#02", -1, stream.ReadByte ());
285 AssertEquals ("test#03", -1, stream.ReadByte ());
287 stream.Seek (0, SeekOrigin.Begin);
288 AssertEquals ("test#04", 0, stream.ReadByte ());
289 AssertEquals ("test#05", 1, stream.ReadByte ());
290 AssertEquals ("test#06", 2, stream.ReadByte ());
291 AssertEquals ("test#07", 3, stream.ReadByte ());
297 BufferedStream stream = new BufferedStream (mem);
299 stream.Write (new byte [] {0, 1, 2, 3, 4}, 0, 4);
300 AssertEquals ("test#01", 4, stream.Length);
301 byte [] bytes = mem.GetBuffer ();
302 AssertEquals ("test#02", 0, bytes [0]);
303 AssertEquals ("test#03", 1, bytes [1]);
304 AssertEquals ("test#04", 2, bytes [2]);
305 AssertEquals ("test#05", 3, bytes [3]);
307 bytes = new byte [] {1, 4, 3};
308 stream.Write (bytes, 0, 3);
310 bytes = mem.GetBuffer ();
311 AssertEquals ("test#06", 0, bytes [0]);
312 AssertEquals ("test#07", 1, bytes [1]);
313 AssertEquals ("test#08", 2, bytes [2]);
314 AssertEquals ("test#09", 3, bytes [3]);
315 AssertEquals ("test#10", 1, bytes [4]);
316 AssertEquals ("test#11", 4, bytes [5]);
317 AssertEquals ("test#10", 3, bytes [6]);
318 AssertEquals ("test#11", 0, bytes [7]);
319 AssertEquals ("test#12", 7, stream.Length);
323 [ExpectedException(typeof (ArgumentException))]
324 public void WriteException ()
326 BufferedStream stream = new BufferedStream (mem);
327 stream.Write (new byte [] {0,1,2,3}, 0, 10);
331 [ExpectedException(typeof(ArgumentNullException))]
332 public void Write_Null ()
334 BufferedStream stream = new BufferedStream (mem);
335 stream.Write (null, 0, 0);
339 [ExpectedException(typeof(NotSupportedException))]
340 public void Write_CantWrite ()
342 ReadOnlyStream ro = new ReadOnlyStream ();
343 BufferedStream stream = new BufferedStream (ro);
344 stream.Write (new byte [1], 0, 1);
348 [ExpectedException(typeof(ArgumentOutOfRangeException))]
349 public void Write_OffsetNegative ()
351 BufferedStream stream = new BufferedStream (mem);
352 stream.Write (new byte [1], -1, 1);
356 [ExpectedException(typeof(ArgumentException))]
357 public void Write_OffsetOverflow ()
359 BufferedStream stream = new BufferedStream (mem);
360 stream.Write (new byte [1], Int32.MaxValue, 1);
364 [ExpectedException(typeof(ArgumentOutOfRangeException))]
365 public void Write_CountNegative ()
367 BufferedStream stream = new BufferedStream (mem);
368 stream.Write (new byte [1], 1, -1);
372 [ExpectedException(typeof(ArgumentException))]
373 public void Write_CountOverflow ()
375 BufferedStream stream = new BufferedStream (mem);
376 stream.Write (new byte [1], 1, Int32.MaxValue);
380 public void WriteByte ()
382 BufferedStream stream = new BufferedStream (mem);
383 stream.WriteByte (1);
384 stream.WriteByte (2);
385 stream.WriteByte (3);
387 AssertEquals ("test#01", 256, mem.GetBuffer ().Length);
388 AssertEquals ("test#02", 3, stream.Length);
389 AssertEquals ("test#03", 1, mem.GetBuffer () [0]);
390 AssertEquals ("test#04", 2, mem.GetBuffer () [1]);
391 AssertEquals ("test#05", 3, mem.GetBuffer () [2]);
397 BufferedStream stream = new BufferedStream (mem);
398 stream.WriteByte (1);
399 stream.WriteByte (2);
401 byte [] bytes = mem.GetBuffer ();
402 AssertEquals ("test#01", 0, bytes.Length);
405 bytes = mem.GetBuffer ();
406 AssertEquals ("test#02", 256, bytes.Length);
407 AssertEquals ("test#03", 1, bytes [0]);
408 AssertEquals ("test#04", 2, bytes [1]);
410 mem = new MemoryStream ();
411 bytes = new byte [] {0, 1, 2, 3, 4, 5};
412 stream = new BufferedStream (mem);
413 stream.Write (bytes, 0, 2);
414 AssertEquals ("test#05", 2, stream.Length);
415 bytes = mem.GetBuffer ();
416 AssertEquals ("test#06", 256, bytes.Length);
418 AssertEquals ("test#07", 0, bytes [0]);
419 AssertEquals ("test#08", 1, bytes [1]);
421 stream.Write (bytes, 0, 2);
423 bytes = mem.GetBuffer ();
424 AssertEquals ("test#09", 0, bytes [0]);
425 AssertEquals ("test#10", 1, bytes [1]);
426 AssertEquals ("test#11", 0, bytes [2]);
427 AssertEquals ("test#12", 0, bytes [3]);
429 bytes = mem.GetBuffer ();
430 AssertEquals ("test#13", 0, bytes [2]);
431 AssertEquals ("test#14", 1, bytes [3]);
437 BufferedStream stream = new BufferedStream (mem);
438 stream.Write (new byte [] {0, 1, 2, 3, 4, 5}, 0, 6);
440 AssertEquals ("test#01", 6, stream.Position);
442 stream.Seek (-5, SeekOrigin.End);
443 AssertEquals ("test#02", 1, stream.Position);
445 stream.Seek (3, SeekOrigin.Current);
446 AssertEquals ("test#03", 4, stream.Position);
448 stream.Seek (300, SeekOrigin.Current);
449 AssertEquals ("test#04", 304, stream.Position);
453 [ExpectedException(typeof (IOException))]
454 public void SeekException ()
456 BufferedStream stream = new BufferedStream (mem);
457 stream.Seek (-1, SeekOrigin.Begin);
461 public void SetLength ()
463 BufferedStream stream = new BufferedStream (mem);
464 stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6);
466 AssertEquals ("test#01", 6, stream.Length);
467 stream.SetLength (60);
468 AssertEquals ("test#02", 60, stream.Length);
470 stream.SetLength (2);
471 AssertEquals ("test#03", 2, stream.Length);
475 [ExpectedException(typeof(ArgumentOutOfRangeException))]
476 public void SetLengthException ()
478 BufferedStream stream = new BufferedStream (mem);
479 stream.SetLength (-1);
483 [ExpectedException(typeof(NotSupportedException))]
484 public void SetLengthException2 ()
486 BufferedStream stream = new BufferedStream (mem);
488 stream.SetLength (1);
492 public void SetLengthException3 ()
494 BufferedStream stream = new BufferedStream (mem);
496 // Strangely, this does not throw an exception on .NET 1.1
497 stream.SetLength (1);
501 public void Position ()
503 mem = new MemoryStream ();
504 BufferedStream stream = new BufferedStream (mem,5);
505 stream.Write (new byte [] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, 0, 16);
508 AssertEquals ("test#01", 0, stream.Position);
509 AssertEquals ("test#02", 0, stream.ReadByte ());
512 AssertEquals ("test#01", 5, stream.Position);
513 AssertEquals ("test#02", 5, stream.ReadByte ());
515 // Should not need to read from the underlying stream:
517 AssertEquals ("test#01", 7, stream.Position);
518 AssertEquals ("test#02", 7, stream.ReadByte ());
520 // Should not need to read from the underlying stream:
522 AssertEquals ("test#01", 5, stream.Position);
523 AssertEquals ("test#02", 5, stream.ReadByte ());
525 // Should not need to read from the underlying stream:
527 AssertEquals ("test#01", 9, stream.Position);
528 AssertEquals ("test#02", 9, stream.ReadByte ());
530 stream.Position = 10;
531 AssertEquals ("test#01", 10, stream.Position);
532 AssertEquals ("test#02", 10, stream.ReadByte ());
535 AssertEquals ("test#01", 9, stream.Position);
536 AssertEquals ("test#02", 9, stream.ReadByte ());
540 public void PositionAfterSetLength ()
542 BufferedStream stream = new BufferedStream (new MemoryStream ());
543 stream.SetLength (32);
544 stream.Position = 32;
545 stream.SetLength (16);
546 AssertEquals ("Position==16", 16, stream.Position);
550 [ExpectedException (typeof (ObjectDisposedException))]
551 public void SetLength_Disposed ()
553 BufferedStream stream = new BufferedStream (new MemoryStream ());
555 stream.SetLength (16);
559 [ExpectedException (typeof (NotSupportedException))]
560 public void Seek_ClosedMemoryStream ()
562 MemoryStream ms = new MemoryStream ();
563 BufferedStream stream = new BufferedStream (ms);
565 stream.Seek (0, SeekOrigin.Begin);
569 [ExpectedException (typeof (ObjectDisposedException))]
570 public void Seek_ClosedBufferedStream ()
572 BufferedStream stream = new BufferedStream (new MemoryStream ());
574 stream.Seek (0, SeekOrigin.Begin);