StreamReaderTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. public void TestReadToEnd() {
  535. // TODO Out Of Memory Exc? IO Exc?
  536. Byte[] b = new Byte[8];
  537. b[0] = (byte)'a';
  538. b[1] = (byte)'\n';
  539. b[2] = (byte)'b';
  540. b[3] = (byte)'\n';
  541. b[4] = (byte)'c';
  542. b[5] = (byte)'\n';
  543. b[6] = (byte)'d';
  544. b[7] = (byte)'\n';
  545. MemoryStream m = new MemoryStream(b);
  546. StreamReader r = new StreamReader(m);
  547. AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
  548. AssertEquals("line doesn't match", "", r.ReadToEnd());
  549. }
  550. [Test]
  551. public void TestBaseStreamClosed ()
  552. {
  553. byte [] b = {};
  554. MemoryStream m = new MemoryStream (b);
  555. StreamReader r = new StreamReader (m);
  556. m.Close ();
  557. bool thrown = false;
  558. try {
  559. r.Peek ();
  560. } catch (ObjectDisposedException) {
  561. thrown = true;
  562. }
  563. AssertEquals ("#01", true, thrown);
  564. }
  565. [Test]
  566. [ExpectedException (typeof (ArgumentNullException))]
  567. public void Contructor_Stream_NullEncoding ()
  568. {
  569. StreamReader r = new StreamReader (new MemoryStream (), null);
  570. }
  571. [Test]
  572. [ExpectedException (typeof (ArgumentNullException))]
  573. public void Contructor_Path_NullEncoding ()
  574. {
  575. StreamReader r = new StreamReader (_codeFileName, null);
  576. }
  577. [Test]
  578. [ExpectedException (typeof (ArgumentNullException))]
  579. public void Read_Null ()
  580. {
  581. StreamReader r = new StreamReader (new MemoryStream ());
  582. r.Read (null, 0, 0);
  583. }
  584. [Test]
  585. [ExpectedException (typeof (ArgumentException))]
  586. public void Read_IndexOverflow ()
  587. {
  588. char[] array = new char [16];
  589. StreamReader r = new StreamReader (new MemoryStream (16));
  590. r.Read (array, 1, Int32.MaxValue);
  591. }
  592. [Test]
  593. [ExpectedException (typeof (ArgumentException))]
  594. public void Read_CountOverflow ()
  595. {
  596. char[] array = new char [16];
  597. StreamReader r = new StreamReader (new MemoryStream (16));
  598. r.Read (array, Int32.MaxValue, 1);
  599. }
  600. }
  601. }