/src/libraries/System.IO/tests/Stream/Stream.Methods.cs

https://github.com/dotnet/runtime · C# · 288 lines · 222 code · 57 blank · 9 comment · 20 complexity · fea38829706d2c11fe9e747389bb8da8 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace System.IO.Tests
  9. {
  10. public partial class StreamMethods
  11. {
  12. protected virtual Stream CreateStream()
  13. {
  14. return new MemoryStream();
  15. }
  16. protected virtual Stream CreateStream(int bufferSize)
  17. {
  18. return new MemoryStream(new byte[bufferSize]);
  19. }
  20. [Fact]
  21. public void MemoryStreamSeekStress()
  22. {
  23. var ms1 = CreateStream();
  24. SeekTest(ms1, false);
  25. }
  26. [Fact]
  27. public void MemoryStreamSeekStressWithInitialBuffer()
  28. {
  29. var ms1 = CreateStream(1024);
  30. SeekTest(ms1, false);
  31. }
  32. [Fact]
  33. public async Task MemoryStreamStress()
  34. {
  35. var ms1 = CreateStream();
  36. await StreamTest(ms1, false);
  37. }
  38. private static void SeekTest(Stream stream, bool fSuppres)
  39. {
  40. long lngPos;
  41. byte btValue;
  42. stream.Position = 0;
  43. Assert.Equal(0, stream.Position);
  44. int length = 1 << 10; //fancy way of writing 2 to the pow 10
  45. byte[] btArr = new byte[length];
  46. for (int i = 0; i < btArr.Length; i++)
  47. btArr[i] = unchecked((byte)i);
  48. if (stream.CanWrite)
  49. stream.Write(btArr, 0, btArr.Length);
  50. else
  51. stream.Position = btArr.Length;
  52. Assert.Equal(btArr.Length, stream.Position);
  53. lngPos = stream.Seek(0, SeekOrigin.Begin);
  54. Assert.Equal(0, lngPos);
  55. Assert.Equal(0, stream.Position);
  56. for (int i = 0; i < btArr.Length; i++)
  57. {
  58. if (stream.CanWrite)
  59. {
  60. btValue = (byte)stream.ReadByte();
  61. Assert.Equal(btArr[i], btValue);
  62. }
  63. else
  64. {
  65. stream.Seek(1, SeekOrigin.Current);
  66. }
  67. Assert.Equal(i + 1, stream.Position);
  68. }
  69. Assert.Throws<IOException>(() => stream.Seek(-5, SeekOrigin.Begin));
  70. lngPos = stream.Seek(5, SeekOrigin.Begin);
  71. Assert.Equal(5, lngPos);
  72. Assert.Equal(5, stream.Position);
  73. lngPos = stream.Seek(5, SeekOrigin.End);
  74. Assert.Equal(length + 5, lngPos);
  75. Assert.Throws<IOException>(() => stream.Seek(-(btArr.Length + 1), SeekOrigin.End));
  76. lngPos = stream.Seek(-5, SeekOrigin.End);
  77. Assert.Equal(btArr.Length - 5, lngPos);
  78. Assert.Equal(btArr.Length - 5, stream.Position);
  79. lngPos = stream.Seek(0, SeekOrigin.End);
  80. Assert.Equal(btArr.Length, stream.Position);
  81. for (int i = btArr.Length; i > 0; i--)
  82. {
  83. stream.Seek(-1, SeekOrigin.Current);
  84. Assert.Equal(i - 1, stream.Position);
  85. }
  86. Assert.Throws<IOException>(() => stream.Seek(-1, SeekOrigin.Current));
  87. }
  88. private static async Task StreamTest(Stream stream, bool fSuppress)
  89. {
  90. string strValue;
  91. int iValue;
  92. //[] We will first use the stream's 2 writing methods
  93. int iLength = 1 << 10;
  94. stream.Seek(0, SeekOrigin.Begin);
  95. for (int i = 0; i < iLength; i++)
  96. stream.WriteByte(unchecked((byte)i));
  97. byte[] btArr = new byte[iLength];
  98. for (int i = 0; i < iLength; i++)
  99. btArr[i] = unchecked((byte)i);
  100. stream.Write(btArr, 0, iLength);
  101. //we will write many things here using a binary writer
  102. BinaryWriter bw1 = new BinaryWriter(stream);
  103. bw1.Write(false);
  104. bw1.Write(true);
  105. for (int i = 0; i < 10; i++)
  106. {
  107. bw1.Write((byte)i);
  108. bw1.Write((sbyte)i);
  109. bw1.Write((short)i);
  110. bw1.Write((char)i);
  111. bw1.Write((ushort)i);
  112. bw1.Write(i);
  113. bw1.Write((uint)i);
  114. bw1.Write((long)i);
  115. bw1.Write((ulong)i);
  116. bw1.Write((float)i);
  117. bw1.Write((double)i);
  118. }
  119. //Some strings, chars and Bytes
  120. char[] chArr = new char[iLength];
  121. for (int i = 0; i < iLength; i++)
  122. chArr[i] = (char)i;
  123. bw1.Write(chArr);
  124. bw1.Write(chArr, 512, 512);
  125. bw1.Write(new string(chArr));
  126. bw1.Write(new string(chArr));
  127. //[] we will now read
  128. stream.Seek(0, SeekOrigin.Begin);
  129. for (int i = 0; i < iLength; i++)
  130. {
  131. Assert.Equal(i % 256, stream.ReadByte());
  132. }
  133. btArr = new byte[iLength];
  134. stream.Read(btArr, 0, iLength);
  135. for (int i = 0; i < iLength; i++)
  136. {
  137. Assert.Equal(unchecked((byte)i), btArr[i]);
  138. }
  139. //Now, for the binary reader
  140. BinaryReader br1 = new BinaryReader(stream);
  141. Assert.False(br1.ReadBoolean());
  142. Assert.True(br1.ReadBoolean());
  143. for (int i = 0; i < 10; i++)
  144. {
  145. Assert.Equal( (byte)i, br1.ReadByte());
  146. Assert.Equal((sbyte)i, br1.ReadSByte());
  147. Assert.Equal((short)i, br1.ReadInt16());
  148. Assert.Equal((char)i, br1.ReadChar());
  149. Assert.Equal((ushort)i, br1.ReadUInt16());
  150. Assert.Equal(i, br1.ReadInt32());
  151. Assert.Equal((uint)i, br1.ReadUInt32());
  152. Assert.Equal((long)i, br1.ReadInt64());
  153. Assert.Equal((ulong)i, br1.ReadUInt64());
  154. Assert.Equal((float)i, br1.ReadSingle());
  155. Assert.Equal((double)i, br1.ReadDouble());
  156. }
  157. chArr = br1.ReadChars(iLength);
  158. for (int i = 0; i < iLength; i++)
  159. {
  160. Assert.Equal((char)i, chArr[i]);
  161. }
  162. chArr = new char[512];
  163. chArr = br1.ReadChars(iLength / 2);
  164. for (int i = 0; i < iLength / 2; i++)
  165. {
  166. Assert.Equal((char)(iLength / 2 + i), chArr[i]);
  167. }
  168. chArr = new char[iLength];
  169. for (int i = 0; i < iLength; i++)
  170. chArr[i] = (char)i;
  171. strValue = br1.ReadString();
  172. Assert.Equal(new string(chArr), strValue);
  173. strValue = br1.ReadString();
  174. Assert.Equal(new string(chArr), strValue);
  175. stream.Seek(1, SeekOrigin.Current); // In the original test, success here would end the test
  176. //[] we will do some async tests here now
  177. stream.Position = 0;
  178. btArr = new byte[iLength];
  179. for (int i = 0; i < iLength; i++)
  180. btArr[i] = unchecked((byte)(i + 5));
  181. await stream.WriteAsync(btArr, 0, btArr.Length);
  182. stream.Position = 0;
  183. for (int i = 0; i < iLength; i++)
  184. {
  185. Assert.Equal(unchecked((byte)(i + 5)), stream.ReadByte());
  186. }
  187. //we will read asynchronously
  188. stream.Position = 0;
  189. byte[] compArr = new byte[iLength];
  190. iValue = await stream.ReadAsync(compArr, 0, compArr.Length);
  191. Assert.Equal(btArr.Length, iValue);
  192. for (int i = 0; i < iLength; i++)
  193. {
  194. Assert.Equal(compArr[i], btArr[i]);
  195. }
  196. }
  197. [Fact]
  198. public async Task FlushAsyncTest()
  199. {
  200. byte[] data = Enumerable.Range(0, 8000).Select(i => unchecked((byte)i)).ToArray();
  201. Stream stream = CreateStream();
  202. for (int i = 0; i < 4; i++)
  203. {
  204. await stream.WriteAsync(data, 2000 * i, 2000);
  205. await stream.FlushAsync();
  206. }
  207. stream.Position = 0;
  208. byte[] output = new byte[data.Length];
  209. int bytesRead, totalRead = 0;
  210. while ((bytesRead = await stream.ReadAsync(output, totalRead, data.Length - totalRead)) > 0)
  211. totalRead += bytesRead;
  212. Assert.Equal(data, output);
  213. }
  214. [Fact]
  215. public void ArgumentValidation()
  216. {
  217. Stream stream = CreateStream();
  218. Assert.Equal(TaskStatus.Canceled, stream.ReadAsync(new byte[1], 0, 1, new CancellationToken(canceled: true)).Status);
  219. Assert.Equal(TaskStatus.Canceled, stream.WriteAsync(new byte[1], 0, 1, new CancellationToken(canceled: true)).Status);
  220. Assert.Equal(TaskStatus.Canceled, stream.FlushAsync(new CancellationToken(canceled: true)).Status);
  221. AssertExtensions.Throws<ArgumentNullException>("buffer", () => { stream.ReadAsync(null, 0, 0); });
  222. AssertExtensions.Throws<ArgumentNullException>("buffer", () => { stream.WriteAsync(null, 0, 0); });
  223. AssertExtensions.Throws<ArgumentOutOfRangeException>("offset", () => { stream.ReadAsync(new byte[1], -1, 0); });
  224. AssertExtensions.Throws<ArgumentOutOfRangeException>("offset", () => { stream.WriteAsync(new byte[1], -1, 0); });
  225. AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => { stream.ReadAsync(new byte[1], 0, -1); });
  226. AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => { stream.WriteAsync(new byte[1], 0, -1); });
  227. AssertExtensions.Throws<ArgumentException>(null, () => { stream.ReadAsync(new byte[1], 0, 2); });
  228. AssertExtensions.Throws<ArgumentException>(null, () => { stream.WriteAsync(new byte[1], 0, 2); });
  229. }
  230. }
  231. }