Got nunit tests running in windows. Reorganized Test case directories.
[mono.git] / mcs / class / corlib / Test / System.IO / MemoryStreamTest.cs
1 //\r
2 // System.IO.StringWriter\r
3 //\r
4 // Author: Marcin Szczepanski (marcins@zipworld.com.au)\r
5 //\r
6 // TODO: Add some testing for exceptions\r
7 //\r
8 // TODO: Add some testing for the CanXXX properties, exceptions,\r
9 // various different constructors.\r
10 //\r
11 \r
12 using NUnit.Framework;\r
13 using System.IO;\r
14 using System;\r
15 using System.Text;\r
16 \r
17 namespace MonoTests.System.IO\r
18 {\r
19 \r
20 public class MemoryStreamTest : TestCase {\r
21         \r
22         private MemoryStream testStream;\r
23         private byte[] testStreamData;\r
24         \r
25         public MemoryStreamTest( string name ): base(name) { }\r
26 \r
27         public static ITest Suite {\r
28                 get {\r
29                         return new TestSuite(typeof(MemoryStreamTest));\r
30                 }\r
31         }\r
32 \r
33         protected override void SetUp() {\r
34                 testStreamData = new byte[100];\r
35 \r
36                 for( int i = 0; i < 100; i++ ) {\r
37                         testStreamData[i] = (byte)(100 - i);\r
38                 }\r
39 \r
40                 testStream = new MemoryStream( testStreamData );\r
41         }\r
42 \r
43         public void TestConstructors() {\r
44                 MemoryStream ms = new MemoryStream();\r
45 \r
46                 AssertEquals( 0, ms.Length );\r
47                 AssertEquals( 0, ms.Capacity );\r
48                 AssertEquals( true, ms.CanWrite );\r
49                 \r
50                 ms = new MemoryStream( 10 );\r
51 \r
52                 // FIXME: Should ms.Length be 0 if there is no data?\r
53                 // the spec is a little unclear.  If this is changed then\r
54                 // the code will probably need to change\r
55 \r
56                 AssertEquals( 10, ms.Length );\r
57                 AssertEquals( 10, ms.Capacity );\r
58         }\r
59 \r
60         // \r
61         // Verify that the first count bytes in testBytes are the same as\r
62         // the count bytes from index start in testStreamData\r
63         //\r
64         private void VerifyTestData( byte[] testBytes, int start, int count) {\r
65                 if( testBytes == null ) {\r
66                         throw new ArgumentNullException();\r
67                 } else if( ( start < 0 || count < 0 ) || start + count > testStreamData.Length || start > testStreamData.Length ) {\r
68                         throw new ArgumentOutOfRangeException();\r
69                 }\r
70 \r
71                 for( int test = 0; test < count; test++ ) {\r
72                         if( testBytes[ test ] != testStreamData[ start + test ] ) {\r
73                                 string failStr = String.Format( "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>", test, start+test, \r
74                                         testBytes[ test ], testStreamData[ start+test] );\r
75                                 Fail( failStr );\r
76                         }\r
77                 }\r
78         }\r
79 \r
80         public void TestRead() {\r
81                 byte[] readBytes = new byte[20];\r
82 \r
83                 /* Test simple read */\r
84                 testStream.Read( readBytes, 0, 10 );\r
85                 VerifyTestData( readBytes, 0, 10 );\r
86 \r
87                 /* Seek back to beginning */\r
88 \r
89                 testStream.Seek( 0, SeekOrigin.Begin );\r
90  \r
91                 /* Read again, bit more this time */\r
92                 testStream.Read( readBytes, 0, 20 );\r
93                 VerifyTestData( readBytes, 0, 20 );\r
94 \r
95                 /* Seek to 20 bytes from End */\r
96                 testStream.Seek( -20, SeekOrigin.End );\r
97                 testStream.Read( readBytes, 0, 20);\r
98                 VerifyTestData( readBytes, 80, 20);\r
99 \r
100                 int readByte = testStream.ReadByte();\r
101                 AssertEquals( -1, readByte );\r
102                                       \r
103         }\r
104 \r
105         public void TestWriteBytes() {\r
106                 byte[] readBytes = new byte[100];\r
107                 MemoryStream ms = new MemoryStream( 100 );\r
108 \r
109                 for( int i = 0; i < 100; i++ ) {\r
110                         ms.WriteByte( testStreamData[i] );\r
111                 }\r
112 \r
113                 ms.Seek( 0, SeekOrigin.Begin); \r
114                 \r
115                 testStream.Read( readBytes, 0, 100 );\r
116 \r
117                 VerifyTestData( readBytes, 0, 100 );\r
118         }               \r
119 \r
120         public void TestWriteBlock() {\r
121                 byte[] readBytes = new byte[100];\r
122                 MemoryStream ms = new MemoryStream( 100 );\r
123 \r
124                 ms.Write( testStreamData, 0, 100 );\r
125 \r
126                 ms.Seek( 0, SeekOrigin.Begin); \r
127                 \r
128                 testStream.Read( readBytes, 0, 100 );\r
129 \r
130                 VerifyTestData( readBytes, 0, 100 );\r
131 \r
132                 byte[] arrayBytes = testStream.ToArray();\r
133 \r
134                 VerifyTestData( arrayBytes, 0, 100 );\r
135 \r
136         }\r
137 }\r
138 \r
139 }\r