StreamReaderTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace MonoTests.System.IO
  12. {
  13. [TestFixture]
  14. public class StreamReaderTest : Assertion
  15. {
  16. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  17. private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  18. [SetUp]
  19. public void SetUp ()
  20. {
  21. if (!Directory.Exists (TempFolder))
  22. Directory.CreateDirectory (TempFolder);
  23. if (!File.Exists (_codeFileName))
  24. File.Create (_codeFileName).Close ();
  25. }
  26. [TearDown]
  27. public void TearDown ()
  28. {
  29. if (Directory.Exists (TempFolder))
  30. Directory.Delete (TempFolder, true);
  31. }
  32. [Test]
  33. public void TestCtor1() {
  34. {
  35. bool errorThrown = false;
  36. try {
  37. StreamReader r = new StreamReader((Stream)null);
  38. } catch (ArgumentNullException) {
  39. errorThrown = true;
  40. }
  41. Assert("null string error not thrown", errorThrown);
  42. }
  43. {
  44. bool errorThrown = false;
  45. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  46. try {
  47. StreamReader r = new StreamReader(f);
  48. r.Close();
  49. } catch (ArgumentException) {
  50. errorThrown = true;
  51. }
  52. f.Close();
  53. Assert("no read error not thrown", errorThrown);
  54. }
  55. {
  56. // this is probably incestuous, but, oh well.
  57. FileStream f = new FileStream(_codeFileName,
  58. FileMode.Open,
  59. FileAccess.Read);
  60. StreamReader r = new StreamReader(f);
  61. AssertNotNull("no stream reader", r);
  62. r.Close();
  63. f.Close();
  64. }
  65. }
  66. [Test]
  67. public void TestCtor2() {
  68. {
  69. bool errorThrown = false;
  70. try {
  71. StreamReader r = new StreamReader("");
  72. } catch (ArgumentException) {
  73. errorThrown = true;
  74. } catch (Exception e) {
  75. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  76. }
  77. Assert("empty string error not thrown", errorThrown);
  78. }
  79. {
  80. bool errorThrown = false;
  81. try {
  82. StreamReader r = new StreamReader((string)null);
  83. } catch (ArgumentNullException) {
  84. errorThrown = true;
  85. } catch (Exception e) {
  86. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  87. }
  88. Assert("null string error not thrown", errorThrown);
  89. }
  90. {
  91. bool errorThrown = false;
  92. try {
  93. StreamReader r = new StreamReader("nonexistentfile");
  94. } catch (FileNotFoundException) {
  95. errorThrown = true;
  96. } catch (Exception e) {
  97. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  98. }
  99. Assert("fileNotFound error not thrown", errorThrown);
  100. }
  101. {
  102. bool errorThrown = false;
  103. try {
  104. StreamReader r = new StreamReader("nonexistentdir/file");
  105. } catch (DirectoryNotFoundException) {
  106. errorThrown = true;
  107. } catch (Exception e) {
  108. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  109. }
  110. Assert("dirNotFound error not thrown", errorThrown);
  111. }
  112. {
  113. bool errorThrown = false;
  114. try {
  115. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  116. } catch (IOException) {
  117. errorThrown = true;
  118. } catch (ArgumentException) {
  119. // FIXME - the spec says 'IOExc', but the
  120. // compiler says 'ArgExc'...
  121. errorThrown = true;
  122. } catch (Exception e) {
  123. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  124. }
  125. Assert("invalid filename error not thrown", errorThrown);
  126. }
  127. {
  128. // this is probably incestuous, but, oh well.
  129. StreamReader r = new StreamReader(_codeFileName);
  130. AssertNotNull("no stream reader", r);
  131. r.Close();
  132. }
  133. }
  134. [Test]
  135. public void TestCtor3() {
  136. {
  137. bool errorThrown = false;
  138. try {
  139. StreamReader r = new StreamReader((Stream)null, false);
  140. } catch (ArgumentNullException) {
  141. errorThrown = true;
  142. } catch (Exception e) {
  143. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  144. }
  145. Assert("null stream error not thrown", errorThrown);
  146. }
  147. {
  148. bool errorThrown = false;
  149. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  150. try {
  151. StreamReader r = new StreamReader(f, false);
  152. r.Close();
  153. } catch (ArgumentException) {
  154. errorThrown = true;
  155. } catch (Exception e) {
  156. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  157. }
  158. f.Close();
  159. Assert("no read error not thrown", errorThrown);
  160. }
  161. {
  162. // this is probably incestuous, but, oh well.
  163. FileStream f = new FileStream(_codeFileName,
  164. FileMode.Open,
  165. FileAccess.Read);
  166. StreamReader r = new StreamReader(f, false);
  167. AssertNotNull("no stream reader", r);
  168. r.Close();
  169. f.Close();
  170. }
  171. {
  172. bool errorThrown = false;
  173. try {
  174. StreamReader r = new StreamReader((Stream)null, true);
  175. } catch (ArgumentNullException) {
  176. errorThrown = true;
  177. } catch (Exception e) {
  178. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  179. }
  180. Assert("null string error not thrown", errorThrown);
  181. }
  182. {
  183. bool errorThrown = false;
  184. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  185. try {
  186. StreamReader r = new StreamReader(f, true);
  187. r.Close();
  188. } catch (ArgumentException) {
  189. errorThrown = true;
  190. } catch (Exception e) {
  191. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  192. }
  193. f.Close();
  194. Assert("no read error not thrown", errorThrown);
  195. }
  196. {
  197. // this is probably incestuous, but, oh well.
  198. FileStream f = new FileStream(_codeFileName,
  199. FileMode.Open,
  200. FileAccess.Read);
  201. StreamReader r = new StreamReader(f, true);
  202. AssertNotNull("no stream reader", r);
  203. r.Close();
  204. f.Close();
  205. }
  206. }
  207. [Test]
  208. public void TestCtor4() {
  209. {
  210. bool errorThrown = false;
  211. try {
  212. StreamReader r = new StreamReader("", false);
  213. } catch (ArgumentException) {
  214. errorThrown = true;
  215. } catch (Exception e) {
  216. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  217. }
  218. Assert("empty string error not thrown", errorThrown);
  219. }
  220. {
  221. bool errorThrown = false;
  222. try {
  223. StreamReader r = new StreamReader((string)null, false);
  224. } catch (ArgumentNullException) {
  225. errorThrown = true;
  226. } catch (Exception e) {
  227. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  228. }
  229. Assert("null string error not thrown", errorThrown);
  230. }
  231. {
  232. bool errorThrown = false;
  233. try {
  234. StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", false);
  235. } catch (FileNotFoundException) {
  236. errorThrown = true;
  237. } catch (Exception e) {
  238. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  239. }
  240. Assert("fileNotFound error not thrown", errorThrown);
  241. }
  242. {
  243. bool errorThrown = false;
  244. try {
  245. StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", false);
  246. } catch (DirectoryNotFoundException) {
  247. errorThrown = true;
  248. } catch (Exception e) {
  249. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  250. }
  251. Assert("dirNotFound error not thrown", errorThrown);
  252. }
  253. {
  254. bool errorThrown = false;
  255. try {
  256. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
  257. } catch (IOException) {
  258. errorThrown = true;
  259. } catch (ArgumentException) {
  260. // FIXME - the spec says 'IOExc', but the
  261. // compiler says 'ArgExc'...
  262. errorThrown = true;
  263. } catch (Exception e) {
  264. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  265. }
  266. Assert("invalid filename error not thrown", errorThrown);
  267. }
  268. {
  269. // this is probably incestuous, but, oh well.
  270. StreamReader r = new StreamReader(_codeFileName, false);
  271. AssertNotNull("no stream reader", r);
  272. r.Close();
  273. }
  274. {
  275. bool errorThrown = false;
  276. try {
  277. StreamReader r = new StreamReader("", true);
  278. } catch (ArgumentException) {
  279. errorThrown = true;
  280. } catch (Exception e) {
  281. Fail ("Incorrect exception thrown at 6: " + e.ToString());
  282. }
  283. Assert("empty string error not thrown", errorThrown);
  284. }
  285. {
  286. bool errorThrown = false;
  287. try {
  288. StreamReader r = new StreamReader((string)null, true);
  289. } catch (ArgumentNullException) {
  290. errorThrown = true;
  291. } catch (Exception e) {
  292. Fail ("Incorrect exception thrown at 7: " + e.ToString());
  293. }
  294. Assert("null string error not thrown", errorThrown);
  295. }
  296. {
  297. bool errorThrown = false;
  298. try {
  299. StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", true);
  300. } catch (FileNotFoundException) {
  301. errorThrown = true;
  302. } catch (Exception e) {
  303. Fail ("Incorrect exception thrown at 8: " + e.ToString());
  304. }
  305. Assert("fileNotFound error not thrown", errorThrown);
  306. }
  307. {
  308. bool errorThrown = false;
  309. try {
  310. StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", true);
  311. } catch (DirectoryNotFoundException) {
  312. errorThrown = true;
  313. } catch (Exception e) {
  314. Fail ("Incorrect exception thrown at 9: " + e.ToString());
  315. }
  316. Assert("dirNotFound error not thrown", errorThrown);
  317. }
  318. {
  319. bool errorThrown = false;
  320. try {
  321. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
  322. } catch (IOException) {
  323. errorThrown = true;
  324. } catch (ArgumentException) {
  325. // FIXME - the spec says 'IOExc', but the
  326. // compiler says 'ArgExc'...
  327. errorThrown = true;
  328. } catch (Exception e) {
  329. Fail ("Incorrect exception thrown at 10: " + e.ToString());
  330. }
  331. Assert("invalid filename error not thrown", errorThrown);
  332. }
  333. {
  334. // this is probably incestuous, but, oh well.
  335. StreamReader r = new StreamReader(_codeFileName, true);
  336. AssertNotNull("no stream reader", r);
  337. r.Close();
  338. }
  339. }
  340. // TODO - Ctor with Encoding
  341. [Test]
  342. public void TestBaseStream() {
  343. string progress = "beginning";
  344. try {
  345. Byte[] b = {};
  346. MemoryStream m = new MemoryStream(b);
  347. StreamReader r = new StreamReader(m);
  348. AssertEquals("wrong base stream ", m, r.BaseStream);
  349. progress = "Closing StreamReader";
  350. r.Close();
  351. progress = "Closing MemoryStream";
  352. m.Close();
  353. } catch (Exception e) {
  354. Fail ("At '" + progress + "' an unexpected exception was thrown: " + e.ToString());
  355. }
  356. }
  357. public void TestCurrentEncoding() {
  358. try {
  359. Byte[] b = {};
  360. MemoryStream m = new MemoryStream(b);
  361. StreamReader r = new StreamReader(m);
  362. AssertEquals("wrong encoding",
  363. Encoding.UTF8.GetType (), r.CurrentEncoding.GetType ());
  364. } catch (Exception e) {
  365. Fail ("Unexpected exception thrown: " + e.ToString());
  366. }
  367. }
  368. // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
  369. // TODO - DiscardBufferedData - I have no clue how to test this function.
  370. [Test]
  371. public void TestPeek() {
  372. // FIXME - how to get an IO Exception?
  373. {
  374. bool errorThrown = false;
  375. try {
  376. Byte[] b = {};
  377. MemoryStream m = new MemoryStream(b);
  378. StreamReader r = new StreamReader(m);
  379. m.Close();
  380. int nothing = r.Peek();
  381. } catch (ObjectDisposedException) {
  382. errorThrown = true;
  383. }
  384. Assert("nothing-to-peek-at error not thrown", errorThrown);
  385. }
  386. {
  387. Byte[] b = {1, 2, 3, 4, 5, 6};
  388. MemoryStream m = new MemoryStream(b);
  389. StreamReader r = new StreamReader(m);
  390. for (int i = 1; i <= 6; i++) {
  391. AssertEquals("peek incorrect", i, r.Peek());
  392. r.Read();
  393. }
  394. AssertEquals("should be none left", -1, r.Peek());
  395. }
  396. }
  397. [Test]
  398. public void TestRead() {
  399. // FIXME - how to get an IO Exception?
  400. {
  401. bool errorThrown = false;
  402. try {
  403. Byte[] b = {};
  404. MemoryStream m = new MemoryStream(b);
  405. StreamReader r = new StreamReader(m);
  406. m.Close();
  407. int nothing = r.Read();
  408. } catch (ObjectDisposedException) {
  409. errorThrown = true;
  410. } catch (Exception e) {
  411. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  412. }
  413. Assert("nothing-to-read error not thrown", errorThrown);
  414. }
  415. {
  416. Byte[] b = {1, 2, 3, 4, 5, 6};
  417. MemoryStream m = new MemoryStream(b);
  418. StreamReader r = new StreamReader(m);
  419. for (int i = 1; i <= 6; i++) {
  420. AssertEquals("read incorrect", i, r.Read());
  421. }
  422. AssertEquals("Should be none left", -1, r.Read());
  423. }
  424. {
  425. bool errorThrown = false;
  426. try {
  427. Byte[] b = {};
  428. StreamReader r = new StreamReader(new MemoryStream(b));
  429. r.Read(null, 0, 0);
  430. } catch (ArgumentNullException) {
  431. errorThrown = true;
  432. } catch (ArgumentException) {
  433. errorThrown = true;
  434. } catch (Exception e) {
  435. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  436. }
  437. Assert("null buffer error not thrown", errorThrown);
  438. }
  439. {
  440. bool errorThrown = false;
  441. try {
  442. Byte[] b = {};
  443. StreamReader r = new StreamReader(new MemoryStream(b));
  444. Char[] c = new Char[1];
  445. r.Read(c, 0, 2);
  446. } catch (ArgumentException) {
  447. errorThrown = true;
  448. } catch (Exception e) {
  449. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  450. }
  451. Assert("too-long range error not thrown", errorThrown);
  452. }
  453. {
  454. bool errorThrown = false;
  455. try {
  456. Byte[] b = {};
  457. StreamReader r = new StreamReader(new MemoryStream(b));
  458. Char[] c = new Char[1];
  459. r.Read(c, -1, 2);
  460. } catch (ArgumentOutOfRangeException) {
  461. errorThrown = true;
  462. } catch (Exception e) {
  463. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  464. }
  465. Assert("out of range error not thrown", errorThrown);
  466. }
  467. {
  468. bool errorThrown = false;
  469. try {
  470. Byte[] b = {};
  471. StreamReader r = new StreamReader(new MemoryStream(b));
  472. Char[] c = new Char[1];
  473. r.Read(c, 0, -1);
  474. } catch (ArgumentOutOfRangeException) {
  475. errorThrown = true;
  476. } catch (Exception e) {
  477. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  478. }
  479. Assert("out of range error not thrown", errorThrown);
  480. }
  481. {
  482. int ii = 1;
  483. try {
  484. Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
  485. (byte)'d', (byte)'e', (byte)'f',
  486. (byte)'g'};
  487. MemoryStream m = new MemoryStream(b);
  488. ii++;
  489. StreamReader r = new StreamReader(m);
  490. ii++;
  491. char[] buffer = new Char[7];
  492. ii++;
  493. char[] target = {'g','d','e','f','b','c','a'};
  494. ii++;
  495. r.Read(buffer, 6, 1);
  496. ii++;
  497. r.Read(buffer, 4, 2);
  498. ii++;
  499. r.Read(buffer, 1, 3);
  500. ii++;
  501. r.Read(buffer, 0, 1);
  502. ii++;
  503. for (int i = 0; i < target.Length; i++) {
  504. AssertEquals("read no work",
  505. target[i], buffer[i]);
  506. i++;
  507. }
  508. } catch (Exception e) {
  509. Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
  510. }
  511. }
  512. }
  513. [Test]
  514. public void TestReadLine() {
  515. // TODO Out Of Memory Exc? IO Exc?
  516. Byte[] b = new Byte[8];
  517. b[0] = (byte)'a';
  518. b[1] = (byte)'\n';
  519. b[2] = (byte)'b';
  520. b[3] = (byte)'\n';
  521. b[4] = (byte)'c';
  522. b[5] = (byte)'\n';
  523. b[6] = (byte)'d';
  524. b[7] = (byte)'\n';
  525. MemoryStream m = new MemoryStream(b);
  526. StreamReader r = new StreamReader(m);
  527. AssertEquals("line doesn't match", "a", r.ReadLine());
  528. AssertEquals("line doesn't match", "b", r.ReadLine());
  529. AssertEquals("line doesn't match", "c", r.ReadLine());
  530. AssertEquals("line doesn't match", "d", r.ReadLine());
  531. AssertEquals("line doesn't match", null, r.ReadLine());
  532. }
  533. public void TestReadToEnd() {
  534. // TODO Out Of Memory Exc? IO Exc?
  535. Byte[] b = new Byte[8];
  536. b[0] = (byte)'a';
  537. b[1] = (byte)'\n';
  538. b[2] = (byte)'b';
  539. b[3] = (byte)'\n';
  540. b[4] = (byte)'c';
  541. b[5] = (byte)'\n';
  542. b[6] = (byte)'d';
  543. b[7] = (byte)'\n';
  544. MemoryStream m = new MemoryStream(b);
  545. StreamReader r = new StreamReader(m);
  546. AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
  547. AssertEquals("line doesn't match", "", r.ReadToEnd());
  548. }
  549. [Test]
  550. public void TestBaseStreamClosed ()
  551. {
  552. byte [] b = {};
  553. MemoryStream m = new MemoryStream (b);
  554. StreamReader r = new StreamReader (m);
  555. m.Close ();
  556. bool thrown = false;
  557. try {
  558. r.Peek ();
  559. } catch (ObjectDisposedException) {
  560. thrown = true;
  561. }
  562. AssertEquals ("#01", true, thrown);
  563. }
  564. }
  565. }