StreamReaderTest.cs 15 KB

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