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