public void ReadBufferedDataTest() { this.CombinatorialEngineProvider.RunCombinations( new[] { new int[] { 1 }, new int[] { 1, 20, 1 }, new int[] { 20 * 1024 }, new int[] { 100 * 1024 }, new int[] { 100 * 1024, 20 } }, (readCounts) => { int streamSize = 0; foreach (int i in readCounts) { streamSize += i; } streamSize *= 3; byte[] data = new byte[streamSize]; for (int i = 0; i < streamSize; i++) { data[i] = (byte)(i % 256); } var task = BufferedReadStreamTestWrapper.BufferStream(new MemoryStream(data)); BufferedReadStreamTestWrapper bufferedStream = task.WaitForResult(); IEnumerator <int> readCountsEnumerator = readCounts.EndLessLoop(); int count = 0; int readCount = 0; byte[] buffer = new byte[streamSize]; do { readCountsEnumerator.MoveNext(); int readSize = readCountsEnumerator.Current; readCount = bufferedStream.Read(buffer, 0, readSize); this.Assert.IsTrue(count + readCount <= data.Length, "The stream returned more data than expected."); Array.Copy(buffer, 0, data, count, readCount); count += readCount; } while (readCount > 0); this.Assert.AreEqual(streamSize, count, "The stream returned wrong number of bytes."); for (int i = 0; i < streamSize; i++) { this.Assert.AreEqual(i % 256, data[i], "Wrong data written to the stream."); } }); }
public void BufferStreamAsyncTests() { this.CombinatorialEngineProvider.RunCombinations( new[] { new int[] {}, new int[] { 1 }, new int[] { 1, 20, 1 }, new int[] { 20 * 1024 }, new int[] { 100 * 1024 }, new int[] { 100 * 1024, 20 } }, AsyncTestStream.InterestingBehaviors, (readCounts, asyncBehaviors) => { int streamSize = 0; foreach (int i in readCounts) { streamSize += i; } streamSize *= 3; byte[] data = new byte[streamSize]; for (int i = 0; i < streamSize; i++) { data[i] = (byte)(i % 256); } ReadTestStream readTestStream = new ReadTestStream(new MemoryStream(data)); readTestStream.ReadSizesEnumerator = readCounts.Length == 0 ? null : readCounts.EndLessLoop(); AsyncTestStream asyncTestStream = new AsyncTestStream(readTestStream) { FailOnWrite = true, AsyncMethodBehaviorsEnumerator = asyncBehaviors.EndLessLoop() }; var task = BufferedReadStreamTestWrapper.BufferStream(asyncTestStream); BufferedReadStreamTestWrapper bufferedStream = task.WaitForResult(); int count = 0; int readCount = 0; do { if (count == data.Length) { // This is to ask for more data than what's in the stream to verify that the stream // correctly returns 0 bytes read at the end. readCount = bufferedStream.Read(new byte[1], 0, 1); } else { readCount = bufferedStream.Read(data, count, data.Length - count); } count += readCount; } while (readCount > 0); this.Assert.AreEqual(streamSize, count, "The stream returned wrong number of bytes."); for (int i = 0; i < streamSize; i++) { this.Assert.AreEqual(i % 256, data[i], "Wrong data written to the stream."); } this.Assert.IsTrue(asyncTestStream.Disposed, "The input stream was not disposed."); }); }