UnicodeEncodingTest.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // UnicodeEncodingTest.cs - NUnit Test Cases for System.Text.UnicodeEncoding
  3. //
  4. // Author:
  5. // Patrick Kalkman [email protected]
  6. //
  7. // (C) 2003 Patrick Kalkman
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Text;
  12. namespace MonoTests.System.Text
  13. {
  14. [TestFixture]
  15. public class UnicodeEncodingTest
  16. {
  17. [Test]
  18. public void IsBrowserDisplay ()
  19. {
  20. UnicodeEncoding enc = new UnicodeEncoding ();
  21. Assert.IsFalse (enc.IsBrowserDisplay);
  22. }
  23. [Test]
  24. public void IsBrowserSave ()
  25. {
  26. UnicodeEncoding enc = new UnicodeEncoding ();
  27. Assert.IsTrue (enc.IsBrowserSave);
  28. }
  29. [Test]
  30. public void IsMailNewsDisplay ()
  31. {
  32. UnicodeEncoding enc = new UnicodeEncoding ();
  33. Assert.IsFalse (enc.IsMailNewsDisplay);
  34. }
  35. [Test]
  36. public void IsMailNewsSave ()
  37. {
  38. UnicodeEncoding enc = new UnicodeEncoding ();
  39. Assert.IsFalse (enc.IsMailNewsSave);
  40. }
  41. [Test]
  42. public void TestEncodingGetBytes1()
  43. {
  44. //pi and sigma in unicode
  45. string Unicode = "\u03a0\u03a3";
  46. byte[] UniBytes;
  47. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
  48. UniBytes = UnicodeEnc.GetBytes (Unicode);
  49. Assert.AreEqual (0xA0, UniBytes [0], "Uni #1");
  50. Assert.AreEqual (0x03, UniBytes [1], "Uni #2");
  51. Assert.AreEqual (0xA3, UniBytes [2], "Uni #3");
  52. Assert.AreEqual (0x03, UniBytes [3], "Uni #4");
  53. }
  54. [Test]
  55. public void TestEncodingGetBytes2()
  56. {
  57. //pi and sigma in unicode
  58. string Unicode = "\u03a0\u03a3";
  59. byte[] UniBytes;
  60. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
  61. UniBytes = UnicodeEnc.GetBytes (Unicode);
  62. Assert.AreEqual (0x03, UniBytes [0], "Uni #1");
  63. Assert.AreEqual (0xA0, UniBytes [1], "Uni #2");
  64. Assert.AreEqual (0x03, UniBytes [2], "Uni #3");
  65. Assert.AreEqual (0xA3, UniBytes [3], "Uni #4");
  66. }
  67. [Test]
  68. public void TestEncodingGetBytes3()
  69. {
  70. //pi and sigma in unicode
  71. string Unicode = "\u03a0\u03a3";
  72. byte[] UniBytes = new byte [4];
  73. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
  74. int Cnt = UnicodeEnc.GetBytes (Unicode.ToCharArray(), 0, Unicode.Length, UniBytes, 0);
  75. Assert.AreEqual (4, Cnt, "Uni #1");
  76. Assert.AreEqual (0xA0, UniBytes [0], "Uni #2");
  77. Assert.AreEqual (0x03, UniBytes [1], "Uni #3");
  78. Assert.AreEqual (0xA3, UniBytes [2], "Uni #4");
  79. Assert.AreEqual (0x03, UniBytes [3], "Uni #5");
  80. }
  81. [Test]
  82. public void TestEncodingDecodingGetBytes1()
  83. {
  84. //pi and sigma in unicode
  85. string Unicode = "\u03a0\u03a3";
  86. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
  87. //Encode the unicode string.
  88. byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
  89. //Decode the bytes to a unicode char array.
  90. char[] UniChars = UnicodeEnc.GetChars (UniBytes);
  91. string Result = new string(UniChars);
  92. Assert.AreEqual (Unicode, Result, "Uni #1");
  93. }
  94. [Test]
  95. public void TestEncodingDecodingGetBytes2()
  96. {
  97. //pi and sigma in unicode
  98. string Unicode = "\u03a0\u03a3";
  99. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
  100. //Encode the unicode string.
  101. byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
  102. //Decode the bytes to a unicode char array.
  103. char[] UniChars = UnicodeEnc.GetChars (UniBytes);
  104. string Result = new string(UniChars);
  105. Assert.AreEqual (Unicode, Result, "Uni #1");
  106. }
  107. [Test]
  108. public void TestEncodingGetCharCount ()
  109. {
  110. byte[] b = new byte[] {255, 254, 115, 0, 104, 0, 105, 0};
  111. UnicodeEncoding encoding = new UnicodeEncoding ();
  112. Assert.AreEqual (3, encoding.GetCharCount (b, 2, b.Length - 2),
  113. "GetCharCount #1");
  114. }
  115. [Test]
  116. public void TestPreamble1()
  117. {
  118. //litle-endian with byte order mark.
  119. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, true);
  120. byte[] PreAmble = UnicodeEnc.GetPreamble();
  121. Assert.AreEqual (0xFF, PreAmble [0], "Uni #1");
  122. Assert.AreEqual (0xFE, PreAmble [1], "Uni #2");
  123. }
  124. [Test]
  125. public void TestPreamble2()
  126. {
  127. //big-endian with byte order mark.
  128. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true);
  129. byte[] PreAmble = UnicodeEnc.GetPreamble();
  130. Assert.AreEqual (0xFE, PreAmble [0], "Uni #1");
  131. Assert.AreEqual (0xFF, PreAmble [1], "Uni #2");
  132. }
  133. [Test]
  134. public void TestPreamble3()
  135. {
  136. //little-endian without byte order mark.
  137. UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, false);
  138. byte[] PreAmble = UnicodeEnc.GetPreamble();
  139. Assert.AreEqual (0, PreAmble.Length, "Uni #1");
  140. }
  141. [Test]
  142. #if NET_2_0
  143. [Category ("NotWorking")]
  144. #endif
  145. public void TestMaxCharCount()
  146. {
  147. UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
  148. #if NET_2_0
  149. // where is this extra 1 coming from?
  150. Assert.AreEqual (26, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
  151. Assert.AreEqual (27, UnicodeEnc.GetMaxCharCount(51), "UTF #2");
  152. #else
  153. Assert.AreEqual (25, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
  154. #endif
  155. }
  156. [Test]
  157. #if NET_2_0
  158. [Category ("NotWorking")]
  159. #endif
  160. public void TestMaxByteCount()
  161. {
  162. UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
  163. #if NET_2_0
  164. // is this extra 2 BOM?
  165. Assert.AreEqual (102, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
  166. #else
  167. Assert.AreEqual (100, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
  168. #endif
  169. }
  170. [Test]
  171. public void ZeroLengthArrays ()
  172. {
  173. UnicodeEncoding encoding = new UnicodeEncoding ();
  174. encoding.GetCharCount (new byte [0]);
  175. encoding.GetChars (new byte [0]);
  176. encoding.GetCharCount (new byte [0], 0, 0);
  177. encoding.GetChars (new byte [0], 0, 0);
  178. encoding.GetChars (new byte [0], 0, 0, new char [0], 0);
  179. encoding.GetByteCount (new char [0]);
  180. encoding.GetBytes (new char [0]);
  181. encoding.GetByteCount (new char [0], 0, 0);
  182. encoding.GetBytes (new char [0], 0, 0);
  183. encoding.GetBytes (new char [0], 0, 0, new byte [0], 0);
  184. encoding.GetByteCount ("");
  185. encoding.GetBytes ("");
  186. }
  187. [Test]
  188. public void ByteOrderMark ()
  189. {
  190. string littleEndianString = "\ufeff\u0042\u004f\u004d";
  191. string bigEndianString = "\ufffe\u4200\u4f00\u4d00";
  192. byte [] littleEndianBytes = new byte [] {0xff, 0xfe, 0x42, 0x00, 0x4f, 0x00, 0x4d, 0x00};
  193. byte [] bigEndianBytes = new byte [] {0xfe, 0xff, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4d};
  194. UnicodeEncoding encoding;
  195. encoding = new UnicodeEncoding (false, true);
  196. Assert.AreEqual (encoding.GetBytes (littleEndianString), littleEndianBytes, "BOM #1");
  197. Assert.AreEqual (encoding.GetBytes (bigEndianString), bigEndianBytes, "BOM #2");
  198. Assert.AreEqual (encoding.GetString (littleEndianBytes), littleEndianString, "BOM #3");
  199. Assert.AreEqual (encoding.GetString (bigEndianBytes), bigEndianString, "BOM #4");
  200. encoding = new UnicodeEncoding (true, true);
  201. Assert.AreEqual (encoding.GetBytes (littleEndianString), bigEndianBytes, "BOM #5");
  202. Assert.AreEqual (encoding.GetBytes (bigEndianString), littleEndianBytes, "BOM #6");
  203. Assert.AreEqual (encoding.GetString (littleEndianBytes), bigEndianString, "BOM #7");
  204. Assert.AreEqual (encoding.GetString (bigEndianBytes), littleEndianString, "BOM #8");
  205. }
  206. [Test]
  207. [Category ("NotWorking")]
  208. public void GetString_Odd_Count_0 ()
  209. {
  210. byte [] array = new byte [3];
  211. string s = Encoding.Unicode.GetString (array, 0, 3);
  212. Assert.AreEqual (0, (int) s [0], "0");
  213. Assert.AreEqual (2, s.Length, "Length");
  214. Assert.AreEqual (65533, (int) s [1], "1");
  215. }
  216. [Test]
  217. [Category ("NotWorking")]
  218. public void GetString_Odd_Count_ff ()
  219. {
  220. byte [] array = new byte [3] { 0xff, 0xff, 0xff };
  221. string s = Encoding.Unicode.GetString (array, 0, 3);
  222. Assert.AreEqual (65535, (int) s [0], "0");
  223. Assert.AreEqual (2, s.Length, "Length");
  224. Assert.AreEqual (65533, (int) s [1], "1");
  225. }
  226. }
  227. }