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. [TestFixture]
  16. public class ASCIIEncodingTest
  17. {
  18. private char[] testchars;
  19. private byte[] testbytes;
  20. [SetUp]
  21. public void SetUp ()
  22. {
  23. testchars = new char[4];
  24. testchars[0] = 'T';
  25. testchars[1] = 'e';
  26. testchars[2] = 's';
  27. testchars[3] = 't';
  28. testbytes = new byte[4];
  29. testbytes[0] = (byte) 'T';
  30. testbytes[1] = (byte) 'e';
  31. testbytes[2] = (byte) 's';
  32. testbytes[3] = (byte) 't';
  33. }
  34. [Test]
  35. public void IsBrowserDisplay ()
  36. {
  37. Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
  38. }
  39. [Test]
  40. public void IsBrowserSave ()
  41. {
  42. Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
  43. }
  44. [Test]
  45. public void IsMailNewsDisplay ()
  46. {
  47. Assert.IsTrue (Encoding.ASCII.IsMailNewsDisplay);
  48. }
  49. [Test]
  50. public void IsMailNewsSave ()
  51. {
  52. Assert.IsTrue (Encoding.ASCII.IsMailNewsSave);
  53. }
  54. [Test] // Test GetBytes(char[])
  55. public void TestGetBytes1 ()
  56. {
  57. Encoding ascii_encoding = Encoding.ASCII;
  58. byte[] bytes = ascii_encoding.GetBytes(testchars);
  59. for (int i = 0; i < testchars.Length; i++)
  60. Assert.AreEqual (testchars[i], (char) bytes[i]);
  61. }
  62. [Test] // Test GetBytes(char[], int, int)
  63. public void TestGetBytes2 ()
  64. {
  65. Encoding ascii_encoding = Encoding.ASCII;
  66. byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
  67. Assert.AreEqual (1, bytes.Length, "#1");
  68. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  69. }
  70. [Test] // Test non-ASCII char in char[]
  71. public void TestGetBytes3 ()
  72. {
  73. Encoding ascii_encoding = Encoding.ASCII;
  74. testchars[2] = (char) 0x80;
  75. byte[] bytes = ascii_encoding.GetBytes(testchars);
  76. Assert.AreEqual ('T', (char) bytes [0], "#1");
  77. Assert.AreEqual ('e', (char) bytes [1], "#2");
  78. Assert.AreEqual ('?', (char) bytes [2], "#3");
  79. Assert.AreEqual ('t', (char) bytes [3], "#4");
  80. }
  81. [Test] // Test GetBytes(char[], int, int, byte[], int)
  82. public void TestGetBytes4 ()
  83. {
  84. Encoding ascii_encoding = Encoding.ASCII;
  85. byte[] bytes = new Byte[1];
  86. int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
  87. Assert.AreEqual (1, cnt, "#1");
  88. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  89. }
  90. [Test] // Test GetBytes(string, int, int, byte[], int)
  91. public void TestGetBytes5 ()
  92. {
  93. Encoding ascii_encoding = Encoding.ASCII;
  94. byte[] bytes = new Byte[1];
  95. int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
  96. Assert.AreEqual ('e', (char) bytes [0], "#1");
  97. }
  98. [Test] // Test GetBytes(string)
  99. public void TestGetBytes6 ()
  100. {
  101. Encoding ascii_encoding = Encoding.ASCII;
  102. byte[] bytes = ascii_encoding.GetBytes("Test");
  103. for (int i = 0; i < testchars.Length; i++)
  104. Assert.AreEqual (testchars [i], (char) bytes [i]);
  105. }
  106. [Test] // Test GetChars(byte[])
  107. public void TestGetChars1 ()
  108. {
  109. Encoding ascii_encoding = Encoding.ASCII;
  110. char[] chars = ascii_encoding.GetChars(testbytes);
  111. for (int i = 0; i < testbytes.Length; i++)
  112. Assert.AreEqual (testbytes[i], (byte) chars[i]);
  113. }
  114. [Test] // Test GetChars(byte[], int, int)
  115. public void TestGetChars2 ()
  116. {
  117. Encoding ascii_encoding = Encoding.ASCII;
  118. char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
  119. Assert.AreEqual (1, chars.Length, "#1");
  120. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  121. }
  122. [Test] // Test non-ASCII char in byte[]
  123. public void TestGetChars3 ()
  124. {
  125. Encoding ascii_encoding = Encoding.ASCII;
  126. testbytes[2] = 0x80;
  127. char[] chars = ascii_encoding.GetChars(testbytes);
  128. Assert.AreEqual ('T', chars [0], "#1");
  129. Assert.AreEqual ('e', chars [1], "#2");
  130. Assert.AreEqual ('?', chars [2], "#3");
  131. Assert.AreEqual ('t', chars [3], "#4");
  132. }
  133. [Test] // Test GetChars(byte[], int, int, char[], int)
  134. public void TestGetChars4 ()
  135. {
  136. Encoding ascii_encoding = Encoding.ASCII;
  137. char[] chars = new char[1];
  138. int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
  139. Assert.AreEqual (1, cnt, "#1");
  140. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  141. }
  142. [Test] // Test GetString(char[])
  143. public void TestGetString1 ()
  144. {
  145. Encoding ascii_encoding = Encoding.ASCII;
  146. string str = ascii_encoding.GetString(testbytes);
  147. Assert.AreEqual ("Test", str);
  148. }
  149. [Test] // Test GetString(char[], int, int)
  150. public void TestGetString2 ()
  151. {
  152. Encoding ascii_encoding = Encoding.ASCII;
  153. string str = ascii_encoding.GetString(testbytes, 1, 2);
  154. Assert.AreEqual ("es", str);
  155. }
  156. [Test] // Test invalid byte handling
  157. public void TestGetString3 ()
  158. {
  159. Encoding encoding = Encoding.ASCII;
  160. byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
  161. string s = encoding.GetString (bytes, 0, 3);
  162. #if NET_2_0
  163. Assert.AreEqual ("a??", s);
  164. #else
  165. Assert.AreEqual ("aab", s);
  166. #endif
  167. }
  168. [Test] // Test Decoder
  169. public void TestDecoder ()
  170. {
  171. Encoding ascii_encoding = Encoding.ASCII;
  172. char[] chars = new char[1];
  173. int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
  174. Assert.AreEqual (1, cnt, "#1");
  175. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  176. }
  177. [Test] // Test Decoder
  178. public void TestEncoder ()
  179. {
  180. Encoding ascii_encoding = Encoding.ASCII;
  181. byte[] bytes = new Byte[1];
  182. int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
  183. Assert.AreEqual (1, cnt, "#1");
  184. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  185. }
  186. [Test]
  187. public void TestZero ()
  188. {
  189. Encoding encoding = Encoding.ASCII;
  190. Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
  191. Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
  192. }
  193. [Test]
  194. [ExpectedException (typeof (DecoderFallbackException))]
  195. public void DecoderFallback ()
  196. {
  197. Encoding e = Encoding.ASCII.Clone () as Encoding;
  198. e.DecoderFallback = new DecoderExceptionFallback ();
  199. e.GetChars (new byte [] {0x80});
  200. }
  201. [Test]
  202. // [ExpectedException (typeof (ArgumentException))]
  203. public void DecoderFallback2 ()
  204. {
  205. var bytes = new byte[] {
  206. 0x30, 0xa0, 0x31, 0xa8
  207. };
  208. var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
  209. enc.DecoderFallback = new TestFallbackDecoder ();
  210. var chars = new char [7];
  211. var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
  212. Console.WriteLine (ret);
  213. for (int i = 0; i < chars.Length; i++) {
  214. Console.Write ("{0:x2} ", (int)chars [i]);
  215. }
  216. Console.WriteLine ();
  217. }
  218. [Test]
  219. public void DecoderFallback3 ()
  220. {
  221. var bytes = new byte[] {
  222. 0x30, 0xa0, 0x31, 0xa8
  223. };
  224. var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
  225. enc.DecoderFallback = new TestFallbackDecoder ();
  226. var chars = new char[] { '9', '8', '7', '6', '5' };
  227. var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
  228. Assert.That (ret, Is.EqualTo (4), "ret"); // FIXME: Wrong it should be 2
  229. Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
  230. Assert.That (chars [1], Is.EqualTo ('1'), "chars[1]");
  231. Assert.That (chars [2], Is.EqualTo ('7'), "chars[2]");
  232. Assert.That (chars [3], Is.EqualTo ('6'), "chars[3]");
  233. Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
  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. }