2 // System.IO.StringWriter
\r
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)
\r
6 // TODO: Add some testing for exceptions
\r
8 // TODO: Add some testing for the CanXXX properties, exceptions,
\r
9 // various different constructors.
\r
12 using NUnit.Framework;
\r
17 namespace MonoTests.System.IO
\r
20 public class MemoryStreamTest : TestCase {
\r
22 private MemoryStream testStream;
\r
23 private byte[] testStreamData;
\r
25 public MemoryStreamTest() : base ("MonoTests.System.IO.MemoryStream testcase") { }
\r
26 public MemoryStreamTest( string name ): base(name) { }
\r
28 public static ITest Suite {
\r
30 return new TestSuite(typeof(MemoryStreamTest));
\r
34 protected override void SetUp() {
\r
35 testStreamData = new byte[100];
\r
37 for( int i = 0; i < 100; i++ ) {
\r
38 testStreamData[i] = (byte)(100 - i);
\r
41 testStream = new MemoryStream( testStreamData );
\r
45 // Verify that the first count bytes in testBytes are the same as
\r
46 // the count bytes from index start in testStreamData
\r
48 private void VerifyTestData( byte[] testBytes, int start, int count) {
\r
49 if( testBytes == null ) {
\r
50 throw new ArgumentNullException();
\r
51 } else if( ( start < 0 || count < 0 ) || start + count > testStreamData.Length || start > testStreamData.Length ) {
\r
52 throw new ArgumentOutOfRangeException();
\r
55 for( int test = 0; test < count; test++ ) {
\r
56 if( testBytes[ test ] != testStreamData[ start + test ] ) {
\r
57 string failStr = String.Format( "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>", test, start+test,
\r
58 testBytes[ test ], testStreamData[ start+test] );
\r
64 public void TestConstructors() {
\r
65 MemoryStream ms = new MemoryStream();
\r
67 AssertEquals("A1", 0L, ms.Length );
\r
68 AssertEquals("A2", 0, ms.Capacity );
\r
69 AssertEquals("A3", true, ms.CanWrite );
\r
71 ms = new MemoryStream( 10 );
\r
73 AssertEquals("A4", 0L, ms.Length );
\r
74 AssertEquals("A5", 10, ms.Capacity );
\r
77 public void TestRead() {
\r
78 byte[] readBytes = new byte[20];
\r
81 /* Test simple read */
\r
82 testStream.Read( readBytes, 0, 10 );
\r
83 VerifyTestData( readBytes, 0, 10 );
\r
85 /* Seek back to beginning */
\r
87 testStream.Seek( 0, SeekOrigin.Begin );
\r
89 /* Read again, bit more this time */
\r
90 testStream.Read( readBytes, 0, 20 );
\r
91 VerifyTestData( readBytes, 0, 20 );
\r
93 /* Seek to 20 bytes from End */
\r
94 testStream.Seek( -20, SeekOrigin.End );
\r
95 testStream.Read( readBytes, 0, 20);
\r
96 VerifyTestData( readBytes, 80, 20);
\r
98 int readByte = testStream.ReadByte();
\r
99 AssertEquals( -1, readByte );
\r
101 catch(Exception e){
\r
102 Fail("Threw an unexpected exception:"+e.ToString());
\r
107 public void TestWriteBytes() {
\r
108 byte[] readBytes = new byte[100];
\r
111 MemoryStream ms = new MemoryStream( 100 );
\r
113 for( int i = 0; i < 100; i++ ) {
\r
114 ms.WriteByte( testStreamData[i] );
\r
117 ms.Seek( 0, SeekOrigin.Begin);
\r
119 testStream.Read( readBytes, 0, 100 );
\r
121 VerifyTestData( readBytes, 0, 100 );
\r
123 catch(Exception e){
\r
124 Fail("Threw an unexpected exception:"+e.ToString());
\r
129 public void TestWriteBlock() {
\r
130 byte[] readBytes = new byte[100];
\r
133 MemoryStream ms = new MemoryStream( 100 );
\r
135 ms.Write( testStreamData, 0, 100 );
\r
137 ms.Seek( 0, SeekOrigin.Begin);
\r
139 testStream.Read( readBytes, 0, 100 );
\r
141 VerifyTestData( readBytes, 0, 100 );
\r
143 byte[] arrayBytes = testStream.ToArray();
\r
145 VerifyTestData( arrayBytes, 0, 100 );
\r
147 catch(Exception e){
\r
148 Fail("Threw an unexpected exception:"+e.ToString());
\r