New test.
[mono.git] / mcs / class / corlib / Test / System.Text / ASCIIEncodingTest.cs
1 // ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
2 // 
3 // Author: Mike Kestner <mkestner@speakeasy.net>
4 //
5 // <c> 2002 Mike Kestner
6
7 using NUnit.Framework;
8 using System.Text;
9 using System;
10
11
12 namespace MonoTests.System.Text {
13
14         public class ASCIIEncodingTest : TestCase {
15
16                 private char[] testchars;
17                 private byte[] testbytes;
18
19                 protected override void SetUp ()
20                 {
21                         testchars = new char[4];
22                         testchars[0] = 'T';
23                         testchars[1] = 'e';
24                         testchars[2] = 's';
25                         testchars[3] = 't';
26                         testbytes = new byte[4];
27                         testbytes[0] = (byte) 'T';
28                         testbytes[1] = (byte) 'e';
29                         testbytes[2] = (byte) 's';
30                         testbytes[3] = (byte) 't';
31                 }
32
33                 // Test GetBytes(char[])
34                 public void TestGetBytes1 () 
35                 {
36                         Encoding ascii_encoding = Encoding.ASCII;
37                         byte[] bytes = ascii_encoding.GetBytes(testchars);
38                         for (int i = 0; i < testchars.Length; i++)
39                                 AssertEquals (testchars[i], (char) bytes[i]);
40                 }
41
42                 // Test GetBytes(char[], int, int)
43                 public void TestGetBytes2 () 
44                 {
45                         Encoding ascii_encoding = Encoding.ASCII;
46                         byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
47                         AssertEquals (1, bytes.Length);
48                         AssertEquals (testchars[1], (char) bytes[0]);
49                 }
50
51                 // Test non-ASCII char in char[]
52                 public void TestGetBytes3 () 
53                 {
54                         Encoding ascii_encoding = Encoding.ASCII;
55                         testchars[2] = (char) 0x80;
56                         byte[] bytes = ascii_encoding.GetBytes(testchars);
57                         AssertEquals ('T', (char) bytes[0]);
58                         AssertEquals ('e', (char) bytes[1]);
59                         AssertEquals ('?', (char) bytes[2]);
60                         AssertEquals ('t', (char) bytes[3]);
61                 }
62
63                 // Test GetBytes(char[], int, int, byte[], int)
64                 public void TestGetBytes4 () 
65                 {
66                         Encoding ascii_encoding = Encoding.ASCII;
67                         byte[] bytes = new Byte[1];
68                         int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
69                         AssertEquals (1, cnt);
70                         AssertEquals (testchars[1], (char) bytes[0]);
71                 }
72
73                 // Test GetBytes(string, int, int, byte[], int)
74                 public void TestGetBytes5 () 
75                 {
76                         Encoding ascii_encoding = Encoding.ASCII;
77                         byte[] bytes = new Byte[1];
78                         int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
79                         AssertEquals ('e', (char) bytes[0]);
80                 }
81
82                 // Test GetBytes(string)
83                 public void TestGetBytes6 () 
84                 {
85                         Encoding ascii_encoding = Encoding.ASCII;
86                         byte[] bytes = ascii_encoding.GetBytes("Test");
87                         for (int i = 0; i < testchars.Length; i++)
88                                 AssertEquals (testchars[i], (char) bytes[i]);
89                 }
90
91                 // Test GetChars(byte[])
92                 public void TestGetChars1 () 
93                 {
94                         Encoding ascii_encoding = Encoding.ASCII;
95                         char[] chars = ascii_encoding.GetChars(testbytes);
96                         for (int i = 0; i < testbytes.Length; i++)
97                                 AssertEquals (testbytes[i], (byte) chars[i]);
98                 }
99
100                 // Test GetChars(byte[], int, int)
101                 public void TestGetChars2 () 
102                 {
103                         Encoding ascii_encoding = Encoding.ASCII;
104                         char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
105                         AssertEquals (1, chars.Length);
106                         AssertEquals (testbytes[1], (byte) chars[0]);
107                 }
108
109                 // Test non-ASCII char in byte[]
110                 public void TestGetChars3 () 
111                 {
112                         Encoding ascii_encoding = Encoding.ASCII;
113                         testbytes[2] = 0x80;
114                         char[] chars = ascii_encoding.GetChars(testbytes);
115                         AssertEquals ('T', chars[0]);
116                         AssertEquals ('e', chars[1]);
117                         AssertEquals ('?', chars[2]);
118                         AssertEquals ('t', chars[3]);
119                 }
120
121                 // Test GetChars(byte[], int, int, char[], int)
122                 public void TestGetChars4 () 
123                 {
124                         Encoding ascii_encoding = Encoding.ASCII;
125                         char[] chars = new char[1];
126                         int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
127                         AssertEquals (1, cnt);
128                         AssertEquals (testbytes[1], (byte) chars[0]);
129                 }
130
131                 // Test GetString(char[])
132                 public void TestGetString1 () 
133                 {
134                         Encoding ascii_encoding = Encoding.ASCII;
135                         string str = ascii_encoding.GetString(testbytes);
136                         AssertEquals ("Test", str);
137                 }
138
139                 // Test GetString(char[], int, int)
140                 public void TestGetString2 () 
141                 {
142                         Encoding ascii_encoding = Encoding.ASCII;
143                         string str = ascii_encoding.GetString(testbytes, 1, 2);
144                         AssertEquals ("es", str);
145                 }
146
147                 // Test invalid byte handling
148                 public void TestGetString3 () 
149                 {
150                         Encoding encoding = Encoding.ASCII;
151                         byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
152                         string s = encoding.GetString (bytes, 0, 3);
153 #if NET_2_0
154                         AssertEquals ("a??", s);
155 #else
156                         AssertEquals ("aab", s);
157 #endif
158                 }
159
160                 // Test Decoder
161                 public void TestDecoder ()
162                 {
163                         Encoding ascii_encoding = Encoding.ASCII;
164                         char[] chars = new char[1];
165                         int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
166                         AssertEquals (1, cnt);
167                         AssertEquals (testbytes[1], (byte) chars[0]);
168                 }
169
170                 // Test Decoder
171                 public void TestEncoder ()
172                 {
173                         Encoding ascii_encoding = Encoding.ASCII;
174                         byte[] bytes = new Byte[1];
175                         int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
176                         AssertEquals (1, cnt);
177                         AssertEquals (testchars[1], (char) bytes[0]);
178                 }
179
180                 public void TestZero ()
181                 {
182                         Encoding encoding = Encoding.ASCII;
183                         AssertEquals ("#01", encoding.GetString (new byte [0]), "");
184                         AssertEquals ("#02", encoding.GetString (new byte [0], 0, 0), "");
185                 }
186
187 #if NET_2_0
188                 [Test]
189                 [ExpectedException (typeof (DecoderFallbackException))]
190                 public void DecoderFallback ()
191                 {
192                         Encoding e = Encoding.ASCII.Clone () as Encoding;
193                         e.DecoderFallback = new DecoderExceptionFallback ();
194                         e.GetChars (new byte [] {0x80});
195                 }
196 #endif
197         }
198
199 }