ccca1807243962827f16cb288c48330408b400db
[mono.git] / mcs / class / corlib / Test / System.IO / BinaryReaderTest.cs
1 // BinaryReaderTest.cs - NUnit Test Cases for the SystemIO.BinaryReader class
2 //
3 // Eduardo Garcia Cebollero (kiwnix@yahoo.es)
4 //
5 // (C) Eduardo Garcia Cebollero.
6 // (C) Ximian, Inc.  http://www.ximian.com
7 // 
8 using NUnit.Framework;
9 using System;
10 using System.IO;
11 using System.Text;
12
13 namespace MonoTests.System.IO
14 {
15         public class BinaryReaderTest : TestCase
16         {               
17                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
18                 private string _codeFileName;
19                         
20                 public BinaryReaderTest() 
21                 {
22                         if (Directory.Exists (TempFolder))
23                                 Directory.Delete (TempFolder, true);
24                         Directory.CreateDirectory (TempFolder);
25                         _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
26                 }
27
28                 ~BinaryReaderTest ()
29                 {
30                         if (Directory.Exists (TempFolder))
31                                 Directory.Delete (TempFolder, true);
32                 }
33
34                 override protected void SetUp() {
35
36                         if (!Directory.Exists (TempFolder))
37                                 Directory.CreateDirectory (TempFolder);
38                         if (!File.Exists (_codeFileName))
39                                 File.Create (_codeFileName).Close ();
40                 }
41
42                 public void TestCtor1() 
43                 {
44                         {
45                                 bool errorThrown = false;
46                                 try {
47                                         BinaryReader r = new BinaryReader ((Stream) null);
48                                 } catch (ArgumentNullException) {
49                                         errorThrown = true;
50                                 }
51                                 Assert ("#01 null string error not thrown", errorThrown);
52                         }
53                         {
54                                 bool errorThrown = false;
55                                 FileStream f = new FileStream (_codeFileName, FileMode.Open, FileAccess.Write);
56                                 try {
57                                         BinaryReader r = new BinaryReader (f);
58                                         r.Close ();
59                                 } catch  (ArgumentException) {
60                                         errorThrown = true;
61                                 }
62                                 f.Close ();
63                                 Assert ("#02 no read error not thrown", errorThrown);
64                         }
65                         {
66                                 FileStream f = new FileStream (_codeFileName, 
67                                                                 FileMode.Open, 
68                                                                 FileAccess.Read);
69                                 BinaryReader r = new BinaryReader (f);
70                                 AssertNotNull ("#03 no binary reader created", r);
71                                 r.Close ();
72                                 f.Close ();
73                         }
74                                 
75                 }
76
77                 public void TestCtor2 () 
78                 {
79                         {
80                                 bool errorThrown = false;
81                                 try {
82                                         BinaryReader r = new BinaryReader ((Stream) null, Encoding.ASCII);
83                                 } catch (ArgumentNullException) {
84                                         errorThrown = true;
85                                 } catch (Exception e) {
86                                         Fail ("#04 Incorrect exception thrown: " + e.ToString ());
87                                 }
88                                 Assert ("#05 null stream error not thrown", errorThrown);
89                         }
90                         {
91                                 bool errorThrown = false;
92                                 try {
93                                         BinaryReader r = new BinaryReader ((Stream) null, Encoding.Unicode);
94                                 } catch (ArgumentNullException) {
95                                         errorThrown = true;
96                                 } catch (Exception e) {
97                                         Fail ("#06 Incorrect exception thrown: " + e.ToString ());
98                                 }
99                                 Assert("#07 null stream error not thrown", errorThrown);
100                         }
101                         {
102                                 bool errorThrown = false;
103                                 try {
104                                         BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF7);
105                                 } catch (ArgumentNullException) {
106                                         errorThrown = true;
107                                 } catch (Exception e) {
108                                         Fail ("#08 Incorrect exception thrown: " + e.ToString ());
109                                 }
110                                 Assert ("#09 null stream error not thrown", errorThrown);
111                         }
112                         {
113                                 bool errorThrown = false;
114                                 try {
115                                         BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF8);
116                                 } catch (ArgumentNullException) {
117                                         errorThrown = true;
118                                 } catch (Exception e) {
119                                         Fail ("#0A Incorrect exception thrown: " + e.ToString ());
120                                 }
121                                 Assert ("#0B null stream error not thrown", errorThrown);
122                         }
123                 }
124
125                 public void TestCtor3 ()
126                 {
127                         bool errorThrown = false;
128                         byte [] b = new byte [30];
129                         MemoryStream m = new MemoryStream (b);
130                         try {
131                                 BinaryReader r = new BinaryReader (m, (Encoding) null);
132                         } catch (ArgumentNullException) {
133                                 errorThrown = true;
134                         } catch(Exception e) {
135                                 Fail ("#0C Incorrect Exception thrown: " + e.ToString ());
136                         }
137                         Assert ("#0D No exception trown: ", errorThrown);
138                 }
139
140                 //TODO: (TestCtor*) Verify the Use of a wrong Stream
141                 //TODO: (TestClose*) Verify the Close Method
142                 public void TestClose1 ()
143                 {
144                         {
145                                 byte [] b = new byte [30];
146                                 MemoryStream m = new MemoryStream (b);
147                                 try {
148                                         BinaryReader r = new BinaryReader (m);
149                                         r.Close ();
150                                 } catch (Exception e) {
151                                         Fail ("#0E Unhandled Exception: "+ e.ToString ());
152                                 }
153                         }
154                 }
155
156                 //TODO: (TestRead*) Verify Read Method
157                 public void TestReadBoolean ()
158                 {
159                         bool [] a = {true, true, false};
160                         byte [] arr_a = new byte [3];
161                         int i = 0;
162                         foreach (bool a1 in a) {
163                                   arr_a [i] = Convert.ToByte (a1);
164                                   i++;
165                         }
166                                   
167                         bool b;
168                         MemoryStream m = new MemoryStream (arr_a);
169                         try {   
170                                 BinaryReader r = new BinaryReader (m);
171                                 b = r.ReadBoolean ();
172                                 AssertEquals ("#11 No well readed boolean: ", a [0], b);
173                         } catch (Exception e) {
174                                 Fail ("#12 Unexpected exception thrown: " + e.ToString ());
175                         }
176                 }
177
178                 public void TestReadByte ()
179                 {
180                         byte [] a = {0, 2, 3, 1, 5, 2};
181                         byte b;
182                         MemoryStream m = new MemoryStream (a);
183                         try {
184                                 BinaryReader r = new BinaryReader (m);
185                                 b = r.ReadByte ();
186                                 AssertEquals ("#13 No well readed byte: ", a [0], b);
187                         } catch (Exception e) {
188                                 Fail ("#14 Unexpected Exception thrown: " + e.ToString ());
189                         }
190                 }
191
192                 public void TestReadChar()
193                 {
194                         char [] a = {'a','b','c','d','e'};
195                         byte [] arr_a = new byte [5];
196                         int i = 0;
197                         char c;
198
199                         foreach (char a1 in a) {
200                            arr_a [i] = Convert.ToByte (a1);
201                            i++;
202                         }
203
204                         MemoryStream m = new MemoryStream (arr_a);
205                         BinaryReader r = new BinaryReader (m);
206                         try {
207                                 c = r.ReadChar ();
208                                 AssertEquals ("#15 No well readed Char", a [0], c);
209                         } catch (Exception e)  {
210                                 Fail ("#16 Unexpeted Exception: " + e.ToString ());
211                         }
212                 }
213
214                 public void TestReadInt32 () //Uses BinaryWriter!!
215                 {
216                         int [] arr_int = {1,10,200,3000,40000,500000,6000000};
217                         byte [] arr_byte = new byte [28]; //Sizeof arr_int * 4
218                         int [] arr_int2 = new int [7];
219                         int i;
220                         
221                         MemoryStream mem_stream = new MemoryStream (arr_byte);
222                         BinaryWriter bin_writer = new BinaryWriter (mem_stream);
223                         
224                         foreach (int elem in arr_int)   {
225                                 bin_writer.Write(elem);
226                         }
227                         
228                         mem_stream.Seek(0,SeekOrigin.Begin);
229                         BinaryReader bin_reader = new BinaryReader (mem_stream);
230                         bin_reader.BaseStream.Seek(0,SeekOrigin.Begin);
231
232                         for (i=0;i<7;i++) {
233                                 try{
234                                         arr_int2 [i] = bin_reader.ReadInt32();
235                                         AssertEquals("#2E Wrong Readed Int32 in iteration "+ i,arr_int [i],arr_int2 [i]);
236                                 } catch (IOException e) {
237                                         Fail("#2F Unexpected IO Exception" + e.ToString());
238                                 }
239                         }
240                 }
241
242
243                 //-TODO: (TestRead[Type]*) Verify the ReadBoolean, ReadByte ....
244                 // ReadBoolean, ReadByte, ReadChar, ReadInt32 Done
245                 
246                 //TODO: (TestFillBuffer*) Verify the FillBuffer Method
247                 public void TestPeekChar ()
248                 {
249                         char char1, char2;
250                         char [] b = {'A', 'B', 'C'};
251                         byte [] arr_b = new byte [3];
252                         int i = 0;
253
254                         foreach (char b1 in b) {
255                                 arr_b [i] = Convert.ToByte (b1);
256                                 i++;
257                         }
258                                   
259                         MemoryStream m = new MemoryStream (arr_b);
260                         BinaryReader r = new BinaryReader (m);
261                         try {   
262                                 char1 = (char) r.PeekChar ();
263                                 char2 = (char) r.PeekChar ();
264                                 AssertEquals ("#20 the stream pointer have been altered in peek", char1, char2);
265                         } catch (Exception e) {
266                                 Fail ("#21 Unexpected exception thrown: " + e.ToString ());
267                         }
268                 }
269                 
270                 public void TestBaseSeek1 ()
271                 {
272                         char char1, char2;
273                         char [] b = {'A','B','C','D','E','F'};
274                         byte [] arr_b = new byte[6];
275                         int i = 0;
276                         foreach (char b1 in b) {
277                                 arr_b [i] = Convert.ToByte (b1);
278                                 i++;
279                         }
280
281                         MemoryStream m = new MemoryStream (arr_b);
282                         BinaryReader r = new BinaryReader (m);
283                         try {
284                                 char1 = (char) r.PeekChar ();
285                                 r.BaseStream.Seek (0,SeekOrigin.Current);
286                                 char2 = (char) r.PeekChar ();
287                                 AssertEquals ("#22 the stream Has been altered in Seek", char1, char2);
288                         } catch (Exception e) {
289                                 Fail ("#23 Unexpected exception thrown: " + e.ToString ());
290                         }
291                 }
292
293                 public void TestBaseSeek2 ()
294                 {
295                         char char1, char2;
296                         char [] b = {'A','B','C','D','E','F'};
297                         byte [] arr_b = new byte[6];
298                         int i = 0;
299                         foreach (char b1 in b) {
300                                 arr_b [i] = Convert.ToByte (b1);
301                                 i++;
302                         }
303                         
304                         MemoryStream m = new MemoryStream (arr_b);
305                         BinaryReader r = new BinaryReader (m);
306                         try {
307                                 char1 = (char) r.PeekChar ();
308                                 r.BaseStream.Seek (3,SeekOrigin.Current);
309                                 r.BaseStream.Seek (-3,SeekOrigin.Current);
310                                 char2 = (char) r.PeekChar ();
311                                 AssertEquals ("#24 the stream Has been altered in Seek", char1, char2);
312                         } catch (Exception e) {
313                                 Fail ("#25 Unexpected exception thrown: " + e.ToString ());
314                         }
315                 }
316                 public void TestInterleavedSeek1 ()
317                 {
318                         byte int1;
319                         byte [] arr_byte = {0,1,2,3,4,5,6,7,8,9};
320                         
321                         MemoryStream m = new MemoryStream (arr_byte);
322                         BinaryReader r = new BinaryReader (m);
323
324                         {
325                                 try {
326                                         int1 = r.ReadByte();
327                                         AssertEquals("#26 Not well readed Byte", int1, arr_byte[0]);
328                                 } catch (Exception e) {
329                                 Fail ("#27 Unexpected exception thrown: " + e.ToString ());
330                                 }
331                         }
332                         {
333                                 try {
334                                         r.BaseStream.Seek(-1,SeekOrigin.End);
335                                         int1 = r.ReadByte();
336                                         AssertEquals("#28 Not well readed Byte",int1,arr_byte[9]);
337                                 } catch (Exception e) {
338                                 Fail ("#29 Unexpected exception thrown: " + e.ToString ());
339                                 }
340                         }
341                         {
342                                 try {
343                                         r.BaseStream.Seek(3,SeekOrigin.Begin);
344                                         int1 = r.ReadByte();
345                                         AssertEquals("#2A Not well readed Byte",int1,arr_byte[3]);
346                                 } catch (Exception e) {
347                                         Fail ("#2B Unexpected exception thrown: " + e.ToString ());
348                                 }
349                         }
350                         {
351                                 try {
352                                         r.BaseStream.Seek(2,SeekOrigin.Current);
353                                         int1 = r.ReadByte();
354                                         AssertEquals("#2C Not well readed Int32",int1,arr_byte [6]);
355                                 } catch (Exception e) {
356                                 Fail ("#2D Unexpected exception thrown: " + e.ToString ());
357                                 }
358                         }
359
360                 }
361
362         /// <summary>
363         /// Throws an exception if stream is null
364         /// </summary>
365         [Test]
366         [ExpectedException(typeof(ArgumentNullException))]
367         public void CtorNullExceptionStream () 
368         {
369                 BinaryReader reader = new BinaryReader (null);
370                 Assertion.Fail();
371         }
372
373         /// <summary>
374         /// Throws an exception if encoding is null
375         /// </summary>
376         [Test]
377         [ExpectedException(typeof(ArgumentNullException))]
378         public void CtorNullExceptionEncoding () 
379         {
380                 MemoryStream stream = new MemoryStream (64);    
381                 BinaryReader reader = new BinaryReader (stream, null);
382                 Assertion.Fail();
383         }
384         
385         /// <summary>
386         /// Throws an exception if stream does not support writing
387         /// </summary>
388         [Test]
389         [ExpectedException(typeof(ArgumentException))]
390         public void CtorArgumentExceptionCannotWrite ()
391         {
392                 string path = TempFolder + "/BinaryReaderTestFile.1";
393                 DeleteFile (path);
394
395                 FileStream file = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
396                 BinaryReader breader = new BinaryReader (file);
397
398                 if (File.Exists (path))
399                         File.Delete (path);             
400         }
401
402         /// <summary>
403         /// Throws an exception if stream is already closed
404         /// </summary>
405         [Test]
406         [ExpectedException(typeof(ArgumentException))]
407         public void CtorArgumentExceptionClosedStream ()
408         {
409                 string path = TempFolder + "/BinaryReaderTestFile.2";
410                 DeleteFile (path);
411
412                 FileStream file = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
413                 file.Close ();
414                 BinaryReader breader = new BinaryReader (file);
415
416                 if (File.Exists (path))
417                         File.Delete (path);             
418         }
419
420         /// <summary>
421         /// Throws an exception if stream is closed
422         /// </summary>
423         [Test]
424         [ExpectedException(typeof(ArgumentException))]
425         public void CtorArgumentExceptionEncoding () 
426         {
427                 MemoryStream stream = new MemoryStream (64);    
428                 stream.Close ();
429                 
430                 BinaryReader reader = new BinaryReader (stream, new ASCIIEncoding ());
431                 Assertion.Fail();
432         }
433         
434         /// <summary>
435         /// Tests read () method
436         /// </summary>
437         [Test]
438         public void Read ()
439         {
440                 byte [] bytes = new byte [] {0, 1, 2, 3};
441                 MemoryStream stream = new MemoryStream (bytes);
442                 BinaryReader reader = new BinaryReader (stream);
443                 
444                 Assertion.AssertEquals ("test#01", 0, reader.Read ());
445                 Assertion.AssertEquals ("test#02", 1, reader.Read ());
446                 Assertion.AssertEquals ("test#03", 2, reader.Read ());
447                 Assertion.AssertEquals ("test#04", 3, reader.Read ());
448                 Assertion.AssertEquals ("test#05", -1, reader.Read ());         
449         }
450         
451         [Test]
452         public void PeakChar ()
453         {
454                 byte [] bytes = new byte [] {0, 1, 2, 3};
455                 MemoryStream stream = new MemoryStream (bytes);
456                 BinaryReader reader = new BinaryReader (stream);
457                 
458                 Assertion.AssertEquals ("test#01", 0, reader.PeekChar ());
459                 Assertion.AssertEquals ("test#02", 0, reader.PeekChar ());
460                 Assertion.AssertEquals ("test#03", 0, reader.Read ());
461                 Assertion.AssertEquals ("test#03", 1, reader.Read ());
462                 Assertion.AssertEquals ("test#03", 2, reader.PeekChar ());
463         }
464         
465         [Test]
466         [ExpectedException(typeof(ObjectDisposedException))]            
467         public void CloseRead ()
468         {
469                 byte [] bytes = new byte [] {0, 1, 2, 3};
470                 MemoryStream stream = new MemoryStream (bytes);
471                 BinaryReader reader = new BinaryReader (stream);
472                 reader.Close ();
473                 reader.Read ();
474         }
475
476         [Test]
477         [ExpectedException(typeof(ObjectDisposedException))]            
478         public void ClosePeakChar ()
479         {
480                 byte [] bytes = new byte [] {0, 1, 2, 3};
481                 MemoryStream stream = new MemoryStream (bytes);
482                 BinaryReader reader = new BinaryReader (stream);
483                 reader.Close ();
484                 reader.PeekChar ();
485         }
486
487         [Test]
488         [ExpectedException(typeof(ObjectDisposedException))]
489         public void CloseReadBytes ()
490         {
491                 byte [] bytes = new byte [] {0, 1, 2, 3};
492                 MemoryStream stream = new MemoryStream (bytes);
493                 BinaryReader reader = new BinaryReader (stream);
494                 reader.Close ();
495                 reader.ReadBytes (1);
496         }
497
498         [Test]
499         public void BaseStream ()
500         {
501                 byte [] bytes = new byte [] {0, 1, 2, 3};
502                 MemoryStream stream = new MemoryStream (bytes);
503                 BinaryReader reader = new BinaryReader (stream);
504                 
505                 Assertion.AssertEquals ("test#01", 4, reader.BaseStream.Length);
506                 Assertion.AssertEquals ("test#02", true, reader.BaseStream.CanRead);            
507                 reader.Close ();
508                 Assertion.AssertEquals ("test#03", null, reader.BaseStream);
509         }
510
511         
512         /// <summary>
513         /// Tests read (byte [], int, int) method
514         /// </summary>
515         [Test]
516         public void ReadByteArray ()
517         {
518                 byte [] bytes = new byte [] {0, 1, 2, 3, 4, 5};
519                 MemoryStream stream = new MemoryStream (bytes);
520                 BinaryReader reader = new BinaryReader (stream);
521                 
522                 bytes = new byte [3];
523                 reader.Read (bytes, 0, 3);
524                 Assertion.AssertEquals ("test#01", 0, bytes [0]);
525                 Assertion.AssertEquals ("test#02", 1, bytes [1]);
526                 Assertion.AssertEquals ("test#03", 2, bytes [2]);
527
528                 bytes = new byte [6];
529                 reader.Read (bytes, 3, 3);
530                 Assertion.AssertEquals ("test#04", 0, bytes [0]);
531                 Assertion.AssertEquals ("test#05", 0, bytes [1]);
532                 Assertion.AssertEquals ("test#06", 0, bytes [2]);
533                 Assertion.AssertEquals ("test#07", 3, bytes [3]);
534                 Assertion.AssertEquals ("test#08", 4, bytes [4]);
535                 Assertion.AssertEquals ("test#09", 5, bytes [5]);
536                 
537                 bytes = new byte [2];
538                 reader.Read (bytes, 0, 2);
539                 Assertion.AssertEquals ("test#10", 0, bytes [0]);
540                 Assertion.AssertEquals ("test#11", 0, bytes [1]);                               
541         }
542         
543         /// <summary>
544         /// Test Read (char [], int, int)
545         /// </summary>
546         [Test]
547         public void ReadCharArray ()
548         {
549                 
550                 MemoryStream stream = new MemoryStream (new byte [] {109, 111, 110, 111, 58, 58});
551                 BinaryReader reader = new BinaryReader (stream);
552                 
553                 char [] chars = new char [3];
554                 reader.Read (chars, 0, 3);
555                 Assertion.AssertEquals ("test#01", 'm', chars [0]);
556                 Assertion.AssertEquals ("test#02", 'o', chars [1]);
557                 Assertion.AssertEquals ("test#03", 'n', chars [2]);
558
559                 chars = new char [6];
560                 reader.Read (chars, 3, 3);
561                 Assertion.AssertEquals ("test#04", 0, chars [0]);
562                 Assertion.AssertEquals ("test#05", 0, chars [1]);
563                 Assertion.AssertEquals ("test#06", 0, chars [2]);
564                 Assertion.AssertEquals ("test#07", 'o', chars [3]);
565                 Assertion.AssertEquals ("test#08", ':', chars [4]);
566                 Assertion.AssertEquals ("test#09", ':', chars [5]);
567                 
568                 chars = new char [2];
569                 reader.Read (chars, 0, 2);
570                 Assertion.AssertEquals ("test#08", 0, chars [0]);
571                 Assertion.AssertEquals ("test#09", 0, chars [1]);
572
573         }
574         
575         /// <summary>
576         /// Test ReadBoolean () method.
577         /// </summary>
578         [Test]
579         public void ReadBoolean ()
580         {
581                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
582                 BinaryReader reader = new BinaryReader (stream);
583                 
584                 Assertion.AssertEquals ("test#01", false, reader.ReadBoolean ());
585                 Assertion.AssertEquals ("test#02", true, reader.ReadBoolean ());
586                 Assertion.AssertEquals ("test#03", true, reader.ReadBoolean ());
587                 Assertion.AssertEquals ("test#04", false, reader.ReadBoolean ());
588                 Assertion.AssertEquals ("test#05", true, reader.ReadBoolean ());                
589         }
590         
591         /// <summary>
592         /// Test ReadBoolean () method exceptions.
593         /// </summary>
594         [Test]
595         [ExpectedException(typeof(EndOfStreamException))]       
596         public void ReadBooleanException ()
597         {
598                 MemoryStream stream = new MemoryStream (new byte [] {0, 1});
599                 BinaryReader reader = new BinaryReader (stream);
600                 
601                 reader.ReadBoolean ();
602                 reader.ReadBoolean ();
603                 reader.ReadBoolean ();
604                 Assertion.Fail ();              
605         }
606         
607         /// <summary>
608         /// Test ReadByte () method.
609         /// </summary>
610         [Test]
611         public void ReadByte ()
612         {
613                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
614                 BinaryReader reader = new BinaryReader (stream);
615                 
616                 Assertion.AssertEquals ("test#01", 0, reader.ReadByte ());
617                 Assertion.AssertEquals ("test#02", 1, reader.ReadByte ());
618                 Assertion.AssertEquals ("test#03", 99, reader.ReadByte ());
619                 Assertion.AssertEquals ("test#04", 0, reader.ReadByte ());
620                 Assertion.AssertEquals ("test#05", 13, reader.ReadByte ());             
621         }
622         
623         /// <summary>
624         /// Test ReadByte () method exceptions.
625         /// </summary>
626         [Test]
627         [ExpectedException(typeof(EndOfStreamException))]       
628         public void ReadByteException ()
629         {
630                 MemoryStream stream = new MemoryStream (new byte [] {0, 1});
631                 BinaryReader reader = new BinaryReader (stream);
632                 reader.ReadByte ();
633                 reader.ReadByte ();
634                 reader.ReadByte ();
635                 Assertion.Fail ();              
636         }
637         
638         /// <summary>
639         /// Test ReadBytes (int) method.
640         /// </summary>
641         [Test]
642         public void ReadBytes ()
643         {
644                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
645                 BinaryReader reader = new BinaryReader (stream);
646                 
647                 byte [] bytes = reader.ReadBytes (2);
648                 Assertion.AssertEquals ("test#01", 0, bytes [0]);
649                 Assertion.AssertEquals ("test#02", 1, bytes [1]);
650                 
651                 bytes = reader.ReadBytes (2);
652                 Assertion.AssertEquals ("test#03", 99, bytes [0]);
653                 Assertion.AssertEquals ("test#04", 0, bytes [1]);
654                 
655                 bytes = reader.ReadBytes (2);
656                 Assertion.AssertEquals ("test#05", 13, bytes [0]);
657                 Assertion.AssertEquals ("test#06", 1, bytes.Length);
658         }
659         
660         /// <summary>
661         /// Test ReadBytes (int) method exception.
662         /// </summary>
663         [Test]
664         [ExpectedException(typeof(ArgumentOutOfRangeException))]
665         public void ReadBytesException ()
666         {
667                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
668                 BinaryReader reader = new BinaryReader (stream);
669                 reader.ReadBytes (-1);          
670         }
671         
672         /// <summary>
673         /// Test ReadChar () method.
674         /// </summary>
675         [Test]
676         public void ReadChar ()
677         {
678                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
679                 BinaryReader reader = new BinaryReader (stream);
680                 
681                 Assertion.AssertEquals ("test#01", 0, reader.ReadChar ());
682                 Assertion.AssertEquals ("test#02", 1, reader.ReadChar ());
683                 Assertion.AssertEquals ("test#03", 99, reader.ReadChar ());
684                 Assertion.AssertEquals ("test#04", 0, reader.ReadChar ());
685                 Assertion.AssertEquals ("test#05", 13, reader.ReadChar ());
686         }
687         
688         /// <summary>
689         /// Test ReadChar () method exception.
690         /// </summary>
691         [Test]
692         [ExpectedException(typeof(EndOfStreamException))]       
693         public void ReadCharException ()
694         {
695                 MemoryStream stream = new MemoryStream (new byte [] {0, 1});
696                 BinaryReader reader = new BinaryReader (stream);
697                 reader.ReadChar ();
698                 reader.ReadChar ();
699                 reader.ReadChar ();
700                 Assertion.Fail ();
701         }
702
703         /// <summary>
704         /// Test ReadChars (int) method.
705         /// </summary>
706         [Test]
707         public void ReadChars ()
708         {
709                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
710                 BinaryReader reader = new BinaryReader (stream);
711                 
712                 char [] chars = reader.ReadChars (2);
713                 Assertion.AssertEquals ("test#01", 0, chars [0]);
714                 Assertion.AssertEquals ("test#02", 1, chars [1]);
715                 
716                 chars = reader.ReadChars (2);
717                 Assertion.AssertEquals ("test#03", 99, chars [0]);
718                 Assertion.AssertEquals ("test#04", 0, chars [1]);
719                 
720                 chars = reader.ReadChars (2);
721                 Assertion.AssertEquals ("test#05", 13, chars [0]);
722                 Assertion.AssertEquals ("test#06", 1, chars.Length);
723         }
724         
725         /// <summary>
726         /// Test ReadChars (int value) exceptions. If value is negative exception is thrown
727         /// </summary>
728         [Test]
729         [ExpectedException(typeof(ArgumentOutOfRangeException))]
730         public void ReadCharsException ()
731         {
732                 MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
733                 BinaryReader reader = new BinaryReader (stream);
734                 reader.ReadChars (-1);
735         }
736         
737         
738         /// <summary>
739         /// Test ReadDecimal () method.
740         /// </summary>
741         [Test]
742         public void ReadDecimal ()
743         {
744                 MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0});
745                 BinaryReader reader = new BinaryReader (stream);                
746                 Assertion.AssertEquals ("test#01", -18295873486192640, reader.ReadDecimal ());
747         }
748         
749         [Test]
750         [ExpectedException(typeof(EndOfStreamException))]
751         public void ReadDecimalException ()
752         {
753                 MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0, 0});
754                 BinaryReader reader = new BinaryReader (stream);                
755                 reader.ReadDecimal ();
756                 reader.ReadDecimal ();          
757         }
758         
759         [Test]
760         public void ReadDouble ()
761         {
762                 MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0});
763                 BinaryReader reader = new BinaryReader (stream);
764
765                 Assertion.AssertEquals ("test#01", 1.89131277973112E-307, reader.ReadDouble ());
766                 Assertion.AssertEquals ("test#02", 1.2024538023802E+111, reader.ReadDouble ()); 
767         }
768         
769         [Test]
770         [ExpectedException(typeof(EndOfStreamException))]
771         public void ReadDoubleException ()
772         {
773                 MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0});
774                 BinaryReader reader = new BinaryReader (stream);
775
776                 reader.ReadDouble ();
777                 reader.ReadDouble ();
778                 reader.ReadDouble ();
779         }
780         
781         [Test]
782         public void ReadInt16 ()
783         {
784                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
785                 BinaryReader reader = new BinaryReader (stream);
786                 
787                 Assertion.AssertEquals ("test#01", 321, reader.ReadInt16 ());
788                 Assertion.AssertEquals ("test#02", 11040, reader.ReadInt16 ());
789                 Assertion.AssertEquals ("test#03", 773, reader.ReadInt16 ());
790                 Assertion.AssertEquals ("test#04", 54, reader.ReadInt16 ());            
791         }
792         
793         [Test]
794         [ExpectedException(typeof(EndOfStreamException))]       
795         public void ReadInt16Exception ()
796         {
797                 MemoryStream stream = new MemoryStream (new byte [] {65, 1});
798                 BinaryReader reader = new BinaryReader (stream);
799                 reader.ReadInt16 ();
800                 reader.ReadInt16 ();
801         }
802
803         [Test]
804         public void ReadInt32 ()
805         {
806                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
807                 BinaryReader reader = new BinaryReader (stream);
808                 
809                 Assertion.AssertEquals ("test#01", 723517761, reader.ReadInt32 ());
810                 Assertion.AssertEquals ("test#02", 3539717, reader.ReadInt32 ());
811         }
812         
813         [Test]
814         [ExpectedException(typeof(EndOfStreamException))]       
815         public void ReadInt32Exception ()
816         {
817                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43});
818                 BinaryReader reader = new BinaryReader (stream);
819                 
820                 reader.ReadInt32 ();
821                 reader.ReadInt32 ();
822         }
823
824         [Test]
825         public void ReadInt64 ()
826         {
827                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
828                 BinaryReader reader = new BinaryReader (stream);
829                 
830                 Assertion.AssertEquals ("test#01", 15202969475612993, reader.ReadInt64 ());
831                 Assertion.AssertEquals ("test#02", 2471354792417887522, reader.ReadInt64 ());
832         }
833         
834         [Test]
835         [ExpectedException(typeof(EndOfStreamException))]       
836         public void ReadInt64Exception ()
837         {
838                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
839                 BinaryReader reader = new BinaryReader (stream);
840                 
841                 reader.ReadInt64 ();
842                 reader.ReadInt64 ();
843                 reader.ReadInt64 ();
844         }
845         
846         [Test]
847         public void ReadSByte ()
848         {
849                 MemoryStream stream = new MemoryStream (new byte [] {65, 200, 32});
850                 BinaryReader reader = new BinaryReader (stream);
851                 
852                 Assertion.AssertEquals ("test#01", 65, reader.ReadSByte ());
853                 Assertion.AssertEquals ("test#02", -56, reader.ReadSByte ());
854                 Assertion.AssertEquals ("test#03", 32, reader.ReadSByte ());
855         }
856         
857         [Test]
858         [ExpectedException(typeof(EndOfStreamException))]               
859         public void ReadSByteException ()
860         {
861                 MemoryStream stream = new MemoryStream (new byte [] {65, 200});
862                 BinaryReader reader = new BinaryReader (stream);
863                 
864                 reader.ReadSByte ();
865                 reader.ReadSByte ();
866                 reader.ReadSByte ();            
867         }
868         
869         [Test]
870         public void ReadSingle ()
871         {
872                 MemoryStream stream = new MemoryStream (new byte [] {65, 200, 0, 0, 0, 1, 2, 3, 4});
873                 BinaryReader reader = new BinaryReader (stream);
874                 
875                 Assertion.AssertEquals ("test#01", 7.183757E-41, reader.ReadSingle ());
876                 Assertion.AssertEquals ("test#01", 3.820471E-37, reader.ReadSingle ());
877         }
878         
879         [Test]
880         [ExpectedException(typeof(EndOfStreamException))]               
881         public void ReadSingleException ()
882         {
883                 MemoryStream stream = new MemoryStream (new byte [] {65, 200, 0, 0, 0, 1, 2, 3, 4});
884                 BinaryReader reader = new BinaryReader (stream);
885                 
886                 reader.ReadSingle ();
887                 reader.ReadSingle ();
888                 reader.ReadSingle ();
889         }
890         
891         [Test]
892         public void ReadString ()
893         {
894                 MemoryStream stream = new MemoryStream (new byte [] {6,109, 111, 110, 111, 58, 58});
895                 BinaryReader reader = new BinaryReader (stream);
896                 
897                 Assertion.AssertEquals ("test#01", "mono::", reader.ReadString ());
898                 
899                 stream = new MemoryStream (new byte [] {2,109, 111, 3, 111, 58, 58});
900                 reader = new BinaryReader (stream);
901                 Assertion.AssertEquals ("test#02", "mo", reader.ReadString ());
902                 Assertion.AssertEquals ("test#03", "o::", reader.ReadString ());
903         }
904         
905         [Test]
906         [ExpectedException(typeof(EndOfStreamException))]               
907         public void ReadStringException ()
908         {
909                 MemoryStream stream = new MemoryStream (new byte [] {2,109, 111, 3, 111, 58, 58});
910                 BinaryReader reader = new BinaryReader (stream);
911                 reader.ReadString ();
912                 reader.ReadString ();
913                 reader.ReadString ();
914         }
915         
916         [Test]
917         public void ReadUInt16 ()
918         {
919                 MemoryStream stream = new MemoryStream (new byte [] {200, 200, 32, 43, 5, 3, 54, 0});
920                 BinaryReader reader = new BinaryReader (stream);
921                 
922                 Assertion.AssertEquals ("test#01", 51400, reader.ReadUInt16 ());
923                 Assertion.AssertEquals ("test#02", 11040, reader.ReadUInt16 ());
924                 Assertion.AssertEquals ("test#03", 773, reader.ReadUInt16 ());
925                 Assertion.AssertEquals ("test#04", 54, reader.ReadUInt16 ());           
926         }
927         
928         [Test]
929         [ExpectedException(typeof(EndOfStreamException))]       
930         public void ReadUInt16Exception ()
931         {
932                 MemoryStream stream = new MemoryStream (new byte [] {65, 1});
933                 BinaryReader reader = new BinaryReader (stream);
934                 reader.ReadUInt16 ();
935                 reader.ReadUInt16 ();
936         }
937
938         [Test]
939         public void ReadUInt32 ()
940         {
941                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
942                 BinaryReader reader = new BinaryReader (stream);
943                 
944                 Assertion.AssertEquals ("test#01", 723517761, reader.ReadUInt32 ());
945                 Assertion.AssertEquals ("test#02", 3539717, reader.ReadUInt32 ());
946         }
947         
948         [Test]
949         [ExpectedException(typeof(EndOfStreamException))]       
950         public void ReadUInt32Exception ()
951         {
952                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43});
953                 BinaryReader reader = new BinaryReader (stream);
954                 
955                 reader.ReadUInt32 ();
956                 reader.ReadUInt32 ();
957         }
958
959         [Test]
960         public void ReadUInt64 ()
961         {
962                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
963                 BinaryReader reader = new BinaryReader (stream);
964                 
965                 Assertion.AssertEquals ("test#01", 15202969475612993, reader.ReadUInt64 ());
966                 Assertion.AssertEquals ("test#02", 2471354792417887522, reader.ReadUInt64 ());
967         }
968         
969         [Test]
970         [ExpectedException(typeof(EndOfStreamException))]       
971         public void ReadUInt64Exception ()
972         {
973                 MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
974                 BinaryReader reader = new BinaryReader (stream);
975                 
976                 reader.ReadUInt64 ();
977                 reader.ReadUInt64 ();
978                 reader.ReadUInt64 ();
979         }       
980
981         private void DeleteFile (string path)
982         {
983                 if (File.Exists (path))
984                         File.Delete (path);
985         }       
986 }
987 }