StreamReaderTest.cs 16 KB

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