UTF8EncodingTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // UTF8EncodingTest.cs - NUnit Test Cases for System.Text.UTF8Encoding
  2. //
  3. // Patrick Kalkman [email protected]
  4. //
  5. // (C) 2003 Patrick Kalkman
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.Text;
  10. namespace MonoTests.System.Text
  11. {
  12. [TestFixture]
  13. public class UTF8EncodingTest
  14. {
  15. [SetUp]
  16. public void GetReady() {}
  17. [TearDown]
  18. public void Clean() {}
  19. [Test]
  20. public void TestEncodingGetBytes1()
  21. {
  22. UTF8Encoding utf8Enc = new UTF8Encoding ();
  23. string UniCode = "\u0041\u2262\u0391\u002E";
  24. // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E
  25. // see (RFC 2044)
  26. byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
  27. Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]);
  28. Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]);
  29. Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]);
  30. Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]);
  31. Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]);
  32. Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]);
  33. Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]);
  34. }
  35. [Test]
  36. public void TestEncodingGetBytes2()
  37. {
  38. UTF8Encoding utf8Enc = new UTF8Encoding ();
  39. string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
  40. // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21
  41. // see (RFC 2044)
  42. byte[] utf8Bytes = new byte [11];
  43. int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
  44. Assertion.AssertEquals ("UTF #1", 11, ByteCnt);
  45. Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]);
  46. Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]);
  47. Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]);
  48. Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]);
  49. Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]);
  50. Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]);
  51. Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]);
  52. Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]);
  53. Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]);
  54. Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]);
  55. Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]);
  56. }
  57. [Test]
  58. public void TestDecodingGetChars1()
  59. {
  60. UTF8Encoding utf8Enc = new UTF8Encoding ();
  61. // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>."
  62. // see (RFC 2044)
  63. byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
  64. char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
  65. Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]);
  66. Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]);
  67. Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]);
  68. Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]);
  69. }
  70. [Test]
  71. public void TestMaxCharCount()
  72. {
  73. UTF8Encoding UTF8enc = new UTF8Encoding ();
  74. Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
  75. }
  76. [Test]
  77. public void TestMaxByteCount()
  78. {
  79. UTF8Encoding UTF8enc = new UTF8Encoding ();
  80. Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
  81. }
  82. }
  83. }