BinaryReaderTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // BinaryReaderTest.cs - NUnit Test Cases for the SystemIO.BinaryReader class
  2. //
  3. // Eduardo Garcia Cebollero ([email protected])
  4. //
  5. // (C) Eduardo Garcia Cebollero.
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. namespace MonoTests.System.IO
  13. {
  14. public class BinaryReaderTest : TestCase
  15. {
  16. protected override void SetUp () { }
  17. private string _codeFileName = "resources" + Path.DirectorySeparatorChar + "AFile.txt";
  18. public void TestCtor1()
  19. {
  20. {
  21. bool errorThrown = false;
  22. try {
  23. BinaryReader r = new BinaryReader ((Stream) null);
  24. } catch (ArgumentNullException) {
  25. errorThrown = true;
  26. }
  27. Assert ("#01 null string error not thrown", errorThrown);
  28. }
  29. {
  30. bool errorThrown = false;
  31. FileStream f = new FileStream (_codeFileName, FileMode.Open, FileAccess.Write);
  32. try {
  33. BinaryReader r = new BinaryReader (f);
  34. r.Close ();
  35. } catch (ArgumentException) {
  36. errorThrown = true;
  37. }
  38. f.Close ();
  39. Assert ("#02 no read error not thrown", errorThrown);
  40. }
  41. {
  42. FileStream f = new FileStream (_codeFileName,
  43. FileMode.Open,
  44. FileAccess.Read);
  45. BinaryReader r = new BinaryReader (f);
  46. AssertNotNull ("#03 no binary reader created", r);
  47. r.Close ();
  48. f.Close ();
  49. }
  50. }
  51. public void TestCtor2 ()
  52. {
  53. {
  54. bool errorThrown = false;
  55. try {
  56. BinaryReader r = new BinaryReader ((Stream) null, Encoding.ASCII);
  57. } catch (ArgumentNullException) {
  58. errorThrown = true;
  59. } catch (Exception e) {
  60. Fail ("#04 Incorrect exception thrown: " + e.ToString ());
  61. }
  62. Assert ("#05 null stream error not thrown", errorThrown);
  63. }
  64. {
  65. bool errorThrown = false;
  66. try {
  67. BinaryReader r = new BinaryReader ((Stream) null, Encoding.Unicode);
  68. } catch (ArgumentNullException) {
  69. errorThrown = true;
  70. } catch (Exception e) {
  71. Fail ("#06 Incorrect exception thrown: " + e.ToString ());
  72. }
  73. Assert("#07 null stream error not thrown", errorThrown);
  74. }
  75. {
  76. bool errorThrown = false;
  77. try {
  78. BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF7);
  79. } catch (ArgumentNullException) {
  80. errorThrown = true;
  81. } catch (Exception e) {
  82. Fail ("#08 Incorrect exception thrown: " + e.ToString ());
  83. }
  84. Assert ("#09 null stream error not thrown", errorThrown);
  85. }
  86. {
  87. bool errorThrown = false;
  88. try {
  89. BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF8);
  90. } catch (ArgumentNullException) {
  91. errorThrown = true;
  92. } catch (Exception e) {
  93. Fail ("#0A Incorrect exception thrown: " + e.ToString ());
  94. }
  95. Assert ("#0B null stream error not thrown", errorThrown);
  96. }
  97. }
  98. public void TestCtor3 ()
  99. {
  100. bool errorThrown = false;
  101. byte [] b = new byte [30];
  102. MemoryStream m = new MemoryStream (b);
  103. try {
  104. BinaryReader r = new BinaryReader (m, (Encoding) null);
  105. } catch (ArgumentNullException) {
  106. errorThrown = true;
  107. } catch(Exception e) {
  108. Fail ("#0C Incorrect Exception thrown: " + e.ToString ());
  109. }
  110. Assert ("#0D No exception trown: ", errorThrown);
  111. }
  112. //TODO: (TestCtor*) Verify the Use of a wrong Stream
  113. //TODO: (TestClose*) Verify the Close Method
  114. public void TestClose1 ()
  115. {
  116. {
  117. byte [] b = new byte [30];
  118. MemoryStream m = new MemoryStream (b);
  119. try {
  120. BinaryReader r = new BinaryReader (m);
  121. r.Close ();
  122. } catch (Exception e) {
  123. Fail ("#0E Unhandled Exception: "+ e.ToString ());
  124. }
  125. }
  126. }
  127. //TODO: (TestRead*) Verify Read Method
  128. public void TestReadBoolean ()
  129. {
  130. bool [] a = {true, true, false};
  131. byte [] arr_a = new byte [3];
  132. int i = 0;
  133. foreach (bool a1 in a) {
  134. arr_a [i] = Convert.ToByte (a1);
  135. i++;
  136. }
  137. bool b;
  138. MemoryStream m = new MemoryStream (arr_a);
  139. try {
  140. BinaryReader r = new BinaryReader (m);
  141. b = r.ReadBoolean ();
  142. AssertEquals ("#11 No well readed boolean: ", a [0], b);
  143. } catch (Exception e) {
  144. Fail ("#12 Unexpected exception thrown: " + e.ToString ());
  145. }
  146. }
  147. public void TestReadByte ()
  148. {
  149. byte [] a = {0, 2, 3, 1, 5, 2};
  150. byte b;
  151. MemoryStream m = new MemoryStream (a);
  152. try {
  153. BinaryReader r = new BinaryReader (m);
  154. b = r.ReadByte ();
  155. AssertEquals ("#13 No well readed byte: ", a [0], b);
  156. } catch (Exception e) {
  157. Fail ("#14 Unexpected Exception thrown: " + e.ToString ());
  158. }
  159. }
  160. public void TestReadChar()
  161. {
  162. char [] a = {'a','b','c','d','e'};
  163. byte [] arr_a = new byte [5];
  164. int i = 0;
  165. char c;
  166. foreach (char a1 in a) {
  167. arr_a [i] = Convert.ToByte (a1);
  168. i++;
  169. }
  170. MemoryStream m = new MemoryStream (arr_a);
  171. BinaryReader r = new BinaryReader (m);
  172. try {
  173. c = r.ReadChar ();
  174. AssertEquals ("#15 No well readed Char", a [0], c);
  175. } catch (Exception e) {
  176. Fail ("#16 Unexpeted Exception: " + e.ToString ());
  177. }
  178. }
  179. public void TestReadInt32 () //Uses BinaryWriter!!
  180. {
  181. int [] arr_int = {1,10,200,3000,40000,500000,6000000};
  182. byte [] arr_byte = new byte [28]; //Sizeof arr_int * 4
  183. int [] arr_int2 = new int [7];
  184. int i;
  185. MemoryStream mem_stream = new MemoryStream (arr_byte);
  186. BinaryWriter bin_writer = new BinaryWriter (mem_stream);
  187. foreach (int elem in arr_int) {
  188. bin_writer.Write(elem);
  189. }
  190. mem_stream.Seek(0,SeekOrigin.Begin);
  191. BinaryReader bin_reader = new BinaryReader (mem_stream);
  192. bin_reader.BaseStream.Seek(0,SeekOrigin.Begin);
  193. for (i=0;i<7;i++) {
  194. try{
  195. arr_int2 [i] = bin_reader.ReadInt32();
  196. AssertEquals("#2E Wrong Readed Int32 in iteration "+ i,arr_int [i],arr_int2 [i]);
  197. } catch (IOException e) {
  198. Fail("#2F Unexpected IO Exception" + e.ToString());
  199. }
  200. }
  201. }
  202. //-TODO: (TestRead[Type]*) Verify the ReadBoolean, ReadByte ....
  203. // ReadBoolean, ReadByte, ReadChar, ReadInt32 Done
  204. //TODO: (TestFillBuffer*) Verify the FillBuffer Method
  205. public void TestPeekChar ()
  206. {
  207. char char1, char2;
  208. char [] b = {'A', 'B', 'C'};
  209. byte [] arr_b = new byte [3];
  210. int i = 0;
  211. foreach (char b1 in b) {
  212. arr_b [i] = Convert.ToByte (b1);
  213. i++;
  214. }
  215. MemoryStream m = new MemoryStream (arr_b);
  216. BinaryReader r = new BinaryReader (m);
  217. try {
  218. char1 = (char) r.PeekChar ();
  219. char2 = (char) r.PeekChar ();
  220. AssertEquals ("#20 the stream pointer have been altered in peek", char1, char2);
  221. } catch (Exception e) {
  222. Fail ("#21 Unexpected exception thrown: " + e.ToString ());
  223. }
  224. }
  225. public void TestBaseSeek1 ()
  226. {
  227. char char1, char2;
  228. char [] b = {'A','B','C','D','E','F'};
  229. byte [] arr_b = new byte[6];
  230. int i = 0;
  231. foreach (char b1 in b) {
  232. arr_b [i] = Convert.ToByte (b1);
  233. i++;
  234. }
  235. MemoryStream m = new MemoryStream (arr_b);
  236. BinaryReader r = new BinaryReader (m);
  237. try {
  238. char1 = (char) r.PeekChar ();
  239. r.BaseStream.Seek (0,SeekOrigin.Current);
  240. char2 = (char) r.PeekChar ();
  241. AssertEquals ("#22 the stream Has been altered in Seek", char1, char2);
  242. } catch (Exception e) {
  243. Fail ("#23 Unexpected exception thrown: " + e.ToString ());
  244. }
  245. }
  246. public void TestBaseSeek2 ()
  247. {
  248. char char1, char2;
  249. char [] b = {'A','B','C','D','E','F'};
  250. byte [] arr_b = new byte[6];
  251. int i = 0;
  252. foreach (char b1 in b) {
  253. arr_b [i] = Convert.ToByte (b1);
  254. i++;
  255. }
  256. MemoryStream m = new MemoryStream (arr_b);
  257. BinaryReader r = new BinaryReader (m);
  258. try {
  259. char1 = (char) r.PeekChar ();
  260. r.BaseStream.Seek (3,SeekOrigin.Current);
  261. r.BaseStream.Seek (-3,SeekOrigin.Current);
  262. char2 = (char) r.PeekChar ();
  263. AssertEquals ("#24 the stream Has been altered in Seek", char1, char2);
  264. } catch (Exception e) {
  265. Fail ("#25 Unexpected exception thrown: " + e.ToString ());
  266. }
  267. }
  268. public void TestInterleavedSeek1 ()
  269. {
  270. byte int1;
  271. byte [] arr_byte = {0,1,2,3,4,5,6,7,8,9};
  272. MemoryStream m = new MemoryStream (arr_byte);
  273. BinaryReader r = new BinaryReader (m);
  274. {
  275. try {
  276. int1 = r.ReadByte();
  277. AssertEquals("#26 Not well readed Byte", int1, arr_byte[0]);
  278. } catch (Exception e) {
  279. Fail ("#27 Unexpected exception thrown: " + e.ToString ());
  280. }
  281. }
  282. {
  283. try {
  284. r.BaseStream.Seek(-1,SeekOrigin.End);
  285. int1 = r.ReadByte();
  286. AssertEquals("#28 Not well readed Byte",int1,arr_byte[9]);
  287. } catch (Exception e) {
  288. Fail ("#29 Unexpected exception thrown: " + e.ToString ());
  289. }
  290. }
  291. {
  292. try {
  293. r.BaseStream.Seek(3,SeekOrigin.Begin);
  294. int1 = r.ReadByte();
  295. AssertEquals("#2A Not well readed Byte",int1,arr_byte[3]);
  296. } catch (Exception e) {
  297. Fail ("#2B Unexpected exception thrown: " + e.ToString ());
  298. }
  299. }
  300. {
  301. try {
  302. r.BaseStream.Seek(2,SeekOrigin.Current);
  303. int1 = r.ReadByte();
  304. AssertEquals("#2C Not well readed Int32",int1,arr_byte [6]);
  305. } catch (Exception e) {
  306. Fail ("#2D Unexpected exception thrown: " + e.ToString ());
  307. }
  308. }
  309. }
  310. }
  311. }