StreamReaderTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // StreamReaderTest.cs - NUnit Test Cases for the SystemIO.StreamReader class
  2. //
  3. // David Brandt ([email protected])
  4. //
  5. // (C) Ximian, Inc. http://www.ximian.com
  6. // Copyright (C) 2004 Novell (http://www.novell.com)
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.IO
  13. {
  14. [TestFixture]
  15. public class StreamReaderTest
  16. {
  17. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  18. private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  19. [SetUp]
  20. public void SetUp ()
  21. {
  22. if (!Directory.Exists (TempFolder))
  23. Directory.CreateDirectory (TempFolder);
  24. if (!File.Exists (_codeFileName))
  25. File.Create (_codeFileName).Close ();
  26. }
  27. [TearDown]
  28. public void TearDown ()
  29. {
  30. if (Directory.Exists (TempFolder))
  31. Directory.Delete (TempFolder, true);
  32. }
  33. [Test]
  34. public void TestCtor1() {
  35. {
  36. bool errorThrown = false;
  37. try {
  38. new StreamReader((Stream)null);
  39. } catch (ArgumentNullException) {
  40. errorThrown = true;
  41. }
  42. Assert.IsTrue (errorThrown, "null string error not thrown");
  43. }
  44. {
  45. bool errorThrown = false;
  46. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  47. try {
  48. StreamReader r = new StreamReader(f);
  49. r.Close();
  50. } catch (ArgumentException) {
  51. errorThrown = true;
  52. }
  53. f.Close();
  54. Assert.IsTrue (errorThrown, "no read error not thrown");
  55. }
  56. {
  57. // this is probably incestuous, but, oh well.
  58. FileStream f = new FileStream(_codeFileName,
  59. FileMode.Open,
  60. FileAccess.Read);
  61. StreamReader r = new StreamReader(f);
  62. Assert.IsNotNull (r, "no stream reader");
  63. r.Close();
  64. f.Close();
  65. }
  66. }
  67. [Test]
  68. public void TestCtor2() {
  69. {
  70. bool errorThrown = false;
  71. try {
  72. new StreamReader("");
  73. } catch (ArgumentException) {
  74. errorThrown = true;
  75. } catch (Exception e) {
  76. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  77. }
  78. Assert.IsTrue (errorThrown, "empty string error not thrown");
  79. }
  80. {
  81. bool errorThrown = false;
  82. try {
  83. new StreamReader((string)null);
  84. } catch (ArgumentNullException) {
  85. errorThrown = true;
  86. } catch (Exception e) {
  87. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  88. }
  89. Assert.IsTrue (errorThrown, "null string error not thrown");
  90. }
  91. {
  92. bool errorThrown = false;
  93. try {
  94. new StreamReader("nonexistentfile");
  95. } catch (FileNotFoundException) {
  96. errorThrown = true;
  97. } catch (Exception e) {
  98. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  99. }
  100. Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
  101. }
  102. {
  103. bool errorThrown = false;
  104. try {
  105. new StreamReader("nonexistentdir/file");
  106. } catch (DirectoryNotFoundException) {
  107. errorThrown = true;
  108. } catch (Exception e) {
  109. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  110. }
  111. Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
  112. }
  113. {
  114. bool errorThrown = false;
  115. try {
  116. new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  117. } catch (IOException) {
  118. errorThrown = true;
  119. } catch (ArgumentException) {
  120. // FIXME - the spec says 'IOExc', but the
  121. // compiler says 'ArgExc'...
  122. errorThrown = true;
  123. } catch (Exception e) {
  124. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
  125. }
  126. Assert.IsTrue (errorThrown, "invalid filename error not thrown");
  127. }
  128. {
  129. // this is probably incestuous, but, oh well.
  130. StreamReader r = new StreamReader(_codeFileName);
  131. Assert.IsNotNull (r, "no stream reader");
  132. r.Close();
  133. }
  134. }
  135. [Test]
  136. public void TestCtor3() {
  137. {
  138. bool errorThrown = false;
  139. try {
  140. new StreamReader((Stream)null, false);
  141. } catch (ArgumentNullException) {
  142. errorThrown = true;
  143. } catch (Exception e) {
  144. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  145. }
  146. Assert.IsTrue (errorThrown, "null stream error not thrown");
  147. }
  148. {
  149. bool errorThrown = false;
  150. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  151. try {
  152. StreamReader r = new StreamReader(f, false);
  153. r.Close();
  154. } catch (ArgumentException) {
  155. errorThrown = true;
  156. } catch (Exception e) {
  157. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  158. }
  159. f.Close();
  160. Assert.IsTrue (errorThrown, "no read error not thrown");
  161. }
  162. {
  163. // this is probably incestuous, but, oh well.
  164. FileStream f = new FileStream(_codeFileName,
  165. FileMode.Open,
  166. FileAccess.Read);
  167. StreamReader r = new StreamReader(f, false);
  168. Assert.IsNotNull (r, "no stream reader");
  169. r.Close();
  170. f.Close();
  171. }
  172. {
  173. bool errorThrown = false;
  174. try {
  175. StreamReader r = new StreamReader((Stream)null, true);
  176. } catch (ArgumentNullException) {
  177. errorThrown = true;
  178. } catch (Exception e) {
  179. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  180. }
  181. Assert.IsTrue (errorThrown, "null string error not thrown");
  182. }
  183. {
  184. bool errorThrown = false;
  185. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  186. try {
  187. StreamReader r = new StreamReader(f, true);
  188. r.Close();
  189. } catch (ArgumentException) {
  190. errorThrown = true;
  191. } catch (Exception e) {
  192. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  193. }
  194. f.Close();
  195. Assert.IsTrue (errorThrown, "no read error not thrown");
  196. }
  197. {
  198. // this is probably incestuous, but, oh well.
  199. FileStream f = new FileStream(_codeFileName,
  200. FileMode.Open,
  201. FileAccess.Read);
  202. StreamReader r = new StreamReader(f, true);
  203. Assert.IsNotNull (r, "no stream reader");
  204. r.Close();
  205. f.Close();
  206. }
  207. }
  208. [Test]
  209. public void TestCtor4() {
  210. {
  211. bool errorThrown = false;
  212. try {
  213. new StreamReader("", false);
  214. } catch (ArgumentException) {
  215. errorThrown = true;
  216. } catch (Exception e) {
  217. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  218. }
  219. Assert.IsTrue (errorThrown, "empty string error not thrown");
  220. }
  221. {
  222. bool errorThrown = false;
  223. try {
  224. new StreamReader((string)null, false);
  225. } catch (ArgumentNullException) {
  226. errorThrown = true;
  227. } catch (Exception e) {
  228. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  229. }
  230. Assert.IsTrue (errorThrown, "null string error not thrown");
  231. }
  232. {
  233. bool errorThrown = false;
  234. try {
  235. new StreamReader(TempFolder + "/nonexistentfile", false);
  236. } catch (FileNotFoundException) {
  237. errorThrown = true;
  238. } catch (Exception e) {
  239. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  240. }
  241. Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
  242. }
  243. {
  244. bool errorThrown = false;
  245. try {
  246. new StreamReader(TempFolder + "/nonexistentdir/file", false);
  247. } catch (DirectoryNotFoundException) {
  248. errorThrown = true;
  249. } catch (Exception e) {
  250. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  251. }
  252. Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
  253. }
  254. {
  255. bool errorThrown = false;
  256. try {
  257. new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
  258. } catch (IOException) {
  259. errorThrown = true;
  260. } catch (ArgumentException) {
  261. // FIXME - the spec says 'IOExc', but the
  262. // compiler says 'ArgExc'...
  263. errorThrown = true;
  264. } catch (Exception e) {
  265. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
  266. }
  267. Assert.IsTrue (errorThrown, "invalid filename error not thrown");
  268. }
  269. {
  270. // this is probably incestuous, but, oh well.
  271. StreamReader r = new StreamReader(_codeFileName, false);
  272. Assert.IsNotNull (r, "no stream reader");
  273. r.Close();
  274. }
  275. {
  276. bool errorThrown = false;
  277. try {
  278. new StreamReader("", true);
  279. } catch (ArgumentException) {
  280. errorThrown = true;
  281. } catch (Exception e) {
  282. Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString());
  283. }
  284. Assert.IsTrue (errorThrown, "empty string error not thrown");
  285. }
  286. {
  287. bool errorThrown = false;
  288. try {
  289. new StreamReader((string)null, true);
  290. } catch (ArgumentNullException) {
  291. errorThrown = true;
  292. } catch (Exception e) {
  293. Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString());
  294. }
  295. Assert.IsTrue (errorThrown, "null string error not thrown");
  296. }
  297. {
  298. bool errorThrown = false;
  299. try {
  300. new StreamReader(TempFolder + "/nonexistentfile", true);
  301. } catch (FileNotFoundException) {
  302. errorThrown = true;
  303. } catch (Exception e) {
  304. Assert.Fail ("Incorrect exception thrown at 8: " + e.ToString());
  305. }
  306. Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
  307. }
  308. {
  309. bool errorThrown = false;
  310. try {
  311. new StreamReader(TempFolder + "/nonexistentdir/file", true);
  312. } catch (DirectoryNotFoundException) {
  313. errorThrown = true;
  314. } catch (Exception e) {
  315. Assert.Fail ("Incorrect exception thrown at 9: " + e.ToString());
  316. }
  317. Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
  318. }
  319. {
  320. bool errorThrown = false;
  321. try {
  322. new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
  323. } catch (IOException) {
  324. errorThrown = true;
  325. } catch (ArgumentException) {
  326. // FIXME - the spec says 'IOExc', but the
  327. // compiler says 'ArgExc'...
  328. errorThrown = true;
  329. } catch (Exception e) {
  330. Assert.Fail ("Incorrect exception thrown at 10: " + e.ToString());
  331. }
  332. Assert.IsTrue (errorThrown, "invalid filename error not thrown");
  333. }
  334. {
  335. // this is probably incestuous, but, oh well.
  336. StreamReader r = new StreamReader(_codeFileName, true);
  337. Assert.IsNotNull (r, "no stream reader");
  338. r.Close();
  339. }
  340. }
  341. // TODO - Ctor with Encoding
  342. [Test]
  343. public void TestBaseStream() {
  344. Byte[] b = {};
  345. MemoryStream m = new MemoryStream(b);
  346. StreamReader r = new StreamReader(m);
  347. Assert.AreSame (m, r.BaseStream, "wrong base stream ");
  348. r.Close();
  349. m.Close();
  350. }
  351. public void TestCurrentEncoding() {
  352. Byte[] b = {};
  353. MemoryStream m = new MemoryStream(b);
  354. StreamReader r = new StreamReader(m);
  355. Assert.AreEqual (Encoding.UTF8.GetType (), r.CurrentEncoding.GetType (),
  356. "wrong encoding");
  357. }
  358. // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
  359. // TODO - DiscardBufferedData - I have no clue how to test this function.
  360. [Test]
  361. public void TestPeek() {
  362. // FIXME - how to get an IO Exception?
  363. Byte [] b;
  364. MemoryStream m;
  365. StreamReader r;
  366. try {
  367. b = new byte [0];
  368. m = new MemoryStream (b);
  369. r = new StreamReader(m);
  370. m.Close();
  371. int nothing = r.Peek();
  372. Assert.Fail ("#1");
  373. } catch (ObjectDisposedException) {
  374. }
  375. b = new byte [] {1, 2, 3, 4, 5, 6};
  376. m = new MemoryStream (b);
  377. r = new StreamReader(m);
  378. for (int i = 1; i <= 6; i++) {
  379. Assert.AreEqual (i, r.Peek(), "#2");
  380. r.Read();
  381. }
  382. Assert.AreEqual (-1, r.Peek(), "#3");
  383. }
  384. [Test]
  385. public void TestRead() {
  386. // FIXME - how to get an IO Exception?
  387. {
  388. bool errorThrown = false;
  389. try {
  390. Byte[] b = {};
  391. MemoryStream m = new MemoryStream(b);
  392. StreamReader r = new StreamReader(m);
  393. m.Close();
  394. int nothing = r.Read();
  395. } catch (ObjectDisposedException) {
  396. errorThrown = true;
  397. } catch (Exception e) {
  398. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  399. }
  400. Assert.IsTrue (errorThrown, "nothing-to-read error not thrown");
  401. }
  402. {
  403. Byte[] b = {1, 2, 3, 4, 5, 6};
  404. MemoryStream m = new MemoryStream(b);
  405. StreamReader r = new StreamReader(m);
  406. for (int i = 1; i <= 6; i++) {
  407. Assert.AreEqual (i, r.Read (), "read incorrect");
  408. }
  409. Assert.AreEqual (-1, r.Read (), "Should be none left");
  410. }
  411. {
  412. bool errorThrown = false;
  413. try {
  414. Byte[] b = {};
  415. StreamReader r = new StreamReader(new MemoryStream(b));
  416. r.Read(null, 0, 0);
  417. } catch (ArgumentNullException) {
  418. errorThrown = true;
  419. } catch (ArgumentException) {
  420. errorThrown = true;
  421. } catch (Exception e) {
  422. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  423. }
  424. Assert.IsTrue (errorThrown, "null buffer error not thrown");
  425. }
  426. {
  427. bool errorThrown = false;
  428. try {
  429. Byte[] b = {};
  430. StreamReader r = new StreamReader(new MemoryStream(b));
  431. Char[] c = new Char[1];
  432. r.Read(c, 0, 2);
  433. } catch (ArgumentException) {
  434. errorThrown = true;
  435. } catch (Exception e) {
  436. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  437. }
  438. Assert.IsTrue (errorThrown, "too-long range error not thrown");
  439. }
  440. {
  441. bool errorThrown = false;
  442. try {
  443. Byte[] b = {};
  444. StreamReader r = new StreamReader(new MemoryStream(b));
  445. Char[] c = new Char[1];
  446. r.Read(c, -1, 2);
  447. } catch (ArgumentOutOfRangeException) {
  448. errorThrown = true;
  449. } catch (Exception e) {
  450. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  451. }
  452. Assert.IsTrue (errorThrown, "out of range error not thrown");
  453. }
  454. {
  455. bool errorThrown = false;
  456. try {
  457. Byte[] b = {};
  458. StreamReader r = new StreamReader(new MemoryStream(b));
  459. Char[] c = new Char[1];
  460. r.Read(c, 0, -1);
  461. } catch (ArgumentOutOfRangeException) {
  462. errorThrown = true;
  463. } catch (Exception e) {
  464. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
  465. }
  466. Assert.IsTrue (errorThrown, "out of range error not thrown");
  467. }
  468. {
  469. int ii = 1;
  470. try {
  471. Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
  472. (byte)'d', (byte)'e', (byte)'f',
  473. (byte)'g'};
  474. MemoryStream m = new MemoryStream(b);
  475. ii++;
  476. StreamReader r = new StreamReader(m);
  477. ii++;
  478. char[] buffer = new Char[7];
  479. ii++;
  480. char[] target = {'g','d','e','f','b','c','a'};
  481. ii++;
  482. r.Read(buffer, 6, 1);
  483. ii++;
  484. r.Read(buffer, 4, 2);
  485. ii++;
  486. r.Read(buffer, 1, 3);
  487. ii++;
  488. r.Read(buffer, 0, 1);
  489. ii++;
  490. for (int i = 0; i < target.Length; i++) {
  491. Assert.AreEqual (target[i], buffer[i], "read no work");
  492. i++;
  493. }
  494. } catch (Exception e) {
  495. Assert.Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
  496. }
  497. }
  498. }
  499. [Test]
  500. public void TestReadLine() {
  501. // TODO Out Of Memory Exc? IO Exc?
  502. Byte[] b = new Byte[8];
  503. b[0] = (byte)'a';
  504. b[1] = (byte)'\n';
  505. b[2] = (byte)'b';
  506. b[3] = (byte)'\n';
  507. b[4] = (byte)'c';
  508. b[5] = (byte)'\n';
  509. b[6] = (byte)'d';
  510. b[7] = (byte)'\n';
  511. MemoryStream m = new MemoryStream(b);
  512. StreamReader r = new StreamReader(m);
  513. Assert.AreEqual ("a", r.ReadLine(), "#1");
  514. Assert.AreEqual ("b", r.ReadLine (), "#2");
  515. Assert.AreEqual ("c", r.ReadLine (), "#3");
  516. Assert.AreEqual ("d", r.ReadLine(), "#4");
  517. Assert.IsNull (r.ReadLine (), "#5");
  518. }
  519. [Test]
  520. public void ReadLine1() {
  521. Byte[] b = new Byte[10];
  522. b[0] = (byte)'a';
  523. b[1] = (byte)'\r';
  524. b[2] = (byte)'b';
  525. b[3] = (byte)'\n';
  526. b[4] = (byte)'c';
  527. b[5] = (byte)'\n';
  528. b[5] = (byte)'\r';
  529. b[6] = (byte)'d';
  530. b[7] = (byte)'\n';
  531. b[8] = (byte)'\r';
  532. b[9] = (byte)'\n';
  533. MemoryStream m = new MemoryStream(b);
  534. StreamReader r = new StreamReader(m);
  535. Assert.AreEqual ("a", r.ReadLine (), "#1");
  536. Assert.AreEqual ("b", r.ReadLine (), "#2");
  537. Assert.AreEqual ("c", r.ReadLine (), "#3");
  538. Assert.AreEqual ("d", r.ReadLine (), "#4");
  539. Assert.AreEqual (string.Empty, r.ReadLine (), "#5");
  540. Assert.IsNull (r.ReadLine(), "#6");
  541. }
  542. [Test]
  543. public void ReadLine2() {
  544. Byte[] b = new Byte[10];
  545. b[0] = (byte)'\r';
  546. b[1] = (byte)'\r';
  547. b[2] = (byte)'\n';
  548. b[3] = (byte)'\n';
  549. b[4] = (byte)'c';
  550. b[5] = (byte)'\n';
  551. b[5] = (byte)'\r';
  552. b[6] = (byte)'d';
  553. b[7] = (byte)'\n';
  554. b[8] = (byte)'\r';
  555. b[9] = (byte)'\n';
  556. MemoryStream m = new MemoryStream(b);
  557. StreamReader r = new StreamReader(m);
  558. Assert.AreEqual (string.Empty, r.ReadLine (), "#1");
  559. Assert.AreEqual (string.Empty, r.ReadLine (), "#2");
  560. Assert.AreEqual (string.Empty, r.ReadLine (), "#3");
  561. Assert.AreEqual ("c", r.ReadLine (), "#4");
  562. Assert.AreEqual ("d", r.ReadLine (), "#5");
  563. Assert.AreEqual (string.Empty, r.ReadLine (), "#6");
  564. Assert.IsNull (r.ReadLine (), "#7");
  565. }
  566. [Test]
  567. public void ReadLine3() {
  568. StringBuilder sb = new StringBuilder ();
  569. sb.Append (new string ('1', 32767));
  570. sb.Append ('\r');
  571. sb.Append ('\n');
  572. sb.Append ("Hola\n");
  573. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  574. MemoryStream m = new MemoryStream(bytes);
  575. StreamReader r = new StreamReader(m);
  576. Assert.AreEqual (new string ('1', 32767), r.ReadLine(), "#1");
  577. Assert.AreEqual ("Hola", r.ReadLine (), "#2");
  578. Assert.IsNull (r.ReadLine (), "#3");
  579. }
  580. [Test]
  581. public void ReadLine4() {
  582. StringBuilder sb = new StringBuilder ();
  583. sb.Append (new string ('1', 32767));
  584. sb.Append ('\r');
  585. sb.Append ('\n');
  586. sb.Append ("Hola\n");
  587. sb.Append (sb.ToString ());
  588. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  589. MemoryStream m = new MemoryStream(bytes);
  590. StreamReader r = new StreamReader(m);
  591. Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#1");
  592. Assert.AreEqual ("Hola", r.ReadLine (), "#2");
  593. Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#3");
  594. Assert.AreEqual ("Hola", r.ReadLine (), "#4");
  595. Assert.IsNull (r.ReadLine (), "#5");
  596. }
  597. [Test]
  598. public void ReadLine5() {
  599. StringBuilder sb = new StringBuilder ();
  600. sb.Append (new string ('1', 32768));
  601. sb.Append ('\r');
  602. sb.Append ('\n');
  603. sb.Append ("Hola\n");
  604. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  605. MemoryStream m = new MemoryStream(bytes);
  606. StreamReader r = new StreamReader(m);
  607. Assert.AreEqual (new string ('1', 32768), r.ReadLine (), "#1");
  608. Assert.AreEqual ("Hola", r.ReadLine (), "#2");
  609. Assert.IsNull (r.ReadLine (), "#3");
  610. }
  611. public void TestReadToEnd() {
  612. // TODO Out Of Memory Exc? IO Exc?
  613. Byte[] b = new Byte[8];
  614. b[0] = (byte)'a';
  615. b[1] = (byte)'\n';
  616. b[2] = (byte)'b';
  617. b[3] = (byte)'\n';
  618. b[4] = (byte)'c';
  619. b[5] = (byte)'\n';
  620. b[6] = (byte)'d';
  621. b[7] = (byte)'\n';
  622. MemoryStream m = new MemoryStream(b);
  623. StreamReader r = new StreamReader(m);
  624. Assert.AreEqual ("a\nb\nc\nd\n", r.ReadToEnd (), "#1");
  625. Assert.AreEqual (string.Empty, r.ReadToEnd (), "#2");
  626. }
  627. [Test]
  628. public void TestBaseStreamClosed ()
  629. {
  630. byte [] b = {};
  631. MemoryStream m = new MemoryStream (b);
  632. StreamReader r = new StreamReader (m);
  633. m.Close ();
  634. try {
  635. r.Peek ();
  636. Assert.Fail ();
  637. } catch (ObjectDisposedException) {
  638. }
  639. }
  640. [Test]
  641. [ExpectedException (typeof (ArgumentNullException))]
  642. public void Contructor_Stream_NullEncoding ()
  643. {
  644. new StreamReader (new MemoryStream (), null);
  645. }
  646. [Test]
  647. [ExpectedException (typeof (ArgumentNullException))]
  648. public void Contructor_Path_NullEncoding ()
  649. {
  650. new StreamReader (_codeFileName, null);
  651. }
  652. [Test]
  653. [ExpectedException (typeof (ArgumentNullException))]
  654. public void Read_Null ()
  655. {
  656. StreamReader r = new StreamReader (new MemoryStream ());
  657. r.Read (null, 0, 0);
  658. }
  659. [Test]
  660. [ExpectedException (typeof (ArgumentException))]
  661. public void Read_IndexOverflow ()
  662. {
  663. char[] array = new char [16];
  664. StreamReader r = new StreamReader (new MemoryStream (16));
  665. r.Read (array, 1, Int32.MaxValue);
  666. }
  667. [Test]
  668. [ExpectedException (typeof (ArgumentException))]
  669. public void Read_CountOverflow ()
  670. {
  671. char[] array = new char [16];
  672. StreamReader r = new StreamReader (new MemoryStream (16));
  673. r.Read (array, Int32.MaxValue, 1);
  674. }
  675. [Test]
  676. public void Read_DoesntStopAtLineEndings ()
  677. {
  678. MemoryStream ms = new MemoryStream (Encoding.ASCII.GetBytes ("Line1\rLine2\r\nLine3\nLine4"));
  679. StreamReader reader = new StreamReader (ms);
  680. Assert.AreEqual (24, reader.Read (new char[24], 0, 24));
  681. }
  682. [Test]
  683. public void bug75526 ()
  684. {
  685. StreamReader sr = new StreamReader (new Bug75526Stream ());
  686. int len = sr.Read (new char [10], 0, 10);
  687. Assert.AreEqual (2, len);
  688. }
  689. class Bug75526Stream : MemoryStream
  690. {
  691. public override int Read (byte [] buffer, int offset, int count)
  692. {
  693. buffer [offset + 0] = (byte) 'a';
  694. buffer [offset + 1] = (byte) 'b';
  695. return 2;
  696. }
  697. }
  698. }
  699. }