StreamReaderTest.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. #if NET_4_5
  12. using System.Threading.Tasks;
  13. #endif
  14. using NUnit.Framework;
  15. namespace MonoTests.System.IO
  16. {
  17. [TestFixture]
  18. public class StreamReaderTest
  19. {
  20. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  21. private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  22. private const string TestString = "Hello World!";
  23. [SetUp]
  24. public void SetUp ()
  25. {
  26. if (!Directory.Exists (TempFolder))
  27. Directory.CreateDirectory (TempFolder);
  28. if (!File.Exists (_codeFileName))
  29. File.Create (_codeFileName).Close ();
  30. }
  31. [TearDown]
  32. public void TearDown ()
  33. {
  34. if (Directory.Exists (TempFolder))
  35. Directory.Delete (TempFolder, true);
  36. }
  37. [Test]
  38. public void TestCtor1() {
  39. {
  40. bool errorThrown = false;
  41. try {
  42. new StreamReader((Stream)null);
  43. } catch (ArgumentNullException) {
  44. errorThrown = true;
  45. }
  46. Assert.IsTrue (errorThrown, "null string error not thrown");
  47. }
  48. {
  49. bool errorThrown = false;
  50. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  51. try {
  52. StreamReader r = new StreamReader(f);
  53. r.Close();
  54. } catch (ArgumentException) {
  55. errorThrown = true;
  56. }
  57. f.Close();
  58. Assert.IsTrue (errorThrown, "no read error not thrown");
  59. }
  60. {
  61. // this is probably incestuous, but, oh well.
  62. FileStream f = new FileStream(_codeFileName,
  63. FileMode.Open,
  64. FileAccess.Read);
  65. StreamReader r = new StreamReader(f);
  66. Assert.IsNotNull (r, "no stream reader");
  67. r.Close();
  68. f.Close();
  69. }
  70. }
  71. [Test]
  72. public void TestCtor2() {
  73. {
  74. bool errorThrown = false;
  75. try {
  76. new StreamReader("");
  77. } catch (ArgumentException) {
  78. errorThrown = true;
  79. } catch (Exception e) {
  80. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  81. }
  82. Assert.IsTrue (errorThrown, "empty string error not thrown");
  83. }
  84. {
  85. bool errorThrown = false;
  86. try {
  87. new StreamReader((string)null);
  88. } catch (ArgumentNullException) {
  89. errorThrown = true;
  90. } catch (Exception e) {
  91. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  92. }
  93. Assert.IsTrue (errorThrown, "null string error not thrown");
  94. }
  95. {
  96. bool errorThrown = false;
  97. try {
  98. new StreamReader("nonexistentfile");
  99. } catch (FileNotFoundException) {
  100. errorThrown = true;
  101. } catch (Exception e) {
  102. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  103. }
  104. Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
  105. }
  106. {
  107. bool errorThrown = false;
  108. try {
  109. new StreamReader("nonexistentdir/file");
  110. } catch (DirectoryNotFoundException) {
  111. errorThrown = true;
  112. } catch (Exception e) {
  113. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  114. }
  115. Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
  116. }
  117. {
  118. bool errorThrown = false;
  119. try {
  120. new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  121. } catch (IOException) {
  122. errorThrown = true;
  123. } catch (ArgumentException) {
  124. // FIXME - the spec says 'IOExc', but the
  125. // compiler says 'ArgExc'...
  126. errorThrown = true;
  127. } catch (Exception e) {
  128. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
  129. }
  130. Assert.IsTrue (errorThrown, "invalid filename error not thrown");
  131. }
  132. {
  133. // this is probably incestuous, but, oh well.
  134. StreamReader r = new StreamReader(_codeFileName);
  135. Assert.IsNotNull (r, "no stream reader");
  136. r.Close();
  137. }
  138. }
  139. [Test]
  140. public void TestCtor3() {
  141. {
  142. bool errorThrown = false;
  143. try {
  144. new StreamReader((Stream)null, false);
  145. } catch (ArgumentNullException) {
  146. errorThrown = true;
  147. } catch (Exception e) {
  148. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  149. }
  150. Assert.IsTrue (errorThrown, "null stream error not thrown");
  151. }
  152. {
  153. bool errorThrown = false;
  154. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  155. try {
  156. StreamReader r = new StreamReader(f, false);
  157. r.Close();
  158. } catch (ArgumentException) {
  159. errorThrown = true;
  160. } catch (Exception e) {
  161. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  162. }
  163. f.Close();
  164. Assert.IsTrue (errorThrown, "no read error not thrown");
  165. }
  166. {
  167. // this is probably incestuous, but, oh well.
  168. FileStream f = new FileStream(_codeFileName,
  169. FileMode.Open,
  170. FileAccess.Read);
  171. StreamReader r = new StreamReader(f, false);
  172. Assert.IsNotNull (r, "no stream reader");
  173. r.Close();
  174. f.Close();
  175. }
  176. {
  177. bool errorThrown = false;
  178. try {
  179. StreamReader r = new StreamReader((Stream)null, true);
  180. } catch (ArgumentNullException) {
  181. errorThrown = true;
  182. } catch (Exception e) {
  183. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  184. }
  185. Assert.IsTrue (errorThrown, "null string error not thrown");
  186. }
  187. {
  188. bool errorThrown = false;
  189. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  190. try {
  191. StreamReader r = new StreamReader(f, true);
  192. r.Close();
  193. } catch (ArgumentException) {
  194. errorThrown = true;
  195. } catch (Exception e) {
  196. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  197. }
  198. f.Close();
  199. Assert.IsTrue (errorThrown, "no read error not thrown");
  200. }
  201. {
  202. // this is probably incestuous, but, oh well.
  203. FileStream f = new FileStream(_codeFileName,
  204. FileMode.Open,
  205. FileAccess.Read);
  206. StreamReader r = new StreamReader(f, true);
  207. Assert.IsNotNull (r, "no stream reader");
  208. r.Close();
  209. f.Close();
  210. }
  211. }
  212. [Test]
  213. public void TestCtor4() {
  214. {
  215. bool errorThrown = false;
  216. try {
  217. new StreamReader("", false);
  218. } catch (ArgumentException) {
  219. errorThrown = true;
  220. } catch (Exception e) {
  221. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  222. }
  223. Assert.IsTrue (errorThrown, "empty string error not thrown");
  224. }
  225. {
  226. bool errorThrown = false;
  227. try {
  228. new StreamReader((string)null, false);
  229. } catch (ArgumentNullException) {
  230. errorThrown = true;
  231. } catch (Exception e) {
  232. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  233. }
  234. Assert.IsTrue (errorThrown, "null string error not thrown");
  235. }
  236. {
  237. bool errorThrown = false;
  238. try {
  239. new StreamReader(TempFolder + "/nonexistentfile", false);
  240. } catch (FileNotFoundException) {
  241. errorThrown = true;
  242. } catch (Exception e) {
  243. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  244. }
  245. Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
  246. }
  247. {
  248. bool errorThrown = false;
  249. try {
  250. new StreamReader(TempFolder + "/nonexistentdir/file", false);
  251. } catch (DirectoryNotFoundException) {
  252. errorThrown = true;
  253. } catch (Exception e) {
  254. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  255. }
  256. Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
  257. }
  258. {
  259. bool errorThrown = false;
  260. try {
  261. new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
  262. } catch (IOException) {
  263. errorThrown = true;
  264. } catch (ArgumentException) {
  265. // FIXME - the spec says 'IOExc', but the
  266. // compiler says 'ArgExc'...
  267. errorThrown = true;
  268. } catch (Exception e) {
  269. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
  270. }
  271. Assert.IsTrue (errorThrown, "invalid filename error not thrown");
  272. }
  273. {
  274. // this is probably incestuous, but, oh well.
  275. StreamReader r = new StreamReader(_codeFileName, false);
  276. Assert.IsNotNull (r, "no stream reader");
  277. r.Close();
  278. }
  279. {
  280. bool errorThrown = false;
  281. try {
  282. new StreamReader("", true);
  283. } catch (ArgumentException) {
  284. errorThrown = true;
  285. } catch (Exception e) {
  286. Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString());
  287. }
  288. Assert.IsTrue (errorThrown, "empty string error not thrown");
  289. }
  290. {
  291. bool errorThrown = false;
  292. try {
  293. new StreamReader((string)null, true);
  294. } catch (ArgumentNullException) {
  295. errorThrown = true;
  296. } catch (Exception e) {
  297. Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString());
  298. }
  299. Assert.IsTrue (errorThrown, "null string error not thrown");
  300. }
  301. {
  302. bool errorThrown = false;
  303. try {
  304. new StreamReader(TempFolder + "/nonexistentfile", true);
  305. } catch (FileNotFoundException) {
  306. errorThrown = true;
  307. } catch (Exception e) {
  308. Assert.Fail ("Incorrect exception thrown at 8: " + e.ToString());
  309. }
  310. Assert.IsTrue (errorThrown, "fileNotFound error not thrown");
  311. }
  312. {
  313. bool errorThrown = false;
  314. try {
  315. new StreamReader(TempFolder + "/nonexistentdir/file", true);
  316. } catch (DirectoryNotFoundException) {
  317. errorThrown = true;
  318. } catch (Exception e) {
  319. Assert.Fail ("Incorrect exception thrown at 9: " + e.ToString());
  320. }
  321. Assert.IsTrue (errorThrown, "dirNotFound error not thrown");
  322. }
  323. {
  324. bool errorThrown = false;
  325. try {
  326. new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
  327. } catch (IOException) {
  328. errorThrown = true;
  329. } catch (ArgumentException) {
  330. // FIXME - the spec says 'IOExc', but the
  331. // compiler says 'ArgExc'...
  332. errorThrown = true;
  333. } catch (Exception e) {
  334. Assert.Fail ("Incorrect exception thrown at 10: " + e.ToString());
  335. }
  336. Assert.IsTrue (errorThrown, "invalid filename error not thrown");
  337. }
  338. {
  339. // this is probably incestuous, but, oh well.
  340. StreamReader r = new StreamReader(_codeFileName, true);
  341. Assert.IsNotNull (r, "no stream reader");
  342. r.Close();
  343. }
  344. }
  345. // TODO - Ctor with Encoding
  346. [Test]
  347. public void TestBaseStream() {
  348. Byte[] b = {};
  349. MemoryStream m = new MemoryStream(b);
  350. StreamReader r = new StreamReader(m);
  351. Assert.AreSame (m, r.BaseStream, "wrong base stream ");
  352. r.Close();
  353. m.Close();
  354. }
  355. public void TestCurrentEncoding() {
  356. Byte[] b = {};
  357. MemoryStream m = new MemoryStream(b);
  358. StreamReader r = new StreamReader(m);
  359. Assert.AreEqual (Encoding.UTF8.GetType (), r.CurrentEncoding.GetType (),
  360. "wrong encoding");
  361. }
  362. // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
  363. // TODO - DiscardBufferedData - I have no clue how to test this function.
  364. [Test]
  365. public void TestPeek() {
  366. // FIXME - how to get an IO Exception?
  367. Byte [] b;
  368. MemoryStream m;
  369. StreamReader r;
  370. try {
  371. b = new byte [0];
  372. m = new MemoryStream (b);
  373. r = new StreamReader(m);
  374. m.Close();
  375. int nothing = r.Peek();
  376. Assert.Fail ("#1");
  377. } catch (ObjectDisposedException) {
  378. }
  379. b = new byte [] {1, 2, 3, 4, 5, 6};
  380. m = new MemoryStream (b);
  381. r = new StreamReader(m);
  382. for (int i = 1; i <= 6; i++) {
  383. Assert.AreEqual (i, r.Peek(), "#2");
  384. r.Read();
  385. }
  386. Assert.AreEqual (-1, r.Peek(), "#3");
  387. }
  388. [Test]
  389. public void TestRead() {
  390. // FIXME - how to get an IO Exception?
  391. {
  392. bool errorThrown = false;
  393. try {
  394. Byte[] b = {};
  395. MemoryStream m = new MemoryStream(b);
  396. StreamReader r = new StreamReader(m);
  397. m.Close();
  398. int nothing = r.Read();
  399. } catch (ObjectDisposedException) {
  400. errorThrown = true;
  401. } catch (Exception e) {
  402. Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString());
  403. }
  404. Assert.IsTrue (errorThrown, "nothing-to-read error not thrown");
  405. }
  406. {
  407. Byte[] b = {1, 2, 3, 4, 5, 6};
  408. MemoryStream m = new MemoryStream(b);
  409. StreamReader r = new StreamReader(m);
  410. for (int i = 1; i <= 6; i++) {
  411. Assert.AreEqual (i, r.Read (), "read incorrect");
  412. }
  413. Assert.AreEqual (-1, r.Read (), "Should be none left");
  414. }
  415. {
  416. bool errorThrown = false;
  417. try {
  418. Byte[] b = {};
  419. StreamReader r = new StreamReader(new MemoryStream(b));
  420. r.Read(null, 0, 0);
  421. } catch (ArgumentNullException) {
  422. errorThrown = true;
  423. } catch (ArgumentException) {
  424. errorThrown = true;
  425. } catch (Exception e) {
  426. Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString());
  427. }
  428. Assert.IsTrue (errorThrown, "null buffer error not thrown");
  429. }
  430. {
  431. bool errorThrown = false;
  432. try {
  433. Byte[] b = {};
  434. StreamReader r = new StreamReader(new MemoryStream(b));
  435. Char[] c = new Char[1];
  436. r.Read(c, 0, 2);
  437. } catch (ArgumentException) {
  438. errorThrown = true;
  439. } catch (Exception e) {
  440. Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString());
  441. }
  442. Assert.IsTrue (errorThrown, "too-long range error not thrown");
  443. }
  444. {
  445. bool errorThrown = false;
  446. try {
  447. Byte[] b = {};
  448. StreamReader r = new StreamReader(new MemoryStream(b));
  449. Char[] c = new Char[1];
  450. r.Read(c, -1, 2);
  451. } catch (ArgumentOutOfRangeException) {
  452. errorThrown = true;
  453. } catch (Exception e) {
  454. Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString());
  455. }
  456. Assert.IsTrue (errorThrown, "out of range error not thrown");
  457. }
  458. {
  459. bool errorThrown = false;
  460. try {
  461. Byte[] b = {};
  462. StreamReader r = new StreamReader(new MemoryStream(b));
  463. Char[] c = new Char[1];
  464. r.Read(c, 0, -1);
  465. } catch (ArgumentOutOfRangeException) {
  466. errorThrown = true;
  467. } catch (Exception e) {
  468. Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString());
  469. }
  470. Assert.IsTrue (errorThrown, "out of range error not thrown");
  471. }
  472. {
  473. int ii = 1;
  474. try {
  475. Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
  476. (byte)'d', (byte)'e', (byte)'f',
  477. (byte)'g'};
  478. MemoryStream m = new MemoryStream(b);
  479. ii++;
  480. StreamReader r = new StreamReader(m);
  481. ii++;
  482. char[] buffer = new Char[7];
  483. ii++;
  484. char[] target = {'g','d','e','f','b','c','a'};
  485. ii++;
  486. r.Read(buffer, 6, 1);
  487. ii++;
  488. r.Read(buffer, 4, 2);
  489. ii++;
  490. r.Read(buffer, 1, 3);
  491. ii++;
  492. r.Read(buffer, 0, 1);
  493. ii++;
  494. for (int i = 0; i < target.Length; i++) {
  495. Assert.AreEqual (target[i], buffer[i], "read no work");
  496. i++;
  497. }
  498. } catch (Exception e) {
  499. Assert.Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
  500. }
  501. }
  502. }
  503. [Test]
  504. public void TestReadLine() {
  505. // TODO Out Of Memory Exc? IO Exc?
  506. Byte[] b = new Byte[8];
  507. b[0] = (byte)'a';
  508. b[1] = (byte)'\n';
  509. b[2] = (byte)'b';
  510. b[3] = (byte)'\n';
  511. b[4] = (byte)'c';
  512. b[5] = (byte)'\n';
  513. b[6] = (byte)'d';
  514. b[7] = (byte)'\n';
  515. MemoryStream m = new MemoryStream(b);
  516. StreamReader r = new StreamReader(m);
  517. Assert.AreEqual ("a", r.ReadLine(), "#1");
  518. Assert.AreEqual ("b", r.ReadLine (), "#2");
  519. Assert.AreEqual ("c", r.ReadLine (), "#3");
  520. Assert.AreEqual ("d", r.ReadLine(), "#4");
  521. Assert.IsNull (r.ReadLine (), "#5");
  522. }
  523. [Test]
  524. public void ReadLine1() {
  525. Byte[] b = new Byte[10];
  526. b[0] = (byte)'a';
  527. b[1] = (byte)'\r';
  528. b[2] = (byte)'b';
  529. b[3] = (byte)'\n';
  530. b[4] = (byte)'c';
  531. b[5] = (byte)'\n';
  532. b[5] = (byte)'\r';
  533. b[6] = (byte)'d';
  534. b[7] = (byte)'\n';
  535. b[8] = (byte)'\r';
  536. b[9] = (byte)'\n';
  537. MemoryStream m = new MemoryStream(b);
  538. StreamReader r = new StreamReader(m);
  539. Assert.AreEqual ("a", r.ReadLine (), "#1");
  540. Assert.AreEqual ("b", r.ReadLine (), "#2");
  541. Assert.AreEqual ("c", r.ReadLine (), "#3");
  542. Assert.AreEqual ("d", r.ReadLine (), "#4");
  543. Assert.AreEqual (string.Empty, r.ReadLine (), "#5");
  544. Assert.IsNull (r.ReadLine(), "#6");
  545. }
  546. [Test]
  547. public void ReadLine2() {
  548. Byte[] b = new Byte[10];
  549. b[0] = (byte)'\r';
  550. b[1] = (byte)'\r';
  551. b[2] = (byte)'\n';
  552. b[3] = (byte)'\n';
  553. b[4] = (byte)'c';
  554. b[5] = (byte)'\n';
  555. b[5] = (byte)'\r';
  556. b[6] = (byte)'d';
  557. b[7] = (byte)'\n';
  558. b[8] = (byte)'\r';
  559. b[9] = (byte)'\n';
  560. MemoryStream m = new MemoryStream(b);
  561. StreamReader r = new StreamReader(m);
  562. Assert.AreEqual (string.Empty, r.ReadLine (), "#1");
  563. Assert.AreEqual (string.Empty, r.ReadLine (), "#2");
  564. Assert.AreEqual (string.Empty, r.ReadLine (), "#3");
  565. Assert.AreEqual ("c", r.ReadLine (), "#4");
  566. Assert.AreEqual ("d", r.ReadLine (), "#5");
  567. Assert.AreEqual (string.Empty, r.ReadLine (), "#6");
  568. Assert.IsNull (r.ReadLine (), "#7");
  569. }
  570. [Test]
  571. public void ReadLine3() {
  572. StringBuilder sb = new StringBuilder ();
  573. sb.Append (new string ('1', 32767));
  574. sb.Append ('\r');
  575. sb.Append ('\n');
  576. sb.Append ("Hola\n");
  577. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  578. MemoryStream m = new MemoryStream(bytes);
  579. StreamReader r = new StreamReader(m);
  580. Assert.AreEqual (new string ('1', 32767), r.ReadLine(), "#1");
  581. Assert.AreEqual ("Hola", r.ReadLine (), "#2");
  582. Assert.IsNull (r.ReadLine (), "#3");
  583. }
  584. [Test]
  585. public void ReadLine4() {
  586. StringBuilder sb = new StringBuilder ();
  587. sb.Append (new string ('1', 32767));
  588. sb.Append ('\r');
  589. sb.Append ('\n');
  590. sb.Append ("Hola\n");
  591. sb.Append (sb.ToString ());
  592. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  593. MemoryStream m = new MemoryStream(bytes);
  594. StreamReader r = new StreamReader(m);
  595. Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#1");
  596. Assert.AreEqual ("Hola", r.ReadLine (), "#2");
  597. Assert.AreEqual (new string ('1', 32767), r.ReadLine (), "#3");
  598. Assert.AreEqual ("Hola", r.ReadLine (), "#4");
  599. Assert.IsNull (r.ReadLine (), "#5");
  600. }
  601. [Test]
  602. public void ReadLine5() {
  603. StringBuilder sb = new StringBuilder ();
  604. sb.Append (new string ('1', 32768));
  605. sb.Append ('\r');
  606. sb.Append ('\n');
  607. sb.Append ("Hola\n");
  608. byte [] bytes = Encoding.Default.GetBytes (sb.ToString ());
  609. MemoryStream m = new MemoryStream(bytes);
  610. StreamReader r = new StreamReader(m);
  611. Assert.AreEqual (new string ('1', 32768), r.ReadLine (), "#1");
  612. Assert.AreEqual ("Hola", r.ReadLine (), "#2");
  613. Assert.IsNull (r.ReadLine (), "#3");
  614. }
  615. public void TestReadToEnd() {
  616. // TODO Out Of Memory Exc? IO Exc?
  617. Byte[] b = new Byte[8];
  618. b[0] = (byte)'a';
  619. b[1] = (byte)'\n';
  620. b[2] = (byte)'b';
  621. b[3] = (byte)'\n';
  622. b[4] = (byte)'c';
  623. b[5] = (byte)'\n';
  624. b[6] = (byte)'d';
  625. b[7] = (byte)'\n';
  626. MemoryStream m = new MemoryStream(b);
  627. StreamReader r = new StreamReader(m);
  628. Assert.AreEqual ("a\nb\nc\nd\n", r.ReadToEnd (), "#1");
  629. Assert.AreEqual (string.Empty, r.ReadToEnd (), "#2");
  630. }
  631. [Test]
  632. public void TestBaseStreamClosed ()
  633. {
  634. byte [] b = {};
  635. MemoryStream m = new MemoryStream (b);
  636. StreamReader r = new StreamReader (m);
  637. m.Close ();
  638. try {
  639. r.Peek ();
  640. Assert.Fail ();
  641. } catch (ObjectDisposedException) {
  642. }
  643. }
  644. [Test]
  645. [ExpectedException (typeof (ArgumentNullException))]
  646. public void Contructor_Stream_NullEncoding ()
  647. {
  648. new StreamReader (new MemoryStream (), null);
  649. }
  650. [Test]
  651. [ExpectedException (typeof (ArgumentNullException))]
  652. public void Contructor_Path_NullEncoding ()
  653. {
  654. new StreamReader (_codeFileName, null);
  655. }
  656. [Test]
  657. [ExpectedException (typeof (ArgumentNullException))]
  658. public void Read_Null ()
  659. {
  660. StreamReader r = new StreamReader (new MemoryStream ());
  661. r.Read (null, 0, 0);
  662. }
  663. [Test]
  664. [ExpectedException (typeof (ArgumentException))]
  665. public void Read_IndexOverflow ()
  666. {
  667. char[] array = new char [16];
  668. StreamReader r = new StreamReader (new MemoryStream (16));
  669. r.Read (array, 1, Int32.MaxValue);
  670. }
  671. [Test]
  672. [ExpectedException (typeof (ArgumentException))]
  673. public void Read_CountOverflow ()
  674. {
  675. char[] array = new char [16];
  676. StreamReader r = new StreamReader (new MemoryStream (16));
  677. r.Read (array, Int32.MaxValue, 1);
  678. }
  679. [Test]
  680. public void Read_DoesntStopAtLineEndings ()
  681. {
  682. MemoryStream ms = new MemoryStream (Encoding.ASCII.GetBytes ("Line1\rLine2\r\nLine3\nLine4"));
  683. StreamReader reader = new StreamReader (ms);
  684. Assert.AreEqual (24, reader.Read (new char[24], 0, 24));
  685. }
  686. [Test]
  687. public void EncodingDetection()
  688. {
  689. if (!CheckEncodingDetected(Encoding.UTF8))
  690. Assert.Fail ("Failed to detect UTF8 encoded string");
  691. if (!CheckEncodingDetected(Encoding.Unicode))
  692. Assert.Fail ("Failed to detect UTF16LE encoded string");
  693. if (!CheckEncodingDetected(Encoding.BigEndianUnicode))
  694. Assert.Fail ("Failed to detect UTF16BE encoded string");
  695. if (!CheckEncodingDetected(Encoding.UTF32))
  696. Assert.Fail ("Failed to detect UTF32LE encoded string");
  697. if (!CheckEncodingDetected(new UTF32Encoding(true, true)))
  698. Assert.Fail ("Failed to detect UTF32BE encoded string");
  699. }
  700. // This is a special case, where the StreamReader has less than 4 bytes at
  701. // encoding detection time, so it tries to check for Unicode encoding, instead of
  702. // waiting for more bytes to test against the UTF32 BOM.
  703. [Test]
  704. public void EncodingDetectionUnicode ()
  705. {
  706. byte [] bytes = new byte [3];
  707. bytes [0] = 0xff;
  708. bytes [1] = 0xfe;
  709. bytes [2] = 0;
  710. MemoryStream inStream = new MemoryStream (bytes);
  711. StreamReader reader = new StreamReader (inStream, Encoding.UTF8, true);
  712. // It should start with the encoding we used in the .ctor
  713. Assert.AreEqual (Encoding.UTF8, reader.CurrentEncoding, "#A1");
  714. reader.Read ();
  715. //reader.Read ();
  716. Assert.AreEqual (Encoding.Unicode, reader.CurrentEncoding, "#B1");
  717. reader.Close ();
  718. }
  719. private bool CheckEncodingDetected(Encoding encoding)
  720. {
  721. MemoryStream outStream = new MemoryStream();
  722. using (StreamWriter outWriter = new StreamWriter(outStream, encoding))
  723. {
  724. outWriter.Write(TestString);
  725. }
  726. byte[] testBytes = outStream.ToArray();
  727. StreamReader inReader = new StreamReader(new MemoryStream(testBytes, false));
  728. string decodedString = inReader.ReadToEnd();
  729. return decodedString == TestString;
  730. }
  731. [Test] // Bug445326
  732. [Category ("MobileNotWorking")]
  733. public void EndOfBufferIsCR ()
  734. {
  735. using (StreamReader reader = new StreamReader ("Test/resources/Fergie.GED")) {
  736. string line;
  737. int count = 0;
  738. while ((line = reader.ReadLine ()) != null) {
  739. Assert.IsFalse (line.Length > 1000, "#1 " + count);
  740. count++;
  741. }
  742. Assert.AreEqual (16107, count, "#2");
  743. }
  744. }
  745. [Test]
  746. public void bug75526 ()
  747. {
  748. StreamReader sr = new StreamReader (new Bug75526Stream ());
  749. int len = sr.Read (new char [10], 0, 10);
  750. Assert.AreEqual (2, len);
  751. }
  752. class Bug75526Stream : MemoryStream
  753. {
  754. public override int Read (byte [] buffer, int offset, int count)
  755. {
  756. buffer [offset + 0] = (byte) 'a';
  757. buffer [offset + 1] = (byte) 'b';
  758. return 2;
  759. }
  760. }
  761. [Test]
  762. public void PeekWhileBlocking ()
  763. {
  764. StreamReader reader = new StreamReader (new MyStream ());
  765. int c = reader.Read ();
  766. Assert.IsFalse (reader.EndOfStream);
  767. string str = reader.ReadToEnd ();
  768. Assert.AreEqual ("bc", str);
  769. }
  770. [Test]
  771. public void EncodingChangedAuto ()
  772. {
  773. int testlines = 2048; // all data should larger than stream reader default buffer size
  774. string testdata = "test";
  775. MemoryStream ms = new MemoryStream();
  776. // write utf8 encoding data.
  777. using (StreamWriter sw = new StreamWriter (ms, Encoding.UTF8)) {
  778. for (int i = 0; i < testlines; i++)
  779. sw.WriteLine (testdata);
  780. }
  781. MemoryStream readms = new MemoryStream (ms.GetBuffer());
  782. using (StreamReader sr = new StreamReader(readms, Encoding.Unicode, true)) {
  783. for (int i = 0; i < testlines; i++) {
  784. string line = sr.ReadLine ();
  785. if (line != testdata)
  786. Assert.Fail ("Wrong line content");
  787. }
  788. }
  789. }
  790. #if NET_4_5
  791. [Test]
  792. public void ReadLineAsync ()
  793. {
  794. MemoryStream ms = new MemoryStream ();
  795. StreamWriter sw = new StreamWriter (ms, Encoding.UTF8);
  796. sw.WriteLine ("a");
  797. sw.WriteLine ("b");
  798. sw.Flush ();
  799. ms.Seek (0, SeekOrigin.Begin);
  800. Func<Task<string>> res = async () => {
  801. using (StreamReader reader = new StreamReader (ms)) {
  802. return await reader.ReadLineAsync () + await reader.ReadToEndAsync () + await reader.ReadToEndAsync ();
  803. }
  804. };
  805. var result = res ();
  806. Assert.IsTrue (result.Wait (3000), "#1");
  807. Assert.AreEqual ("ab" + Environment.NewLine, result.Result);
  808. }
  809. #endif
  810. }
  811. class MyStream : Stream {
  812. int n;
  813. public override int Read (byte [] buffer, int offset, int size)
  814. {
  815. if (n == 0) {
  816. buffer [offset] = (byte) 'a';
  817. n++;
  818. return 1;
  819. } else if (n == 1) {
  820. buffer [offset] = (byte) 'b';
  821. buffer [offset + 1] = (byte) 'c';
  822. n++;
  823. return 2;
  824. }
  825. return 0;
  826. }
  827. public override bool CanRead {
  828. get { return true; }
  829. }
  830. public override bool CanSeek {
  831. get { return false; }
  832. }
  833. public override bool CanWrite {
  834. get { return false; }
  835. }
  836. public override long Length {
  837. get { return 0; }
  838. }
  839. public override long Position {
  840. get { return 0; }
  841. set { }
  842. }
  843. public override void Flush ()
  844. {
  845. }
  846. public override long Seek (long offset, SeekOrigin origin)
  847. {
  848. return 0;
  849. }
  850. public override void SetLength (long value)
  851. {
  852. }
  853. public override void Write (byte[] buffer, int offset, int count)
  854. {
  855. }
  856. }
  857. }