2002-02-28 Nick Drochak <ndrochak@gol.com>
[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() : base ("MonoTests.System.IO.MemoryStream testcase") { }\r
26         public MemoryStreamTest( string name ): base(name) { }\r
27 \r
28         public static ITest Suite {\r
29                 get {\r
30                         return new TestSuite(typeof(MemoryStreamTest));\r
31                 }\r
32         }\r
33 \r
34         protected override void SetUp() {\r
35                 testStreamData = new byte[100];\r
36 \r
37                 for( int i = 0; i < 100; i++ ) {\r
38                         testStreamData[i] = (byte)(100 - i);\r
39                 }\r
40 \r
41                 testStream = new MemoryStream( testStreamData );\r
42         }\r
43 \r
44         // \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
47         //\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
53                 }\r
54 \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
59                                 Fail( failStr );\r
60                         }\r
61                 }\r
62         }\r
63 \r
64         public void TestConstructors() {\r
65                 MemoryStream ms = new MemoryStream();\r
66 \r
67                 AssertEquals("A1", 0L, ms.Length );\r
68                 AssertEquals("A2", 0, ms.Capacity );\r
69                 AssertEquals("A3", true, ms.CanWrite );\r
70                 \r
71                 ms = new MemoryStream( 10 );\r
72 \r
73                 AssertEquals("A4", 0L, ms.Length );\r
74                 AssertEquals("A5", 10, ms.Capacity );\r
75         }\r
76 \r
77         public void TestRead() {\r
78                 byte[] readBytes = new byte[20];\r
79 \r
80                 try {\r
81                         /* Test simple read */\r
82                         testStream.Read( readBytes, 0, 10 );\r
83                         VerifyTestData( readBytes, 0, 10 );\r
84 \r
85                         /* Seek back to beginning */\r
86 \r
87                         testStream.Seek( 0, SeekOrigin.Begin );\r
88  \r
89                         /* Read again, bit more this time */\r
90                         testStream.Read( readBytes, 0, 20 );\r
91                         VerifyTestData( readBytes, 0, 20 );\r
92 \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
97 \r
98                         int readByte = testStream.ReadByte();\r
99                         AssertEquals( -1, readByte );\r
100                 }\r
101                 catch(Exception e){\r
102                         Fail("Threw an unexpected exception:"+e.ToString());\r
103                         return;\r
104                 }\r
105         }\r
106 \r
107         public void TestWriteBytes() {\r
108                 byte[] readBytes = new byte[100];\r
109 \r
110                 try {\r
111                         MemoryStream ms = new MemoryStream( 100 );\r
112 \r
113                         for( int i = 0; i < 100; i++ ) {\r
114                                 ms.WriteByte( testStreamData[i] );\r
115                         }\r
116 \r
117                         ms.Seek( 0, SeekOrigin.Begin); \r
118                         \r
119                         testStream.Read( readBytes, 0, 100 );\r
120 \r
121                         VerifyTestData( readBytes, 0, 100 );\r
122                 }\r
123                 catch(Exception e){\r
124                         Fail("Threw an unexpected exception:"+e.ToString());\r
125                         return;\r
126                 }\r
127         }               \r
128 \r
129         public void TestWriteBlock() {\r
130                 byte[] readBytes = new byte[100];\r
131 \r
132                 try {\r
133                         MemoryStream ms = new MemoryStream( 100 );\r
134 \r
135                         ms.Write( testStreamData, 0, 100 );\r
136 \r
137                         ms.Seek( 0, SeekOrigin.Begin); \r
138                         \r
139                         testStream.Read( readBytes, 0, 100 );\r
140 \r
141                         VerifyTestData( readBytes, 0, 100 );\r
142 \r
143                         byte[] arrayBytes = testStream.ToArray();\r
144 \r
145                         VerifyTestData( arrayBytes, 0, 100 );\r
146                 }\r
147                 catch(Exception e){\r
148                         Fail("Threw an unexpected exception:"+e.ToString());\r
149                         return;\r
150                 }\r
151         }\r
152 }\r
153 \r
154 }\r