StreamReaderTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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? !$*#");
  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? !$*#", 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? !$*#", 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. try {
  337. Byte[] b = {};
  338. MemoryStream m = new MemoryStream(b);
  339. StreamReader r = new StreamReader(m);
  340. AssertEquals("wrong base stream ", m, r.BaseStream);
  341. r.Close();
  342. m.Close();
  343. } catch (Exception e) {
  344. Fail ("Unexpected exception thrown: " + e.ToString());
  345. }
  346. }
  347. public void TestCurrentEncoding() {
  348. try {
  349. Byte[] b = {};
  350. MemoryStream m = new MemoryStream(b);
  351. StreamReader r = new StreamReader(m);
  352. AssertEquals("wrong encoding",
  353. Encoding.UTF8, r.CurrentEncoding);
  354. } catch (Exception e) {
  355. Fail ("Unexpected exception thrown: " + e.ToString());
  356. }
  357. }
  358. // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
  359. // TODO - DiscardBufferedData - I have no clue how to test this function.
  360. public void TestPeek() {
  361. // FIXME - how to get an IO Exception?
  362. {
  363. bool errorThrown = false;
  364. try {
  365. Byte[] b = {};
  366. MemoryStream m = new MemoryStream(b);
  367. StreamReader r = new StreamReader(m);
  368. m.Close();
  369. int nothing = r.Peek();
  370. } catch (ObjectDisposedException) {
  371. errorThrown = true;
  372. }
  373. Assert("nothing-to-peek-at error not thrown", errorThrown);
  374. }
  375. {
  376. Byte[] b = {1, 2, 3, 4, 5, 6};
  377. MemoryStream m = new MemoryStream(b);
  378. StreamReader r = new StreamReader(m);
  379. for (int i = 1; i <= 6; i++) {
  380. AssertEquals("peek incorrect", i, r.Peek());
  381. r.Read();
  382. }
  383. AssertEquals("should be none left", -1, r.Peek());
  384. }
  385. }
  386. public void TestRead() {
  387. // FIXME - how to get an IO Exception?
  388. {
  389. bool errorThrown = false;
  390. try {
  391. Byte[] b = {};
  392. MemoryStream m = new MemoryStream(b);
  393. StreamReader r = new StreamReader(m);
  394. m.Close();
  395. int nothing = r.Read();
  396. } catch (ObjectDisposedException) {
  397. errorThrown = true;
  398. } catch (Exception e) {
  399. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  400. }
  401. Assert("nothing-to-read error not thrown", errorThrown);
  402. }
  403. {
  404. Byte[] b = {1, 2, 3, 4, 5, 6};
  405. MemoryStream m = new MemoryStream(b);
  406. StreamReader r = new StreamReader(m);
  407. for (int i = 1; i <= 6; i++) {
  408. AssertEquals("read incorrect", i, r.Read());
  409. }
  410. AssertEquals("Should be none left", -1, r.Read());
  411. }
  412. {
  413. bool errorThrown = false;
  414. try {
  415. Byte[] b = {};
  416. StreamReader r = new StreamReader(new MemoryStream(b));
  417. r.Read(null, 0, 0);
  418. } catch (ArgumentNullException) {
  419. errorThrown = true;
  420. } catch (ArgumentException) {
  421. errorThrown = true;
  422. } catch (Exception e) {
  423. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  424. }
  425. Assert("null buffer error not thrown", errorThrown);
  426. }
  427. {
  428. bool errorThrown = false;
  429. try {
  430. Byte[] b = {};
  431. StreamReader r = new StreamReader(new MemoryStream(b));
  432. Char[] c = new Char[1];
  433. r.Read(c, 0, 2);
  434. } catch (ArgumentException) {
  435. errorThrown = true;
  436. } catch (Exception e) {
  437. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  438. }
  439. Assert("too-long range error not thrown", errorThrown);
  440. }
  441. {
  442. bool errorThrown = false;
  443. try {
  444. Byte[] b = {};
  445. StreamReader r = new StreamReader(new MemoryStream(b));
  446. Char[] c = new Char[1];
  447. r.Read(c, -1, 2);
  448. } catch (ArgumentOutOfRangeException) {
  449. errorThrown = true;
  450. } catch (Exception e) {
  451. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  452. }
  453. Assert("out of range error not thrown", errorThrown);
  454. }
  455. {
  456. bool errorThrown = false;
  457. try {
  458. Byte[] b = {};
  459. StreamReader r = new StreamReader(new MemoryStream(b));
  460. Char[] c = new Char[1];
  461. r.Read(c, 0, -1);
  462. } catch (ArgumentOutOfRangeException) {
  463. errorThrown = true;
  464. } catch (Exception e) {
  465. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  466. }
  467. Assert("out of range error not thrown", errorThrown);
  468. }
  469. {
  470. int ii = 1;
  471. try {
  472. Byte[] b = {(byte)'a', (byte)'b', (byte)'c',
  473. (byte)'d', (byte)'e', (byte)'f',
  474. (byte)'g'};
  475. MemoryStream m = new MemoryStream(b);
  476. ii++;
  477. StreamReader r = new StreamReader(m);
  478. ii++;
  479. char[] buffer = new Char[7];
  480. ii++;
  481. char[] target = {'g','d','e','f','b','c','a'};
  482. ii++;
  483. r.Read(buffer, 6, 1);
  484. ii++;
  485. r.Read(buffer, 4, 2);
  486. ii++;
  487. r.Read(buffer, 1, 3);
  488. ii++;
  489. r.Read(buffer, 0, 1);
  490. ii++;
  491. for (int i = 0; i < target.Length; i++) {
  492. AssertEquals("read no work",
  493. target[i], buffer[i]);
  494. i++;
  495. }
  496. } catch (Exception e) {
  497. Fail ("Caught when ii=" + ii + ". e:" + e.ToString());
  498. }
  499. }
  500. }
  501. public void TestReadLine() {
  502. // TODO Out Of Memory Exc? IO Exc?
  503. Byte[] b = new Byte[8];
  504. b[0] = (byte)'a';
  505. b[1] = (byte)'\n';
  506. b[2] = (byte)'b';
  507. b[3] = (byte)'\n';
  508. b[4] = (byte)'c';
  509. b[5] = (byte)'\n';
  510. b[6] = (byte)'d';
  511. b[7] = (byte)'\n';
  512. MemoryStream m = new MemoryStream(b);
  513. StreamReader r = new StreamReader(m);
  514. AssertEquals("line doesn't match", "a", r.ReadLine());
  515. AssertEquals("line doesn't match", "b", r.ReadLine());
  516. AssertEquals("line doesn't match", "c", r.ReadLine());
  517. AssertEquals("line doesn't match", "d", r.ReadLine());
  518. AssertEquals("line doesn't match", null, r.ReadLine());
  519. }
  520. public void TestReadToEnd() {
  521. // TODO Out Of Memory Exc? IO Exc?
  522. Byte[] b = new Byte[8];
  523. b[0] = (byte)'a';
  524. b[1] = (byte)'\n';
  525. b[2] = (byte)'b';
  526. b[3] = (byte)'\n';
  527. b[4] = (byte)'c';
  528. b[5] = (byte)'\n';
  529. b[6] = (byte)'d';
  530. b[7] = (byte)'\n';
  531. MemoryStream m = new MemoryStream(b);
  532. StreamReader r = new StreamReader(m);
  533. AssertEquals("line doesn't match", "a\nb\nc\nd\n", r.ReadToEnd());
  534. AssertEquals("line doesn't match", "", r.ReadToEnd());
  535. }
  536. }
  537. }