ASCIIEncodingTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
  2. //
  3. // Author: Mike Kestner <[email protected]>
  4. //
  5. // <c> 2002 Mike Kestner
  6. using NUnit.Framework;
  7. using System.Text;
  8. using System;
  9. namespace MonoTests.System.Text {
  10. public class ASCIIEncodingTest : TestCase {
  11. private char[] testchars;
  12. private byte[] testbytes;
  13. protected override void SetUp ()
  14. {
  15. testchars = new char[4];
  16. testchars[0] = 'T';
  17. testchars[1] = 'e';
  18. testchars[2] = 's';
  19. testchars[3] = 't';
  20. testbytes = new byte[4];
  21. testbytes[0] = (byte) 'T';
  22. testbytes[1] = (byte) 'e';
  23. testbytes[2] = (byte) 's';
  24. testbytes[3] = (byte) 't';
  25. }
  26. // Test GetBytes(char[])
  27. public void TestGetBytes1 ()
  28. {
  29. Encoding ascii_encoding = Encoding.ASCII;
  30. byte[] bytes = ascii_encoding.GetBytes(testchars);
  31. for (int i = 0; i < testchars.Length; i++)
  32. AssertEquals (testchars[i], (char) bytes[i]);
  33. }
  34. // Test GetBytes(char[], int, int)
  35. public void TestGetBytes2 ()
  36. {
  37. Encoding ascii_encoding = Encoding.ASCII;
  38. byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
  39. AssertEquals (1, bytes.Length);
  40. AssertEquals (testchars[1], (char) bytes[0]);
  41. }
  42. // Test non-ASCII char in char[]
  43. public void TestGetBytes3 ()
  44. {
  45. Encoding ascii_encoding = Encoding.ASCII;
  46. testchars[2] = (char) 0x80;
  47. byte[] bytes = ascii_encoding.GetBytes(testchars);
  48. AssertEquals ('T', (char) bytes[0]);
  49. AssertEquals ('e', (char) bytes[1]);
  50. AssertEquals ('?', (char) bytes[2]);
  51. AssertEquals ('t', (char) bytes[3]);
  52. }
  53. // Test GetBytes(char[], int, int, byte[], int)
  54. public void TestGetBytes4 ()
  55. {
  56. Encoding ascii_encoding = Encoding.ASCII;
  57. byte[] bytes = new Byte[1];
  58. int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
  59. AssertEquals (1, cnt);
  60. AssertEquals (testchars[1], (char) bytes[0]);
  61. }
  62. // Test GetBytes(string, int, int, byte[], int)
  63. public void TestGetBytes5 ()
  64. {
  65. Encoding ascii_encoding = Encoding.ASCII;
  66. byte[] bytes = new Byte[1];
  67. int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
  68. AssertEquals ('e', (char) bytes[0]);
  69. }
  70. // Test GetBytes(string)
  71. public void TestGetBytes6 ()
  72. {
  73. Encoding ascii_encoding = Encoding.ASCII;
  74. byte[] bytes = ascii_encoding.GetBytes("Test");
  75. for (int i = 0; i < testchars.Length; i++)
  76. AssertEquals (testchars[i], (char) bytes[i]);
  77. }
  78. // Test GetChars(byte[])
  79. public void TestGetChars1 ()
  80. {
  81. Encoding ascii_encoding = Encoding.ASCII;
  82. char[] chars = ascii_encoding.GetChars(testbytes);
  83. for (int i = 0; i < testbytes.Length; i++)
  84. AssertEquals (testbytes[i], (byte) chars[i]);
  85. }
  86. // Test GetChars(byte[], int, int)
  87. public void TestGetChars2 ()
  88. {
  89. Encoding ascii_encoding = Encoding.ASCII;
  90. char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
  91. AssertEquals (1, chars.Length);
  92. AssertEquals (testbytes[1], (byte) chars[0]);
  93. }
  94. // Test non-ASCII char in byte[]
  95. public void TestGetChars3 ()
  96. {
  97. Encoding ascii_encoding = Encoding.ASCII;
  98. testbytes[2] = 0x80;
  99. char[] chars = ascii_encoding.GetChars(testbytes);
  100. AssertEquals ('T', chars[0]);
  101. AssertEquals ('e', chars[1]);
  102. AssertEquals ('?', chars[2]);
  103. AssertEquals ('t', chars[3]);
  104. }
  105. // Test GetChars(byte[], int, int, char[], int)
  106. public void TestGetChars4 ()
  107. {
  108. Encoding ascii_encoding = Encoding.ASCII;
  109. char[] chars = new char[1];
  110. int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
  111. AssertEquals (1, cnt);
  112. AssertEquals (testbytes[1], (byte) chars[0]);
  113. }
  114. // Test GetString(char[])
  115. public void TestGetString1 ()
  116. {
  117. Encoding ascii_encoding = Encoding.ASCII;
  118. string str = ascii_encoding.GetString(testbytes);
  119. AssertEquals ("Test", str);
  120. }
  121. // Test GetString(char[], int, int)
  122. public void TestGetString2 ()
  123. {
  124. Encoding ascii_encoding = Encoding.ASCII;
  125. string str = ascii_encoding.GetString(testbytes, 1, 2);
  126. AssertEquals ("es", str);
  127. }
  128. // Test Decoder
  129. public void TestDecoder ()
  130. {
  131. Encoding ascii_encoding = Encoding.ASCII;
  132. char[] chars = new char[1];
  133. int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
  134. AssertEquals (1, cnt);
  135. AssertEquals (testbytes[1], (byte) chars[0]);
  136. }
  137. // Test Decoder
  138. public void TestEncoder ()
  139. {
  140. Encoding ascii_encoding = Encoding.ASCII;
  141. byte[] bytes = new Byte[1];
  142. int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
  143. AssertEquals (1, cnt);
  144. AssertEquals (testchars[1], (char) bytes[0]);
  145. }
  146. public void TestZero ()
  147. {
  148. Encoding encoding = Encoding.ASCII;
  149. AssertEquals ("#01", encoding.GetString (new byte [0]), "");
  150. AssertEquals ("#02", encoding.GetString (new byte [0], 0, 0), "");
  151. }
  152. }
  153. }