StreamReaderTest.cs 21 KB

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