UTF7EncodingTest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // UTF7EncodingTest.cs - NUnit Test Cases for System.Text.UTF7Encoding
  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 UTF7EncodingTest
  14. {
  15. [Test]
  16. public void TestDirectlyEncoded1()
  17. {
  18. // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
  19. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
  20. byte[] UTF7Bytes = null;
  21. UTF7Encoding UTF7enc = new UTF7Encoding ();
  22. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  23. Assertion.AssertEquals ("UTF7 #1", 0x61, UTF7Bytes [0]);
  24. Assertion.AssertEquals ("UTF7 #2", 0x7A, UTF7Bytes [1]);
  25. Assertion.AssertEquals ("UTF7 #3", 0x41, UTF7Bytes [2]);
  26. Assertion.AssertEquals ("UTF7 #4", 0x5A, UTF7Bytes [3]);
  27. Assertion.AssertEquals ("UTF7 #5", 0x30, UTF7Bytes [4]);
  28. Assertion.AssertEquals ("UTF7 #6", 0x39, UTF7Bytes [5]);
  29. Assertion.AssertEquals ("UTF7 #7", 0x27, UTF7Bytes [6]);
  30. Assertion.AssertEquals ("UTF7 #8", 0x3F, UTF7Bytes [7]);
  31. }
  32. [Test]
  33. public void TestDirectlyEncoded2()
  34. {
  35. // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
  36. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
  37. byte[] UTF7Bytes = new byte [8];
  38. int Length = UniCodeString.Length;
  39. UTF7Encoding UTF7enc = new UTF7Encoding ();
  40. int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
  41. Assertion.AssertEquals ("UTF7 #1", 0x61, UTF7Bytes [0]);
  42. Assertion.AssertEquals ("UTF7 #2", 0x7A, UTF7Bytes [1]);
  43. Assertion.AssertEquals ("UTF7 #3", 0x41, UTF7Bytes [2]);
  44. Assertion.AssertEquals ("UTF7 #4", 0x5A, UTF7Bytes [3]);
  45. Assertion.AssertEquals ("UTF7 #5", 0x30, UTF7Bytes [4]);
  46. Assertion.AssertEquals ("UTF7 #6", 0x39, UTF7Bytes [5]);
  47. Assertion.AssertEquals ("UTF7 #7", 0x27, UTF7Bytes [6]);
  48. Assertion.AssertEquals ("UTF7 #8", 0x3F, UTF7Bytes [7]);
  49. }
  50. [Test]
  51. public void TestEncodeOptionalEncoded()
  52. {
  53. string UniCodeString = "\u0021\u0026\u002A\u003B";
  54. byte[] UTF7Bytes = null;
  55. //Optional Characters are allowed.
  56. UTF7Encoding UTF7enc = new UTF7Encoding (true);
  57. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  58. Assertion.AssertEquals ("UTF7 #1", 0x21, UTF7Bytes [0]);
  59. Assertion.AssertEquals ("UTF7 #2", 0x26, UTF7Bytes [1]);
  60. Assertion.AssertEquals ("UTF7 #3", 0x2A, UTF7Bytes [2]);
  61. Assertion.AssertEquals ("UTF7 #4", 0x3B, UTF7Bytes [3]);
  62. //Optional characters are not allowed.
  63. UTF7enc = new UTF7Encoding (false);
  64. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  65. Assertion.AssertEquals ("UTF7 #5", 0x2B, UTF7Bytes [0]);
  66. Assertion.AssertEquals ("UTF7 #6", 0x41, UTF7Bytes [1]);
  67. Assertion.AssertEquals ("UTF7 #7", 0x43, UTF7Bytes [2]);
  68. Assertion.AssertEquals ("UTF7 #8", 0x45, UTF7Bytes [3]);
  69. Assertion.AssertEquals ("UTF7 #6", 0x41, UTF7Bytes [1]);
  70. }
  71. [Test]
  72. public void TestEncodeUnicodeShifted1()
  73. {
  74. string UniCodeString = "\u0041\u2262\u0391\u002E";
  75. byte[] UTF7Bytes = null;
  76. UTF7Encoding UTF7enc = new UTF7Encoding();
  77. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  78. //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
  79. Assertion.AssertEquals ("UTF7 #1", 0x41, UTF7Bytes [0]);
  80. Assertion.AssertEquals ("UTF7 #2", 0x2B, UTF7Bytes [1]);
  81. Assertion.AssertEquals ("UTF7 #3", 0x49, UTF7Bytes [2]);
  82. Assertion.AssertEquals ("UTF7 #4", 0x6D, UTF7Bytes [3]);
  83. Assertion.AssertEquals ("UTF7 #5", 0x49, UTF7Bytes [4]);
  84. Assertion.AssertEquals ("UTF7 #6", 0x44, UTF7Bytes [5]);
  85. Assertion.AssertEquals ("UTF7 #7", 0x6B, UTF7Bytes [6]);
  86. Assertion.AssertEquals ("UTF7 #8", 0x51, UTF7Bytes [7]);
  87. Assertion.AssertEquals ("UTF7 #9", 0x2D, UTF7Bytes [8]);
  88. Assertion.AssertEquals ("UTF7 #10", 0x2E, UTF7Bytes [9]);
  89. }
  90. [Test]
  91. public void TestEncodeUnicodeShifted2()
  92. {
  93. string UniCodeString = "\u0041\u2262\u0391\u002E";
  94. byte[] UTF7Bytes = new byte [10];
  95. int Length = UniCodeString.Length;
  96. UTF7Encoding UTF7enc = new UTF7Encoding ();
  97. int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
  98. //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
  99. Assertion.AssertEquals ("UTF7 #1", 0x41, UTF7Bytes [0]);
  100. Assertion.AssertEquals ("UTF7 #2", 0x2B, UTF7Bytes [1]);
  101. Assertion.AssertEquals ("UTF7 #3", 0x49, UTF7Bytes [2]);
  102. Assertion.AssertEquals ("UTF7 #4", 0x6D, UTF7Bytes [3]);
  103. Assertion.AssertEquals ("UTF7 #5", 0x49, UTF7Bytes [4]);
  104. Assertion.AssertEquals ("UTF7 #6", 0x44, UTF7Bytes [5]);
  105. Assertion.AssertEquals ("UTF7 #7", 0x6B, UTF7Bytes [6]);
  106. Assertion.AssertEquals ("UTF7 #8", 0x51, UTF7Bytes [7]);
  107. Assertion.AssertEquals ("UTF7 #9", 0x2D, UTF7Bytes [8]);
  108. Assertion.AssertEquals ("UTF7 #10", 0x2E, UTF7Bytes [9]);
  109. }
  110. [Test]
  111. public void TestDecodeUnicodeShifted1()
  112. {
  113. string UniCodeString = "\u0041\u2262\u0391\u002E";
  114. byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E};
  115. UTF7Encoding UTF7enc = new UTF7Encoding ();
  116. char[] UniCodeChars = UTF7enc.GetChars (UTF7Bytes);
  117. //A+ImIDkQ-. is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642
  118. Assertion.AssertEquals ("UTF #1", UniCodeString.ToCharArray() [0], UniCodeChars [0]);
  119. Assertion.AssertEquals ("UTF #2", UniCodeString.ToCharArray() [1], UniCodeChars [1]);
  120. Assertion.AssertEquals ("UTF #3", UniCodeString.ToCharArray() [2], UniCodeChars [2]);
  121. Assertion.AssertEquals ("UTF #4", UniCodeString.ToCharArray() [3], UniCodeChars [3]);
  122. }
  123. [Test]
  124. public void TestMaxCharCount()
  125. {
  126. UTF7Encoding UTF7enc = new UTF7Encoding ();
  127. Assertion.AssertEquals ("UTF #1", 50, UTF7enc.GetMaxCharCount(50));
  128. }
  129. [Test]
  130. public void TestMaxByteCount()
  131. {
  132. UTF7Encoding UTF7enc = new UTF7Encoding ();
  133. Assertion.AssertEquals ("UTF #1", 136, UTF7enc.GetMaxByteCount(50));
  134. }
  135. }
  136. }