1 // ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
\r
3 // Author: Mike Kestner <mkestner@speakeasy.net>
\r
5 // <c> 2002 Mike Kestner
\r
7 using NUnit.Framework;
\r
12 namespace MonoTests.System.Text {
\r
14 public class ASCIIEncodingTest : TestCase {
\r
16 private char[] testchars;
\r
17 private byte[] testbytes;
\r
19 protected override void SetUp ()
\r
21 testchars = new char[4];
\r
26 testbytes = new byte[4];
\r
27 testbytes[0] = (byte) 'T';
\r
28 testbytes[1] = (byte) 'e';
\r
29 testbytes[2] = (byte) 's';
\r
30 testbytes[3] = (byte) 't';
\r
33 // Test GetBytes(char[])
\r
34 public void TestGetBytes1 ()
\r
36 Encoding ascii_encoding = Encoding.ASCII;
\r
37 byte[] bytes = ascii_encoding.GetBytes(testchars);
\r
38 for (int i = 0; i < testchars.Length; i++)
\r
39 AssertEquals (testchars[i], (char) bytes[i]);
\r
42 // Test GetBytes(char[], int, int)
\r
43 public void TestGetBytes2 ()
\r
45 Encoding ascii_encoding = Encoding.ASCII;
\r
46 byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
\r
47 AssertEquals (1, bytes.Length);
\r
48 AssertEquals (testchars[1], (char) bytes[0]);
\r
51 // Test non-ASCII char in char[]
\r
52 public void TestGetBytes3 ()
\r
54 Encoding ascii_encoding = Encoding.ASCII;
\r
55 testchars[2] = (char) 0x80;
\r
56 byte[] bytes = ascii_encoding.GetBytes(testchars);
\r
57 AssertEquals ('T', (char) bytes[0]);
\r
58 AssertEquals ('e', (char) bytes[1]);
\r
59 AssertEquals ('?', (char) bytes[2]);
\r
60 AssertEquals ('t', (char) bytes[3]);
\r
63 // Test GetBytes(char[], int, int, byte[], int)
\r
64 public void TestGetBytes4 ()
\r
66 Encoding ascii_encoding = Encoding.ASCII;
\r
67 byte[] bytes = new Byte[1];
\r
68 int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
\r
69 AssertEquals (1, cnt);
\r
70 AssertEquals (testchars[1], (char) bytes[0]);
\r
73 // Test GetBytes(string, int, int, byte[], int)
\r
74 public void TestGetBytes5 ()
\r
76 Encoding ascii_encoding = Encoding.ASCII;
\r
77 byte[] bytes = new Byte[1];
\r
78 int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
\r
79 AssertEquals ('e', (char) bytes[0]);
\r
82 // Test GetBytes(string)
\r
83 public void TestGetBytes6 ()
\r
85 Encoding ascii_encoding = Encoding.ASCII;
\r
86 byte[] bytes = ascii_encoding.GetBytes("Test");
\r
87 for (int i = 0; i < testchars.Length; i++)
\r
88 AssertEquals (testchars[i], (char) bytes[i]);
\r
91 // Test GetChars(byte[])
\r
92 public void TestGetChars1 ()
\r
94 Encoding ascii_encoding = Encoding.ASCII;
\r
95 char[] chars = ascii_encoding.GetChars(testbytes);
\r
96 for (int i = 0; i < testbytes.Length; i++)
\r
97 AssertEquals (testbytes[i], (byte) chars[i]);
\r
100 // Test GetChars(byte[], int, int)
\r
101 public void TestGetChars2 ()
\r
103 Encoding ascii_encoding = Encoding.ASCII;
\r
104 char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
\r
105 AssertEquals (1, chars.Length);
\r
106 AssertEquals (testbytes[1], (byte) chars[0]);
\r
109 // Test non-ASCII char in byte[]
\r
110 public void TestGetChars3 ()
\r
112 Encoding ascii_encoding = Encoding.ASCII;
\r
113 testbytes[2] = 0x80;
\r
114 char[] chars = ascii_encoding.GetChars(testbytes);
\r
115 AssertEquals ('T', chars[0]);
\r
116 AssertEquals ('e', chars[1]);
\r
117 AssertEquals ('?', chars[2]);
\r
118 AssertEquals ('t', chars[3]);
\r
121 // Test GetChars(byte[], int, int, char[], int)
\r
122 public void TestGetChars4 ()
\r
124 Encoding ascii_encoding = Encoding.ASCII;
\r
125 char[] chars = new char[1];
\r
126 int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
\r
127 AssertEquals (1, cnt);
\r
128 AssertEquals (testbytes[1], (byte) chars[0]);
\r
131 // Test GetString(char[])
\r
132 public void TestGetString1 ()
\r
134 Encoding ascii_encoding = Encoding.ASCII;
\r
135 string str = ascii_encoding.GetString(testbytes);
\r
136 AssertEquals ("Test", str);
\r
139 // Test GetString(char[], int, int)
\r
140 public void TestGetString2 ()
\r
142 Encoding ascii_encoding = Encoding.ASCII;
\r
143 string str = ascii_encoding.GetString(testbytes, 1, 2);
\r
144 AssertEquals ("es", str);
\r
148 public void TestDecoder ()
\r
150 Encoding ascii_encoding = Encoding.ASCII;
\r
151 char[] chars = new char[1];
\r
152 int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
\r
153 AssertEquals (1, cnt);
\r
154 AssertEquals (testbytes[1], (byte) chars[0]);
\r
158 public void TestEncoder ()
\r
160 Encoding ascii_encoding = Encoding.ASCII;
\r
161 byte[] bytes = new Byte[1];
\r
162 int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
\r
163 AssertEquals (1, cnt);
\r
164 AssertEquals (testchars[1], (char) bytes[0]);
\r
167 public void TestZero ()
\r
169 Encoding encoding = Encoding.ASCII;
\r
170 AssertEquals ("#01", encoding.GetString (new byte [0]), "");
\r
171 AssertEquals ("#02", encoding.GetString (new byte [0], 0, 0), "");
\r