Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MemoryStreamTest.cs « System.IO « Test « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9ff831877df94236ee87d35877f2013fd6c4d7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// System.IO.StringWriter
//
// Author: Marcin Szczepanski (marcins@zipworld.com.au)
//
// TODO: Add some testing for exceptions
//
// TODO: Add some testing for the CanXXX properties, exceptions,
// various different constructors.
//

using NUnit.Framework;
using System.IO;
using System;
using System.Text;

namespace MonoTests.System.IO
{

public class MemoryStreamTest : TestCase {
	
        private MemoryStream testStream;
        private byte[] testStreamData;
        
	public MemoryStreamTest() : base ("MonoTests.System.IO.MemoryStream testcase") { }
	public MemoryStreamTest( string name ): base(name) { }

	public static ITest Suite {
		get {
			return new TestSuite(typeof(MemoryStreamTest));
		}
	}

	protected override void SetUp() {
                testStreamData = new byte[100];

                for( int i = 0; i < 100; i++ ) {
                        testStreamData[i] = (byte)(100 - i);
                }

                testStream = new MemoryStream( testStreamData );
        }

        // 
        // Verify that the first count bytes in testBytes are the same as
        // the count bytes from index start in testStreamData
        //
        private void VerifyTestData( byte[] testBytes, int start, int count) {
                if( testBytes == null ) {
                        throw new ArgumentNullException();
                } else if( ( start < 0 || count < 0 ) || start + count > testStreamData.Length || start > testStreamData.Length ) {
                        throw new ArgumentOutOfRangeException();
                }

                for( int test = 0; test < count; test++ ) {
                        if( testBytes[ test ] != testStreamData[ start + test ] ) {
                                string failStr = String.Format( "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>", test, start+test, 
                                        testBytes[ test ], testStreamData[ start+test] );
                                Fail( failStr );
                        }
                }
        }

	public void TestConstructors() {
                MemoryStream ms = new MemoryStream();

                AssertEquals("A1", 0L, ms.Length );
                AssertEquals("A2", 0, ms.Capacity );
                AssertEquals("A3", true, ms.CanWrite );
                
                ms = new MemoryStream( 10 );

                AssertEquals("A4", 0L, ms.Length );
                AssertEquals("A5", 10, ms.Capacity );
        }

        public void TestRead() {
                byte[] readBytes = new byte[20];

		try {
			/* Test simple read */
			testStream.Read( readBytes, 0, 10 );
			VerifyTestData( readBytes, 0, 10 );

			/* Seek back to beginning */

			testStream.Seek( 0, SeekOrigin.Begin );
 
			/* Read again, bit more this time */
			testStream.Read( readBytes, 0, 20 );
			VerifyTestData( readBytes, 0, 20 );

			/* Seek to 20 bytes from End */
			testStream.Seek( -20, SeekOrigin.End );
			testStream.Read( readBytes, 0, 20);
			VerifyTestData( readBytes, 80, 20);

			int readByte = testStream.ReadByte();
			AssertEquals( -1, readByte );
		}
		catch(Exception e){
			Fail("Threw an unexpected exception:"+e.ToString());
			return;
		}
        }

        public void TestWriteBytes() {
                byte[] readBytes = new byte[100];

		try {
			MemoryStream ms = new MemoryStream( 100 );

			for( int i = 0; i < 100; i++ ) {
				ms.WriteByte( testStreamData[i] );
			}

			ms.Seek( 0, SeekOrigin.Begin); 
			
			testStream.Read( readBytes, 0, 100 );

			VerifyTestData( readBytes, 0, 100 );
		}
		catch(Exception e){
			Fail("Threw an unexpected exception:"+e.ToString());
			return;
		}
        }               

        public void TestWriteBlock() {
                byte[] readBytes = new byte[100];

		try {
			MemoryStream ms = new MemoryStream( 100 );

			ms.Write( testStreamData, 0, 100 );

			ms.Seek( 0, SeekOrigin.Begin); 
			
			testStream.Read( readBytes, 0, 100 );

			VerifyTestData( readBytes, 0, 100 );

			byte[] arrayBytes = testStream.ToArray();

			VerifyTestData( arrayBytes, 0, 100 );
		}
		catch(Exception e){
			Fail("Threw an unexpected exception:"+e.ToString());
			return;
		}
        }
}

}