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( string name ): base(name) { }
\r
27 public static ITest Suite {
\r
29 return new TestSuite(typeof(MemoryStreamTest));
\r
33 protected override void SetUp() {
\r
34 testStreamData = new byte[100];
\r
36 for( int i = 0; i < 100; i++ ) {
\r
37 testStreamData[i] = (byte)(100 - i);
\r
40 testStream = new MemoryStream( testStreamData );
\r
43 public void TestConstructors() {
\r
44 MemoryStream ms = new MemoryStream();
\r
46 AssertEquals( 0, ms.Length );
\r
47 AssertEquals( 0, ms.Capacity );
\r
48 AssertEquals( true, ms.CanWrite );
\r
50 ms = new MemoryStream( 10 );
\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
56 AssertEquals( 10, ms.Length );
\r
57 AssertEquals( 10, ms.Capacity );
\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
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
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
80 public void TestRead() {
\r
81 byte[] readBytes = new byte[20];
\r
83 /* Test simple read */
\r
84 testStream.Read( readBytes, 0, 10 );
\r
85 VerifyTestData( readBytes, 0, 10 );
\r
87 /* Seek back to beginning */
\r
89 testStream.Seek( 0, SeekOrigin.Begin );
\r
91 /* Read again, bit more this time */
\r
92 testStream.Read( readBytes, 0, 20 );
\r
93 VerifyTestData( readBytes, 0, 20 );
\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
100 int readByte = testStream.ReadByte();
\r
101 AssertEquals( -1, readByte );
\r
105 public void TestWriteBytes() {
\r
106 byte[] readBytes = new byte[100];
\r
107 MemoryStream ms = new MemoryStream( 100 );
\r
109 for( int i = 0; i < 100; i++ ) {
\r
110 ms.WriteByte( testStreamData[i] );
\r
113 ms.Seek( 0, SeekOrigin.Begin);
\r
115 testStream.Read( readBytes, 0, 100 );
\r
117 VerifyTestData( readBytes, 0, 100 );
\r
120 public void TestWriteBlock() {
\r
121 byte[] readBytes = new byte[100];
\r
122 MemoryStream ms = new MemoryStream( 100 );
\r
124 ms.Write( testStreamData, 0, 100 );
\r
126 ms.Seek( 0, SeekOrigin.Begin);
\r
128 testStream.Read( readBytes, 0, 100 );
\r
130 VerifyTestData( readBytes, 0, 100 );
\r
132 byte[] arrayBytes = testStream.ToArray();
\r
134 VerifyTestData( arrayBytes, 0, 100 );
\r