UTF32EncodingTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #if NET_2_0
  2. using System;
  3. using System.Text;
  4. using NUnit.Framework;
  5. namespace MonoTests.System.Text
  6. {
  7. [TestFixture]
  8. public class UTF32EncodingTest
  9. {
  10. [Test] // GetByteCount (Char [])
  11. [Category ("NotDotNet")] // A1/B1 return 24 on MS
  12. public void GetByteCount1 ()
  13. {
  14. char [] chars = new char[] { 'z', 'a', '\u0306',
  15. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  16. UTF32Encoding le = new UTF32Encoding (false, true);
  17. Assert.AreEqual (28, le.GetByteCount (chars), "#A1");
  18. Assert.AreEqual (0, le.GetByteCount (new char [0]), "#A2");
  19. UTF32Encoding be = new UTF32Encoding (true, true);
  20. Assert.AreEqual (28, be.GetByteCount (chars), "#B1");
  21. Assert.AreEqual (0, be.GetByteCount (new char [0]), "#B2");
  22. }
  23. [Test] // GetByteCount (Char [])
  24. public void GetByteCount1_Chars_Null ()
  25. {
  26. UTF32Encoding enc = new UTF32Encoding ();
  27. try {
  28. enc.GetByteCount ((Char []) null);
  29. Assert.Fail ("#1");
  30. } catch (ArgumentNullException ex) {
  31. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  32. Assert.IsNull (ex.InnerException, "#3");
  33. Assert.IsNotNull (ex.Message, "#4");
  34. Assert.AreEqual ("chars", ex.ParamName, "#5");
  35. }
  36. }
  37. [Test] // GetByteCount (String)
  38. [Category ("NotDotNet")] // A1/B1 return 24 on MS
  39. public void GetByteCount2 ()
  40. {
  41. string s = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
  42. UTF32Encoding le = new UTF32Encoding (false, true);
  43. Assert.AreEqual (28, le.GetByteCount (s), "#A1");
  44. Assert.AreEqual (0, le.GetByteCount (string.Empty), "#A2");
  45. UTF32Encoding be = new UTF32Encoding (true, true);
  46. Assert.AreEqual (28, be.GetByteCount (s), "#B1");
  47. Assert.AreEqual (0, be.GetByteCount (string.Empty), "#B2");
  48. }
  49. [Test] // GetByteCount (String)
  50. public void GetByteCount2_S_Null ()
  51. {
  52. UTF32Encoding enc = new UTF32Encoding ();
  53. try {
  54. enc.GetByteCount ((string) null);
  55. Assert.Fail ("#1");
  56. } catch (ArgumentNullException ex) {
  57. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  58. Assert.IsNull (ex.InnerException, "#3");
  59. Assert.IsNotNull (ex.Message, "#4");
  60. Assert.AreEqual ("s", ex.ParamName, "#5");
  61. }
  62. }
  63. [Test] // GetByteCount (Char *)
  64. public unsafe void GetByteCount3 ()
  65. {
  66. char [] chars = new char[] { 'z', 'a', '\u0306',
  67. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  68. fixed (char* cp = chars) {
  69. UTF32Encoding le = new UTF32Encoding (false, true);
  70. Assert.AreEqual (12, le.GetByteCount (cp, 3), "#A1");
  71. Assert.AreEqual (4, le.GetByteCount (cp, 1), "#A2");
  72. Assert.AreEqual (0, le.GetByteCount (cp, 0), "#A3");
  73. Assert.AreEqual (24, le.GetByteCount (cp, 6), "#A4");
  74. //Assert.AreEqual (24, le.GetByteCount (cp, 7), "#A5");
  75. UTF32Encoding be = new UTF32Encoding (true, true);
  76. Assert.AreEqual (12, be.GetByteCount (cp, 3), "#B1");
  77. Assert.AreEqual (4, be.GetByteCount (cp, 1), "#B2");
  78. Assert.AreEqual (0, be.GetByteCount (cp, 0), "#B3");
  79. Assert.AreEqual (24, be.GetByteCount (cp, 6), "#B4");
  80. //Assert.AreEqual (24, be.GetByteCount (cp, 7), "#B5");
  81. }
  82. }
  83. [Test] // GetByteCount (Char *)
  84. public unsafe void GetByteCount3_Chars_Null ()
  85. {
  86. UTF32Encoding enc = new UTF32Encoding ();
  87. try {
  88. enc.GetByteCount ((char *) null, 1);
  89. Assert.Fail ("#1");
  90. } catch (ArgumentNullException ex) {
  91. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  92. Assert.IsNull (ex.InnerException, "#3");
  93. Assert.IsNotNull (ex.Message, "#4");
  94. Assert.AreEqual ("chars", ex.ParamName, "#5");
  95. }
  96. }
  97. [Test] // GetByteCount (Char [], Int32, Int32)
  98. public void GetByteCount4 ()
  99. {
  100. char [] chars = new char[] { 'z', 'a', '\u0306',
  101. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  102. UTF32Encoding le = new UTF32Encoding (false, true);
  103. Assert.AreEqual (12, le.GetByteCount (chars, 0, 3), "#A1");
  104. Assert.AreEqual (16, le.GetByteCount (chars, 2, 4), "#A2");
  105. Assert.AreEqual (4, le.GetByteCount (chars, 4, 1), "#A3");
  106. Assert.AreEqual (4, le.GetByteCount (chars, 6, 1), "#A4");
  107. Assert.AreEqual (0, le.GetByteCount (chars, 6, 0), "#A5");
  108. Assert.AreEqual (24, le.GetByteCount (chars, 0, 6), "#A6");
  109. //Assert.AreEqual (24, le.GetByteCount (chars, 0, 7), "#A7");
  110. UTF32Encoding be = new UTF32Encoding (true, true);
  111. Assert.AreEqual (12, be.GetByteCount (chars, 0, 3), "#B1");
  112. Assert.AreEqual (16, be.GetByteCount (chars, 2, 4), "#B2");
  113. Assert.AreEqual (4, be.GetByteCount (chars, 4, 1), "#B3");
  114. Assert.AreEqual (4, be.GetByteCount (chars, 6, 1), "#B4");
  115. Assert.AreEqual (0, be.GetByteCount (chars, 6, 0), "#B5");
  116. Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B6");
  117. //Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B7");
  118. }
  119. [Test] // GetByteCount (Char [], Int32, Int32)
  120. public void GetByteCount4_Chars_Null ()
  121. {
  122. UTF32Encoding enc = new UTF32Encoding ();
  123. try {
  124. enc.GetByteCount ((Char []) null, 0, 1);
  125. Assert.Fail ("#1");
  126. } catch (ArgumentNullException ex) {
  127. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  128. Assert.IsNull (ex.InnerException, "#3");
  129. Assert.IsNotNull (ex.Message, "#4");
  130. Assert.AreEqual ("chars", ex.ParamName, "#5");
  131. }
  132. }
  133. [Test] // GetByteCount (Char [], Int32, Int32)
  134. public void GetByteCount4_Count_Negative ()
  135. {
  136. char [] chars = new char[] { 'z', 'a', '\u0306',
  137. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  138. UTF32Encoding enc = new UTF32Encoding ();
  139. try {
  140. enc.GetByteCount (chars, 1, -1);
  141. Assert.Fail ("#1");
  142. } catch (ArgumentOutOfRangeException ex) {
  143. // Non-negative number required
  144. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  145. Assert.IsNull (ex.InnerException, "#3");
  146. Assert.IsNotNull (ex.Message, "#4");
  147. Assert.AreEqual ("count", ex.ParamName, "#5");
  148. }
  149. }
  150. [Test] // GetByteCount (Char [], Int32, Int32)
  151. public void GetByteCount4_Count_Overflow ()
  152. {
  153. char [] chars = new char[] { 'z', 'a', '\u0306',
  154. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  155. UTF32Encoding enc = new UTF32Encoding ();
  156. try {
  157. enc.GetByteCount (chars, 6, 2);
  158. Assert.Fail ("#1");
  159. } catch (ArgumentOutOfRangeException ex) {
  160. // Index and count must refer to a location
  161. // within the buffer
  162. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  163. Assert.IsNull (ex.InnerException, "#3");
  164. Assert.IsNotNull (ex.Message, "#4");
  165. //Assert.AreEqual ("chars", ex.ParamName, "#5");
  166. }
  167. }
  168. [Test] // GetByteCount (Char [], Int32, Int32)
  169. public void GetByteCount4_Index_Negative ()
  170. {
  171. char [] chars = new char[] { 'z', 'a', '\u0306',
  172. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  173. UTF32Encoding enc = new UTF32Encoding ();
  174. try {
  175. enc.GetByteCount (chars, -1, 1);
  176. Assert.Fail ("#1");
  177. } catch (ArgumentOutOfRangeException ex) {
  178. // Non-negative number required
  179. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  180. Assert.IsNull (ex.InnerException, "#3");
  181. Assert.IsNotNull (ex.Message, "#4");
  182. Assert.AreEqual ("index", ex.ParamName, "#5");
  183. }
  184. }
  185. [Test] // GetByteCount (Char [], Int32, Int32)
  186. public void GetByteCount4_Index_Overflow ()
  187. {
  188. char [] chars = new char[] { 'z', 'a', '\u0306',
  189. '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
  190. UTF32Encoding enc = new UTF32Encoding ();
  191. try {
  192. enc.GetByteCount (chars, 7, 1);
  193. Assert.Fail ("#1");
  194. } catch (ArgumentOutOfRangeException ex) {
  195. // Index and count must refer to a location
  196. // within the buffer
  197. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
  198. Assert.IsNull (ex.InnerException, "#3");
  199. Assert.IsNotNull (ex.Message, "#4");
  200. //Assert.AreEqual ("chars", ex.ParamName, "#5");
  201. }
  202. }
  203. [Test]
  204. public void GetPreamble ()
  205. {
  206. byte[] lePreamble = new UTF32Encoding(false, true).GetPreamble();
  207. Assert.AreEqual (new byte [] { 0xff, 0xfe, 0, 0 }, lePreamble, "#1");
  208. byte[] bePreamble = new UTF32Encoding(true, true).GetPreamble();
  209. Assert.AreEqual (new byte [] { 0, 0, 0xfe, 0xff }, bePreamble, "#2");
  210. }
  211. [Test]
  212. public void IsBrowserDisplay ()
  213. {
  214. UTF32Encoding le = new UTF32Encoding (false, true);
  215. Assert.IsFalse (le.IsBrowserDisplay, "#1");
  216. UTF32Encoding be = new UTF32Encoding (true, true);
  217. Assert.IsFalse (be.IsBrowserDisplay, "#2");
  218. }
  219. [Test]
  220. public void IsBrowserSave ()
  221. {
  222. UTF32Encoding le = new UTF32Encoding (false, true);
  223. Assert.IsFalse (le.IsBrowserSave);
  224. UTF32Encoding be = new UTF32Encoding (true, true);
  225. Assert.IsFalse (be.IsBrowserSave, "#2");
  226. }
  227. [Test]
  228. public void IsMailNewsDisplay ()
  229. {
  230. UTF32Encoding le = new UTF32Encoding (false, true);
  231. Assert.IsFalse (le.IsMailNewsDisplay);
  232. UTF32Encoding be = new UTF32Encoding (true, true);
  233. Assert.IsFalse (be.IsMailNewsDisplay, "#2");
  234. }
  235. [Test]
  236. public void IsMailNewsSave ()
  237. {
  238. UTF32Encoding le = new UTF32Encoding (false, true);
  239. Assert.IsFalse (le.IsMailNewsSave);
  240. UTF32Encoding be = new UTF32Encoding (true, true);
  241. Assert.IsFalse (be.IsMailNewsSave, "#2");
  242. }
  243. }
  244. }
  245. #endif