2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.IO / BufferedStreamTest.cs
1 //
2 // System.IO.BufferedStream Unit Tests
3 //
4 // Authors: 
5 //      Ville Palo (vi64pa@kolumbus.fi)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Ville Palo
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System.IO;
14 using System.Text;
15 using System;
16
17 namespace MonoTests.System.IO {
18
19 [TestFixture]
20 public class BufferedStreamTest : Assertion {
21         
22         private MemoryStream mem;
23         
24         [SetUp]
25         protected void SetUp ()
26         {
27                 mem = new MemoryStream ();
28         }
29
30         [TearDown]
31         protected void TearDown ()
32         {
33                 //Some tests might mess with mem, so let's check it first
34                 if (mem != null)
35                         mem.Close ();
36         }
37
38
39         [Test]
40         public void Ctor ()
41         {
42                 MemoryStream str = new MemoryStream ();
43                 str.Write (new byte [] {1, 2, 3, 4, 5, 6}, 0, 6);
44                 BufferedStream stream = new BufferedStream (str);
45                 
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);
51                 
52                 string path = Path.GetTempFileName ();
53                 if (File.Exists (path))
54                         File.Delete (path);
55                 
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);           
63                 file.Close ();
64                 
65                 if (File.Exists (path))
66                         File.Delete (path);
67                 
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);           
75
76                 file.Close ();
77                 if (File.Exists (path))
78                         File.Delete (path);
79         }
80
81         /// <summary>
82         /// Throws an exception if stream is null
83         /// </summary>
84         [Test]
85         [ExpectedException(typeof(ArgumentNullException))]
86         public void CtorNullExceptionStream () 
87         {
88                 BufferedStream stream = new BufferedStream (null);
89         }
90
91         /// <summary>
92         /// Throws an exception if stream is null
93         /// </summary>
94         [Test]
95         [ExpectedException(typeof(ArgumentNullException))]
96         public void CtorNullExceptionStream1 () 
97         {
98                 BufferedStream stream = new BufferedStream (null, 12);
99         }
100         
101         /// <summary>
102         /// Throws an exception if stream is null
103         /// </summary>
104         [Test]
105         [ExpectedException(typeof(ArgumentOutOfRangeException))]
106         public void CtorOutOfRangeExceptionStream1 () 
107         {
108                 MemoryStream str = new MemoryStream ();
109                 BufferedStream stream = new BufferedStream (str, -12);
110         }
111
112
113         [Test]
114         [ExpectedException(typeof(ObjectDisposedException))]
115         public void CtorOutOfRangeException2 () 
116         {
117                 MemoryStream str = new MemoryStream ();
118                 str.Close ();
119                 BufferedStream stream = new BufferedStream (str);               
120         }
121         
122         [Test]  
123         public void Close1 ()
124         {
125                 BufferedStream stream = new BufferedStream (mem);
126                 stream.Close ();
127                 stream.Close ();
128         }
129
130         [Test]  
131         public void Close2 ()
132         {
133                 BufferedStream stream = new BufferedStream (mem);
134                 stream.Close ();
135                 AssertEquals ("test#01", false, stream.CanRead);
136                 AssertEquals ("test#02", false, stream.CanSeek);
137                 AssertEquals ("test#03", false, stream.CanWrite);
138         }
139
140         [Test]  
141         [ExpectedException(typeof(ObjectDisposedException))]    
142         public void Close3 ()
143         {
144                 BufferedStream stream = new BufferedStream (mem);
145                 stream.Close ();
146                 long l = stream.Position;
147         }
148
149         [Test]  
150         [ExpectedException(typeof(ObjectDisposedException))]    
151         public void Close4 ()
152         {
153                 BufferedStream stream = new BufferedStream (mem);
154                 stream.Close ();
155                 long l = stream.Length;
156         }
157         
158         [Test]  
159         [ExpectedException(typeof(ObjectDisposedException))]    
160         public void Close5 ()
161         {
162                 BufferedStream stream = new BufferedStream (mem);
163                 stream.Close ();
164                 stream.WriteByte (1);
165         }
166         
167         [Test]  
168         [ExpectedException(typeof(ObjectDisposedException))]    
169         public void Close6 ()
170         {
171                 BufferedStream stream = new BufferedStream (mem);
172                 stream.Close ();
173                 stream.ReadByte ();
174         }
175
176         [Test]  
177         [ExpectedException(typeof(NotSupportedException))]      
178         public void Close7 ()
179         {
180                 BufferedStream stream = new BufferedStream (mem);
181                 mem.Close ();
182                 stream.WriteByte (1);
183         }
184         
185         [Test]
186         public void Read ()
187         {
188                 mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
189                 BufferedStream stream = new BufferedStream (mem);
190
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]);
196
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]);         
203
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]);         
209
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]);                         
215         }
216         
217         [Test]
218         [ExpectedException(typeof(ArgumentException))]          
219         public void ReadException ()
220         {
221                 mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
222                 BufferedStream stream = new BufferedStream (mem);
223
224                 byte [] bytes = new byte [10];
225                 stream.Read (bytes, 0, 30);             
226         }
227
228         [Test]
229         [ExpectedException(typeof(ArgumentNullException))]
230         public void Read_Null () 
231         {
232                 BufferedStream stream = new BufferedStream (mem);
233                 stream.Read (null, 0, 0);
234         }
235
236         [Test]
237         [ExpectedException(typeof(NotSupportedException))]
238         public void Read_CantRead () 
239         {
240                 WriteOnlyStream wo = new WriteOnlyStream ();
241                 BufferedStream stream = new BufferedStream (wo);
242                 stream.Read (new byte [1], 0, 1);
243         }
244
245         [Test]
246         [ExpectedException(typeof(ArgumentOutOfRangeException))]
247         public void Read_OffsetNegative () 
248         {
249                 BufferedStream stream = new BufferedStream (mem);
250                 stream.Read (new byte [1], -1, 1); 
251         }
252
253         [Test]
254         [ExpectedException(typeof(ArgumentException))]
255         public void Read_OffsetOverflow () 
256         {
257                 BufferedStream stream = new BufferedStream (mem);
258                 stream.Read (new byte [1], Int32.MaxValue, 1); 
259         }
260
261         [Test]
262         [ExpectedException(typeof(ArgumentOutOfRangeException))]
263         public void Read_CountNegative () 
264         {
265                 BufferedStream stream = new BufferedStream (mem);
266                 stream.Read (new byte [1], 1, -1); 
267         }
268
269         [Test]
270         [ExpectedException(typeof(ArgumentException))]
271         public void Read_CountOverflow () 
272         {
273                 BufferedStream stream = new BufferedStream (mem);
274                 stream.Read (new byte [1], 1, Int32.MaxValue); 
275         }
276         
277         [Test]
278         public void ReadByte ()
279         {
280                 mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
281                 BufferedStream stream = new BufferedStream (mem);
282
283                 AssertEquals ("test#01", -1, stream.ReadByte ());
284                 AssertEquals ("test#02", -1, stream.ReadByte ());
285                 AssertEquals ("test#03", -1, stream.ReadByte ());
286
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 ());                
292         }
293         
294         [Test]
295         public void Write ()
296         {
297                 BufferedStream stream = new BufferedStream (mem);
298                 
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]);
306
307                 bytes = new byte [] {1, 4, 3};
308                 stream.Write (bytes, 0, 3);
309                 stream.Flush ();
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);
320         }
321                 
322         [Test]
323         [ExpectedException(typeof (ArgumentException))]
324         public void WriteException ()
325         {
326                 BufferedStream stream = new BufferedStream (mem);
327                 stream.Write (new byte [] {0,1,2,3}, 0, 10);
328         }
329
330         [Test]
331         [ExpectedException(typeof(ArgumentNullException))]
332         public void Write_Null () 
333         {
334                 BufferedStream stream = new BufferedStream (mem);
335                 stream.Write (null, 0, 0);
336         }
337
338         [Test]
339         [ExpectedException(typeof(NotSupportedException))]
340         public void Write_CantWrite () 
341         {
342                 ReadOnlyStream ro = new ReadOnlyStream ();
343                 BufferedStream stream = new BufferedStream (ro);
344                 stream.Write (new byte [1], 0, 1);
345         }
346
347         [Test]
348         [ExpectedException(typeof(ArgumentOutOfRangeException))]
349         public void Write_OffsetNegative () 
350         {
351                 BufferedStream stream = new BufferedStream (mem);
352                 stream.Write (new byte [1], -1, 1); 
353         }
354
355         [Test]
356         [ExpectedException(typeof(ArgumentException))]
357         public void Write_OffsetOverflow () 
358         {
359                 BufferedStream stream = new BufferedStream (mem);
360                 stream.Write (new byte [1], Int32.MaxValue, 1); 
361         }
362
363         [Test]
364         [ExpectedException(typeof(ArgumentOutOfRangeException))]
365         public void Write_CountNegative () 
366         {
367                 BufferedStream stream = new BufferedStream (mem);
368                 stream.Write (new byte [1], 1, -1); 
369         }
370
371         [Test]
372         [ExpectedException(typeof(ArgumentException))]
373         public void Write_CountOverflow () 
374         {
375                 BufferedStream stream = new BufferedStream (mem);
376                 stream.Write (new byte [1], 1, Int32.MaxValue); 
377         }
378         
379         [Test]
380         public void WriteByte ()
381         {
382                 BufferedStream stream = new BufferedStream (mem);
383                 stream.WriteByte (1);
384                 stream.WriteByte (2);
385                 stream.WriteByte (3);
386                 stream.Flush ();
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]);              
392         }
393         
394         [Test]
395         public void Flush ()
396         {
397                 BufferedStream stream = new BufferedStream (mem);
398                 stream.WriteByte (1);
399                 stream.WriteByte (2);
400                 
401                 byte [] bytes = mem.GetBuffer ();
402                 AssertEquals ("test#01", 0, bytes.Length);
403                 stream.Flush ();
404                 
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]);
409                 mem.Close ();
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);
417
418                 AssertEquals ("test#07", 0, bytes [0]);
419                 AssertEquals ("test#08", 1, bytes [1]);
420                 
421                 stream.Write (bytes, 0, 2);
422                 
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]);
428                 stream.Flush ();
429                 bytes = mem.GetBuffer ();
430                 AssertEquals ("test#13", 0, bytes [2]);
431                 AssertEquals ("test#14", 1, bytes [3]);
432         }
433         
434         [Test]
435         public void Seek ()
436         {
437                 BufferedStream stream = new BufferedStream (mem);
438                 stream.Write (new byte [] {0, 1, 2, 3, 4, 5}, 0, 6);
439                 
440                 AssertEquals ("test#01", 6, stream.Position);
441                 
442                 stream.Seek (-5, SeekOrigin.End);               
443                 AssertEquals ("test#02", 1, stream.Position);
444                 
445                 stream.Seek (3, SeekOrigin.Current);
446                 AssertEquals ("test#03", 4, stream.Position);
447                 
448                 stream.Seek (300, SeekOrigin.Current);          
449                 AssertEquals ("test#04", 304, stream.Position);         
450         }
451         
452         [Test]
453         [ExpectedException(typeof (IOException))]
454         public void SeekException ()
455         {
456                 BufferedStream stream = new BufferedStream (mem);
457                 stream.Seek (-1, SeekOrigin.Begin);
458         }
459         
460         [Test]
461         public void SetLength ()
462         {
463                 BufferedStream stream = new BufferedStream (mem);
464                 stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6);
465                 
466                 AssertEquals ("test#01", 6, stream.Length);
467                 stream.SetLength (60);
468                 AssertEquals ("test#02", 60, stream.Length);
469                 
470                 stream.SetLength (2);
471                 AssertEquals ("test#03", 2, stream.Length);     
472         }
473         
474          [Test]
475          [ExpectedException(typeof(ArgumentOutOfRangeException))]
476          public void SetLengthException ()
477          {
478                 BufferedStream stream = new BufferedStream (mem);
479                 stream.SetLength (-1);          
480          }
481
482          [Test]
483          [ExpectedException(typeof(NotSupportedException))]
484          public void SetLengthException2 ()
485          {
486                 BufferedStream stream = new BufferedStream (mem);
487                 mem.Close ();
488                 stream.SetLength (1);           
489          }
490
491          [Test]
492          public void SetLengthException3 ()
493          {
494                 BufferedStream stream = new BufferedStream (mem);
495                 mem = null;
496                 // Strangely, this does not throw an exception on .NET 1.1
497                 stream.SetLength (1);           
498          }
499          
500          [Test]
501          public void Position ()
502          {
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);
506                 
507                 stream.Position = 0;
508                 AssertEquals ("test#01", 0, stream.Position);           
509                 AssertEquals ("test#02", 0, stream.ReadByte ());                
510                 
511                 stream.Position = 5;
512                 AssertEquals ("test#01", 5, stream.Position);           
513                 AssertEquals ("test#02", 5, stream.ReadByte ());                
514                 
515                 // Should not need to read from the underlying stream:
516                 stream.Position = 7;
517                 AssertEquals ("test#01", 7, stream.Position);           
518                 AssertEquals ("test#02", 7, stream.ReadByte ());                
519                 
520                 // Should not need to read from the underlying stream:
521                 stream.Position = 5;
522                 AssertEquals ("test#01", 5, stream.Position);           
523                 AssertEquals ("test#02", 5, stream.ReadByte ());                
524                                 
525                 // Should not need to read from the underlying stream:
526                 stream.Position = 9;
527                 AssertEquals ("test#01", 9, stream.Position);
528                 AssertEquals ("test#02", 9, stream.ReadByte ());
529                 
530                 stream.Position = 10;
531                 AssertEquals ("test#01", 10, stream.Position);          
532                 AssertEquals ("test#02", 10, stream.ReadByte ());               
533                 
534                 stream.Position = 9;
535                 AssertEquals ("test#01", 9, stream.Position);           
536                 AssertEquals ("test#02", 9, stream.ReadByte ());                
537          }
538
539         [Test]
540         public void PositionAfterSetLength () 
541         {
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);
547         }
548
549         [Test]
550         [ExpectedException (typeof (ObjectDisposedException))]
551         public void SetLength_Disposed ()
552         {
553                 BufferedStream stream = new BufferedStream (new MemoryStream ());
554                 stream.Close ();
555                 stream.SetLength (16);
556         }
557
558         [Test]
559         [ExpectedException (typeof (NotSupportedException))]
560         public void Seek_ClosedMemoryStream ()
561         {
562                 MemoryStream ms = new MemoryStream ();
563                 BufferedStream stream = new BufferedStream (ms);
564                 ms.Close ();
565                 stream.Seek (0, SeekOrigin.Begin);
566         }
567
568         [Test]
569         [ExpectedException (typeof (ObjectDisposedException))]
570         public void Seek_ClosedBufferedStream ()
571         {
572                 BufferedStream stream = new BufferedStream (new MemoryStream ());
573                 stream.Close ();
574                 stream.Seek (0, SeekOrigin.Begin);
575         }
576 }
577 }