StreamReaderTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. public StreamReaderTest() :
  16. base ("MonoTests.System.StreamReaderTest testsuite") {}
  17. public StreamReaderTest(string name) : base(name) {}
  18. protected override void SetUp()
  19. {
  20. }
  21. protected override void TearDown()
  22. {
  23. }
  24. public static ITest Suite {
  25. get {
  26. return new TestSuite(typeof(StreamReaderTest));
  27. }
  28. }
  29. private string _codeFileName = "resources" + Path.DirectorySeparatorChar + "AFile.txt";
  30. public void TestCtor1() {
  31. {
  32. bool errorThrown = false;
  33. try {
  34. StreamReader r = new StreamReader((Stream)null);
  35. } catch (ArgumentNullException) {
  36. errorThrown = true;
  37. }
  38. Assert("null string error not thrown", errorThrown);
  39. }
  40. {
  41. bool errorThrown = false;
  42. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  43. try {
  44. StreamReader r = new StreamReader(f);
  45. r.Close();
  46. } catch (ArgumentException) {
  47. errorThrown = true;
  48. }
  49. f.Close();
  50. Assert("no read error not thrown", errorThrown);
  51. }
  52. {
  53. // this is probably incestuous, but, oh well.
  54. FileStream f = new FileStream(_codeFileName,
  55. FileMode.Open,
  56. FileAccess.Read);
  57. StreamReader r = new StreamReader(f);
  58. AssertNotNull("no stream reader", r);
  59. r.Close();
  60. f.Close();
  61. }
  62. }
  63. public void TestCtor2() {
  64. {
  65. bool errorThrown = false;
  66. try {
  67. StreamReader r = new StreamReader("");
  68. } catch (ArgumentException) {
  69. errorThrown = true;
  70. } catch (Exception e) {
  71. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  72. }
  73. Assert("empty string error not thrown", errorThrown);
  74. }
  75. {
  76. bool errorThrown = false;
  77. try {
  78. StreamReader r = new StreamReader((string)null);
  79. } catch (ArgumentNullException) {
  80. errorThrown = true;
  81. } catch (Exception e) {
  82. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  83. }
  84. Assert("null string error not thrown", errorThrown);
  85. }
  86. {
  87. bool errorThrown = false;
  88. try {
  89. StreamReader r = new StreamReader("nonexistentfile");
  90. } catch (FileNotFoundException) {
  91. errorThrown = true;
  92. } catch (Exception e) {
  93. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  94. }
  95. Assert("fileNotFound error not thrown", errorThrown);
  96. }
  97. {
  98. bool errorThrown = false;
  99. try {
  100. StreamReader r = new StreamReader("nonexistentdir/file");
  101. } catch (DirectoryNotFoundException) {
  102. errorThrown = true;
  103. } catch (Exception e) {
  104. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  105. }
  106. Assert("dirNotFound error not thrown", errorThrown);
  107. }
  108. {
  109. bool errorThrown = false;
  110. try {
  111. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  112. } catch (IOException) {
  113. errorThrown = true;
  114. } catch (ArgumentException) {
  115. // FIXME - the spec says 'IOExc', but the
  116. // compiler says 'ArgExc'...
  117. errorThrown = true;
  118. } catch (Exception e) {
  119. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  120. }
  121. Assert("invalid filename error not thrown", errorThrown);
  122. }
  123. {
  124. // this is probably incestuous, but, oh well.
  125. StreamReader r = new StreamReader(_codeFileName);
  126. AssertNotNull("no stream reader", r);
  127. r.Close();
  128. }
  129. }
  130. public void TestCtor3() {
  131. {
  132. bool errorThrown = false;
  133. try {
  134. StreamReader r = new StreamReader((Stream)null, false);
  135. } catch (ArgumentNullException) {
  136. errorThrown = true;
  137. } catch (Exception e) {
  138. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  139. }
  140. Assert("null stream error not thrown", errorThrown);
  141. }
  142. {
  143. bool errorThrown = false;
  144. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  145. try {
  146. StreamReader r = new StreamReader(f, false);
  147. r.Close();
  148. } catch (ArgumentException) {
  149. errorThrown = true;
  150. } catch (Exception e) {
  151. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  152. }
  153. f.Close();
  154. Assert("no read error not thrown", errorThrown);
  155. }
  156. {
  157. // this is probably incestuous, but, oh well.
  158. FileStream f = new FileStream(_codeFileName,
  159. FileMode.Open,
  160. FileAccess.Read);
  161. StreamReader r = new StreamReader(f, false);
  162. AssertNotNull("no stream reader", r);
  163. r.Close();
  164. f.Close();
  165. }
  166. {
  167. bool errorThrown = false;
  168. try {
  169. StreamReader r = new StreamReader((Stream)null, true);
  170. } catch (ArgumentNullException) {
  171. errorThrown = true;
  172. } catch (Exception e) {
  173. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  174. }
  175. Assert("null string error not thrown", errorThrown);
  176. }
  177. {
  178. bool errorThrown = false;
  179. FileStream f = new FileStream(_codeFileName, FileMode.Open, FileAccess.Write);
  180. try {
  181. StreamReader r = new StreamReader(f, true);
  182. r.Close();
  183. } catch (ArgumentException) {
  184. errorThrown = true;
  185. } catch (Exception e) {
  186. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  187. }
  188. f.Close();
  189. Assert("no read error not thrown", errorThrown);
  190. }
  191. {
  192. // this is probably incestuous, but, oh well.
  193. FileStream f = new FileStream(_codeFileName,
  194. FileMode.Open,
  195. FileAccess.Read);
  196. StreamReader r = new StreamReader(f, true);
  197. AssertNotNull("no stream reader", r);
  198. r.Close();
  199. f.Close();
  200. }
  201. }
  202. public void TestCtor4() {
  203. {
  204. bool errorThrown = false;
  205. try {
  206. StreamReader r = new StreamReader("", false);
  207. } catch (ArgumentException) {
  208. errorThrown = true;
  209. } catch (Exception e) {
  210. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  211. }
  212. Assert("empty string error not thrown", errorThrown);
  213. }
  214. {
  215. bool errorThrown = false;
  216. try {
  217. StreamReader r = new StreamReader((string)null, false);
  218. } catch (ArgumentNullException) {
  219. errorThrown = true;
  220. } catch (Exception e) {
  221. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  222. }
  223. Assert("null string error not thrown", errorThrown);
  224. }
  225. {
  226. bool errorThrown = false;
  227. try {
  228. StreamReader r = new StreamReader("nonexistentfile", false);
  229. } catch (FileNotFoundException) {
  230. errorThrown = true;
  231. } catch (Exception e) {
  232. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  233. }
  234. Assert("fileNotFound error not thrown", errorThrown);
  235. }
  236. {
  237. bool errorThrown = false;
  238. try {
  239. StreamReader r = new StreamReader("nonexistentdir/file", false);
  240. } catch (DirectoryNotFoundException) {
  241. errorThrown = true;
  242. } catch (Exception e) {
  243. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  244. }
  245. Assert("dirNotFound error not thrown", errorThrown);
  246. }
  247. {
  248. bool errorThrown = false;
  249. try {
  250. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
  251. } catch (IOException) {
  252. errorThrown = true;
  253. } catch (ArgumentException) {
  254. // FIXME - the spec says 'IOExc', but the
  255. // compiler says 'ArgExc'...
  256. errorThrown = true;
  257. } catch (Exception e) {
  258. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  259. }
  260. Assert("invalid filename error not thrown", errorThrown);
  261. }
  262. {
  263. // this is probably incestuous, but, oh well.
  264. StreamReader r = new StreamReader(_codeFileName, false);
  265. AssertNotNull("no stream reader", r);
  266. r.Close();
  267. }
  268. {
  269. bool errorThrown = false;
  270. try {
  271. StreamReader r = new StreamReader("", true);
  272. } catch (ArgumentException) {
  273. errorThrown = true;
  274. } catch (Exception e) {
  275. Fail ("Incorrect exception thrown at 6: " + e.ToString());
  276. }
  277. Assert("empty string error not thrown", errorThrown);
  278. }
  279. {
  280. bool errorThrown = false;
  281. try {
  282. StreamReader r = new StreamReader((string)null, true);
  283. } catch (ArgumentNullException) {
  284. errorThrown = true;
  285. } catch (Exception e) {
  286. Fail ("Incorrect exception thrown at 7: " + e.ToString());
  287. }
  288. Assert("null string error not thrown", errorThrown);
  289. }
  290. {
  291. bool errorThrown = false;
  292. try {
  293. StreamReader r = new StreamReader("nonexistentfile", true);
  294. } catch (FileNotFoundException) {
  295. errorThrown = true;
  296. } catch (Exception e) {
  297. Fail ("Incorrect exception thrown at 8: " + e.ToString());
  298. }
  299. Assert("fileNotFound error not thrown", errorThrown);
  300. }
  301. {
  302. bool errorThrown = false;
  303. try {
  304. StreamReader r = new StreamReader("nonexistentdir/file", true);
  305. } catch (DirectoryNotFoundException) {
  306. errorThrown = true;
  307. } catch (Exception e) {
  308. Fail ("Incorrect exception thrown at 9: " + e.ToString());
  309. }
  310. Assert("dirNotFound error not thrown", errorThrown);
  311. }
  312. {
  313. bool errorThrown = false;
  314. try {
  315. StreamReader r = new StreamReader("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
  316. } catch (IOException) {
  317. errorThrown = true;
  318. } catch (ArgumentException) {
  319. // FIXME - the spec says 'IOExc', but the
  320. // compiler says 'ArgExc'...
  321. errorThrown = true;
  322. } catch (Exception e) {
  323. Fail ("Incorrect exception thrown at 10: " + e.ToString());
  324. }
  325. Assert("invalid filename error not thrown", errorThrown);
  326. }
  327. {
  328. // this is probably incestuous, but, oh well.
  329. StreamReader r = new StreamReader(_codeFileName, true);
  330. AssertNotNull("no stream reader", r);
  331. r.Close();
  332. }
  333. }
  334. // TODO - Ctor with Encoding
  335. public void TestBaseStream() {
  336. string progress = "beginning";
  337. try {
  338. Byte[] b = {};
  339. MemoryStream m = new MemoryStream(b);
  340. StreamReader r = new StreamReader(m);
  341. AssertEquals("wrong base stream ", m, r.BaseStream);
  342. progress = "Closing StreamReader";
  343. r.Close();
  344. progress = "Closing MemoryStream";
  345. m.Close();
  346. } catch (Exception e) {
  347. Fail ("At '" + progress + "' an unexpected exception was thrown: " + e.ToString());
  348. }
  349. }
  350. public void TestCurrentEncoding() {
  351. try {
  352. Byte[] b = {};
  353. MemoryStream m = new MemoryStream(b);
  354. StreamReader r = new StreamReader(m);
  355. AssertEquals("wrong encoding",
  356. Encoding.UTF8, r.CurrentEncoding);
  357. } catch (Exception e) {
  358. Fail ("Unexpected exception thrown: " + e.ToString());
  359. }
  360. }
  361. // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
  362. // TODO - DiscardBufferedData - I have no clue how to test this function.
  363. public void TestPeek() {
  364. // FIXME - how to get an IO Exception?
  365. {
  366. bool errorThrown = false;
  367. try {
  368. Byte[] b = {};
  369. MemoryStream m = new MemoryStream(b);
  370. StreamReader r = new StreamReader(m);
  371. m.Close();
  372. int nothing = r.Peek();
  373. } catch (ObjectDisposedException) {
  374. errorThrown = true;
  375. }
  376. Assert("nothing-to-peek-at error not thrown", errorThrown);
  377. }
  378. {
  379. Byte[] b = {1, 2, 3, 4, 5, 6};
  380. MemoryStream m = new MemoryStream(b);
  381. StreamReader r = new StreamReader(m);
  382. for (int i = 1; i <= 6; i++) {
  383. AssertEquals("peek incorrect", i, r.Peek());
  384. r.Read();
  385. }
  386. AssertEquals("should be none left", -1, r.Peek());
  387. }
  388. }
  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. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  403. }
  404. Assert("nothing-to-read error not thrown", errorThrown);
  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. AssertEquals("read incorrect", i, r.Read());
  412. }
  413. AssertEquals("Should be none left", -1, r.Read());
  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. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  427. }
  428. Assert("null buffer error not thrown", errorThrown);
  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. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  441. }
  442. Assert("too-long range error not thrown", errorThrown);
  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. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  455. }
  456. Assert("out of range error not thrown", errorThrown);
  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. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  469. }
  470. Assert("out of range error not thrown", errorThrown);
  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. AssertEquals("read no work",
  496. target[i], buffer[i]);
  497. i++;
  498. }
  499. } catch (Exception e) {
  500. Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
  501. }
  502. }
  503. }
  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. AssertEquals("line doesn't match", "a", r.ReadLine());
  518. AssertEquals("line doesn't match", "b", r.ReadLine());
  519. AssertEquals("line doesn't match", "c", r.ReadLine());
  520. AssertEquals("line doesn't match", "d", r.ReadLine());
  521. AssertEquals("line doesn't match", null, r.ReadLine());
  522. }
  523. public void TestReadToEnd() {
  524. // TODO Out Of Memory Exc? IO Exc?
  525. Byte[] b = new Byte[8];
  526. b[0] = (byte)'a';
  527. b[1] = (byte)'\n';
  528. b[2] = (byte)'b';
  529. b[3] = (byte)'\n';
  530. b[4] = (byte)'c';
  531. b[5] = (byte)'\n';
  532. b[6] = (byte)'d';
  533. b[7] = (byte)'\n';
  534. MemoryStream m = new MemoryStream(b);
  535. StreamReader r = new StreamReader(m);
  536. AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
  537. AssertEquals("line doesn't match", "", r.ReadToEnd());
  538. }
  539. }
  540. }