DecoderTest.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // DecoderTest.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 DecoderTest
  16. {
  17. #if NET_2_0
  18. [Test]
  19. [ExpectedException (typeof (ArgumentNullException))]
  20. public void ConvertNullChars ()
  21. {
  22. int charsUsed, bytesUsed;
  23. bool done;
  24. Encoding.UTF8.GetDecoder ().Convert (
  25. null, 0, 100, new char [100], 0, 100, false,
  26. out charsUsed, out bytesUsed, out done);
  27. }
  28. [Test]
  29. [ExpectedException (typeof (ArgumentNullException))]
  30. public void ConvertNullBytes ()
  31. {
  32. int charsUsed, bytesUsed;
  33. bool done;
  34. Encoding.UTF8.GetDecoder ().Convert (
  35. new byte [100], 0, 100, null, 0, 100, false,
  36. out charsUsed, out bytesUsed, out done);
  37. }
  38. [Test]
  39. public void ConvertLimitedDestination ()
  40. {
  41. char [] chars = new char [10000];
  42. byte [] bytes = new byte [10000];
  43. Decoder conv = Encoding.UTF8.GetDecoder ();
  44. int charsUsed, bytesUsed;
  45. bool done;
  46. conv.Convert (bytes, 0, 10000, chars, 0, 1000, true,
  47. out charsUsed, out bytesUsed, out done);
  48. Assert.IsFalse (done, "#1");
  49. Assert.AreEqual (625, charsUsed, "#2");
  50. Assert.AreEqual (625, bytesUsed, "#3");
  51. }
  52. [Test]
  53. public void CustomEncodingGetDecoder ()
  54. {
  55. var encoding = new CustomEncoding ();
  56. var decoder = encoding.GetDecoder ();
  57. Assert.IsNotNull (decoder);
  58. }
  59. class CustomEncoding : Encoding {
  60. public override int GetByteCount (char [] chars, int index, int count)
  61. {
  62. throw new NotSupportedException ();
  63. }
  64. public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
  65. {
  66. throw new NotSupportedException ();
  67. }
  68. public override int GetCharCount (byte [] bytes, int index, int count)
  69. {
  70. throw new NotSupportedException ();
  71. }
  72. public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
  73. {
  74. throw new NotSupportedException ();
  75. }
  76. public override int GetMaxByteCount (int charCount)
  77. {
  78. throw new NotSupportedException ();
  79. }
  80. public override int GetMaxCharCount (int byteCount)
  81. {
  82. throw new NotSupportedException ();
  83. }
  84. }
  85. #endif
  86. [Test]
  87. public void Bug10789 ()
  88. {
  89. byte[] bytes = new byte[100];
  90. char[] chars = new char[100];
  91. Decoder conv = Encoding.UTF8.GetDecoder ();
  92. int charsUsed, bytesUsed;
  93. bool completed;
  94. conv.Convert (bytes, 0, 0, chars, 100, 0, false, out bytesUsed, out charsUsed, out completed);
  95. Assert.IsTrue (completed, "#1");
  96. Assert.AreEqual (0, charsUsed, "#2");
  97. Assert.AreEqual (0, bytesUsed, "#3");
  98. }
  99. }
  100. }