ASCIIEncodingTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. namespace MonoTests.System.Text
  10. {
  11. public class ASCIIEncodingTest
  12. {
  13. private char[] testchars;
  14. private byte[] testbytes;
  15. [SetUp]
  16. public void SetUp ()
  17. {
  18. testchars = new char[4];
  19. testchars[0] = 'T';
  20. testchars[1] = 'e';
  21. testchars[2] = 's';
  22. testchars[3] = 't';
  23. testbytes = new byte[4];
  24. testbytes[0] = (byte) 'T';
  25. testbytes[1] = (byte) 'e';
  26. testbytes[2] = (byte) 's';
  27. testbytes[3] = (byte) 't';
  28. }
  29. [Test]
  30. public void IsBrowserDisplay ()
  31. {
  32. Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
  33. }
  34. [Test]
  35. public void IsBrowserSave ()
  36. {
  37. Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
  38. }
  39. [Test]
  40. public void IsMailNewsDisplay ()
  41. {
  42. Assert.IsFalse (Encoding.ASCII.IsMailNewsDisplay);
  43. }
  44. [Test]
  45. public void IsMailNewsSave ()
  46. {
  47. Assert.IsFalse (Encoding.ASCII.IsMailNewsSave);
  48. }
  49. [Test] // Test GetBytes(char[])
  50. public void TestGetBytes1 ()
  51. {
  52. Encoding ascii_encoding = Encoding.ASCII;
  53. byte[] bytes = ascii_encoding.GetBytes(testchars);
  54. for (int i = 0; i < testchars.Length; i++)
  55. Assert.AreEqual (testchars[i], (char) bytes[i]);
  56. }
  57. [Test] // Test GetBytes(char[], int, int)
  58. public void TestGetBytes2 ()
  59. {
  60. Encoding ascii_encoding = Encoding.ASCII;
  61. byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
  62. Assert.AreEqual (1, bytes.Length, "#1");
  63. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  64. }
  65. [Test] // Test non-ASCII char in char[]
  66. public void TestGetBytes3 ()
  67. {
  68. Encoding ascii_encoding = Encoding.ASCII;
  69. testchars[2] = (char) 0x80;
  70. byte[] bytes = ascii_encoding.GetBytes(testchars);
  71. Assert.AreEqual ('T', (char) bytes [0], "#1");
  72. Assert.AreEqual ('e', (char) bytes [1], "#2");
  73. Assert.AreEqual ('?', (char) bytes [2], "#3");
  74. Assert.AreEqual ('t', (char) bytes [3], "#4");
  75. }
  76. [Test] // Test GetBytes(char[], int, int, byte[], int)
  77. public void TestGetBytes4 ()
  78. {
  79. Encoding ascii_encoding = Encoding.ASCII;
  80. byte[] bytes = new Byte[1];
  81. int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
  82. Assert.AreEqual (1, cnt, "#1");
  83. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  84. }
  85. [Test] // Test GetBytes(string, int, int, byte[], int)
  86. public void TestGetBytes5 ()
  87. {
  88. Encoding ascii_encoding = Encoding.ASCII;
  89. byte[] bytes = new Byte[1];
  90. int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
  91. Assert.AreEqual ('e', (char) bytes [0], "#1");
  92. }
  93. [Test] // Test GetBytes(string)
  94. public void TestGetBytes6 ()
  95. {
  96. Encoding ascii_encoding = Encoding.ASCII;
  97. byte[] bytes = ascii_encoding.GetBytes("Test");
  98. for (int i = 0; i < testchars.Length; i++)
  99. Assert.AreEqual (testchars [i], (char) bytes [i]);
  100. }
  101. [Test] // Test GetChars(byte[])
  102. public void TestGetChars1 ()
  103. {
  104. Encoding ascii_encoding = Encoding.ASCII;
  105. char[] chars = ascii_encoding.GetChars(testbytes);
  106. for (int i = 0; i < testbytes.Length; i++)
  107. Assert.AreEqual (testbytes[i], (byte) chars[i]);
  108. }
  109. [Test] // Test GetChars(byte[], int, int)
  110. public void TestGetChars2 ()
  111. {
  112. Encoding ascii_encoding = Encoding.ASCII;
  113. char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
  114. Assert.AreEqual (1, chars.Length, "#1");
  115. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  116. }
  117. [Test] // Test non-ASCII char in byte[]
  118. public void TestGetChars3 ()
  119. {
  120. Encoding ascii_encoding = Encoding.ASCII;
  121. testbytes[2] = 0x80;
  122. char[] chars = ascii_encoding.GetChars(testbytes);
  123. Assert.AreEqual ('T', chars [0], "#1");
  124. Assert.AreEqual ('e', chars [1], "#2");
  125. Assert.AreEqual ('?', chars [2], "#3");
  126. Assert.AreEqual ('t', chars [3], "#4");
  127. }
  128. [Test] // Test GetChars(byte[], int, int, char[], int)
  129. public void TestGetChars4 ()
  130. {
  131. Encoding ascii_encoding = Encoding.ASCII;
  132. char[] chars = new char[1];
  133. int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
  134. Assert.AreEqual (1, cnt, "#1");
  135. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  136. }
  137. [Test] // Test GetString(char[])
  138. public void TestGetString1 ()
  139. {
  140. Encoding ascii_encoding = Encoding.ASCII;
  141. string str = ascii_encoding.GetString(testbytes);
  142. Assert.AreEqual ("Test", str);
  143. }
  144. [Test] // Test GetString(char[], int, int)
  145. public void TestGetString2 ()
  146. {
  147. Encoding ascii_encoding = Encoding.ASCII;
  148. string str = ascii_encoding.GetString(testbytes, 1, 2);
  149. Assert.AreEqual ("es", str);
  150. }
  151. [Test] // Test invalid byte handling
  152. public void TestGetString3 ()
  153. {
  154. Encoding encoding = Encoding.ASCII;
  155. byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
  156. string s = encoding.GetString (bytes, 0, 3);
  157. #if NET_2_0
  158. Assert.AreEqual ("a??", s);
  159. #else
  160. Assert.AreEqual ("aab", s);
  161. #endif
  162. }
  163. [Test] // Test Decoder
  164. public void TestDecoder ()
  165. {
  166. Encoding ascii_encoding = Encoding.ASCII;
  167. char[] chars = new char[1];
  168. int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
  169. Assert.AreEqual (1, cnt, "#1");
  170. Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
  171. }
  172. [Test] // Test Decoder
  173. public void TestEncoder ()
  174. {
  175. Encoding ascii_encoding = Encoding.ASCII;
  176. byte[] bytes = new Byte[1];
  177. int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
  178. Assert.AreEqual (1, cnt, "#1");
  179. Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
  180. }
  181. [Test]
  182. public void TestZero ()
  183. {
  184. Encoding encoding = Encoding.ASCII;
  185. Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
  186. Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
  187. }
  188. #if NET_2_0
  189. [Test]
  190. [ExpectedException (typeof (DecoderFallbackException))]
  191. public void DecoderFallback ()
  192. {
  193. Encoding e = Encoding.ASCII.Clone () as Encoding;
  194. e.DecoderFallback = new DecoderExceptionFallback ();
  195. e.GetChars (new byte [] {0x80});
  196. }
  197. #endif
  198. }
  199. }