updating to the latest module.
[mono.git] / mcs / class / corlib / Test / System.IO / MemoryStreamTest.cs
1 //
2 // System.IO.MemoryStreamTest
3 //
4 // Authors:
5 //      Marcin Szczepanski (marcins@zipworld.com.au)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell (http://www.novell.com)
11 //
12
13 using NUnit.Framework;
14 using System.IO;
15 using System;
16 using System.Text;
17
18 namespace MonoTests.System.IO
19 {
20         [TestFixture]
21         public class MemoryStreamTest : Assertion
22         {
23                 MemoryStream testStream;
24                 byte [] testStreamData;
25
26                 [SetUp]
27                 public void SetUp ()
28                 {
29                         testStreamData = new byte [100];
30
31                         for (int i = 0; i < 100; i++)
32                                 testStreamData[i] = (byte) (100 - i);
33
34                         testStream = new MemoryStream (testStreamData);
35                 }
36
37                 // 
38                 // Verify that the first count bytes in testBytes are the same as
39                 // the count bytes from index start in testStreamData
40                 //
41                 void VerifyTestData (string id, byte [] testBytes, int start, int count)
42                 {
43                         if (testBytes == null)
44                                 Fail (id + "+1 testBytes is null");
45
46                         if (start < 0 ||
47                             count < 0  ||
48                             start + count > testStreamData.Length ||
49                             start > testStreamData.Length)
50                                 throw new ArgumentOutOfRangeException (id + "+2");
51
52                         for (int test = 0; test < count; test++) {
53                                 if (testBytes [test] == testStreamData [start + test])
54                                         continue;
55
56                                 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
57                                 failStr = String.Format (failStr,
58                                                         test,
59                                                         start + test,
60                                                         testBytes [test],
61                                                         testStreamData [start + test]);
62                                 Fail (id + "-3" + failStr);
63                         }
64                 }
65
66                 [Test]
67                 public void ConstructorsOne ()
68                 {
69                         MemoryStream ms = new MemoryStream();
70
71                         AssertEquals ("#01", 0L, ms.Length);
72                         AssertEquals ("#02", 0, ms.Capacity);
73                         AssertEquals ("#03", true, ms.CanWrite);
74                 }
75
76                 [Test]
77                 public void ConstructorsTwo ()
78                 {
79                         MemoryStream ms = new MemoryStream (10);
80
81                         AssertEquals ("#01", 0L, ms.Length);
82                         AssertEquals ("#02", 10, ms.Capacity);
83                         ms.Capacity = 0;
84                         byte [] buffer = ms.GetBuffer ();
85                         // Begin: wow!!!
86                         AssertEquals ("#03", -1, ms.ReadByte ());
87                         AssertEquals ("#04", null, buffer); // <--
88                         ms.Read (new byte [5], 0, 5);
89                         AssertEquals ("#05", 0, ms.Position);
90                         AssertEquals ("#06", 0, ms.Length);
91                         // End
92                 }
93
94                 [Test]
95                 public void ConstructorsThree ()
96                 {
97                         MemoryStream ms = new MemoryStream (testStreamData);
98                         AssertEquals ("#01", 100, ms.Length);
99                         AssertEquals ("#02", 0, ms.Position);
100                 }
101
102                 [Test]
103                 public void ConstructorsFour ()
104                 {
105                         MemoryStream ms = new MemoryStream (testStreamData, true);
106                         AssertEquals ("#01", 100, ms.Length);
107                         AssertEquals ("#02", 0, ms.Position);
108                         ms.Position = 50;
109                         byte saved = testStreamData [50];
110                         try {
111                                 ms.WriteByte (23);
112                                 AssertEquals ("#03", testStreamData [50], 23);
113                         } finally {
114                                 testStreamData [50] = saved;
115                         }
116                         ms.Position = 100;
117                         try {
118                                 ms.WriteByte (23);
119                         } catch (Exception) {
120                                 return;
121                         }
122                         Fail ("#04");
123                 }
124
125                 [Test]
126                 public void ConstructorsFive ()
127                 {
128                         MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
129                         AssertEquals ("#01", 50, ms.Length);
130                         AssertEquals ("#02", 0, ms.Position);
131                         AssertEquals ("#03", 50, ms.Capacity);
132                         ms.Position = 1;
133                         byte saved = testStreamData [51];
134                         try {
135                                 ms.WriteByte (23);
136                                 AssertEquals ("#04", testStreamData [51], 23);
137                         } finally {
138                                 testStreamData [51] = saved;
139                         }
140                         ms.Position = 100;
141                         bool gotException = false;
142                         try {
143                                 ms.WriteByte (23);
144                         } catch (NotSupportedException) {
145                                 gotException = true;
146                         }
147
148                         if (!gotException)
149                                 Fail ("#05");
150
151                         gotException = false;
152                         try {
153                                 ms.GetBuffer ();
154                         } catch (UnauthorizedAccessException) {
155                                 gotException = true;
156                         }
157
158                         if (!gotException)
159                                 Fail ("#06");
160
161                         ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
162                                            // This is lame, as the length is 50!!!
163                                            
164                         gotException = false;
165                         try {
166                                 ms.Capacity = 51;
167                         } catch (NotSupportedException) {
168                                 gotException = true;
169                         }
170
171                         if (!gotException)
172                                 Fail ("#07");
173
174                         AssertEquals ("#08", 50, ms.ToArray ().Length);
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179                 public void ConstructorsSix ()
180                 {
181                         MemoryStream ms = new MemoryStream (-2);
182                 }
183
184                 [Test]
185                 public void Read ()
186                 {
187                         byte [] readBytes = new byte [20];
188
189                         /* Test simple read */
190                         testStream.Read (readBytes, 0, 10);
191                         VerifyTestData ("R1", readBytes, 0, 10);
192
193                         /* Seek back to beginning */
194
195                         testStream.Seek (0, SeekOrigin.Begin);
196
197                         /* Read again, bit more this time */
198                         testStream.Read (readBytes, 0, 20);
199                         VerifyTestData ("R2", readBytes, 0, 20);
200
201                         /* Seek to 20 bytes from End */
202                         testStream.Seek (-20, SeekOrigin.End);
203                         testStream.Read (readBytes, 0, 20);
204                         VerifyTestData ("R3", readBytes, 80, 20);
205
206                         int readByte = testStream.ReadByte();
207                         AssertEquals (-1, readByte);
208                 }
209
210                 [Test]
211                 public void WriteBytes ()
212                 {
213                         byte[] readBytes = new byte[100];
214
215                         MemoryStream ms = new MemoryStream (100);
216
217                         for (int i = 0; i < 100; i++)
218                                 ms.WriteByte (testStreamData [i]);
219
220                         ms.Seek (0, SeekOrigin.Begin); 
221                         testStream.Read (readBytes, 0, 100);
222                         VerifyTestData ("W1", readBytes, 0, 100);
223                 }               
224
225                 [Test]
226                 public void WriteBlock ()
227                 {
228                         byte[] readBytes = new byte[100];
229
230                         MemoryStream ms = new MemoryStream (100);
231
232                         ms.Write (testStreamData, 0, 100);
233                         ms.Seek (0, SeekOrigin.Begin); 
234                         testStream.Read (readBytes, 0, 100);
235                         VerifyTestData ("WB1", readBytes, 0, 100);
236                         byte[] arrayBytes = testStream.ToArray();
237                         AssertEquals ("#01", 100, arrayBytes.Length);
238                         VerifyTestData ("WB2", arrayBytes, 0, 100);
239                 }
240
241                 [Test]
242                 public void PositionLength ()
243                 {
244                         MemoryStream ms = new MemoryStream ();
245                         ms.Position = 4;
246                         ms.WriteByte ((byte) 'M');
247                         ms.WriteByte ((byte) 'O');
248                         AssertEquals ("#01", 6, ms.Length);
249                         AssertEquals ("#02", 6, ms.Position);
250                         ms.Position = 0;
251                         AssertEquals ("#03", 0, ms.Position);
252                 }
253
254                 [Test]
255                 [ExpectedException (typeof (NotSupportedException))]
256                 public void MorePositionLength ()
257                 {
258                         MemoryStream ms = new MemoryStream (testStreamData);
259                         ms.Position = 101;
260                         AssertEquals ("#01", 101, ms.Position);
261                         AssertEquals ("#02", 100, ms.Length);
262                         ms.WriteByte (1); // This should throw the exception
263                 }
264
265                 [Test]
266                 public void GetBufferOne ()
267                 {
268                         MemoryStream ms = new MemoryStream ();
269                         byte [] buffer = ms.GetBuffer ();
270                         AssertEquals ("#01", 0, buffer.Length);
271                 }
272
273                 [Test]
274                 public void GetBufferTwo ()
275                 {
276                         MemoryStream ms = new MemoryStream (100);
277                         byte [] buffer = ms.GetBuffer ();
278                         AssertEquals ("#01", 100, buffer.Length);
279
280                         ms.Write (testStreamData, 0, 100);
281                         ms.Write (testStreamData, 0, 100);
282                         AssertEquals ("#02", 200, ms.Length);
283                         buffer = ms.GetBuffer ();
284                         AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
285                 }
286
287                 [Test]
288                 public void Closed ()
289                 {
290                         MemoryStream ms = new MemoryStream (100);
291                         ms.Close ();
292                         bool thrown = false;
293                         try {
294                                 int x = ms.Capacity;
295                         } catch (ObjectDisposedException) {
296                                 thrown = true;
297                         }
298
299                         if (!thrown)
300                                 Fail ("#01");
301
302                         thrown = false;
303                         try {
304                                 ms.Capacity = 1;
305                         } catch (ObjectDisposedException) {
306                                 thrown = true;
307                         }
308
309                         if (!thrown)
310                                 Fail ("#02");
311
312                         // The first exception thrown is ObjectDisposed, not ArgumentNull
313                         thrown = false;
314                         try {
315                                 ms.Read (null, 0, 1);
316                         } catch (ObjectDisposedException) {
317                                 thrown = true;
318                         }
319
320                         if (!thrown)
321                                 Fail ("#03");
322
323                         thrown = false;
324                         try {
325                                 ms.Write (null, 0, 1);
326                         } catch (ObjectDisposedException) {
327                                 thrown = true;
328                         }
329
330                         if (!thrown)
331                                 Fail ("#03");
332                 }
333
334                 [Test]
335                 [ExpectedException (typeof (ObjectDisposedException))]
336                 public void Close_get_Length () 
337                 {
338                         MemoryStream ms = new MemoryStream (100);
339                         ms.Close ();
340                         long x = ms.Length;
341                 }
342
343                 [Test]
344                 [ExpectedException (typeof (ObjectDisposedException))]
345                 public void Close_get_Position () 
346                 {
347                         MemoryStream ms = new MemoryStream (100);
348                         ms.Close ();
349                         long x = ms.Position;
350                 }
351
352                 [Test]
353                 [ExpectedException (typeof (ObjectDisposedException))]
354                 public void Close_set_Position () 
355                 {
356                         MemoryStream ms = new MemoryStream (100);
357                         ms.Close ();
358                         ms.Position = 0;
359                 }
360
361                 [Test]
362                 public void Seek ()
363                 {
364                         MemoryStream ms = new MemoryStream (100);
365                         ms.Write (testStreamData, 0, 100);
366                         ms.Seek (0, SeekOrigin.Begin);
367                         ms.Position = 50;
368                         ms.Seek (-50, SeekOrigin.Current);
369                         ms.Seek (-50, SeekOrigin.End);
370
371                         bool thrown = false;
372                         ms.Position = 49;
373                         try {
374                                 ms.Seek (-50, SeekOrigin.Current);
375                         } catch (IOException) {
376                                 thrown = true;
377                         }
378                         if (!thrown)
379                                 Fail ("#01");
380                         
381                         thrown = false;
382                         try {
383                                 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
384                         } catch (ArgumentOutOfRangeException) {
385                                 thrown = true;
386                         }
387
388                         if (!thrown)
389                                 Fail ("#02");
390
391                         thrown=false;
392                         try {
393                                 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
394                                 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
395                         } catch (IOException) {
396                                 thrown = true;
397                         }
398
399                         if (!thrown)
400                                 Fail ("#03");
401
402                         ms=new MemoryStream (256);
403
404                         ms.Write (testStreamData, 0, 100);
405                         ms.Position=0;
406                         AssertEquals ("#01", 100, ms.Length);
407                         AssertEquals ("#02", 0, ms.Position);
408
409                         ms.Position=128;
410                         AssertEquals ("#03", 100, ms.Length);
411                         AssertEquals ("#04", 128, ms.Position);
412
413                         ms.Position=768;
414                         AssertEquals ("#05", 100, ms.Length);
415                         AssertEquals ("#06", 768, ms.Position);
416
417                         ms.WriteByte (0);
418                         AssertEquals ("#07", 769, ms.Length);
419                         AssertEquals ("#08", 769, ms.Position);
420                 }
421
422                 [Test]
423                 [ExpectedException (typeof (ObjectDisposedException))]
424                 public void Seek_Disposed () 
425                 {
426                         MemoryStream ms = new MemoryStream ();
427                         ms.Close ();
428                         ms.Seek (0, SeekOrigin.Begin);
429                 }
430
431                 [Test]
432                 public void SetLength ()
433                 {
434                         MemoryStream ms = new MemoryStream ();
435                         ms.Write (testStreamData, 0, 100);
436                         ms.Position = 100;
437                         ms.SetLength (150);
438                         AssertEquals ("#01", 150, ms.Length);
439                         AssertEquals ("#02", 100, ms.Position);
440                         ms.SetLength (80);
441                         AssertEquals ("#03", 80, ms.Length);
442                         AssertEquals ("#04", 80, ms.Position);
443                 }
444
445                 [Test]
446                 [ExpectedException (typeof (NotSupportedException))]
447                 public void SetLength_ReadOnly ()
448                 {
449                         MemoryStream ms = new MemoryStream (testStreamData, false);
450                         ms.SetLength (10);
451                 }
452
453                 [Test]
454                 [ExpectedException (typeof (NotSupportedException))]
455                 public void WriteNonWritable ()
456                 {
457                         MemoryStream ms = new MemoryStream (testStreamData, false);
458                         ms.Write (testStreamData, 0, 100);
459                 }
460
461                 [Test]
462                 [ExpectedException (typeof (NotSupportedException))]
463                 public void WriteExpand ()
464                 {
465                         MemoryStream ms = new MemoryStream (testStreamData);
466                         ms.Write (testStreamData, 0, 100);
467                         ms.Write (testStreamData, 0, 100); // This one throws the exception
468                 }
469
470                 [Test]
471                 public void WriteByte ()
472                 {
473                         MemoryStream ms = new MemoryStream (100);
474                         ms.Write (testStreamData, 0, 100);
475                         ms.Position = 100;
476                         ms.WriteByte (101);
477                         AssertEquals ("#01", 101, ms.Position);
478                         AssertEquals ("#02", 101, ms.Length);
479                         AssertEquals ("#03", 256, ms.Capacity);
480                         ms.Write (testStreamData, 0, 100);
481                         ms.Write (testStreamData, 0, 100);
482                         // 301
483                         AssertEquals ("#04", 301, ms.Position);
484                         AssertEquals ("#05", 301, ms.Length);
485                         AssertEquals ("#06", 512, ms.Capacity);
486                 }
487
488                 [Test]
489                 public void WriteLengths () {
490                         MemoryStream ms=new MemoryStream (256);
491                         BinaryWriter writer=new BinaryWriter (ms);
492
493                         writer.Write ((byte)'1');
494                         AssertEquals ("#01", 1, ms.Length);
495                         AssertEquals ("#02", 256, ms.Capacity);
496                         
497                         writer.Write ((ushort)0);
498                         AssertEquals ("#03", 3, ms.Length);
499                         AssertEquals ("#04", 256, ms.Capacity);
500
501                         writer.Write (testStreamData, 0, 23);
502                         AssertEquals ("#05", 26, ms.Length);
503                         AssertEquals ("#06", 256, ms.Capacity);
504
505                         writer.Write (testStreamData);
506                         writer.Write (testStreamData);
507                         writer.Write (testStreamData);
508                         AssertEquals ("#07", 326, ms.Length);
509                 }
510
511                 [Test]
512                 public void MoreWriteByte ()
513                 {
514                         byte[] buffer = new byte [44];
515                         
516                         MemoryStream ms = new MemoryStream (buffer);
517                         BinaryWriter bw = new BinaryWriter (ms);
518                         for(int i=0; i < 44; i++)
519                                 bw.Write ((byte) 1);
520                 }
521
522                 [Test]
523                 [ExpectedException (typeof (NotSupportedException))]
524                 public void MoreWriteByte2 ()
525                 {
526                         byte[] buffer = new byte [43]; // Note the 43 here
527                         
528                         MemoryStream ms = new MemoryStream (buffer);
529                         BinaryWriter bw = new BinaryWriter (ms);
530                         for(int i=0; i < 44; i++)
531                                 bw.Write ((byte) 1);
532                 }
533
534                 [Test]
535                 public void Expand () 
536                 {
537                         byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
538                         MemoryStream ms = new MemoryStream ();
539                         ms.Write (array, 0, array.Length);
540                         ms.SetLength (4);
541                         ms.Seek (4, SeekOrigin.End);
542                         ms.WriteByte (0xFF);
543                         AssertEquals ("Result", "01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()));
544                 }
545         }
546 }
547