2006-01-20 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / I18N / CJK / Test / I18N.CJK.Test.cs
1 //
2 // I18N.CJK.Test.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using NUnit.Framework;
14
15 namespace MonoTests.I18N.CJK
16 {
17         [TestFixture]
18         public class TestCJK
19         {
20                 void AssertEncode (string utf8file, string decfile, int codepage)
21                 {
22                         string decoded = null;
23                         byte [] encoded = null;
24                         using (StreamReader sr = new StreamReader (utf8file,
25                                 Encoding.UTF8)) {
26                                 decoded = sr.ReadToEnd ();
27                         }
28                         using (FileStream fs = File.OpenRead (decfile)) {
29                                 encoded = new byte [fs.Length];
30                                 fs.Read (encoded, 0, (int) fs.Length);
31                         }
32                         Encoding enc = Encoding.GetEncoding (codepage);
33                         byte [] actual;
34
35                         // simple string case
36                         //Assert.AreEqual (encoded.Length,
37                         //      enc.GetByteCount (decoded),
38                         //      "GetByteCount(string)");
39                         actual = enc.GetBytes (decoded);
40                         Assert.AreEqual (encoded, actual,
41                                 "GetBytes(string)");
42
43                         // simple char[] case
44                         Assert.AreEqual (encoded.Length,
45                                 enc.GetByteCount (decoded.ToCharArray (), 0, decoded.Length),
46                                 "GetByteCount(char[], 0, len)");
47                         actual = enc.GetBytes (decoded.ToCharArray (), 0, decoded.Length);
48                         Assert.AreEqual (encoded, actual,
49                                 "GetBytes(char[], 0, len)");
50                 }
51
52                 void AssertDecode (string utf8file, string decfile, int codepage)
53                 {
54                         string decoded = null;
55                         byte [] encoded = null;
56                         using (StreamReader sr = new StreamReader (utf8file,
57                                 Encoding.UTF8)) {
58                                 decoded = sr.ReadToEnd ();
59                         }
60                         using (FileStream fs = File.OpenRead (decfile)) {
61                                 encoded = new byte [fs.Length];
62                                 fs.Read (encoded, 0, (int) fs.Length);
63                         }
64                         Encoding enc = Encoding.GetEncoding (codepage);
65                         char [] actual;
66
67                         Assert.AreEqual (decoded.Length,
68                                 enc.GetCharCount (encoded, 0, encoded.Length),
69                                 "GetCharCount(byte[], 0, len)");
70                         actual = enc.GetChars (encoded, 0, encoded.Length);
71                         Assert.AreEqual (decoded.ToCharArray (), actual,
72                                 "GetChars(byte[], 0, len)");
73                 }
74
75                 #region Chinese
76
77                 // GB2312
78
79                 [Test]
80                 public void CP936_Encode ()
81                 {
82                         AssertEncode ("Test/texts/chinese-utf8.txt", "Test/texts/chinese-936.txt", 936);
83                 }
84
85                 [Test]
86                 public void CP936_Decode ()
87                 {
88                         AssertDecode ("Test/texts/chinese-utf8.txt", "Test/texts/chinese-936.txt", 936);
89                 }
90
91                 // BIG5
92
93                 [Test]
94                 public void CP950_Encode ()
95                 {
96                         AssertEncode ("Test/texts/chinese2-utf8.txt", "Test/texts/chinese2-950.txt", 950);
97                 }
98
99                 [Test]
100                 public void CP950_Decode ()
101                 {
102                         AssertDecode ("Test/texts/chinese2-utf8.txt", "Test/texts/chinese2-950.txt", 950);
103                 }
104
105                 // GB18030
106
107                 [Test]
108                 public void CP54936_Encode ()
109                 {
110                         AssertEncode ("Test/texts/chinese-utf8.txt", "Test/texts/chinese-54936.txt", 54936);
111                 }
112
113                 [Test]
114                 public void CP54936_Decode ()
115                 {
116                         AssertDecode ("Test/texts/chinese-utf8.txt", "Test/texts/chinese-54936.txt", 54936);
117                 }
118
119                 #endregion
120
121                 #region Japanese
122
123                 // Shift_JIS
124
125                 [Test]
126                 public void CP932_Encode ()
127                 {
128                         AssertEncode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-932.txt", 932);
129                 }
130
131                 [Test]
132                 public void CP932_Decode ()
133                 {
134                         AssertDecode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-932.txt", 932);
135                 }
136
137                 // EUC-JP
138
139                 [Test]
140                 public void CP51932_Encode ()
141                 {
142                         AssertEncode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-51932.txt", 51932);
143                 }
144
145                 [Test]
146                 public void CP51932_Decode ()
147                 {
148                         AssertDecode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-51932.txt", 51932);
149                 }
150
151                 // ISO-2022-JP
152
153                 [Test]
154                 public void CP50220_Encode ()
155                 {
156                         AssertEncode ("Test/texts/japanese2-utf8.txt", "Test/texts/japanese2-50220.txt", 50220);
157                 }
158
159                 [Test]
160                 public void CP50220_Decode ()
161                 {
162                         AssertDecode ("Test/texts/japanese2-utf8.txt", "Test/texts/japanese2-50220.txt", 50220);
163                 }
164
165                 [Test]
166                 public void CP50221_Encode ()
167                 {
168                         AssertEncode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-50221.txt", 50221);
169                 }
170
171                 [Test]
172                 public void CP50221_Decode ()
173                 {
174                         AssertDecode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-50221.txt", 50221);
175                 }
176
177                 [Test]
178 #if !NET_2_0
179                 [Category ("NotDotNet")] // MS is buggy here
180 #endif
181                 public void CP50222_Encode ()
182                 {
183                         AssertEncode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-50222.txt", 50222);
184                 }
185
186                 [Test]
187 #if !NET_2_0
188                 [Category ("NotDotNet")] // MS is buggy here
189 #endif
190                 public void CP50222_Decode ()
191                 {
192                         AssertDecode ("Test/texts/japanese-utf8.txt", "Test/texts/japanese-50222.txt", 50222);
193                 }
194
195                 [Test]
196                 public void Bug77723 ()
197                 {
198                         GetBytesAllSingleChars (51932);
199                 }
200
201                 [Test]
202                 public void Bug77724 ()
203                 {
204                         GetBytesAllSingleChars (932);
205                 }
206
207                 [Test]
208                 public void Bug77307 ()
209                 {
210                         GetBytesAllSingleChars (54936);
211                 }
212
213                 void GetBytesAllSingleChars (int enc)
214                 {
215                         Encoding e = Encoding.GetEncoding (enc);
216                         for (int i = 0; i < 0x10000; i++)
217                                 e.GetBytes (new char [] { (char)i });
218                 }
219
220                 void GetCharsAllBytePairs (int enc)
221                 {
222                         Encoding e = Encoding.GetEncoding (enc);
223                         byte [] bytes = new byte [2];
224                         for (int i0 = 0; i0 < 0x100; i0++) {
225                                 bytes [0] = (byte) i0;
226                                 for (int i1 = 0; i1 < 0x100; i1++) {
227                                         bytes [1] = (byte) i1;
228                                         e.GetChars (bytes);
229                                 }
230                         }
231                 }
232
233                 [Test]
234                 public void Bug77222 ()
235                 {
236                         GetCharsAllBytePairs (51932);
237                 }
238
239                 [Test]
240                 public void Bug77238 ()
241                 {
242                         GetCharsAllBytePairs (936);
243                 }
244
245                 [Test]
246                 public void Bug77306 ()
247                 {
248                         GetCharsAllBytePairs (54936);
249                 }
250
251                 [Test]
252                 public void Bug77298 ()
253                 {
254                         GetCharsAllBytePairs (949);
255                 }
256
257                 [Test]
258                 public void Bug77274 ()
259                 {
260                         GetCharsAllBytePairs (950);
261                 }
262
263                 #endregion
264
265                 #region Korean
266
267                 [Test]
268                 public void CP949_Encode ()
269                 {
270                         AssertEncode ("Test/texts/korean-utf8.txt", "Test/texts/korean-949.txt", 949);
271                 }
272
273                 [Test]
274                 public void CP949_Decode ()
275                 {
276                         AssertDecode ("Test/texts/korean-utf8.txt", "Test/texts/korean-949.txt", 949);
277                 }
278
279                 #endregion
280         }
281 }