2
0

ASCIIEncodingTest.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
  2. //
  3. // Author: Mike Kestner <[email protected]>
  4. //
  5. // <c> 2002 Mike Kestner
  6. using System;
  7. using System.Text;
  8. using NUnit.Framework;
  9. using NUnit.Framework.Constraints;
  10. using NUnit.Framework.SyntaxHelpers;
  11. namespace MonoTests.System.Text
  12. {
  13. public class ASCIIEncodingTest
  14. {
  15. private char[] testchars;
  16. private byte[] testbytes;
  17. [SetUp]
  18. public void SetUp ()
  19. {
  20. testchars = new char[4];
  21. testchars[0] = 'T';
  22. testchars[1] = 'e';
  23. testchars[2] = 's';
  24. testchars[3] = 't';
  25. testbytes = new byte[4];
  26. testbytes[0] = (byte) 'T';
  27. testbytes[1] = (byte) 'e';
  28. testbytes[2] = (byte) 's';
  29. testbytes[3] = (byte) 't';
  30. }
  31. [Test]
  32. public void IsBrowserDisplay ()
  33. {
  34. Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
  35. }
  36. [Test]
  37. public void IsBrowserSave ()
  38. {
  39. Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
  40. }
  41. [Test]
  42. public void IsMailNewsDisplay ()
  43. {
  44. Assert.IsFalse (Encoding.ASCII.IsMailNewsDisplay);
  45. }
  46. [Test]
  47. public void IsMailNewsSave ()
  48. {
  49. Assert.IsFalse (Encoding.ASCII.IsMailNewsSave);
  50. }
  51. [Test] // Test GetBytes(char[])
  52. public void TestGetBytes1 ()
  53. {
  54. Encoding ascii_encoding = Encoding.ASCII;
  55. byte[] bytes = ascii_encoding.GetBytes(testchars);
  56. for (int i = 0; i < testchars.Length; i++)
  57. Assert.AreEqual (testchars[i], (char) bytes[i]);
  58. }
  59. [Test] // Test GetBytes(char[], int, int)
  60. public void TestGetBytes2 ()
  61. {
  62. Encoding ascii_encoding = Encoding.ASCII;
  63. byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
  64. Assert.AreEqual (1, bytes.Length, "#1");
  65. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  66. }
  67. [Test] // Test non-ASCII char in char[]
  68. public void TestGetBytes3 ()
  69. {
  70. Encoding ascii_encoding = Encoding.ASCII;
  71. testchars[2] = (char) 0x80;
  72. byte[] bytes = ascii_encoding.GetBytes(testchars);
  73. Assert.AreEqual ('T', (char) bytes [0], "#1");
  74. Assert.AreEqual ('e', (char) bytes [1], "#2");
  75. Assert.AreEqual ('?', (char) bytes [2], "#3");
  76. Assert.AreEqual ('t', (char) bytes [3], "#4");
  77. }
  78. [Test] // Test GetBytes(char[], int, int, byte[], int)
  79. public void TestGetBytes4 ()
  80. {
  81. Encoding ascii_encoding = Encoding.ASCII;
  82. byte[] bytes = new Byte[1];
  83. int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
  84. Assert.AreEqual (1, cnt, "#1");
  85. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  86. }
  87. [Test] // Test GetBytes(string, int, int, byte[], int)
  88. public void TestGetBytes5 ()
  89. {
  90. Encoding ascii_encoding = Encoding.ASCII;
  91. byte[] bytes = new Byte[1];
  92. int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
  93. Assert.AreEqual ('e', (char) bytes [0], "#1");
  94. }
  95. [Test] // Test GetBytes(string)
  96. public void TestGetBytes6 ()
  97. {
  98. Encoding ascii_encoding = Encoding.ASCII;
  99. byte[] bytes = ascii_encoding.GetBytes("Test");
  100. for (int i = 0; i < testchars.Length; i++)
  101. Assert.AreEqual (testchars [i], (char) bytes [i]);
  102. }
  103. [Test] // Test GetChars(byte[])
  104. public void TestGetChars1 ()
  105. {
  106. Encoding ascii_encoding = Encoding.ASCII;
  107. char[] chars = ascii_encoding.GetChars(testbytes);
  108. for (int i = 0; i < testbytes.Length; i++)
  109. Assert.AreEqual (testbytes[i], (byte) chars[i]);
  110. }
  111. [Test] // Test GetChars(byte[], int, int)
  112. public void TestGetChars2 ()
  113. {
  114. Encoding ascii_encoding = Encoding.ASCII;
  115. char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
  116. Assert.AreEqual (1, chars.Length, "#1");
  117. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  118. }
  119. [Test] // Test non-ASCII char in byte[]
  120. public void TestGetChars3 ()
  121. {
  122. Encoding ascii_encoding = Encoding.ASCII;
  123. testbytes[2] = 0x80;
  124. char[] chars = ascii_encoding.GetChars(testbytes);
  125. Assert.AreEqual ('T', chars [0], "#1");
  126. Assert.AreEqual ('e', chars [1], "#2");
  127. Assert.AreEqual ('?', chars [2], "#3");
  128. Assert.AreEqual ('t', chars [3], "#4");
  129. }
  130. [Test] // Test GetChars(byte[], int, int, char[], int)
  131. public void TestGetChars4 ()
  132. {
  133. Encoding ascii_encoding = Encoding.ASCII;
  134. char[] chars = new char[1];
  135. int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
  136. Assert.AreEqual (1, cnt, "#1");
  137. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  138. }
  139. [Test] // Test GetString(char[])
  140. public void TestGetString1 ()
  141. {
  142. Encoding ascii_encoding = Encoding.ASCII;
  143. string str = ascii_encoding.GetString(testbytes);
  144. Assert.AreEqual ("Test", str);
  145. }
  146. [Test] // Test GetString(char[], int, int)
  147. public void TestGetString2 ()
  148. {
  149. Encoding ascii_encoding = Encoding.ASCII;
  150. string str = ascii_encoding.GetString(testbytes, 1, 2);
  151. Assert.AreEqual ("es", str);
  152. }
  153. [Test] // Test invalid byte handling
  154. public void TestGetString3 ()
  155. {
  156. Encoding encoding = Encoding.ASCII;
  157. byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
  158. string s = encoding.GetString (bytes, 0, 3);
  159. #if NET_2_0
  160. Assert.AreEqual ("a??", s);
  161. #else
  162. Assert.AreEqual ("aab", s);
  163. #endif
  164. }
  165. [Test] // Test Decoder
  166. public void TestDecoder ()
  167. {
  168. Encoding ascii_encoding = Encoding.ASCII;
  169. char[] chars = new char[1];
  170. int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
  171. Assert.AreEqual (1, cnt, "#1");
  172. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  173. }
  174. [Test] // Test Decoder
  175. public void TestEncoder ()
  176. {
  177. Encoding ascii_encoding = Encoding.ASCII;
  178. byte[] bytes = new Byte[1];
  179. int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
  180. Assert.AreEqual (1, cnt, "#1");
  181. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  182. }
  183. [Test]
  184. public void TestZero ()
  185. {
  186. Encoding encoding = Encoding.ASCII;
  187. Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
  188. Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
  189. }
  190. [Test]
  191. [ExpectedException (typeof (DecoderFallbackException))]
  192. public void DecoderFallback ()
  193. {
  194. Encoding e = Encoding.ASCII.Clone () as Encoding;
  195. e.DecoderFallback = new DecoderExceptionFallback ();
  196. e.GetChars (new byte [] {0x80});
  197. }
  198. [Test]
  199. [ExpectedException (typeof (ArgumentException))]
  200. public void DecoderFallback2 ()
  201. {
  202. var bytes = new byte[] {
  203. 0x30, 0xa0, 0x31, 0xa8
  204. };
  205. var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
  206. enc.DecoderFallback = new TestFallbackDecoder ();
  207. var chars = new char [7];
  208. var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
  209. Console.WriteLine (ret);
  210. for (int i = 0; i < chars.Length; i++) {
  211. Console.Write ("{0:x2} ", (int)chars [i]);
  212. }
  213. Console.WriteLine ();
  214. }
  215. [Test]
  216. public void DecoderFallback3 ()
  217. {
  218. var bytes = new byte[] {
  219. 0x30, 0xa0, 0x31, 0xa8
  220. };
  221. var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
  222. enc.DecoderFallback = new TestFallbackDecoder ();
  223. var chars = new char [10];
  224. var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
  225. Assert.That (ret, Is.EqualTo (6), "ret");
  226. Assert.That (chars [0], Is.EqualTo ((char)0x30), "chars[0]");
  227. Assert.That (chars [1], Is.EqualTo ((char)0xa0), "chars[1]");
  228. Assert.That (chars [2], Is.EqualTo ((char)0xa1), "chars[2]");
  229. Assert.That (chars [3], Is.EqualTo ((char)0x31), "chars[3]");
  230. Assert.That (chars [4], Is.EqualTo ((char)0xa8), "chars[4]");
  231. Assert.That (chars [5], Is.EqualTo ((char)0xa9), "chars[5]");
  232. }
  233. class TestFallbackDecoder : DecoderFallback {
  234. const int count = 2;
  235. public override int MaxCharCount {
  236. get { return count; }
  237. }
  238. public override DecoderFallbackBuffer CreateFallbackBuffer ()
  239. {
  240. return new Buffer ();
  241. }
  242. class Buffer : DecoderFallbackBuffer {
  243. char[] queue;
  244. int index;
  245. public override int Remaining {
  246. get {
  247. return queue.Length - index;
  248. }
  249. }
  250. public override char GetNextChar ()
  251. {
  252. return index < queue.Length ? queue [index++] : '\0';
  253. }
  254. public override bool Fallback (byte[] bytes, int unused)
  255. {
  256. queue = new char[bytes.Length * count];
  257. index = 0;
  258. for (int i = 0; i < bytes.Length; i++) {
  259. for (int j = 0; j < count; j++)
  260. queue [index++] = (char)(bytes [i]+j);
  261. }
  262. return true;
  263. }
  264. public override bool MovePrevious ()
  265. {
  266. throw new NotImplementedException ();
  267. }
  268. public override void Reset ()
  269. {
  270. base.Reset ();
  271. }
  272. }
  273. }
  274. }
  275. }