MemoryStreamTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // System.IO.StringWriter
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using NUnit.Framework;
  11. using System.IO;
  12. using System;
  13. using System.Text;
  14. namespace MonoTests.System.IO
  15. {
  16. [TestFixture]
  17. public class MemoryStreamTest : Assertion
  18. {
  19. MemoryStream testStream;
  20. byte [] testStreamData;
  21. [SetUp]
  22. void SetUp ()
  23. {
  24. testStreamData = new byte [100];
  25. for (int i = 0; i < 100; i++)
  26. testStreamData[i] = (byte) (100 - i);
  27. testStream = new MemoryStream (testStreamData);
  28. }
  29. //
  30. // Verify that the first count bytes in testBytes are the same as
  31. // the count bytes from index start in testStreamData
  32. //
  33. void VerifyTestData (string id, byte [] testBytes, int start, int count)
  34. {
  35. if (testBytes == null)
  36. Fail (id + "+1 testBytes is null");
  37. if (start < 0 ||
  38. count < 0 ||
  39. start + count > testStreamData.Length ||
  40. start > testStreamData.Length)
  41. throw new ArgumentOutOfRangeException (id + "+2");
  42. for (int test = 0; test < count; test++) {
  43. if (testBytes [test] == testStreamData [start + test])
  44. continue;
  45. string failStr = "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>";
  46. failStr = String.Format (failStr,
  47. test,
  48. start + test,
  49. testBytes [test],
  50. testStreamData [start + test]);
  51. Fail (id + "-3" + failStr);
  52. }
  53. }
  54. [Test]
  55. public void ConstructorsOne ()
  56. {
  57. MemoryStream ms = new MemoryStream();
  58. AssertEquals ("#01", 0L, ms.Length);
  59. AssertEquals ("#02", 0, ms.Capacity);
  60. AssertEquals ("#03", true, ms.CanWrite);
  61. }
  62. [Test]
  63. public void ConstructorsTwo ()
  64. {
  65. MemoryStream ms = new MemoryStream (10);
  66. AssertEquals ("#01", 0L, ms.Length);
  67. AssertEquals ("#02", 10, ms.Capacity);
  68. ms.Capacity = 0;
  69. byte [] buffer = ms.GetBuffer ();
  70. // Begin: wow!!!
  71. AssertEquals ("#03", -1, ms.ReadByte ());
  72. AssertEquals ("#04", null, buffer); // <--
  73. ms.Read (new byte [5], 0, 5);
  74. AssertEquals ("#05", 0, ms.Position);
  75. AssertEquals ("#06", 0, ms.Length);
  76. // End
  77. }
  78. [Test]
  79. public void ConstructorsThree ()
  80. {
  81. MemoryStream ms = new MemoryStream (testStreamData);
  82. AssertEquals ("#01", 100, ms.Length);
  83. AssertEquals ("#02", 0, ms.Position);
  84. }
  85. [Test]
  86. public void ConstructorsFour ()
  87. {
  88. MemoryStream ms = new MemoryStream (testStreamData, true);
  89. AssertEquals ("#01", 100, ms.Length);
  90. AssertEquals ("#02", 0, ms.Position);
  91. ms.Position = 50;
  92. byte saved = testStreamData [50];
  93. try {
  94. ms.WriteByte (23);
  95. AssertEquals ("#03", testStreamData [50], 23);
  96. } finally {
  97. testStreamData [50] = saved;
  98. }
  99. ms.Position = 100;
  100. try {
  101. ms.WriteByte (23);
  102. } catch (Exception) {
  103. return;
  104. }
  105. Fail ("#04");
  106. }
  107. [Test]
  108. public void ConstructorsFive ()
  109. {
  110. MemoryStream ms = new MemoryStream (testStreamData, 50, 50);
  111. AssertEquals ("#01", 50, ms.Length);
  112. AssertEquals ("#02", 0, ms.Position);
  113. AssertEquals ("#03", 50, ms.Capacity);
  114. ms.Position = 1;
  115. byte saved = testStreamData [51];
  116. try {
  117. ms.WriteByte (23);
  118. AssertEquals ("#04", testStreamData [51], 23);
  119. } finally {
  120. testStreamData [51] = saved;
  121. }
  122. ms.Position = 100;
  123. bool gotException = false;
  124. try {
  125. ms.WriteByte (23);
  126. } catch (NotSupportedException) {
  127. gotException = true;
  128. }
  129. if (!gotException)
  130. Fail ("#05");
  131. gotException = false;
  132. try {
  133. ms.GetBuffer ();
  134. } catch (UnauthorizedAccessException) {
  135. gotException = true;
  136. }
  137. if (!gotException)
  138. Fail ("#06");
  139. ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
  140. // This is lame, as the length is 50!!!
  141. gotException = false;
  142. try {
  143. ms.Capacity = 51;
  144. } catch (NotSupportedException) {
  145. gotException = true;
  146. }
  147. if (!gotException)
  148. Fail ("#07");
  149. AssertEquals ("#08", 50, ms.ToArray ().Length);
  150. }
  151. [Test]
  152. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  153. public void ConstructorsSix ()
  154. {
  155. MemoryStream ms = new MemoryStream (-2);
  156. }
  157. [Test]
  158. public void Read ()
  159. {
  160. byte [] readBytes = new byte [20];
  161. /* Test simple read */
  162. testStream.Read (readBytes, 0, 10);
  163. VerifyTestData ("R1", readBytes, 0, 10);
  164. /* Seek back to beginning */
  165. testStream.Seek (0, SeekOrigin.Begin);
  166. /* Read again, bit more this time */
  167. testStream.Read (readBytes, 0, 20);
  168. VerifyTestData ("R2", readBytes, 0, 20);
  169. /* Seek to 20 bytes from End */
  170. testStream.Seek (-20, SeekOrigin.End);
  171. testStream.Read (readBytes, 0, 20);
  172. VerifyTestData ("R3", readBytes, 80, 20);
  173. int readByte = testStream.ReadByte();
  174. AssertEquals (-1, readByte);
  175. }
  176. [Test]
  177. public void WriteBytes ()
  178. {
  179. byte[] readBytes = new byte[100];
  180. MemoryStream ms = new MemoryStream (100);
  181. for (int i = 0; i < 100; i++)
  182. ms.WriteByte (testStreamData [i]);
  183. ms.Seek (0, SeekOrigin.Begin);
  184. testStream.Read (readBytes, 0, 100);
  185. VerifyTestData ("W1", readBytes, 0, 100);
  186. }
  187. [Test]
  188. public void WriteBlock ()
  189. {
  190. byte[] readBytes = new byte[100];
  191. MemoryStream ms = new MemoryStream (100);
  192. ms.Write (testStreamData, 0, 100);
  193. ms.Seek (0, SeekOrigin.Begin);
  194. testStream.Read (readBytes, 0, 100);
  195. VerifyTestData ("WB1", readBytes, 0, 100);
  196. byte[] arrayBytes = testStream.ToArray();
  197. AssertEquals ("#01", 100, arrayBytes.Length);
  198. VerifyTestData ("WB2", arrayBytes, 0, 100);
  199. }
  200. [Test]
  201. public void PositionLength ()
  202. {
  203. MemoryStream ms = new MemoryStream ();
  204. ms.Position = 4;
  205. ms.WriteByte ((byte) 'M');
  206. ms.WriteByte ((byte) 'O');
  207. AssertEquals ("#01", 6, ms.Length);
  208. AssertEquals ("#02", 6, ms.Position);
  209. ms.Position = 0;
  210. AssertEquals ("#03", 0, ms.Position);
  211. }
  212. [Test]
  213. [ExpectedException (typeof (NotSupportedException))]
  214. public void MorePositionLength ()
  215. {
  216. MemoryStream ms = new MemoryStream (testStreamData);
  217. ms.Position = 101;
  218. AssertEquals ("#01", 101, ms.Position);
  219. AssertEquals ("#02", 100, ms.Length);
  220. ms.WriteByte (1); // This should throw the exception
  221. }
  222. [Test]
  223. public void GetBufferOne ()
  224. {
  225. MemoryStream ms = new MemoryStream ();
  226. byte [] buffer = ms.GetBuffer ();
  227. AssertEquals ("#01", 0, buffer.Length);
  228. }
  229. [Test]
  230. public void GetBufferTwo ()
  231. {
  232. MemoryStream ms = new MemoryStream (100);
  233. byte [] buffer = ms.GetBuffer ();
  234. AssertEquals ("#01", 100, buffer.Length);
  235. ms.Write (testStreamData, 0, 100);
  236. ms.Write (testStreamData, 0, 100);
  237. AssertEquals ("#02", 200, ms.Length);
  238. buffer = ms.GetBuffer ();
  239. AssertEquals ("#03", 256, buffer.Length); // Minimun size after writing
  240. }
  241. [Test]
  242. public void Closed ()
  243. {
  244. MemoryStream ms = new MemoryStream (100);
  245. ms.Close ();
  246. bool thrown = false;
  247. try {
  248. int x = ms.Capacity;
  249. } catch (ObjectDisposedException) {
  250. thrown = true;
  251. }
  252. if (!thrown)
  253. Fail ("#01");
  254. thrown = false;
  255. try {
  256. ms.Capacity = 1;
  257. } catch (ObjectDisposedException) {
  258. thrown = true;
  259. }
  260. if (!thrown)
  261. Fail ("#02");
  262. // The first exception thrown is ObjectDisposed, not ArgumentNull
  263. thrown = false;
  264. try {
  265. ms.Read (null, 0, 1);
  266. } catch (ObjectDisposedException) {
  267. thrown = true;
  268. }
  269. if (!thrown)
  270. Fail ("#03");
  271. thrown = false;
  272. try {
  273. ms.Write (null, 0, 1);
  274. } catch (ObjectDisposedException) {
  275. thrown = true;
  276. }
  277. if (!thrown)
  278. Fail ("#03");
  279. }
  280. [Test]
  281. public void Seek ()
  282. {
  283. MemoryStream ms = new MemoryStream (100);
  284. ms.Write (testStreamData, 0, 100);
  285. ms.Seek (0, SeekOrigin.Begin);
  286. ms.Position = 50;
  287. ms.Seek (-50, SeekOrigin.Current);
  288. ms.Seek (-50, SeekOrigin.End);
  289. bool thrown = false;
  290. ms.Position = 49;
  291. try {
  292. ms.Seek (-50, SeekOrigin.Current);
  293. } catch (IOException) {
  294. thrown = true;
  295. }
  296. if (!thrown)
  297. Fail ("#01");
  298. thrown = false;
  299. try {
  300. ms.Seek (Int64.MaxValue, SeekOrigin.Begin);
  301. } catch (ArgumentOutOfRangeException) {
  302. thrown = true;
  303. }
  304. if (!thrown)
  305. Fail ("#02");
  306. thrown=false;
  307. try {
  308. // Oh, yes. They throw IOException for this one, but ArgumentOutOfRange for the previous one
  309. ms.Seek (Int64.MinValue, SeekOrigin.Begin);
  310. } catch (IOException) {
  311. thrown = true;
  312. }
  313. if (!thrown)
  314. Fail ("#03");
  315. ms=new MemoryStream (256);
  316. ms.Write (testStreamData, 0, 100);
  317. ms.Position=0;
  318. AssertEquals ("#01", 100, ms.Length);
  319. AssertEquals ("#02", 0, ms.Position);
  320. ms.Position=128;
  321. AssertEquals ("#03", 100, ms.Length);
  322. AssertEquals ("#04", 128, ms.Position);
  323. ms.Position=768;
  324. AssertEquals ("#05", 100, ms.Length);
  325. AssertEquals ("#06", 768, ms.Position);
  326. ms.WriteByte (0);
  327. AssertEquals ("#07", 769, ms.Length);
  328. AssertEquals ("#08", 769, ms.Position);
  329. }
  330. [Test]
  331. public void SetLength ()
  332. {
  333. MemoryStream ms = new MemoryStream ();
  334. ms.Write (testStreamData, 0, 100);
  335. ms.Position = 100;
  336. ms.SetLength (150);
  337. AssertEquals ("#01", 150, ms.Length);
  338. AssertEquals ("#02", 100, ms.Position);
  339. ms.SetLength (80);
  340. AssertEquals ("#03", 80, ms.Length);
  341. AssertEquals ("#04", 80, ms.Position);
  342. }
  343. [Test]
  344. [ExpectedException (typeof (NotSupportedException))]
  345. public void WriteNonWritable ()
  346. {
  347. MemoryStream ms = new MemoryStream (testStreamData, false);
  348. ms.Write (testStreamData, 0, 100);
  349. }
  350. [Test]
  351. [ExpectedException (typeof (NotSupportedException))]
  352. public void WriteExpand ()
  353. {
  354. MemoryStream ms = new MemoryStream (testStreamData);
  355. ms.Write (testStreamData, 0, 100);
  356. ms.Write (testStreamData, 0, 100); // This one throws the exception
  357. }
  358. [Test]
  359. public void WriteByte ()
  360. {
  361. MemoryStream ms = new MemoryStream (100);
  362. ms.Write (testStreamData, 0, 100);
  363. ms.Position = 100;
  364. ms.WriteByte (101);
  365. AssertEquals ("#01", 101, ms.Position);
  366. AssertEquals ("#02", 101, ms.Length);
  367. AssertEquals ("#03", 256, ms.Capacity);
  368. ms.Write (testStreamData, 0, 100);
  369. ms.Write (testStreamData, 0, 100);
  370. // 301
  371. AssertEquals ("#04", 301, ms.Position);
  372. AssertEquals ("#05", 301, ms.Length);
  373. AssertEquals ("#06", 512, ms.Capacity);
  374. }
  375. [Test]
  376. public void WriteLengths () {
  377. MemoryStream ms=new MemoryStream (256);
  378. BinaryWriter writer=new BinaryWriter (ms);
  379. writer.Write ((byte)'1');
  380. AssertEquals ("#01", 1, ms.Length);
  381. AssertEquals ("#02", 256, ms.Capacity);
  382. writer.Write ((ushort)0);
  383. AssertEquals ("#03", 3, ms.Length);
  384. AssertEquals ("#04", 256, ms.Capacity);
  385. writer.Write (testStreamData, 0, 23);
  386. AssertEquals ("#05", 26, ms.Length);
  387. AssertEquals ("#06", 256, ms.Capacity);
  388. writer.Write (testStreamData);
  389. writer.Write (testStreamData);
  390. writer.Write (testStreamData);
  391. AssertEquals ("#07", 326, ms.Length);
  392. }
  393. }
  394. }