BufferedStreamTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //
  2. // System.IO.BufferedStream
  3. //
  4. // Authors:
  5. // Ville Palo ([email protected])
  6. //
  7. // (C) 2003 Ville Palo
  8. //
  9. using NUnit.Framework;
  10. using System.IO;
  11. using System.Text;
  12. using System;
  13. namespace MonoTests.System.IO {
  14. [TestFixture]
  15. public class BufferedStreamTest : Assertion {
  16. private MemoryStream mem;
  17. [SetUp]
  18. protected void SetUp ()
  19. {
  20. mem = new MemoryStream ();
  21. }
  22. [TearDown]
  23. protected void TearDown ()
  24. {
  25. //Some tests might mess with mem, so let's check it first
  26. if (mem != null)
  27. mem.Close ();
  28. }
  29. [Test]
  30. public void Ctor ()
  31. {
  32. MemoryStream str = new MemoryStream ();
  33. str.Write (new byte [] {1, 2, 3, 4, 5, 6}, 0, 6);
  34. BufferedStream stream = new BufferedStream (str);
  35. AssertEquals ("test#01", true, stream.CanRead);
  36. AssertEquals ("test#02", true, stream.CanSeek);
  37. AssertEquals ("test#03", true, stream.CanWrite);
  38. AssertEquals ("test#04", 6, stream.Length);
  39. AssertEquals ("test#05", 6, stream.Position);
  40. string path = Path.GetTempFileName ();
  41. if (File.Exists (path))
  42. File.Delete (path);
  43. FileStream file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  44. stream = new BufferedStream (file);
  45. AssertEquals ("test#06", false, stream.CanRead);
  46. AssertEquals ("test#07", true, stream.CanSeek);
  47. AssertEquals ("test#08", true, stream.CanWrite);
  48. AssertEquals ("test#09", 0, stream.Length);
  49. AssertEquals ("test#10", 0, stream.Position);
  50. file.Close ();
  51. if (File.Exists (path))
  52. File.Delete (path);
  53. file = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write);
  54. stream = new BufferedStream (file, 12);
  55. AssertEquals ("test#11", false, stream.CanRead);
  56. AssertEquals ("test#12", true, stream.CanSeek);
  57. AssertEquals ("test#13", true, stream.CanWrite);
  58. AssertEquals ("test#14", 0, stream.Length);
  59. AssertEquals ("test#15", 0, stream.Position);
  60. file.Close ();
  61. if (File.Exists (path))
  62. File.Delete (path);
  63. }
  64. /// <summary>
  65. /// Throws an exception if stream is null
  66. /// </summary>
  67. [Test]
  68. [ExpectedException(typeof(ArgumentNullException))]
  69. public void CtorNullExceptionStream ()
  70. {
  71. BufferedStream stream = new BufferedStream (null);
  72. }
  73. /// <summary>
  74. /// Throws an exception if stream is null
  75. /// </summary>
  76. [Test]
  77. [ExpectedException(typeof(ArgumentNullException))]
  78. public void CtorNullExceptionStream1 ()
  79. {
  80. BufferedStream stream = new BufferedStream (null, 12);
  81. }
  82. /// <summary>
  83. /// Throws an exception if stream is null
  84. /// </summary>
  85. [Test]
  86. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  87. public void CtorOutOfRangeExceptionStream1 ()
  88. {
  89. MemoryStream str = new MemoryStream ();
  90. BufferedStream stream = new BufferedStream (str, -12);
  91. }
  92. [Test]
  93. [ExpectedException(typeof(ObjectDisposedException))]
  94. public void CtorOutOfRangeException2 ()
  95. {
  96. MemoryStream str = new MemoryStream ();
  97. str.Close ();
  98. BufferedStream stream = new BufferedStream (str);
  99. }
  100. [Test]
  101. public void Close1 ()
  102. {
  103. BufferedStream stream = new BufferedStream (mem);
  104. stream.Close ();
  105. stream.Close ();
  106. }
  107. [Test]
  108. public void Close2 ()
  109. {
  110. BufferedStream stream = new BufferedStream (mem);
  111. stream.Close ();
  112. AssertEquals ("test#01", false, stream.CanRead);
  113. AssertEquals ("test#02", false, stream.CanSeek);
  114. AssertEquals ("test#03", false, stream.CanWrite);
  115. }
  116. [Test]
  117. [ExpectedException(typeof(ObjectDisposedException))]
  118. public void Close3 ()
  119. {
  120. BufferedStream stream = new BufferedStream (mem);
  121. stream.Close ();
  122. long l = stream.Position;
  123. }
  124. [Test]
  125. [ExpectedException(typeof(ObjectDisposedException))]
  126. public void Close4 ()
  127. {
  128. BufferedStream stream = new BufferedStream (mem);
  129. stream.Close ();
  130. long l = stream.Length;
  131. }
  132. [Test]
  133. [ExpectedException(typeof(ObjectDisposedException))]
  134. public void Close5 ()
  135. {
  136. BufferedStream stream = new BufferedStream (mem);
  137. stream.Close ();
  138. stream.WriteByte (1);
  139. }
  140. [Test]
  141. [ExpectedException(typeof(ObjectDisposedException))]
  142. public void Close6 ()
  143. {
  144. BufferedStream stream = new BufferedStream (mem);
  145. stream.Close ();
  146. stream.ReadByte ();
  147. }
  148. [Test]
  149. [ExpectedException(typeof(NotSupportedException))]
  150. public void Close7 ()
  151. {
  152. BufferedStream stream = new BufferedStream (mem);
  153. mem.Close ();
  154. stream.WriteByte (1);
  155. }
  156. [Test]
  157. public void Read ()
  158. {
  159. mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
  160. BufferedStream stream = new BufferedStream (mem);
  161. byte [] bytes = new byte [10];
  162. stream.Read (bytes, 0, 3);
  163. AssertEquals ("test#01", 0, bytes [0]);
  164. AssertEquals ("test#02", 0, bytes [1]);
  165. AssertEquals ("test#03", 0, bytes [2]);
  166. stream.Seek (0, SeekOrigin.Begin);
  167. stream.Read (bytes, 0, 3);
  168. AssertEquals ("test#04", 0, bytes [0]);
  169. AssertEquals ("test#05", 1, bytes [1]);
  170. AssertEquals ("test#06", 2, bytes [2]);
  171. AssertEquals ("test#07", 0, bytes [0]);
  172. stream.Read (bytes, 5, 3);
  173. AssertEquals ("test#08", 3, bytes [5]);
  174. AssertEquals ("test#09", 4, bytes [6]);
  175. AssertEquals ("test#10", 5, bytes [7]);
  176. AssertEquals ("test#11", 0, bytes [8]);
  177. stream.Read (bytes, 0, 10);
  178. AssertEquals ("test#12", 3, bytes [5]);
  179. AssertEquals ("test#13", 4, bytes [6]);
  180. AssertEquals ("test#14", 5, bytes [7]);
  181. AssertEquals ("test#15", 0, bytes [9]);
  182. }
  183. [Test]
  184. [ExpectedException(typeof(ArgumentException))]
  185. public void ReadException ()
  186. {
  187. mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
  188. BufferedStream stream = new BufferedStream (mem);
  189. byte [] bytes = new byte [10];
  190. stream.Read (bytes, 0, 30);
  191. }
  192. [Test]
  193. [ExpectedException(typeof (ArgumentOutOfRangeException))]
  194. public void ReadException2 ()
  195. {
  196. BufferedStream stream = new BufferedStream (mem);
  197. byte [] bytes = new byte [10];
  198. stream.Read (bytes, -10, 4);
  199. }
  200. [Test]
  201. public void ReadByte ()
  202. {
  203. mem.Write (new byte [] {0, 1, 2, 3, 4, 5, 6, 7}, 0, 8);
  204. BufferedStream stream = new BufferedStream (mem);
  205. AssertEquals ("test#01", -1, stream.ReadByte ());
  206. AssertEquals ("test#02", -1, stream.ReadByte ());
  207. AssertEquals ("test#03", -1, stream.ReadByte ());
  208. stream.Seek (0, SeekOrigin.Begin);
  209. AssertEquals ("test#04", 0, stream.ReadByte ());
  210. AssertEquals ("test#05", 1, stream.ReadByte ());
  211. AssertEquals ("test#06", 2, stream.ReadByte ());
  212. AssertEquals ("test#07", 3, stream.ReadByte ());
  213. }
  214. [Test]
  215. public void Write ()
  216. {
  217. BufferedStream stream = new BufferedStream (mem);
  218. stream.Write (new byte [] {0, 1, 2, 3, 4}, 0, 4);
  219. AssertEquals ("test#01", 4, stream.Length);
  220. byte [] bytes = mem.GetBuffer ();
  221. AssertEquals ("test#02", 0, bytes [0]);
  222. AssertEquals ("test#03", 1, bytes [1]);
  223. AssertEquals ("test#04", 2, bytes [2]);
  224. AssertEquals ("test#05", 3, bytes [3]);
  225. bytes = new byte [] {1, 4, 3};
  226. stream.Write (bytes, 0, 3);
  227. stream.Flush ();
  228. bytes = mem.GetBuffer ();
  229. AssertEquals ("test#06", 0, bytes [0]);
  230. AssertEquals ("test#07", 1, bytes [1]);
  231. AssertEquals ("test#08", 2, bytes [2]);
  232. AssertEquals ("test#09", 3, bytes [3]);
  233. AssertEquals ("test#10", 1, bytes [4]);
  234. AssertEquals ("test#11", 4, bytes [5]);
  235. AssertEquals ("test#10", 3, bytes [6]);
  236. AssertEquals ("test#11", 0, bytes [7]);
  237. AssertEquals ("test#12", 7, stream.Length);
  238. }
  239. [Test]
  240. [ExpectedException(typeof (ArgumentException))]
  241. public void WriteException ()
  242. {
  243. BufferedStream stream = new BufferedStream (mem);
  244. stream.Write (new byte [] {0,1,2,3}, 0, 10);
  245. }
  246. [Test]
  247. [ExpectedException(typeof (ArgumentOutOfRangeException))]
  248. public void WriteException2 ()
  249. {
  250. BufferedStream stream = new BufferedStream (mem);
  251. stream.Write (new byte [] {0,1,2,3}, -10, 4);
  252. }
  253. [Test]
  254. public void WriteByte ()
  255. {
  256. BufferedStream stream = new BufferedStream (mem);
  257. stream.WriteByte (1);
  258. stream.WriteByte (2);
  259. stream.WriteByte (3);
  260. stream.Flush ();
  261. AssertEquals ("test#01", 256, mem.GetBuffer ().Length);
  262. AssertEquals ("test#02", 3, stream.Length);
  263. AssertEquals ("test#03", 1, mem.GetBuffer () [0]);
  264. AssertEquals ("test#04", 2, mem.GetBuffer () [1]);
  265. AssertEquals ("test#05", 3, mem.GetBuffer () [2]);
  266. }
  267. [Test]
  268. public void Flush ()
  269. {
  270. BufferedStream stream = new BufferedStream (mem);
  271. stream.WriteByte (1);
  272. stream.WriteByte (2);
  273. byte [] bytes = mem.GetBuffer ();
  274. AssertEquals ("test#01", 0, bytes.Length);
  275. stream.Flush ();
  276. bytes = mem.GetBuffer ();
  277. AssertEquals ("test#02", 256, bytes.Length);
  278. AssertEquals ("test#03", 1, bytes [0]);
  279. AssertEquals ("test#04", 2, bytes [1]);
  280. mem.Close ();
  281. mem = new MemoryStream ();
  282. bytes = new byte [] {0, 1, 2, 3, 4, 5};
  283. stream = new BufferedStream (mem);
  284. stream.Write (bytes, 0, 2);
  285. AssertEquals ("test#05", 2, stream.Length);
  286. bytes = mem.GetBuffer ();
  287. AssertEquals ("test#06", 256, bytes.Length);
  288. AssertEquals ("test#07", 0, bytes [0]);
  289. AssertEquals ("test#08", 1, bytes [1]);
  290. stream.Write (bytes, 0, 2);
  291. bytes = mem.GetBuffer ();
  292. AssertEquals ("test#09", 0, bytes [0]);
  293. AssertEquals ("test#10", 1, bytes [1]);
  294. AssertEquals ("test#11", 0, bytes [2]);
  295. AssertEquals ("test#12", 0, bytes [3]);
  296. stream.Flush ();
  297. bytes = mem.GetBuffer ();
  298. AssertEquals ("test#13", 0, bytes [2]);
  299. AssertEquals ("test#14", 1, bytes [3]);
  300. }
  301. [Test]
  302. public void Seek ()
  303. {
  304. BufferedStream stream = new BufferedStream (mem);
  305. stream.Write (new byte [] {0, 1, 2, 3, 4, 5}, 0, 6);
  306. AssertEquals ("test#01", 6, stream.Position);
  307. stream.Seek (-5, SeekOrigin.End);
  308. AssertEquals ("test#02", 1, stream.Position);
  309. stream.Seek (3, SeekOrigin.Current);
  310. AssertEquals ("test#03", 4, stream.Position);
  311. stream.Seek (300, SeekOrigin.Current);
  312. AssertEquals ("test#04", 304, stream.Position);
  313. }
  314. [Test]
  315. [ExpectedException(typeof (IOException))]
  316. public void SeekException ()
  317. {
  318. BufferedStream stream = new BufferedStream (mem);
  319. stream.Seek (-1, SeekOrigin.Begin);
  320. }
  321. [Test]
  322. public void SetLength ()
  323. {
  324. BufferedStream stream = new BufferedStream (mem);
  325. stream.Write (new byte [] {0,1,2,3,4,5}, 0, 6);
  326. AssertEquals ("test#01", 6, stream.Length);
  327. stream.SetLength (60);
  328. AssertEquals ("test#02", 60, stream.Length);
  329. stream.SetLength (2);
  330. AssertEquals ("test#03", 2, stream.Length);
  331. }
  332. [Test]
  333. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  334. public void SetLengthException ()
  335. {
  336. BufferedStream stream = new BufferedStream (mem);
  337. stream.SetLength (-1);
  338. }
  339. [Test]
  340. [ExpectedException(typeof(NotSupportedException))]
  341. public void SetLengthException2 ()
  342. {
  343. BufferedStream stream = new BufferedStream (mem);
  344. mem.Close ();
  345. stream.SetLength (1);
  346. }
  347. [Test]
  348. public void SetLengthException3 ()
  349. {
  350. BufferedStream stream = new BufferedStream (mem);
  351. mem = null;
  352. // Strangely, this does not throw an exception on .NET 1.1
  353. stream.SetLength (1);
  354. }
  355. [Test]
  356. public void Position ()
  357. {
  358. mem = new MemoryStream ();
  359. BufferedStream stream = new BufferedStream (mem,5);
  360. stream.Write (new byte [] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, 0, 16);
  361. stream.Position = 0;
  362. AssertEquals ("test#01", 0, stream.Position);
  363. AssertEquals ("test#02", 0, stream.ReadByte ());
  364. stream.Position = 5;
  365. AssertEquals ("test#01", 5, stream.Position);
  366. AssertEquals ("test#02", 5, stream.ReadByte ());
  367. // Should not need to read from the underlying stream:
  368. stream.Position = 7;
  369. AssertEquals ("test#01", 7, stream.Position);
  370. AssertEquals ("test#02", 7, stream.ReadByte ());
  371. // Should not need to read from the underlying stream:
  372. stream.Position = 5;
  373. AssertEquals ("test#01", 5, stream.Position);
  374. AssertEquals ("test#02", 5, stream.ReadByte ());
  375. // Should not need to read from the underlying stream:
  376. stream.Position = 9;
  377. AssertEquals ("test#01", 9, stream.Position);
  378. AssertEquals ("test#02", 9, stream.ReadByte ());
  379. stream.Position = 10;
  380. AssertEquals ("test#01", 10, stream.Position);
  381. AssertEquals ("test#02", 10, stream.ReadByte ());
  382. stream.Position = 9;
  383. AssertEquals ("test#01", 9, stream.Position);
  384. AssertEquals ("test#02", 9, stream.ReadByte ());
  385. }
  386. }
  387. }