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 : TestCase
  14. {
  15. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  16. private string _codeFileName;
  17. public StreamReaderTest ()
  18. {
  19. if (!Directory.Exists (TempFolder))
  20. Directory.CreateDirectory (TempFolder);
  21. _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  22. }
  23. ~StreamReaderTest ()
  24. {
  25. if (Directory.Exists (TempFolder))
  26. Directory.Delete (TempFolder, true);
  27. }
  28. protected override void SetUp()
  29. {
  30. if (!Directory.Exists (TempFolder))
  31. Directory.CreateDirectory (TempFolder);
  32. if (!File.Exists (_codeFileName))
  33. File.Create (_codeFileName).Close ();
  34. }
  35. protected override void TearDown()
  36. {
  37. }
  38. public void TestCtor1() {
  39. {
  40. bool errorThrown = false;
  41. try {
  42. StreamReader r = new StreamReader((Stream)null);
  43. } catch (ArgumentNullException) {
  44. errorThrown = true;
  45. }
  46. Assert("null string error not thrown", errorThrown);
  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("no read error not thrown", errorThrown);
  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. AssertNotNull("no stream reader", r);
  67. r.Close();
  68. f.Close();
  69. }
  70. }
  71. public void TestCtor2() {
  72. {
  73. bool errorThrown = false;
  74. try {
  75. StreamReader r = new StreamReader("");
  76. } catch (ArgumentException) {
  77. errorThrown = true;
  78. } catch (Exception e) {
  79. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  80. }
  81. Assert("empty string error not thrown", errorThrown);
  82. }
  83. {
  84. bool errorThrown = false;
  85. try {
  86. StreamReader r = new StreamReader((string)null);
  87. } catch (ArgumentNullException) {
  88. errorThrown = true;
  89. } catch (Exception e) {
  90. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  91. }
  92. Assert("null string error not thrown", errorThrown);
  93. }
  94. {
  95. bool errorThrown = false;
  96. try {
  97. StreamReader r = new StreamReader("nonexistentfile");
  98. } catch (FileNotFoundException) {
  99. errorThrown = true;
  100. } catch (Exception e) {
  101. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  102. }
  103. Assert("fileNotFound error not thrown", errorThrown);
  104. }
  105. {
  106. bool errorThrown = false;
  107. try {
  108. StreamReader r = new StreamReader("nonexistentdir/file");
  109. } catch (DirectoryNotFoundException) {
  110. errorThrown = true;
  111. } catch (Exception e) {
  112. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  113. }
  114. Assert("dirNotFound error not thrown", errorThrown);
  115. }
  116. {
  117. bool errorThrown = false;
  118. try {
  119. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  120. } catch (IOException) {
  121. errorThrown = true;
  122. } catch (ArgumentException) {
  123. // FIXME - the spec says 'IOExc', but the
  124. // compiler says 'ArgExc'...
  125. errorThrown = true;
  126. } catch (Exception e) {
  127. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  128. }
  129. Assert("invalid filename error not thrown", errorThrown);
  130. }
  131. {
  132. // this is probably incestuous, but, oh well.
  133. StreamReader r = new StreamReader(_codeFileName);
  134. AssertNotNull("no stream reader", r);
  135. r.Close();
  136. }
  137. }
  138. public void TestCtor3() {
  139. {
  140. bool errorThrown = false;
  141. try {
  142. StreamReader r = new StreamReader((Stream)null, false);
  143. } catch (ArgumentNullException) {
  144. errorThrown = true;
  145. } catch (Exception e) {
  146. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  147. }
  148. Assert("null stream error not thrown", errorThrown);
  149. }
  150. {
  151. bool errorThrown = false;
  152. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  153. try {
  154. StreamReader r = new StreamReader(f, false);
  155. r.Close();
  156. } catch (ArgumentException) {
  157. errorThrown = true;
  158. } catch (Exception e) {
  159. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  160. }
  161. f.Close();
  162. Assert("no read error not thrown", errorThrown);
  163. }
  164. {
  165. // this is probably incestuous, but, oh well.
  166. FileStream f = new FileStream(_codeFileName,
  167. FileMode.Open,
  168. FileAccess.Read);
  169. StreamReader r = new StreamReader(f, false);
  170. AssertNotNull("no stream reader", r);
  171. r.Close();
  172. f.Close();
  173. }
  174. {
  175. bool errorThrown = false;
  176. try {
  177. StreamReader r = new StreamReader((Stream)null, true);
  178. } catch (ArgumentNullException) {
  179. errorThrown = true;
  180. } catch (Exception e) {
  181. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  182. }
  183. Assert("null string error not thrown", errorThrown);
  184. }
  185. {
  186. bool errorThrown = false;
  187. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  188. try {
  189. StreamReader r = new StreamReader(f, true);
  190. r.Close();
  191. } catch (ArgumentException) {
  192. errorThrown = true;
  193. } catch (Exception e) {
  194. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  195. }
  196. f.Close();
  197. Assert("no read error not thrown", errorThrown);
  198. }
  199. {
  200. // this is probably incestuous, but, oh well.
  201. FileStream f = new FileStream(_codeFileName,
  202. FileMode.Open,
  203. FileAccess.Read);
  204. StreamReader r = new StreamReader(f, true);
  205. AssertNotNull("no stream reader", r);
  206. r.Close();
  207. f.Close();
  208. }
  209. }
  210. public void TestCtor4() {
  211. {
  212. bool errorThrown = false;
  213. try {
  214. StreamReader r = new StreamReader("", false);
  215. } catch (ArgumentException) {
  216. errorThrown = true;
  217. } catch (Exception e) {
  218. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  219. }
  220. Assert("empty string error not thrown", errorThrown);
  221. }
  222. {
  223. bool errorThrown = false;
  224. try {
  225. StreamReader r = new StreamReader((string)null, false);
  226. } catch (ArgumentNullException) {
  227. errorThrown = true;
  228. } catch (Exception e) {
  229. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  230. }
  231. Assert("null string error not thrown", errorThrown);
  232. }
  233. {
  234. bool errorThrown = false;
  235. try {
  236. StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", false);
  237. } catch (FileNotFoundException) {
  238. errorThrown = true;
  239. } catch (Exception e) {
  240. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  241. }
  242. Assert("fileNotFound error not thrown", errorThrown);
  243. }
  244. {
  245. bool errorThrown = false;
  246. try {
  247. StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", false);
  248. } catch (DirectoryNotFoundException) {
  249. errorThrown = true;
  250. } catch (Exception e) {
  251. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  252. }
  253. Assert("dirNotFound error not thrown", errorThrown);
  254. }
  255. {
  256. bool errorThrown = false;
  257. try {
  258. StreamReader r = 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. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  267. }
  268. Assert("invalid filename error not thrown", errorThrown);
  269. }
  270. {
  271. // this is probably incestuous, but, oh well.
  272. StreamReader r = new StreamReader(_codeFileName, false);
  273. AssertNotNull("no stream reader", r);
  274. r.Close();
  275. }
  276. {
  277. bool errorThrown = false;
  278. try {
  279. StreamReader r = new StreamReader("", true);
  280. } catch (ArgumentException) {
  281. errorThrown = true;
  282. } catch (Exception e) {
  283. Fail ("Incorrect exception thrown at 6: " + e.ToString());
  284. }
  285. Assert("empty string error not thrown", errorThrown);
  286. }
  287. {
  288. bool errorThrown = false;
  289. try {
  290. StreamReader r = new StreamReader((string)null, true);
  291. } catch (ArgumentNullException) {
  292. errorThrown = true;
  293. } catch (Exception e) {
  294. Fail ("Incorrect exception thrown at 7: " + e.ToString());
  295. }
  296. Assert("null string error not thrown", errorThrown);
  297. }
  298. {
  299. bool errorThrown = false;
  300. try {
  301. StreamReader r = new StreamReader(TempFolder + "/nonexistentfile", true);
  302. } catch (FileNotFoundException) {
  303. errorThrown = true;
  304. } catch (Exception e) {
  305. Fail ("Incorrect exception thrown at 8: " + e.ToString());
  306. }
  307. Assert("fileNotFound error not thrown", errorThrown);
  308. }
  309. {
  310. bool errorThrown = false;
  311. try {
  312. StreamReader r = new StreamReader(TempFolder + "/nonexistentdir/file", true);
  313. } catch (DirectoryNotFoundException) {
  314. errorThrown = true;
  315. } catch (Exception e) {
  316. Fail ("Incorrect exception thrown at 9: " + e.ToString());
  317. }
  318. Assert("dirNotFound error not thrown", errorThrown);
  319. }
  320. {
  321. bool errorThrown = false;
  322. try {
  323. StreamReader r = 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. Fail ("Incorrect exception thrown at 10: " + e.ToString());
  332. }
  333. Assert("invalid filename error not thrown", errorThrown);
  334. }
  335. {
  336. // this is probably incestuous, but, oh well.
  337. StreamReader r = new StreamReader(_codeFileName, true);
  338. AssertNotNull("no stream reader", r);
  339. r.Close();
  340. }
  341. }
  342. // TODO - Ctor with Encoding
  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. 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. 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. public void TestReadLine() {
  513. // TODO Out Of Memory Exc? IO Exc?
  514. Byte[] b = new Byte[8];
  515. b[0] = (byte)'a';
  516. b[1] = (byte)'\n';
  517. b[2] = (byte)'b';
  518. b[3] = (byte)'\n';
  519. b[4] = (byte)'c';
  520. b[5] = (byte)'\n';
  521. b[6] = (byte)'d';
  522. b[7] = (byte)'\n';
  523. MemoryStream m = new MemoryStream(b);
  524. StreamReader r = new StreamReader(m);
  525. AssertEquals("line doesn't match", "a", r.ReadLine());
  526. AssertEquals("line doesn't match", "b", r.ReadLine());
  527. AssertEquals("line doesn't match", "c", r.ReadLine());
  528. AssertEquals("line doesn't match", "d", r.ReadLine());
  529. AssertEquals("line doesn't match", null, r.ReadLine());
  530. }
  531. public void TestReadToEnd() {
  532. // TODO Out Of Memory Exc? IO Exc?
  533. Byte[] b = new Byte[8];
  534. b[0] = (byte)'a';
  535. b[1] = (byte)'\n';
  536. b[2] = (byte)'b';
  537. b[3] = (byte)'\n';
  538. b[4] = (byte)'c';
  539. b[5] = (byte)'\n';
  540. b[6] = (byte)'d';
  541. b[7] = (byte)'\n';
  542. MemoryStream m = new MemoryStream(b);
  543. StreamReader r = new StreamReader(m);
  544. AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
  545. AssertEquals("line doesn't match", "", r.ReadToEnd());
  546. }
  547. public void TestBaseStreamClosed ()
  548. {
  549. byte [] b = {};
  550. MemoryStream m = new MemoryStream (b);
  551. StreamReader r = new StreamReader (m);
  552. m.Close ();
  553. bool thrown = false;
  554. try {
  555. r.Peek ();
  556. } catch (ObjectDisposedException) {
  557. thrown = true;
  558. }
  559. AssertEquals ("#01", true, thrown);
  560. }
  561. }
  562. }