MemoryStreamTest.cs 13 KB

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