StreamReaderTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 NUnit.Framework;
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. namespace MonoTests.System.IO
  13. {
  14. [TestFixture]
  15. public class StreamReaderTest : Assertion
  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. StreamReader r = new StreamReader((Stream)null);
  39. } catch (ArgumentNullException) {
  40. errorThrown = true;
  41. }
  42. Assert("null string error not thrown", errorThrown);
  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("no read error not thrown", errorThrown);
  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. AssertNotNull("no stream reader", r);
  63. r.Close();
  64. f.Close();
  65. }
  66. }
  67. [Test]
  68. public void TestCtor2() {
  69. {
  70. bool errorThrown = false;
  71. try {
  72. StreamReader r = new StreamReader("");
  73. } catch (ArgumentException) {
  74. errorThrown = true;
  75. } catch (Exception e) {
  76. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  77. }
  78. Assert("empty string error not thrown", errorThrown);
  79. }
  80. {
  81. bool errorThrown = false;
  82. try {
  83. StreamReader r = new StreamReader((string)null);
  84. } catch (ArgumentNullException) {
  85. errorThrown = true;
  86. } catch (Exception e) {
  87. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  88. }
  89. Assert("null string error not thrown", errorThrown);
  90. }
  91. {
  92. bool errorThrown = false;
  93. try {
  94. StreamReader r = new StreamReader("nonexistentfile");
  95. } catch (FileNotFoundException) {
  96. errorThrown = true;
  97. } catch (Exception e) {
  98. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  99. }
  100. Assert("fileNotFound error not thrown", errorThrown);
  101. }
  102. {
  103. bool errorThrown = false;
  104. try {
  105. StreamReader r = new StreamReader("nonexistentdir/file");
  106. } catch (DirectoryNotFoundException) {
  107. errorThrown = true;
  108. } catch (Exception e) {
  109. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  110. }
  111. Assert("dirNotFound error not thrown", errorThrown);
  112. }
  113. {
  114. bool errorThrown = false;
  115. try {
  116. StreamReader r = 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. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  125. }
  126. Assert("invalid filename error not thrown", errorThrown);
  127. }
  128. {
  129. // this is probably incestuous, but, oh well.
  130. StreamReader r = new StreamReader(_codeFileName);
  131. AssertNotNull("no stream reader", r);
  132. r.Close();
  133. }
  134. }
  135. [Test]
  136. public void TestCtor3() {
  137. {
  138. bool errorThrown = false;
  139. try {
  140. StreamReader r = new StreamReader((Stream)null, false);
  141. } catch (ArgumentNullException) {
  142. errorThrown = true;
  143. } catch (Exception e) {
  144. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  145. }
  146. Assert("null stream error not thrown", errorThrown);
  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. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  158. }
  159. f.Close();
  160. Assert("no read error not thrown", errorThrown);
  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. AssertNotNull("no stream reader", r);
  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. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  180. }
  181. Assert("null string error not thrown", errorThrown);
  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. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  193. }
  194. f.Close();
  195. Assert("no read error not thrown", errorThrown);
  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. AssertNotNull("no stream reader", r);
  204. r.Close();
  205. f.Close();
  206. }
  207. }
  208. [Test]
  209. public void TestCtor4() {
  210. {
  211. bool errorThrown = false;
  212. try {
  213. StreamReader r = new StreamReader("", false);
  214. } catch (ArgumentException) {
  215. errorThrown = true;
  216. } catch (Exception e) {
  217. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  218. }
  219. Assert("empty string error not thrown", errorThrown);
  220. }
  221. {
  222. bool errorThrown = false;
  223. try {
  224. StreamReader r = new StreamReader((string)null, false);
  225. } catch (ArgumentNullException) {
  226. errorThrown = true;
  227. } catch (Exception e) {
  228. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  229. }
  230. Assert("null string error not thrown", errorThrown);
  231. }
  232. {
  233. bool errorThrown = false;
  234. try {
  235. StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", false);
  236. } catch (FileNotFoundException) {
  237. errorThrown = true;
  238. } catch (Exception e) {
  239. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  240. }
  241. Assert("fileNotFound error not thrown", errorThrown);
  242. }
  243. {
  244. bool errorThrown = false;
  245. try {
  246. StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", false);
  247. } catch (DirectoryNotFoundException) {
  248. errorThrown = true;
  249. } catch (Exception e) {
  250. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  251. }
  252. Assert("dirNotFound error not thrown", errorThrown);
  253. }
  254. {
  255. bool errorThrown = false;
  256. try {
  257. StreamReader r = 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. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  266. }
  267. Assert("invalid filename error not thrown", errorThrown);
  268. }
  269. {
  270. // this is probably incestuous, but, oh well.
  271. StreamReader r = new StreamReader(_codeFileName, false);
  272. AssertNotNull("no stream reader", r);
  273. r.Close();
  274. }
  275. {
  276. bool errorThrown = false;
  277. try {
  278. StreamReader r = new StreamReader("", true);
  279. } catch (ArgumentException) {
  280. errorThrown = true;
  281. } catch (Exception e) {
  282. Fail ("Incorrect exception thrown at 6: " + e.ToString());
  283. }
  284. Assert("empty string error not thrown", errorThrown);
  285. }
  286. {
  287. bool errorThrown = false;
  288. try {
  289. StreamReader r = new StreamReader((string)null, true);
  290. } catch (ArgumentNullException) {
  291. errorThrown = true;
  292. } catch (Exception e) {
  293. Fail ("Incorrect exception thrown at 7: " + e.ToString());
  294. }
  295. Assert("null string error not thrown", errorThrown);
  296. }
  297. {
  298. bool errorThrown = false;
  299. try {
  300. StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", true);
  301. } catch (FileNotFoundException) {
  302. errorThrown = true;
  303. } catch (Exception e) {
  304. Fail ("Incorrect exception thrown at 8: " + e.ToString());
  305. }
  306. Assert("fileNotFound error not thrown", errorThrown);
  307. }
  308. {
  309. bool errorThrown = false;
  310. try {
  311. StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", true);
  312. } catch (DirectoryNotFoundException) {
  313. errorThrown = true;
  314. } catch (Exception e) {
  315. Fail ("Incorrect exception thrown at 9: " + e.ToString());
  316. }
  317. Assert("dirNotFound error not thrown", errorThrown);
  318. }
  319. {
  320. bool errorThrown = false;
  321. try {
  322. StreamReader r = 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. Fail ("Incorrect exception thrown at 10: " + e.ToString());
  331. }
  332. Assert("invalid filename error not thrown", errorThrown);
  333. }
  334. {
  335. // this is probably incestuous, but, oh well.
  336. StreamReader r = new StreamReader(_codeFileName, true);
  337. AssertNotNull("no stream reader", r);
  338. r.Close();
  339. }
  340. }
  341. // TODO - Ctor with Encoding
  342. [Test]
  343. public void TestBaseStream() {
  344. string progress = "beginning";
  345. try {
  346. Byte[] b = {};
  347. MemoryStream m = new MemoryStream(b);
  348. StreamReader r = new StreamReader(m);
  349. AssertEquals("wrong base stream ", m, r.BaseStream);
  350. progress = "Closing StreamReader";
  351. r.Close();
  352. progress = "Closing MemoryStream";
  353. m.Close();
  354. } catch (Exception e) {
  355. Fail ("At '" + progress + "' an unexpected exception was thrown: " + e.ToString());
  356. }
  357. }
  358. public void TestCurrentEncoding() {
  359. try {
  360. Byte[] b = {};
  361. MemoryStream m = new MemoryStream(b);
  362. StreamReader r = new StreamReader(m);
  363. AssertEquals("wrong encoding",
  364. Encoding.UTF8.GetType (), r.CurrentEncoding.GetType ());
  365. } catch (Exception e) {
  366. Fail ("Unexpected exception thrown: " + e.ToString());
  367. }
  368. }
  369. // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
  370. // TODO - DiscardBufferedData - I have no clue how to test this function.
  371. [Test]
  372. public void TestPeek() {
  373. // FIXME - how to get an IO Exception?
  374. {
  375. bool errorThrown = false;
  376. try {
  377. Byte[] b = {};
  378. MemoryStream m = new MemoryStream(b);
  379. StreamReader r = new StreamReader(m);
  380. m.Close();
  381. int nothing = r.Peek();
  382. } catch (ObjectDisposedException) {
  383. errorThrown = true;
  384. }
  385. Assert("nothing-to-peek-at error not thrown", errorThrown);
  386. }
  387. {
  388. Byte[] b = {1, 2, 3, 4, 5, 6};
  389. MemoryStream m = new MemoryStream(b);
  390. StreamReader r = new StreamReader(m);
  391. for (int i = 1; i <= 6; i++) {
  392. AssertEquals("peek incorrect", i, r.Peek());
  393. r.Read();
  394. }
  395. AssertEquals("should be none left", -1, r.Peek());
  396. }
  397. }
  398. [Test]
  399. public void TestRead() {
  400. // FIXME - how to get an IO Exception?
  401. {
  402. bool errorThrown = false;
  403. try {
  404. Byte[] b = {};
  405. MemoryStream m = new MemoryStream(b);
  406. StreamReader r = new StreamReader(m);
  407. m.Close();
  408. int nothing = r.Read();
  409. } catch (ObjectDisposedException) {
  410. errorThrown = true;
  411. } catch (Exception e) {
  412. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  413. }
  414. Assert("nothing-to-read error not thrown", errorThrown);
  415. }
  416. {
  417. Byte[] b = {1, 2, 3, 4, 5, 6};
  418. MemoryStream m = new MemoryStream(b);
  419. StreamReader r = new StreamReader(m);
  420. for (int i = 1; i <= 6; i++) {
  421. AssertEquals("read incorrect", i, r.Read());
  422. }
  423. AssertEquals("Should be none left", -1, r.Read());
  424. }
  425. {
  426. bool errorThrown = false;
  427. try {
  428. Byte[] b = {};
  429. StreamReader r = new StreamReader(new MemoryStream(b));
  430. r.Read(null, 0, 0);
  431. } catch (ArgumentNullException) {
  432. errorThrown = true;
  433. } catch (ArgumentException) {
  434. errorThrown = true;
  435. } catch (Exception e) {
  436. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  437. }
  438. Assert("null buffer error not thrown", errorThrown);
  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, 0, 2);
  447. } catch (ArgumentException) {
  448. errorThrown = true;
  449. } catch (Exception e) {
  450. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  451. }
  452. Assert("too-long range error not thrown", errorThrown);
  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, -1, 2);
  461. } catch (ArgumentOutOfRangeException) {
  462. errorThrown = true;
  463. } catch (Exception e) {
  464. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  465. }
  466. Assert("out of range error not thrown", errorThrown);
  467. }
  468. {
  469. bool errorThrown = false;
  470. try {
  471. Byte[] b = {};
  472. StreamReader r = new StreamReader(new MemoryStream(b));
  473. Char[] c = new Char[1];
  474. r.Read(c, 0, -1);
  475. } catch (ArgumentOutOfRangeException) {
  476. errorThrown = true;
  477. } catch (Exception e) {
  478. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  479. }
  480. Assert("out of range error not thrown", errorThrown);
  481. }
  482. {
  483. int ii = 1;
  484. try {
  485. Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
  486. (byte)'d', (byte)'e', (byte)'f',
  487. (byte)'g'};
  488. MemoryStream m = new MemoryStream(b);
  489. ii++;
  490. StreamReader r = new StreamReader(m);
  491. ii++;
  492. char[] buffer = new Char[7];
  493. ii++;
  494. char[] target = {'g','d','e','f','b','c','a'};
  495. ii++;
  496. r.Read(buffer, 6, 1);
  497. ii++;
  498. r.Read(buffer, 4, 2);
  499. ii++;
  500. r.Read(buffer, 1, 3);
  501. ii++;
  502. r.Read(buffer, 0, 1);
  503. ii++;
  504. for (int i = 0; i < target.Length; i++) {
  505. AssertEquals("read no work",
  506. target[i], buffer[i]);
  507. i++;
  508. }
  509. } catch (Exception e) {
  510. Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
  511. }
  512. }
  513. }
  514. [Test]
  515. public void TestReadLine() {
  516. // TODO Out Of Memory Exc? IO Exc?
  517. Byte[] b = new Byte[8];
  518. b[0] = (byte)'a';
  519. b[1] = (byte)'\n';
  520. b[2] = (byte)'b';
  521. b[3] = (byte)'\n';
  522. b[4] = (byte)'c';
  523. b[5] = (byte)'\n';
  524. b[6] = (byte)'d';
  525. b[7] = (byte)'\n';
  526. MemoryStream m = new MemoryStream(b);
  527. StreamReader r = new StreamReader(m);
  528. AssertEquals("line doesn't match", "a", r.ReadLine());
  529. AssertEquals("line doesn't match", "b", r.ReadLine());
  530. AssertEquals("line doesn't match", "c", r.ReadLine());
  531. AssertEquals("line doesn't match", "d", r.ReadLine());
  532. AssertEquals("line doesn't match", null, r.ReadLine());
  533. }
  534. [Test]
  535. public void ReadLine1() {
  536. Byte[] b = new Byte[10];
  537. b[0] = (byte)'a';
  538. b[1] = (byte)'\r';
  539. b[2] = (byte)'b';
  540. b[3] = (byte)'\n';
  541. b[4] = (byte)'c';
  542. b[5] = (byte)'\n';
  543. b[5] = (byte)'\r';
  544. b[6] = (byte)'d';
  545. b[7] = (byte)'\n';
  546. b[8] = (byte)'\r';
  547. b[9] = (byte)'\n';
  548. MemoryStream m = new MemoryStream(b);
  549. StreamReader r = new StreamReader(m);
  550. AssertEquals("line doesn't match", "a", r.ReadLine());
  551. AssertEquals("line doesn't match", "b", r.ReadLine());
  552. AssertEquals("line doesn't match", "c", r.ReadLine());
  553. AssertEquals("line doesn't match", "d", r.ReadLine());
  554. AssertEquals("line doesn't match", "", r.ReadLine());
  555. AssertEquals("line doesn't match", null, r.ReadLine());
  556. }
  557. [Test]
  558. public void ReadLine2() {
  559. Byte[] b = new Byte[10];
  560. b[0] = (byte)'\r';
  561. b[1] = (byte)'\r';
  562. b[2] = (byte)'\n';
  563. b[3] = (byte)'\n';
  564. b[4] = (byte)'c';
  565. b[5] = (byte)'\n';
  566. b[5] = (byte)'\r';
  567. b[6] = (byte)'d';
  568. b[7] = (byte)'\n';
  569. b[8] = (byte)'\r';
  570. b[9] = (byte)'\n';
  571. MemoryStream m = new MemoryStream(b);
  572. StreamReader r = new StreamReader(m);
  573. AssertEquals("line doesn't match", "", r.ReadLine());
  574. AssertEquals("line doesn't match", "", r.ReadLine());
  575. AssertEquals("line doesn't match", "", r.ReadLine());
  576. AssertEquals("line doesn't match", "c", r.ReadLine());
  577. AssertEquals("line doesn't match", "d", r.ReadLine());
  578. AssertEquals("line doesn't match", "", r.ReadLine());
  579. AssertEquals("line doesn't match", null, r.ReadLine());
  580. }
  581. [Test]
  582. public void ReadLine3() {
  583. StringBuilder sb = new StringBuilder ();
  584. sb.Append (new string ('1', 32767));
  585. sb.Append ('\r');
  586. sb.Append ('\n');
  587. sb.Append ("Hola\n");
  588. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  589. MemoryStream m = new MemoryStream(bytes);
  590. StreamReader r = new StreamReader(m);
  591. AssertEquals("line doesn't match", new string ('1', 32767), r.ReadLine());
  592. AssertEquals("line doesn't match", "Hola", r.ReadLine());
  593. AssertEquals("line doesn't match", null, r.ReadLine());
  594. }
  595. [Test]
  596. public void ReadLine4() {
  597. StringBuilder sb = new StringBuilder ();
  598. sb.Append (new string ('1', 32767));
  599. sb.Append ('\r');
  600. sb.Append ('\n');
  601. sb.Append ("Hola\n");
  602. sb.Append (sb.ToString ());
  603. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  604. MemoryStream m = new MemoryStream(bytes);
  605. StreamReader r = new StreamReader(m);
  606. AssertEquals("line doesn't match", new string ('1', 32767), r.ReadLine());
  607. AssertEquals("line doesn't match", "Hola", r.ReadLine());
  608. AssertEquals("line doesn't match", new string ('1', 32767), r.ReadLine());
  609. AssertEquals("line doesn't match", "Hola", r.ReadLine());
  610. AssertEquals("line doesn't match", null, r.ReadLine());
  611. }
  612. [Test]
  613. public void ReadLine5() {
  614. StringBuilder sb = new StringBuilder ();
  615. sb.Append (new string ('1', 32768));
  616. sb.Append ('\r');
  617. sb.Append ('\n');
  618. sb.Append ("Hola\n");
  619. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  620. MemoryStream m = new MemoryStream(bytes);
  621. StreamReader r = new StreamReader(m);
  622. AssertEquals("line doesn't match", new string ('1', 32768), r.ReadLine());
  623. AssertEquals("line doesn't match", "Hola", r.ReadLine());
  624. AssertEquals("line doesn't match", null, r.ReadLine());
  625. }
  626. public void TestReadToEnd() {
  627. // TODO Out Of Memory Exc? IO Exc?
  628. Byte[] b = new Byte[8];
  629. b[0] = (byte)'a';
  630. b[1] = (byte)'\n';
  631. b[2] = (byte)'b';
  632. b[3] = (byte)'\n';
  633. b[4] = (byte)'c';
  634. b[5] = (byte)'\n';
  635. b[6] = (byte)'d';
  636. b[7] = (byte)'\n';
  637. MemoryStream m = new MemoryStream(b);
  638. StreamReader r = new StreamReader(m);
  639. AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
  640. AssertEquals("line doesn't match", "", r.ReadToEnd());
  641. }
  642. [Test]
  643. public void TestBaseStreamClosed ()
  644. {
  645. byte [] b = {};
  646. MemoryStream m = new MemoryStream (b);
  647. StreamReader r = new StreamReader (m);
  648. m.Close ();
  649. bool thrown = false;
  650. try {
  651. r.Peek ();
  652. } catch (ObjectDisposedException) {
  653. thrown = true;
  654. }
  655. AssertEquals ("#01", true, thrown);
  656. }
  657. [Test]
  658. [ExpectedException (typeof (ArgumentNullException))]
  659. public void Contructor_Stream_NullEncoding ()
  660. {
  661. StreamReader r = new StreamReader (new MemoryStream (), null);
  662. }
  663. [Test]
  664. [ExpectedException (typeof (ArgumentNullException))]
  665. public void Contructor_Path_NullEncoding ()
  666. {
  667. StreamReader r = new StreamReader (_codeFileName, null);
  668. }
  669. [Test]
  670. [ExpectedException (typeof (ArgumentNullException))]
  671. public void Read_Null ()
  672. {
  673. StreamReader r = new StreamReader (new MemoryStream ());
  674. r.Read (null, 0, 0);
  675. }
  676. [Test]
  677. [ExpectedException (typeof (ArgumentException))]
  678. public void Read_IndexOverflow ()
  679. {
  680. char[] array = new char [16];
  681. StreamReader r = new StreamReader (new MemoryStream (16));
  682. r.Read (array, 1, Int32.MaxValue);
  683. }
  684. [Test]
  685. [ExpectedException (typeof (ArgumentException))]
  686. public void Read_CountOverflow ()
  687. {
  688. char[] array = new char [16];
  689. StreamReader r = new StreamReader (new MemoryStream (16));
  690. r.Read (array, Int32.MaxValue, 1);
  691. }
  692. [Test]
  693. public void Read_DoesntStopAtLineEndings ()
  694. {
  695. MemoryStream ms = new MemoryStream (Encoding.ASCII.GetBytes ("Line1\rLine2\r\nLine3\nLine4"));
  696. StreamReader reader = new StreamReader (ms);
  697. AssertEquals (24, reader.Read (new char[24], 0, 24));
  698. }
  699. [Test]
  700. public void bug75526 ()
  701. {
  702. StreamReader sr = new StreamReader (new Bug75526Stream ());
  703. int len = sr.Read (new char [10], 0, 10);
  704. AssertEquals (2, len);
  705. }
  706. class Bug75526Stream : MemoryStream
  707. {
  708. public override int Read (byte [] buffer, int offset, int count)
  709. {
  710. buffer [offset + 0] = (byte) 'a';
  711. buffer [offset + 1] = (byte) 'b';
  712. return 2;
  713. }
  714. }
  715. }
  716. }