UTF7EncodingTest.cs 8.7 KB

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