ASCIIEncodingTest.cs 8.3 KB

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