EncoderTest.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // EncoderTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2006 Novell, Inc.
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Text;
  12. namespace MonoTests.System.Text
  13. {
  14. [TestFixture]
  15. public class EncoderTest
  16. {
  17. #if NET_2_0
  18. [Test]
  19. [ExpectedException (typeof (ArgumentNullException))]
  20. public void ConvertNullChars ()
  21. {
  22. int bytesUsed, charsUsed;
  23. bool done;
  24. Encoding.UTF8.GetEncoder ().Convert (
  25. null, 0, 100, new byte [100], 0, 100, false,
  26. out bytesUsed, out charsUsed, out done);
  27. }
  28. [Test]
  29. [ExpectedException (typeof (ArgumentNullException))]
  30. public void ConvertNullBytes ()
  31. {
  32. int bytesUsed, charsUsed;
  33. bool done;
  34. Encoding.UTF8.GetEncoder ().Convert (
  35. new char [100], 0, 100, null, 0, 100, false,
  36. out bytesUsed, out charsUsed, out done);
  37. }
  38. [Test]
  39. public void ConvertLimitedDestination ()
  40. {
  41. byte [] bytes = new byte [10000];
  42. char [] chars = new char [10000];
  43. Encoder conv = Encoding.UTF8.GetEncoder ();
  44. int bytesUsed, charsUsed;
  45. bool done;
  46. conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
  47. out bytesUsed, out charsUsed, out done);
  48. Assert.IsFalse (done, "#1");
  49. Assert.AreEqual (625, bytesUsed, "#2");
  50. Assert.AreEqual (625, charsUsed, "#3");
  51. }
  52. [Test]
  53. public void CustomEncodingGetEncoder ()
  54. {
  55. var encoding = new CustomEncoding ();
  56. var encoder = encoding.GetEncoder ();
  57. Assert.IsNotNull (encoder);
  58. }
  59. [Test]
  60. public void ConvertZeroCharacters ()
  61. {
  62. int charsUsed, bytesUsed;
  63. bool completed;
  64. byte [] bytes = new byte [0];
  65. Encoding.UTF8.GetEncoder ().Convert (
  66. new char[0], 0, 0, bytes, 0, bytes.Length, true,
  67. out charsUsed, out bytesUsed, out completed);
  68. Assert.IsTrue (completed, "#1");
  69. Assert.AreEqual (0, charsUsed, "#2");
  70. Assert.AreEqual (0, bytesUsed, "#3");
  71. }
  72. class CustomEncoding : Encoding {
  73. public override int GetByteCount (char [] chars, int index, int count)
  74. {
  75. throw new NotSupportedException ();
  76. }
  77. public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
  78. {
  79. throw new NotSupportedException ();
  80. }
  81. public override int GetCharCount (byte [] bytes, int index, int count)
  82. {
  83. throw new NotSupportedException ();
  84. }
  85. public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
  86. {
  87. throw new NotSupportedException ();
  88. }
  89. public override int GetMaxByteCount (int charCount)
  90. {
  91. throw new NotSupportedException ();
  92. }
  93. public override int GetMaxCharCount (int byteCount)
  94. {
  95. throw new NotSupportedException ();
  96. }
  97. }
  98. #endif
  99. }
  100. }