2 // System.IO.MemoryStreamTest
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell (http://www.novell.com)
13 using NUnit.Framework;
18 namespace MonoTests.System.IO
21 public class MemoryStreamTest
23 MemoryStream testStream;
24 byte [] testStreamData;
29 testStreamData = new byte [100];
31 for (int i = 0; i < 100; i++)
32 testStreamData[i] = (byte) (100 - i);
34 testStream = new MemoryStream (testStreamData);
38 // Verify that the first count bytes in testBytes are the same as
39 // the count bytes from index start in testStreamData
41 void VerifyTestData (string id, byte [] testBytes, int start, int count)
43 if (testBytes == null)
44 Assert.Fail (id + "+1 testBytes is null");
48 start + count > testStreamData.Length ||
49 start > testStreamData.Length)
50 throw new ArgumentOutOfRangeException (id + "+2");
52 for (int test = 0; test < count; test++) {
53 if (testBytes [test] == testStreamData [start + test])
56 string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
57 failStr = String.Format (failStr,
61 testStreamData [start + test]);
62 Assert.Fail (id + "-3" + failStr);
66 public void AssertEquals (string message, int expected, int actual)
68 Assert.AreEqual (expected, actual, message);
71 public void AssertEquals (string message, long expected, long actual)
73 Assert.AreEqual (expected, actual, message);
76 public void AssertEquals (string message, bool expected, bool actual)
78 Assert.AreEqual (expected, actual, message);
82 public void ConstructorsOne ()
84 MemoryStream ms = new MemoryStream();
86 AssertEquals ("#01", 0L, ms.Length);
87 AssertEquals ("#02", 0, ms.Capacity);
88 AssertEquals ("#03", true, ms.CanWrite);
92 public void ConstructorsTwo ()
94 MemoryStream ms = new MemoryStream (10);
96 AssertEquals ("#01", 0L, ms.Length);
97 AssertEquals ("#02", 10, ms.Capacity);
99 byte [] buffer = ms.GetBuffer ();
101 AssertEquals ("#03", -1, ms.ReadByte ());
102 Assert.IsNull (buffer, "#04"); // <--
103 ms.Read (new byte [5], 0, 5);
104 AssertEquals ("#05", 0, ms.Position);
105 AssertEquals ("#06", 0, ms.Length);
110 public void ConstructorsThree ()
112 MemoryStream ms = new MemoryStream (testStreamData);
113 AssertEquals ("#01", 100, ms.Length);
114 AssertEquals ("#02", 0, ms.Position);
118 public void ConstructorsFour ()
120 MemoryStream ms = new MemoryStream (testStreamData, true);
121 AssertEquals ("#01", 100, ms.Length);
122 AssertEquals ("#02", 0, ms.Position);
124 byte saved = testStreamData [50];
127 AssertEquals ("#03", testStreamData [50], 23);
129 testStreamData [50] = saved;
134 } catch (Exception) {
141 public void ConstructorsFive ()
143 MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
144 AssertEquals ("#01", 50, ms.Length);
145 AssertEquals ("#02", 0, ms.Position);
146 AssertEquals ("#03", 50, ms.Capacity);
148 byte saved = testStreamData [51];
151 AssertEquals ("#04", testStreamData [51], 23);
153 testStreamData [51] = saved;
156 bool gotException = false;
159 } catch (NotSupportedException) {
166 ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
167 // This is lame, as the length is 50!!!
169 gotException = false;
172 } catch (NotSupportedException) {
179 AssertEquals ("#08", 50, ms.ToArray ().Length);
183 [ExpectedException (typeof (ArgumentOutOfRangeException))]
184 public void ConstructorsSix ()
186 MemoryStream ms = new MemoryStream (-2);
192 byte [] readBytes = new byte [20];
194 /* Test simple read */
195 testStream.Read (readBytes, 0, 10);
196 VerifyTestData ("R1", readBytes, 0, 10);
198 /* Seek back to beginning */
200 testStream.Seek (0, SeekOrigin.Begin);
202 /* Read again, bit more this time */
203 testStream.Read (readBytes, 0, 20);
204 VerifyTestData ("R2", readBytes, 0, 20);
206 /* Seek to 20 bytes from End */
207 testStream.Seek (-20, SeekOrigin.End);
208 testStream.Read (readBytes, 0, 20);
209 VerifyTestData ("R3", readBytes, 80, 20);
211 int readByte = testStream.ReadByte();
212 Assert.AreEqual (-1, readByte, "R4");
216 public void WriteBytes ()
218 byte[] readBytes = new byte[100];
220 MemoryStream ms = new MemoryStream (100);
222 for (int i = 0; i < 100; i++)
223 ms.WriteByte (testStreamData [i]);
225 ms.Seek (0, SeekOrigin.Begin);
226 testStream.Read (readBytes, 0, 100);
227 VerifyTestData ("W1", readBytes, 0, 100);
231 public void WriteBlock ()
233 byte[] readBytes = new byte[100];
235 MemoryStream ms = new MemoryStream (100);
237 ms.Write (testStreamData, 0, 100);
238 ms.Seek (0, SeekOrigin.Begin);
239 testStream.Read (readBytes, 0, 100);
240 VerifyTestData ("WB1", readBytes, 0, 100);
241 byte[] arrayBytes = testStream.ToArray();
242 AssertEquals ("#01", 100, arrayBytes.Length);
243 VerifyTestData ("WB2", arrayBytes, 0, 100);
247 public void PositionLength ()
249 MemoryStream ms = new MemoryStream ();
251 ms.WriteByte ((byte) 'M');
252 ms.WriteByte ((byte) 'O');
253 AssertEquals ("#01", 6, ms.Length);
254 AssertEquals ("#02", 6, ms.Position);
256 AssertEquals ("#03", 0, ms.Position);
260 [ExpectedException (typeof (NotSupportedException))]
261 public void MorePositionLength ()
263 MemoryStream ms = new MemoryStream (testStreamData);
265 AssertEquals ("#01", 101, ms.Position);
266 AssertEquals ("#02", 100, ms.Length);
267 ms.WriteByte (1); // This should throw the exception
271 public void GetBufferOne ()
273 MemoryStream ms = new MemoryStream ();
274 byte [] buffer = ms.GetBuffer ();
275 AssertEquals ("#01", 0, buffer.Length);
279 public void GetBufferTwo ()
281 MemoryStream ms = new MemoryStream (100);
282 byte [] buffer = ms.GetBuffer ();
283 AssertEquals ("#01", 100, buffer.Length);
285 ms.Write (testStreamData, 0, 100);
286 ms.Write (testStreamData, 0, 100);
287 AssertEquals ("#02", 200, ms.Length);
288 buffer = ms.GetBuffer ();
289 AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
293 public void Closed ()
295 MemoryStream ms = new MemoryStream (100);
300 } catch (ObjectDisposedException) {
310 } catch (ObjectDisposedException) {
317 // The first exception thrown is ObjectDisposed, not ArgumentNull
320 ms.Read (null, 0, 1);
321 } catch (ObjectDisposedException) {
330 ms.Write (null, 0, 1);
331 } catch (ObjectDisposedException) {
340 [ExpectedException (typeof (ObjectDisposedException))]
341 public void Close_get_Length ()
343 MemoryStream ms = new MemoryStream (100);
349 [ExpectedException (typeof (ObjectDisposedException))]
350 public void Close_get_Position ()
352 MemoryStream ms = new MemoryStream (100);
354 long x = ms.Position;
358 [ExpectedException (typeof (ObjectDisposedException))]
359 public void Close_set_Position ()
361 MemoryStream ms = new MemoryStream (100);
369 MemoryStream ms = new MemoryStream (100);
370 ms.Write (testStreamData, 0, 100);
371 ms.Seek (0, SeekOrigin.Begin);
373 ms.Seek (-50, SeekOrigin.Current);
374 ms.Seek (-50, SeekOrigin.End);
379 ms.Seek (-50, SeekOrigin.Current);
380 } catch (IOException) {
388 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
389 } catch (ArgumentOutOfRangeException) {
398 // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
399 ms.Seek (Int64.MinValue, SeekOrigin.Begin);
400 } catch (IOException) {
407 ms=new MemoryStream (256);
409 ms.Write (testStreamData, 0, 100);
411 AssertEquals ("#01", 100, ms.Length);
412 AssertEquals ("#02", 0, ms.Position);
415 AssertEquals ("#03", 100, ms.Length);
416 AssertEquals ("#04", 128, ms.Position);
419 AssertEquals ("#05", 100, ms.Length);
420 AssertEquals ("#06", 768, ms.Position);
423 AssertEquals ("#07", 769, ms.Length);
424 AssertEquals ("#08", 769, ms.Position);
428 [ExpectedException (typeof (ObjectDisposedException))]
429 public void Seek_Disposed ()
431 MemoryStream ms = new MemoryStream ();
433 ms.Seek (0, SeekOrigin.Begin);
437 public void SetLength ()
439 MemoryStream ms = new MemoryStream ();
440 ms.Write (testStreamData, 0, 100);
443 AssertEquals ("#01", 150, ms.Length);
444 AssertEquals ("#02", 100, ms.Position);
446 AssertEquals ("#03", 80, ms.Length);
447 AssertEquals ("#04", 80, ms.Position);
451 [ExpectedException (typeof (NotSupportedException))]
452 public void SetLength_ReadOnly ()
454 MemoryStream ms = new MemoryStream (testStreamData, false);
459 [ExpectedException (typeof (NotSupportedException))]
460 public void WriteNonWritable ()
462 MemoryStream ms = new MemoryStream (testStreamData, false);
463 ms.Write (testStreamData, 0, 100);
467 [ExpectedException (typeof (NotSupportedException))]
468 public void WriteExpand ()
470 MemoryStream ms = new MemoryStream (testStreamData);
471 ms.Write (testStreamData, 0, 100);
472 ms.Write (testStreamData, 0, 100); // This one throws the exception
476 public void WriteByte ()
478 MemoryStream ms = new MemoryStream (100);
479 ms.Write (testStreamData, 0, 100);
482 AssertEquals ("#01", 101, ms.Position);
483 AssertEquals ("#02", 101, ms.Length);
484 AssertEquals ("#03", 256, ms.Capacity);
485 ms.Write (testStreamData, 0, 100);
486 ms.Write (testStreamData, 0, 100);
488 AssertEquals ("#04", 301, ms.Position);
489 AssertEquals ("#05", 301, ms.Length);
490 AssertEquals ("#06", 512, ms.Capacity);
494 public void WriteLengths () {
495 MemoryStream ms=new MemoryStream (256);
496 BinaryWriter writer=new BinaryWriter (ms);
498 writer.Write ((byte)'1');
499 AssertEquals ("#01", 1, ms.Length);
500 AssertEquals ("#02", 256, ms.Capacity);
502 writer.Write ((ushort)0);
503 AssertEquals ("#03", 3, ms.Length);
504 AssertEquals ("#04", 256, ms.Capacity);
506 writer.Write (testStreamData, 0, 23);
507 AssertEquals ("#05", 26, ms.Length);
508 AssertEquals ("#06", 256, ms.Capacity);
510 writer.Write (testStreamData);
511 writer.Write (testStreamData);
512 writer.Write (testStreamData);
513 AssertEquals ("#07", 326, ms.Length);
517 public void MoreWriteByte ()
519 byte[] buffer = new byte [44];
521 MemoryStream ms = new MemoryStream (buffer);
522 BinaryWriter bw = new BinaryWriter (ms);
523 for(int i=0; i < 44; i++)
528 [ExpectedException (typeof (NotSupportedException))]
529 public void MoreWriteByte2 ()
531 byte[] buffer = new byte [43]; // Note the 43 here
533 MemoryStream ms = new MemoryStream (buffer);
534 BinaryWriter bw = new BinaryWriter (ms);
535 for(int i=0; i < 44; i++)
540 public void Expand ()
542 byte[] array = new byte [8] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
543 MemoryStream ms = new MemoryStream ();
544 ms.Write (array, 0, array.Length);
546 ms.Seek (4, SeekOrigin.End);
548 Assert.AreEqual ("01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()), "Result");
552 public void PubliclyVisible ()
554 MemoryStream ms = new MemoryStream ();
555 Assert.IsNotNull (ms.GetBuffer (), "ctor()");
557 ms = new MemoryStream (1);
558 Assert.IsNotNull (ms.GetBuffer (), "ctor(1)");
560 ms = new MemoryStream (new byte[1], 0, 1, true, true);
561 Assert.IsNotNull (ms.GetBuffer (), "ctor(byte[],int,int,bool,bool");
565 [ExpectedException (typeof (UnauthorizedAccessException))]
566 public void PubliclyVisible_Ctor_ByteArray ()
568 MemoryStream ms = new MemoryStream (new byte[0]);
569 Assert.IsNotNull (ms.GetBuffer ());
573 [ExpectedException (typeof (UnauthorizedAccessException))]
574 public void PubliclyVisible_Ctor_ByteArray_Boolean ()
576 MemoryStream ms = new MemoryStream (new byte[0], true);
577 Assert.IsNotNull (ms.GetBuffer ());
581 [ExpectedException (typeof (UnauthorizedAccessException))]
582 public void PubliclyVisible_Ctor_ByteArray_Int_Int ()
584 MemoryStream ms = new MemoryStream (new byte[1], 0, 1);
585 Assert.IsNotNull (ms.GetBuffer ());
589 [ExpectedException (typeof (UnauthorizedAccessException))]
590 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean ()
592 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true);
593 Assert.IsNotNull (ms.GetBuffer ());
597 [ExpectedException (typeof (UnauthorizedAccessException))]
598 public void PubliclyVisible_Ctor_ByteArray_Int_Int_Boolean_Boolean ()
600 MemoryStream ms = new MemoryStream (new byte[1], 0, 1, true, false);
601 Assert.IsNotNull (ms.GetBuffer ());