BufferedStreamTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. //
  2. // System.IO.BufferedStream Unit Tests
  3. //
  4. // Authors:
  5. // Ville Palo ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Ville Palo
  9. // Copyright (C) 2004 Novell (http://www.novell.com)
  10. //
  11. using NUnit.Framework;
  12. using System.IO;
  13. using System.Text;
  14. using System;
  15. namespace MonoTests.System.IO {
  16. [TestFixture]
  17. public class BufferedStreamTest : Assertion {
  18. private MemoryStream mem;
  19. [SetUp]
  20. protected void SetUp ()
  21. {
  22. mem = new MemoryStream ();
  23. }
  24. [TearDown]
  25. protected void TearDown ()
  26. {
  27. //Some tests might mess with mem, so let's check it first
  28. if (mem != null)
  29. mem.Close ();
  30. }
  31. [Test]
  32. public void Ctor ()
  33. {
  34. MemoryStream str = new MemoryStream ();
  35. str.Write (new byte [] {1, 2, 3, 4, 5, 6}, 0, 6);
  36. BufferedStream stream = new BufferedStream (str);
  37. AssertEquals ("test#01", true, stream.CanRead);
  38. AssertEquals ("test#02", true, stream.CanSeek);
  39. AssertEquals ("test#03", true, stream.CanWrite);
  40. AssertEquals ("test#04", 6, stream.Length);
  41. AssertEquals ("test#05", 6, stream.Position);
  42. string path = Path.GetTempFileName ();
  43. if (File.Exists (path))
  44. File.Delete (path);
  45. FileStream file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  46. stream = new BufferedStream (file);
  47. AssertEquals ("test#06", false, stream.CanRead);
  48. AssertEquals ("test#07", true, stream.CanSeek);
  49. AssertEquals ("test#08", true, stream.CanWrite);
  50. AssertEquals ("test#09", 0, stream.Length);
  51. AssertEquals ("test#10", 0, stream.Position);
  52. file.Close ();
  53. if (File.Exists (path))
  54. File.Delete (path);
  55. file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  56. stream = new BufferedStream (file, 12);
  57. AssertEquals ("test#11", false, stream.CanRead);
  58. AssertEquals ("test#12", true, stream.CanSeek);
  59. AssertEquals ("test#13", true, stream.CanWrite);
  60. AssertEquals ("test#14", 0, stream.Length);
  61. AssertEquals ("test#15", 0, stream.Position);
  62. file.Close ();
  63. if (File.Exists (path))
  64. File.Delete (path);
  65. }
  66. /// <summary>
  67. /// Throws an exception if stream is null
  68. /// </summary>
  69. [Test]
  70. [ExpectedException(typeof(ArgumentNullException))]
  71. public void CtorNullExceptionStream ()
  72. {
  73. BufferedStream stream = new BufferedStream (null);
  74. }
  75. /// <summary>
  76. /// Throws an exception if stream is null
  77. /// </summary>
  78. [Test]
  79. [ExpectedException(typeof(ArgumentNullException))]
  80. public void CtorNullExceptionStream1 ()
  81. {
  82. BufferedStream stream = new BufferedStream (null, 12);
  83. }
  84. /// <summary>
  85. /// Throws an exception if stream is null
  86. /// </summary>
  87. [Test]
  88. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  89. public void CtorOutOfRangeExceptionStream1 ()
  90. {
  91. MemoryStream str = new MemoryStream ();
  92. BufferedStream stream = new BufferedStream (str, -12);
  93. }
  94. [Test]
  95. [ExpectedException(typeof(ObjectDisposedException))]
  96. public void CtorOutOfRangeException2 ()
  97. {
  98. MemoryStream str = new MemoryStream ();
  99. str.Close ();
  100. BufferedStream stream = new BufferedStream (str);
  101. }
  102. [Test]
  103. public void Close1 ()
  104. {
  105. BufferedStream stream = new BufferedStream (mem);
  106. stream.Close ();
  107. stream.Close ();
  108. }
  109. [Test]
  110. public void Close2 ()
  111. {
  112. BufferedStream stream = new BufferedStream (mem);
  113. stream.Close ();
  114. AssertEquals ("test#01", false, stream.CanRead);
  115. AssertEquals ("test#02", false, stream.CanSeek);
  116. AssertEquals ("test#03", false, stream.CanWrite);
  117. }
  118. [Test]
  119. [ExpectedException(typeof(ObjectDisposedException))]
  120. public void Close3 ()
  121. {
  122. BufferedStream stream = new BufferedStream (mem);
  123. stream.Close ();
  124. long l = stream.Position;
  125. }
  126. [Test]
  127. [ExpectedException(typeof(ObjectDisposedException))]
  128. public void Close4 ()
  129. {
  130. BufferedStream stream = new BufferedStream (mem);
  131. stream.Close ();
  132. long l = stream.Length;
  133. }
  134. [Test]
  135. [ExpectedException(typeof(ObjectDisposedException))]
  136. public void Close5 ()
  137. {
  138. BufferedStream stream = new BufferedStream (mem);
  139. stream.Close ();
  140. stream.WriteByte (1);
  141. }
  142. [Test]
  143. [ExpectedException(typeof(ObjectDisposedException))]
  144. public void Close6 ()
  145. {
  146. BufferedStream stream = new BufferedStream (mem);
  147. stream.Close ();
  148. stream.ReadByte ();
  149. }
  150. [Test]
  151. [ExpectedException(typeof(NotSupportedException))]
  152. public void Close7 ()
  153. {
  154. BufferedStream stream = new BufferedStream (mem);
  155. mem.Close ();
  156. stream.WriteByte (1);
  157. }
  158. [Test]
  159. public void Read ()
  160. {
  161. mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
  162. BufferedStream stream = new BufferedStream (mem);
  163. byte [] bytes = new byte [10];
  164. stream.Read (bytes, 0, 3);
  165. AssertEquals ("test#01", 0, bytes [0]);
  166. AssertEquals ("test#02", 0, bytes [1]);
  167. AssertEquals ("test#03", 0, bytes [2]);
  168. stream.Seek (0, SeekOrigin.Begin);
  169. stream.Read (bytes, 0, 3);
  170. AssertEquals ("test#04", 0, bytes [0]);
  171. AssertEquals ("test#05", 1, bytes [1]);
  172. AssertEquals ("test#06", 2, bytes [2]);
  173. AssertEquals ("test#07", 0, bytes [0]);
  174. stream.Read (bytes, 5, 3);
  175. AssertEquals ("test#08", 3, bytes [5]);
  176. AssertEquals ("test#09", 4, bytes [6]);
  177. AssertEquals ("test#10", 5, bytes [7]);
  178. AssertEquals ("test#11", 0, bytes [8]);
  179. stream.Read (bytes, 0, 10);
  180. AssertEquals ("test#12", 3, bytes [5]);
  181. AssertEquals ("test#13", 4, bytes [6]);
  182. AssertEquals ("test#14", 5, bytes [7]);
  183. AssertEquals ("test#15", 0, bytes [9]);
  184. }
  185. [Test]
  186. [ExpectedException(typeof(ArgumentException))]
  187. public void ReadException ()
  188. {
  189. mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
  190. BufferedStream stream = new BufferedStream (mem);
  191. byte [] bytes = new byte [10];
  192. stream.Read (bytes, 0, 30);
  193. }
  194. [Test]
  195. [ExpectedException(typeof(ArgumentNullException))]
  196. public void Read_Null ()
  197. {
  198. BufferedStream stream = new BufferedStream (mem);
  199. stream.Read (null, 0, 0);
  200. }
  201. [Test]
  202. [ExpectedException(typeof(NotSupportedException))]
  203. public void Read_CantRead ()
  204. {
  205. WriteOnlyStream wo = new WriteOnlyStream ();
  206. BufferedStream stream = new BufferedStream (wo);
  207. stream.Read (new byte [1], 0, 1);
  208. }
  209. [Test]
  210. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  211. public void Read_OffsetNegative ()
  212. {
  213. BufferedStream stream = new BufferedStream (mem);
  214. stream.Read (new byte [1], -1, 1);
  215. }
  216. [Test]
  217. [ExpectedException(typeof(ArgumentException))]
  218. public void Read_OffsetOverflow ()
  219. {
  220. BufferedStream stream = new BufferedStream (mem);
  221. stream.Read (new byte [1], Int32.MaxValue, 1);
  222. }
  223. [Test]
  224. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  225. public void Read_CountNegative ()
  226. {
  227. BufferedStream stream = new BufferedStream (mem);
  228. stream.Read (new byte [1], 1, -1);
  229. }
  230. [Test]
  231. [ExpectedException(typeof(ArgumentException))]
  232. public void Read_CountOverflow ()
  233. {
  234. BufferedStream stream = new BufferedStream (mem);
  235. stream.Read (new byte [1], 1, Int32.MaxValue);
  236. }
  237. [Test]
  238. public void ReadByte ()
  239. {
  240. mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
  241. BufferedStream stream = new BufferedStream (mem);
  242. AssertEquals ("test#01", -1, stream.ReadByte ());
  243. AssertEquals ("test#02", -1, stream.ReadByte ());
  244. AssertEquals ("test#03", -1, stream.ReadByte ());
  245. stream.Seek (0, SeekOrigin.Begin);
  246. AssertEquals ("test#04", 0, stream.ReadByte ());
  247. AssertEquals ("test#05", 1, stream.ReadByte ());
  248. AssertEquals ("test#06", 2, stream.ReadByte ());
  249. AssertEquals ("test#07", 3, stream.ReadByte ());
  250. }
  251. [Test]
  252. public void Write ()
  253. {
  254. BufferedStream stream = new BufferedStream (mem);
  255. stream.Write (new byte [] {0, 1, 2, 3, 4}, 0, 4);
  256. AssertEquals ("test#01", 4, stream.Length);
  257. byte [] bytes = mem.GetBuffer ();
  258. AssertEquals ("test#02", 0, bytes [0]);
  259. AssertEquals ("test#03", 1, bytes [1]);
  260. AssertEquals ("test#04", 2, bytes [2]);
  261. AssertEquals ("test#05", 3, bytes [3]);
  262. bytes = new byte [] {1, 4, 3};
  263. stream.Write (bytes, 0, 3);
  264. stream.Flush ();
  265. bytes = mem.GetBuffer ();
  266. AssertEquals ("test#06", 0, bytes [0]);
  267. AssertEquals ("test#07", 1, bytes [1]);
  268. AssertEquals ("test#08", 2, bytes [2]);
  269. AssertEquals ("test#09", 3, bytes [3]);
  270. AssertEquals ("test#10", 1, bytes [4]);
  271. AssertEquals ("test#11", 4, bytes [5]);
  272. AssertEquals ("test#10", 3, bytes [6]);
  273. AssertEquals ("test#11", 0, bytes [7]);
  274. AssertEquals ("test#12", 7, stream.Length);
  275. }
  276. [Test]
  277. [ExpectedException(typeof (ArgumentException))]
  278. public void WriteException ()
  279. {
  280. BufferedStream stream = new BufferedStream (mem);
  281. stream.Write (new byte [] {0,1,2,3}, 0, 10);
  282. }
  283. [Test]
  284. [ExpectedException(typeof(ArgumentNullException))]
  285. public void Write_Null ()
  286. {
  287. BufferedStream stream = new BufferedStream (mem);
  288. stream.Write (null, 0, 0);
  289. }
  290. [Test]
  291. [ExpectedException(typeof(NotSupportedException))]
  292. public void Write_CantWrite ()
  293. {
  294. ReadOnlyStream ro = new ReadOnlyStream ();
  295. BufferedStream stream = new BufferedStream (ro);
  296. stream.Write (new byte [1], 0, 1);
  297. }
  298. [Test]
  299. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  300. public void Write_OffsetNegative ()
  301. {
  302. BufferedStream stream = new BufferedStream (mem);
  303. stream.Write (new byte [1], -1, 1);
  304. }
  305. [Test]
  306. [ExpectedException(typeof(ArgumentException))]
  307. public void Write_OffsetOverflow ()
  308. {
  309. BufferedStream stream = new BufferedStream (mem);
  310. stream.Write (new byte [1], Int32.MaxValue, 1);
  311. }
  312. [Test]
  313. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  314. public void Write_CountNegative ()
  315. {
  316. BufferedStream stream = new BufferedStream (mem);
  317. stream.Write (new byte [1], 1, -1);
  318. }
  319. [Test]
  320. [ExpectedException(typeof(ArgumentException))]
  321. public void Write_CountOverflow ()
  322. {
  323. BufferedStream stream = new BufferedStream (mem);
  324. stream.Write (new byte [1], 1, Int32.MaxValue);
  325. }
  326. [Test]
  327. public void WriteByte ()
  328. {
  329. BufferedStream stream = new BufferedStream (mem);
  330. stream.WriteByte (1);
  331. stream.WriteByte (2);
  332. stream.WriteByte (3);
  333. stream.Flush ();
  334. AssertEquals ("test#01", 256, mem.GetBuffer ().Length);
  335. AssertEquals ("test#02", 3, stream.Length);
  336. AssertEquals ("test#03", 1, mem.GetBuffer () [0]);
  337. AssertEquals ("test#04", 2, mem.GetBuffer () [1]);
  338. AssertEquals ("test#05", 3, mem.GetBuffer () [2]);
  339. }
  340. [Test]
  341. public void Flush ()
  342. {
  343. BufferedStream stream = new BufferedStream (mem);
  344. stream.WriteByte (1);
  345. stream.WriteByte (2);
  346. byte [] bytes = mem.GetBuffer ();
  347. AssertEquals ("test#01", 0, bytes.Length);
  348. stream.Flush ();
  349. bytes = mem.GetBuffer ();
  350. AssertEquals ("test#02", 256, bytes.Length);
  351. AssertEquals ("test#03", 1, bytes [0]);
  352. AssertEquals ("test#04", 2, bytes [1]);
  353. mem.Close ();
  354. mem = new MemoryStream ();
  355. bytes = new byte [] {0, 1, 2, 3, 4, 5};
  356. stream = new BufferedStream (mem);
  357. stream.Write (bytes, 0, 2);
  358. AssertEquals ("test#05", 2, stream.Length);
  359. bytes = mem.GetBuffer ();
  360. AssertEquals ("test#06", 256, bytes.Length);
  361. AssertEquals ("test#07", 0, bytes [0]);
  362. AssertEquals ("test#08", 1, bytes [1]);
  363. stream.Write (bytes, 0, 2);
  364. bytes = mem.GetBuffer ();
  365. AssertEquals ("test#09", 0, bytes [0]);
  366. AssertEquals ("test#10", 1, bytes [1]);
  367. AssertEquals ("test#11", 0, bytes [2]);
  368. AssertEquals ("test#12", 0, bytes [3]);
  369. stream.Flush ();
  370. bytes = mem.GetBuffer ();
  371. AssertEquals ("test#13", 0, bytes [2]);
  372. AssertEquals ("test#14", 1, bytes [3]);
  373. }
  374. [Test]
  375. public void Seek ()
  376. {
  377. BufferedStream stream = new BufferedStream (mem);
  378. stream.Write (new byte [] {0, 1, 2, 3, 4, 5}, 0, 6);
  379. AssertEquals ("test#01", 6, stream.Position);
  380. stream.Seek (-5, SeekOrigin.End);
  381. AssertEquals ("test#02", 1, stream.Position);
  382. stream.Seek (3, SeekOrigin.Current);
  383. AssertEquals ("test#03", 4, stream.Position);
  384. stream.Seek (300, SeekOrigin.Current);
  385. AssertEquals ("test#04", 304, stream.Position);
  386. }
  387. [Test]
  388. [ExpectedException(typeof (IOException))]
  389. public void SeekException ()
  390. {
  391. BufferedStream stream = new BufferedStream (mem);
  392. stream.Seek (-1, SeekOrigin.Begin);
  393. }
  394. [Test]
  395. public void SetLength ()
  396. {
  397. BufferedStream stream = new BufferedStream (mem);
  398. stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6);
  399. AssertEquals ("test#01", 6, stream.Length);
  400. stream.SetLength (60);
  401. AssertEquals ("test#02", 60, stream.Length);
  402. stream.SetLength (2);
  403. AssertEquals ("test#03", 2, stream.Length);
  404. }
  405. [Test]
  406. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  407. public void SetLengthException ()
  408. {
  409. BufferedStream stream = new BufferedStream (mem);
  410. stream.SetLength (-1);
  411. }
  412. [Test]
  413. [ExpectedException(typeof(NotSupportedException))]
  414. public void SetLengthException2 ()
  415. {
  416. BufferedStream stream = new BufferedStream (mem);
  417. mem.Close ();
  418. stream.SetLength (1);
  419. }
  420. [Test]
  421. public void SetLengthException3 ()
  422. {
  423. BufferedStream stream = new BufferedStream (mem);
  424. mem = null;
  425. // Strangely, this does not throw an exception on .NET 1.1
  426. stream.SetLength (1);
  427. }
  428. [Test]
  429. public void Position ()
  430. {
  431. mem = new MemoryStream ();
  432. BufferedStream stream = new BufferedStream (mem,5);
  433. stream.Write (new byte [] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, 0, 16);
  434. stream.Position = 0;
  435. AssertEquals ("test#01", 0, stream.Position);
  436. AssertEquals ("test#02", 0, stream.ReadByte ());
  437. stream.Position = 5;
  438. AssertEquals ("test#01", 5, stream.Position);
  439. AssertEquals ("test#02", 5, stream.ReadByte ());
  440. // Should not need to read from the underlying stream:
  441. stream.Position = 7;
  442. AssertEquals ("test#01", 7, stream.Position);
  443. AssertEquals ("test#02", 7, stream.ReadByte ());
  444. // Should not need to read from the underlying stream:
  445. stream.Position = 5;
  446. AssertEquals ("test#01", 5, stream.Position);
  447. AssertEquals ("test#02", 5, stream.ReadByte ());
  448. // Should not need to read from the underlying stream:
  449. stream.Position = 9;
  450. AssertEquals ("test#01", 9, stream.Position);
  451. AssertEquals ("test#02", 9, stream.ReadByte ());
  452. stream.Position = 10;
  453. AssertEquals ("test#01", 10, stream.Position);
  454. AssertEquals ("test#02", 10, stream.ReadByte ());
  455. stream.Position = 9;
  456. AssertEquals ("test#01", 9, stream.Position);
  457. AssertEquals ("test#02", 9, stream.ReadByte ());
  458. }
  459. [Test]
  460. public void PositionAfterSetLength ()
  461. {
  462. BufferedStream stream = new BufferedStream (new MemoryStream ());
  463. stream.SetLength (32);
  464. stream.Position = 32;
  465. stream.SetLength (16);
  466. AssertEquals ("Position==16", 16, stream.Position);
  467. }
  468. [Test]
  469. [ExpectedException (typeof (ObjectDisposedException))]
  470. public void SetLength_Disposed ()
  471. {
  472. BufferedStream stream = new BufferedStream (new MemoryStream ());
  473. stream.Close ();
  474. stream.SetLength (16);
  475. }
  476. [Test]
  477. [ExpectedException (typeof (NotSupportedException))]
  478. public void Seek_ClosedMemoryStream ()
  479. {
  480. MemoryStream ms = new MemoryStream ();
  481. BufferedStream stream = new BufferedStream (ms);
  482. ms.Close ();
  483. stream.Seek (0, SeekOrigin.Begin);
  484. }
  485. [Test]
  486. [ExpectedException (typeof (ObjectDisposedException))]
  487. public void Seek_ClosedBufferedStream ()
  488. {
  489. BufferedStream stream = new BufferedStream (new MemoryStream ());
  490. stream.Close ();
  491. stream.Seek (0, SeekOrigin.Begin);
  492. }
  493. }
  494. }