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 : Assertion
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 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 Fail (id + "-3" + failStr);
67 public void ConstructorsOne ()
69 MemoryStream ms = new MemoryStream();
71 AssertEquals ("#01", 0L, ms.Length);
72 AssertEquals ("#02", 0, ms.Capacity);
73 AssertEquals ("#03", true, ms.CanWrite);
77 public void ConstructorsTwo ()
79 MemoryStream ms = new MemoryStream (10);
81 AssertEquals ("#01", 0L, ms.Length);
82 AssertEquals ("#02", 10, ms.Capacity);
84 byte [] buffer = ms.GetBuffer ();
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);
95 public void ConstructorsThree ()
97 MemoryStream ms = new MemoryStream (testStreamData);
98 AssertEquals ("#01", 100, ms.Length);
99 AssertEquals ("#02", 0, ms.Position);
103 public void ConstructorsFour ()
105 MemoryStream ms = new MemoryStream (testStreamData, true);
106 AssertEquals ("#01", 100, ms.Length);
107 AssertEquals ("#02", 0, ms.Position);
109 byte saved = testStreamData [50];
112 AssertEquals ("#03", testStreamData [50], 23);
114 testStreamData [50] = saved;
119 } catch (Exception) {
126 public void ConstructorsFive ()
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);
133 byte saved = testStreamData [51];
136 AssertEquals ("#04", testStreamData [51], 23);
138 testStreamData [51] = saved;
141 bool gotException = false;
144 } catch (NotSupportedException) {
151 gotException = false;
154 } catch (UnauthorizedAccessException) {
161 ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
162 // This is lame, as the length is 50!!!
164 gotException = false;
167 } catch (NotSupportedException) {
174 AssertEquals ("#08", 50, ms.ToArray ().Length);
178 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179 public void ConstructorsSix ()
181 MemoryStream ms = new MemoryStream (-2);
187 byte [] readBytes = new byte [20];
189 /* Test simple read */
190 testStream.Read (readBytes, 0, 10);
191 VerifyTestData ("R1", readBytes, 0, 10);
193 /* Seek back to beginning */
195 testStream.Seek (0, SeekOrigin.Begin);
197 /* Read again, bit more this time */
198 testStream.Read (readBytes, 0, 20);
199 VerifyTestData ("R2", readBytes, 0, 20);
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);
206 int readByte = testStream.ReadByte();
207 AssertEquals (-1, readByte);
211 public void WriteBytes ()
213 byte[] readBytes = new byte[100];
215 MemoryStream ms = new MemoryStream (100);
217 for (int i = 0; i < 100; i++)
218 ms.WriteByte (testStreamData [i]);
220 ms.Seek (0, SeekOrigin.Begin);
221 testStream.Read (readBytes, 0, 100);
222 VerifyTestData ("W1", readBytes, 0, 100);
226 public void WriteBlock ()
228 byte[] readBytes = new byte[100];
230 MemoryStream ms = new MemoryStream (100);
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);
242 public void PositionLength ()
244 MemoryStream ms = new MemoryStream ();
246 ms.WriteByte ((byte) 'M');
247 ms.WriteByte ((byte) 'O');
248 AssertEquals ("#01", 6, ms.Length);
249 AssertEquals ("#02", 6, ms.Position);
251 AssertEquals ("#03", 0, ms.Position);
255 [ExpectedException (typeof (NotSupportedException))]
256 public void MorePositionLength ()
258 MemoryStream ms = new MemoryStream (testStreamData);
260 AssertEquals ("#01", 101, ms.Position);
261 AssertEquals ("#02", 100, ms.Length);
262 ms.WriteByte (1); // This should throw the exception
266 public void GetBufferOne ()
268 MemoryStream ms = new MemoryStream ();
269 byte [] buffer = ms.GetBuffer ();
270 AssertEquals ("#01", 0, buffer.Length);
274 public void GetBufferTwo ()
276 MemoryStream ms = new MemoryStream (100);
277 byte [] buffer = ms.GetBuffer ();
278 AssertEquals ("#01", 100, buffer.Length);
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
288 public void Closed ()
290 MemoryStream ms = new MemoryStream (100);
295 } catch (ObjectDisposedException) {
305 } catch (ObjectDisposedException) {
312 // The first exception thrown is ObjectDisposed, not ArgumentNull
315 ms.Read (null, 0, 1);
316 } catch (ObjectDisposedException) {
325 ms.Write (null, 0, 1);
326 } catch (ObjectDisposedException) {
335 [ExpectedException (typeof (ObjectDisposedException))]
336 public void Close_get_Length ()
338 MemoryStream ms = new MemoryStream (100);
344 [ExpectedException (typeof (ObjectDisposedException))]
345 public void Close_get_Position ()
347 MemoryStream ms = new MemoryStream (100);
349 long x = ms.Position;
353 [ExpectedException (typeof (ObjectDisposedException))]
354 public void Close_set_Position ()
356 MemoryStream ms = new MemoryStream (100);
364 MemoryStream ms = new MemoryStream (100);
365 ms.Write (testStreamData, 0, 100);
366 ms.Seek (0, SeekOrigin.Begin);
368 ms.Seek (-50, SeekOrigin.Current);
369 ms.Seek (-50, SeekOrigin.End);
374 ms.Seek (-50, SeekOrigin.Current);
375 } catch (IOException) {
383 ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
384 } catch (ArgumentOutOfRangeException) {
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) {
402 ms=new MemoryStream (256);
404 ms.Write (testStreamData, 0, 100);
406 AssertEquals ("#01", 100, ms.Length);
407 AssertEquals ("#02", 0, ms.Position);
410 AssertEquals ("#03", 100, ms.Length);
411 AssertEquals ("#04", 128, ms.Position);
414 AssertEquals ("#05", 100, ms.Length);
415 AssertEquals ("#06", 768, ms.Position);
418 AssertEquals ("#07", 769, ms.Length);
419 AssertEquals ("#08", 769, ms.Position);
423 [ExpectedException (typeof (ObjectDisposedException))]
424 public void Seek_Disposed ()
426 MemoryStream ms = new MemoryStream ();
428 ms.Seek (0, SeekOrigin.Begin);
432 public void SetLength ()
434 MemoryStream ms = new MemoryStream ();
435 ms.Write (testStreamData, 0, 100);
438 AssertEquals ("#01", 150, ms.Length);
439 AssertEquals ("#02", 100, ms.Position);
441 AssertEquals ("#03", 80, ms.Length);
442 AssertEquals ("#04", 80, ms.Position);
446 [ExpectedException (typeof (NotSupportedException))]
447 public void SetLength_ReadOnly ()
449 MemoryStream ms = new MemoryStream (testStreamData, false);
454 [ExpectedException (typeof (NotSupportedException))]
455 public void WriteNonWritable ()
457 MemoryStream ms = new MemoryStream (testStreamData, false);
458 ms.Write (testStreamData, 0, 100);
462 [ExpectedException (typeof (NotSupportedException))]
463 public void WriteExpand ()
465 MemoryStream ms = new MemoryStream (testStreamData);
466 ms.Write (testStreamData, 0, 100);
467 ms.Write (testStreamData, 0, 100); // This one throws the exception
471 public void WriteByte ()
473 MemoryStream ms = new MemoryStream (100);
474 ms.Write (testStreamData, 0, 100);
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);
483 AssertEquals ("#04", 301, ms.Position);
484 AssertEquals ("#05", 301, ms.Length);
485 AssertEquals ("#06", 512, ms.Capacity);
489 public void WriteLengths () {
490 MemoryStream ms=new MemoryStream (256);
491 BinaryWriter writer=new BinaryWriter (ms);
493 writer.Write ((byte)'1');
494 AssertEquals ("#01", 1, ms.Length);
495 AssertEquals ("#02", 256, ms.Capacity);
497 writer.Write ((ushort)0);
498 AssertEquals ("#03", 3, ms.Length);
499 AssertEquals ("#04", 256, ms.Capacity);
501 writer.Write (testStreamData, 0, 23);
502 AssertEquals ("#05", 26, ms.Length);
503 AssertEquals ("#06", 256, ms.Capacity);
505 writer.Write (testStreamData);
506 writer.Write (testStreamData);
507 writer.Write (testStreamData);
508 AssertEquals ("#07", 326, ms.Length);
512 public void MoreWriteByte ()
514 byte[] buffer = new byte [44];
516 MemoryStream ms = new MemoryStream (buffer);
517 BinaryWriter bw = new BinaryWriter (ms);
518 for(int i=0; i < 44; i++)
523 [ExpectedException (typeof (NotSupportedException))]
524 public void MoreWriteByte2 ()
526 byte[] buffer = new byte [43]; // Note the 43 here
528 MemoryStream ms = new MemoryStream (buffer);
529 BinaryWriter bw = new BinaryWriter (ms);
530 for(int i=0; i < 44; i++)
535 public void Expand ()
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);
541 ms.Seek (4, SeekOrigin.End);
543 AssertEquals ("Result", "01-01-01-01-00-00-00-00-FF", BitConverter.ToString (ms.ToArray ()));