]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/charsetdet/TestCharsetDetector.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / core / src / com / ibm / icu / dev / test / charsetdet / TestCharsetDetector.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2005-2011, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.charsetdet;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.InputStream;
11 import java.io.Reader;
12 import java.io.UnsupportedEncodingException;
13
14 import javax.xml.parsers.DocumentBuilder;
15 import javax.xml.parsers.DocumentBuilderFactory;
16
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.NamedNodeMap;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22
23 import com.ibm.icu.dev.test.TestFmwk;
24 import com.ibm.icu.text.CharsetDetector;
25 import com.ibm.icu.text.CharsetMatch;
26
27
28 /**
29  * @author andy
30  */
31 public class TestCharsetDetector extends TestFmwk
32 {
33     
34     /**
35      * Constructor
36      */
37     public TestCharsetDetector()
38     {
39     }
40
41     public static void main(String[] args) {
42         try
43         {
44             TestCharsetDetector test = new TestCharsetDetector();
45             test.run(args);
46         }
47         catch (Exception e)
48         {
49             e.printStackTrace();
50         }
51     }
52
53     private void CheckAssert(boolean exp) {
54         if (exp == false) {
55             String msg;
56             try {
57                 throw new Exception();
58             }
59             catch (Exception e) {
60                 StackTraceElement failPoint = e.getStackTrace()[1];
61                 msg = "Test failure in file " + failPoint.getFileName() +
62                              " at line " + failPoint.getLineNumber();
63             }
64             errln(msg);
65         }
66         
67     }
68     
69     private String stringFromReader(Reader reader)
70     {
71         StringBuffer sb = new StringBuffer();
72         char[] buffer   = new char[1024];
73         int bytesRead   = 0;
74         
75         try {
76             while ((bytesRead = reader.read(buffer, 0, 1024)) >= 0) {
77                 sb.append(buffer, 0, bytesRead);
78             }
79             
80             return sb.toString();
81         } catch (Exception e) {
82             errln("stringFromReader() failed: " + e.toString());
83             return null;
84         }
85     }
86     
87     public void TestConstruction() {
88         int i;
89         CharsetDetector  det = new CharsetDetector();
90         if(det==null){
91             errln("Could not construct a charset detector");
92         }
93         String [] charsetNames = CharsetDetector.getAllDetectableCharsets();
94         CheckAssert(charsetNames.length != 0);
95         for (i=0; i<charsetNames.length; i++) {
96             CheckAssert(charsetNames[i].equals("") == false); 
97             // System.out.println("\"" + charsetNames[i] + "\"");
98         }
99      }
100
101     public void TestInputFilter() throws Exception
102     {
103         String s = "<a> <lot> <of> <English> <inside> <the> <markup> Un tr\u00E8s petit peu de Fran\u00E7ais. <to> <confuse> <the> <detector>";
104         byte[] bytes = s.getBytes("ISO-8859-1");
105         CharsetDetector det = new CharsetDetector();
106         CharsetMatch m;
107         
108         det.enableInputFilter(true);
109         if (!det.inputFilterEnabled()){
110             errln("input filter should be enabled");
111         }
112         
113         det.setText(bytes);
114         m = det.detect();
115         
116         if (! m.getLanguage().equals("fr")) {
117             errln("input filter did not strip markup!");
118         }
119         
120         det.enableInputFilter(false);
121         det.setText(bytes);
122         m = det.detect();
123         
124         if (! m.getLanguage().equals("en")) {
125             errln("unfiltered input did not detect as English!");
126         }
127     }
128     
129     public void TestUTF8() throws Exception {
130         
131         String  s = "This is a string with some non-ascii characters that will " +
132                     "be converted to UTF-8, then shoved through the detection process.  " +
133                     "\u0391\u0392\u0393\u0394\u0395" +
134                     "Sure would be nice if our source could contain Unicode directly!";
135         byte [] bytes = s.getBytes("UTF-8");
136         CharsetDetector det = new CharsetDetector();
137         String retrievedS;
138         Reader reader;
139         
140         retrievedS = det.getString(bytes, "UTF-8");
141         CheckAssert(s.equals(retrievedS));
142         
143         reader = det.getReader(new ByteArrayInputStream(bytes), "UTF-8");
144         CheckAssert(s.equals(stringFromReader(reader)));
145         det.setDeclaredEncoding("UTF-8"); // Jitterbug 4451, for coverage
146     }
147     
148     public void TestUTF16() throws Exception
149     {
150         String source = 
151                 "u0623\u0648\u0631\u0648\u0628\u0627, \u0628\u0631\u0645\u062c\u064a\u0627\u062a " +
152                 "\u0627\u0644\u062d\u0627\u0633\u0648\u0628 \u002b\u0020\u0627\u0646\u062a\u0631\u0646\u064a\u062a";
153         
154         byte[] beBytes = source.getBytes("UnicodeBig");
155         byte[] leBytes = source.getBytes("UnicodeLittle");
156         CharsetDetector det = new CharsetDetector();
157         CharsetMatch m;
158         
159         det.setText(beBytes);
160         m = det.detect();
161         
162         if (! m.getName().equals("UTF-16BE")) {
163             errln("Encoding detection failure: expected UTF-16BE, got " + m.getName());
164         }
165         
166         det.setText(leBytes);
167         m = det.detect();
168         
169         if (! m.getName().equals("UTF-16LE")) {
170             errln("Encoding detection failure: expected UTF-16LE, got " + m.getName());
171         }
172
173         // Jitterbug 4451, for coverage
174         int confidence = m.getConfidence(); 
175         if(confidence != 100){
176             errln("Did not get the expected confidence level " + confidence);
177         }
178         int matchType = m.getMatchType();
179         if(matchType != 0){
180             errln("Did not get the expected matchType level " + matchType);
181         }
182     }
183     
184     public void TestC1Bytes() throws Exception
185     {
186         String sISO =
187             "This is a small sample of some English text. Just enough to be sure that it detects correctly.";
188         
189         String sWindows =
190             "This is another small sample of some English text. Just enough to be sure that it detects correctly. It also includes some \u201CC1\u201D bytes.";
191
192         byte[] bISO     = sISO.getBytes("ISO-8859-1");
193         byte[] bWindows = sWindows.getBytes("windows-1252");
194         
195         CharsetDetector det = new CharsetDetector();
196         CharsetMatch m;
197         
198         det.setText(bWindows);
199         m = det.detect();
200         
201         if (m.getName() != "windows-1252") {
202             errln("Text with C1 bytes not correctly detected as windows-1252.");
203             return;
204         }
205         
206         det.setText(bISO);
207         m = det.detect();
208         
209         if (m.getName() != "ISO-8859-1") {
210             errln("Text without C1 bytes not correctly detected as ISO-8859-1.");
211         }
212     }
213     
214     public void TestShortInput() {
215         // Test that detection with very short byte strings does not crash and burn.
216         // The shortest input that should produce positive detection result is two bytes, 
217         //   a UTF-16 BOM.
218         // TODO:  Detector confidence levels needs to be refined for very short input.
219         //        Too high now, for some charsets that happen to be compatible with a few bytes of input.
220         byte [][]  shortBytes = new byte [][] 
221             {
222                 {},
223                 {(byte)0x0a},
224                 {(byte)'A', (byte)'B'},
225                 {(byte)'A', (byte)'B', (byte)'C'},
226                 {(byte)'A', (byte)'B', (byte)'C', (byte)'D'}
227             };
228         
229         CharsetDetector det = new CharsetDetector();
230         CharsetMatch m;
231         for (int i=0; i<shortBytes.length; i++) {
232             det.setText(shortBytes[i]);
233             m = det.detect();
234             logln("i=" + i + " -> " + m.getName());
235         }
236     }
237     
238     public void TestBufferOverflow()
239     {
240         byte testStrings[][] = {
241             {(byte) 0x80, (byte) 0x20, (byte) 0x54, (byte) 0x68, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x45, (byte) 0x6E, (byte) 0x67, (byte) 0x6C, (byte) 0x69, (byte) 0x73, (byte) 0x68, (byte) 0x20, (byte) 0x1b}, /* A partial ISO-2022 shift state at the end */
242             {(byte) 0x80, (byte) 0x20, (byte) 0x54, (byte) 0x68, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x45, (byte) 0x6E, (byte) 0x67, (byte) 0x6C, (byte) 0x69, (byte) 0x73, (byte) 0x68, (byte) 0x20, (byte) 0x1b, (byte) 0x24}, /* A partial ISO-2022 shift state at the end */
243             {(byte) 0x80, (byte) 0x20, (byte) 0x54, (byte) 0x68, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x45, (byte) 0x6E, (byte) 0x67, (byte) 0x6C, (byte) 0x69, (byte) 0x73, (byte) 0x68, (byte) 0x20, (byte) 0x1b, (byte) 0x24, (byte) 0x28}, /* A partial ISO-2022 shift state at the end */
244             {(byte) 0x80, (byte) 0x20, (byte) 0x54, (byte) 0x68, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x69, (byte) 0x73, (byte) 0x20, (byte) 0x45, (byte) 0x6E, (byte) 0x67, (byte) 0x6C, (byte) 0x69, (byte) 0x73, (byte) 0x68, (byte) 0x20, (byte) 0x1b, (byte) 0x24, (byte) 0x28, (byte) 0x44}, /* A complete ISO-2022 shift state at the end with a bad one at the start */
245             {(byte) 0x1b, (byte) 0x24, (byte) 0x28, (byte) 0x44}, /* A complete ISO-2022 shift state at the end */
246             {(byte) 0xa1}, /* Could be a single byte shift-jis at the end */
247             {(byte) 0x74, (byte) 0x68, (byte) 0xa1}, /* Could be a single byte shift-jis at the end */
248             {(byte) 0x74, (byte) 0x68, (byte) 0x65, (byte) 0xa1} /* Could be a single byte shift-jis at the end, but now we have English creeping in. */
249         };
250         
251         String testResults[] = {
252             "windows-1252",
253             "windows-1252",
254             "windows-1252",
255             "windows-1252",
256             "ISO-2022-JP",
257             null,
258             null,
259             "ISO-8859-1"
260         };
261         
262         CharsetDetector det = new CharsetDetector();
263         CharsetMatch match;
264
265         det.setDeclaredEncoding("ISO-2022-JP");
266
267         for (int idx = 0; idx < testStrings.length; idx += 1) {
268             det.setText(testStrings[idx]);
269             match = det.detect();
270
271             if (match == null) {
272                 if (testResults[idx] != null) {
273                     errln("Unexpectedly got no results at index " + idx);
274                 }
275                 else {
276                     logln("Got no result as expected at index " + idx);
277                 }
278                 continue;
279             }
280
281             if (testResults[idx] == null || ! testResults[idx].equals(match.getName())) {
282                 errln("Unexpectedly got " + match.getName() + " instead of " + testResults[idx] +
283                       " at index " + idx + " with confidence " + match.getConfidence());
284                 return;
285             }
286         }
287     }
288
289     public void TestDetection()
290     {
291         //
292         //  Open and read the test data file.
293         //
294         //InputStreamReader isr = null;
295         
296         try {
297             InputStream is = TestCharsetDetector.class.getResourceAsStream("CharsetDetectionTests.xml");
298             if (is == null) {
299                 errln("Could not open test data file CharsetDetectionTests.xml");
300                 return;
301             }
302             
303             //isr = new InputStreamReader(is, "UTF-8"); 
304
305             // Set up an xml parser.
306             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
307             
308             factory.setIgnoringComments(true);
309             
310             DocumentBuilder builder = factory.newDocumentBuilder();
311             
312             // Parse the xml content from the test case file.
313             Document doc = builder.parse(is, null);
314             Element root = doc.getDocumentElement();
315             
316             NodeList testCases = root.getElementsByTagName("test-case");
317             
318             // Process each test case
319             for (int n = 0; n < testCases.getLength(); n += 1) {
320                 Node testCase = testCases.item(n);
321                 NamedNodeMap attrs = testCase.getAttributes();
322                 NodeList testData  = testCase.getChildNodes();
323                 StringBuffer testText = new StringBuffer();
324                 String id = attrs.getNamedItem("id").getNodeValue();
325                 String encodings = attrs.getNamedItem("encodings").getNodeValue();
326                 
327                 // Collect the test case text.
328                 for (int t = 0; t < testData.getLength(); t += 1) {
329                     Node textNode = testData.item(t);
330                     
331                     testText.append(textNode.getNodeValue());                    
332                 }
333                 
334                 // Process test text with each encoding / language pair.
335                 String testString = testText.toString();
336                 String[] encodingList = encodings.split(" ");
337                 for (int e = 0; e < encodingList.length; e += 1) {
338                     checkEncoding(testString, encodingList[e], id);
339                 }
340             }
341             
342         } catch (Exception e) {
343             errln("exception while processing test cases: " + e.toString());
344         }
345     }
346
347     private void checkMatch(CharsetDetector det, String testString, String encoding, String language, String id) throws Exception
348     {
349         CharsetMatch m = det.detect();
350         String decoded;
351         
352         if (! m.getName().equals(encoding)) {
353             errln(id + ": encoding detection failure - expected " + encoding + ", got " + m.getName());
354             return;
355         }
356         
357         String charsetMatchLanguage = m.getLanguage();
358         if ((language != null && !charsetMatchLanguage.equals(language))
359             || (language == null && charsetMatchLanguage != null)
360             || (language != null && charsetMatchLanguage == null))
361         {
362             errln(id + ", " + encoding + ": language detection failure - expected " + language + ", got " + m.getLanguage());
363         }
364         
365         if (encoding.startsWith("UTF-32")) {
366             return;
367         }
368         
369         decoded = m.getString();
370         
371         if (! testString.equals(decoded)) {
372             errln(id + ", " + encoding + ": getString() didn't return the original string!");
373         }
374         
375         decoded = stringFromReader(m.getReader());
376         
377         if (! testString.equals(decoded)) {
378             errln(id + ", " + encoding + ": getReader() didn't yield the original string!");
379         }
380     }
381     
382     private void checkEncoding(String testString, String encoding, String id)
383     {
384         String enc = null, lang = null;
385         String[] split = encoding.split("/");
386         
387         enc = split[0];
388         
389         if (split.length > 1) {
390             lang = split[1];
391         }
392
393         try {
394             CharsetDetector det = new CharsetDetector();
395             byte[] bytes;
396             
397             //if (enc.startsWith("UTF-32")) {
398             //    UTF32 utf32 = UTF32.getInstance(enc);
399                 
400             //    bytes = utf32.toBytes(testString);
401             //} else {
402                 String from = enc;
403
404                 while (true) {
405                     try {
406                         bytes = testString.getBytes(from);
407                     } catch (UnsupportedOperationException uoe) {
408                          // In some runtimes, the ISO-2022-CN converter
409                          // only converts *to* Unicode - we have to use
410                          // x-ISO-2022-CN-GB to convert *from* Unicode.
411                         if (from.equals("ISO-2022-CN")) {
412                             from = "x-ISO-2022-CN-GB";
413                             continue;
414                         }
415                         
416                         // Ignore any other converters that can't
417                         // convert from Unicode.
418                         return;
419                     } catch (UnsupportedEncodingException uee) {
420                         // Ignore any encodings that this runtime
421                         // doesn't support.
422                         return;
423                     }
424                     
425                     break;
426                 }
427             //}
428         
429             det.setText(bytes);
430             checkMatch(det, testString, enc, lang, id);
431             
432             det.setText(new ByteArrayInputStream(bytes));
433             checkMatch(det, testString, enc, lang, id);
434          } catch (Exception e) {
435             errln(id + ": " + e.toString() + "enc=" + enc);
436             e.printStackTrace();
437         }
438     }
439     
440     public void TestJapanese() throws Exception {
441         String s = "\u3000\u3001\u3002\u3003\u3005\u3006\u3007\u3008\u3009\u300A\u300B\u300C\u300D\u300E\u300F\u3010\u3011\u3012\u3013\u3014" + 
442         "\u3015\u301C\u3041\u3042\u3043\u3044\u3045\u3046\u3047\u3048\u3049\u304A\u304B\u304C\u304D\u304E\u304F\u3050\u3051\u3052" + 
443         "\u3053\u3054\u3055\u3056\u3057\u3058\u3059\u305A\u305B\u305C\u305D\u305E\u305F\u3060\u3061\u3062\u3063\u3064\u3065\u3066" + 
444         "\u3067\u3068\u3069\u306A\u306B\u306C\u306D\u306E\u306F\u3070\u3071\u3072\u3073\u3074\u3075\u3076\u3077\u3078\u3079\u307A" + 
445         "\u307B\u307C\u307D\u307E\u307F\u3080\u3081\u3082\u3083\u3084\u3085\u3086\u3087\u3088\u3089\u308A\u308B\u308C\u308D\u308E" + 
446         "\u308F\u3090\u3091\u3092\u3093\u309B\u309C\u309D\u309E\u30A1\u30A2\u30A3\u30A4\u30A5\u30A6\u30A7\u30A8\u30A9\u30AA\u30AB" + 
447         "\u30AC\u30AD\u30AE\u30AF\u30B0\u30B1\u30B2\u30B3\u30B4\u30B5\u30B6\u30B7\u30B8\u30B9\u30BA\u30BB\u30BC\u30BD\u30BE\u30BF" + 
448         "\u30C0\u30C1\u30C2\u30C3\u30C4\u30C5\u30C6\u30C7\u30C8\u30C9\u30CA\u30CB\u30CC\u30CD\u30CE\u30CF\u30D0\u30D1\u30D2\u30D3" + 
449         "\u30D4\u30D5\u30D6\u30D7\u30D8\u30D9\u30DA\u30DB\u30DC\u30DD\u30DE\u30DF\u30E0\u30E1\u30E2\u30E3\u30E4\u30E5\u30E6\u30E7" + 
450         "\u30E8\u30E9\u30EA\u30EB\u30EC\u30ED\u30EE\u30EF\u30F0\u30F1\u30F2\u30F3\u30F4\u30F5\u30F6\u30FB\u30FC\u30FD\u30FE\u4E00" + 
451         "\u4E01\u4E02\u4E03\u4E04\u4E05\u4E07\u4E08\u4E09\u4E0A\u4E0B\u4E0C\u4E0D\u4E0E\u4E10\u4E11\u4E12\u4E14\u4E15\u4E16\u4E17" + 
452         "\u4E18\u4E19\u4E1E\u4E1F\u4E21\u4E23\u4E24\u4E26\u4E28\u4E2A\u4E2B\u4E2D\u4E2E\u4E2F\u4E30\u4E31\u4E32\u4E35\u4E36\u4E38" + 
453         "\u4E39\u4E3B\u4E3C\u4E3F\u4E40\u4E41\u4E42\u4E43\u4E44\u4E45\u4E47\u4E4B\u4E4D\u4E4E\u4E4F\u4E51\u4E55\u4E56\u4E57\u4E58" + 
454         "\u4E59\u4E5A\u4E5C\u4E5D\u4E5E\u4E5F\u4E62\u4E63\u4E68\u4E69\u4E71\u4E73\u4E74\u4E75\u4E79\u4E7E\u4E7F\u4E80\u4E82\u4E85" + 
455         "\u4E86\u4E88\u4E89\u4E8A\u4E8B\u4E8C";
456         
457         CharsetDetector det = new CharsetDetector();
458         CharsetMatch m;
459         String charsetMatch;
460         byte[] bytes;
461         {
462             bytes = s.getBytes("EUC-JP");
463             det.setText(bytes);
464             m = det.detect();
465             charsetMatch = m.getName();
466             CheckAssert(charsetMatch.equals("EUC-JP"));
467             
468             // Tests "public String getLanguage()"
469             CheckAssert(m.getLanguage().equals("ja"));
470         }
471     }
472
473     public void TestArabic() throws Exception {
474         String  s = "\u0648\u0636\u0639\u062A \u0648\u0646\u0641\u0630\u062A \u0628\u0631\u0627" +
475         "\u0645\u062C \u062A\u0623\u0645\u064A\u0646 \u0639\u062F\u064A\u062F\u0629 \u0641\u064A " + 
476         "\u0645\u0624\u0633\u0633\u0629 \u0627\u0644\u062A\u0623\u0645\u064A\u0646 \u0627\u0644"  + 
477         "\u0648\u0637\u0646\u064A, \u0645\u0639 \u0645\u0644\u0627\u0626\u0645\u062A\u0647\u0627 " + 
478         "\u062F\u0627\u0626\u0645\u0627 \u0644\u0644\u0627\u062D\u062A\u064A\u0627\u062C" + 
479         "\u0627\u062A \u0627\u0644\u0645\u062A\u063A\u064A\u0631\u0629 \u0644\u0644\u0645\u062C" + 
480         "\u062A\u0645\u0639 \u0648\u0644\u0644\u062F\u0648\u0644\u0629. \u062A\u0648\u0633\u0639" + 
481         "\u062A \u0648\u062A\u0637\u0648\u0631\u062A \u0627\u0644\u0645\u0624\u0633\u0633\u0629 " + 
482         "\u0628\u0647\u062F\u0641 \u0636\u0645\u0627\u0646 \u0634\u0628\u0643\u0629 \u0623\u0645" + 
483         "\u0627\u0646 \u0644\u0633\u0643\u0627\u0646 \u062F\u0648\u0644\u0629 \u0627\u0633\u0631" + 
484         "\u0627\u0626\u064A\u0644 \u0628\u0648\u062C\u0647 \u0627\u0644\u0645\u062E\u0627\u0637" + 
485         "\u0631 \u0627\u0644\u0627\u0642\u062A\u0635\u0627\u062F\u064A\u0629 \u0648\u0627\u0644" + 
486         "\u0627\u062C\u062A\u0645\u0627\u0639\u064A\u0629.";
487
488         CharsetDetector det = new CharsetDetector();
489         CharsetMatch m;
490         String charsetMatch;
491         byte[] bytes;
492         {
493             bytes = s.getBytes("windows-1256");
494             det.setText(bytes);
495             m = det.detect();
496             charsetMatch = m.getName();
497             CheckAssert(charsetMatch.equals("windows-1256"));
498             
499             // Tests "public String getLanguage()"
500             CheckAssert(m.getLanguage().endsWith("ar"));
501         }
502
503         {
504             // We cannot rely on IBM420 converter in Sun Java
505             /*
506             bytes = s.getBytes("IBM420");
507             */
508             bytes = new byte[] {
509                 (byte)0xCF, (byte)0x8D, (byte)0x9A, (byte)0x63, (byte)0x40, (byte)0xCF, (byte)0xBD, (byte)0xAB,
510                 (byte)0x74, (byte)0x63, (byte)0x40, (byte)0x58, (byte)0x75, (byte)0x56, (byte)0xBB, (byte)0x67,
511                 (byte)0x40, (byte)0x63, (byte)0x49, (byte)0xBB, (byte)0xDC, (byte)0xBD, (byte)0x40, (byte)0x9A,
512                 (byte)0x73, (byte)0xDC, (byte)0x73, (byte)0x62, (byte)0x40, (byte)0xAB, (byte)0xDC, (byte)0x40,
513                 (byte)0xBB, (byte)0x52, (byte)0x77, (byte)0x77, (byte)0x62, (byte)0x40, (byte)0x56, (byte)0xB1,
514                 (byte)0x63, (byte)0x49, (byte)0xBB, (byte)0xDC, (byte)0xBD, (byte)0x40, (byte)0x56, (byte)0xB1,
515                 (byte)0xCF, (byte)0x8F, (byte)0xBD, (byte)0xDC, (byte)0x6B, (byte)0x40, (byte)0xBB, (byte)0x9A,
516                 (byte)0x40, (byte)0xBB, (byte)0xB1, (byte)0x56, (byte)0x55, (byte)0xBB, (byte)0x63, (byte)0xBF,
517                 (byte)0x56, (byte)0x40, (byte)0x73, (byte)0x56, (byte)0x55, (byte)0xBB, (byte)0x56, (byte)0x40,
518                 (byte)0xB1, (byte)0xB1, (byte)0x56, (byte)0x69, (byte)0x63, (byte)0xDC, (byte)0x56, (byte)0x67,
519                 (byte)0x56, (byte)0x63, (byte)0x40, (byte)0x56, (byte)0xB1, (byte)0xBB, (byte)0x63, (byte)0x9E,
520                 (byte)0xDC, (byte)0x75, (byte)0x62, (byte)0x40, (byte)0xB1, (byte)0xB1, (byte)0xBB, (byte)0x67,
521                 (byte)0x63, (byte)0xBB, (byte)0x9A, (byte)0x40, (byte)0xCF, (byte)0xB1, (byte)0xB1, (byte)0x73,
522                 (byte)0xCF, (byte)0xB1, (byte)0x62, (byte)0x4B, (byte)0x40, (byte)0x63, (byte)0xCF, (byte)0x77,
523                 (byte)0x9A, (byte)0x63, (byte)0x40, (byte)0xCF, (byte)0x63, (byte)0x8F, (byte)0xCF, (byte)0x75,
524                 (byte)0x63, (byte)0x40, (byte)0x56, (byte)0xB1, (byte)0xBB, (byte)0x52, (byte)0x77, (byte)0x77,
525                 (byte)0x62, (byte)0x40, (byte)0x58, (byte)0xBF, (byte)0x73, (byte)0xAB, (byte)0x40, (byte)0x8D,
526                 (byte)0xBB, (byte)0x56, (byte)0xBD, (byte)0x40, (byte)0x80, (byte)0x58, (byte)0xAF, (byte)0x62,
527                 (byte)0x40, (byte)0x49, (byte)0xBB, (byte)0x56, (byte)0xBD, (byte)0x40, (byte)0xB1, (byte)0x77,
528                 (byte)0xAF, (byte)0x56, (byte)0xBD, (byte)0x40, (byte)0x73, (byte)0xCF, (byte)0xB1, (byte)0x62,
529                 (byte)0x40, (byte)0x56, (byte)0x77, (byte)0x75, (byte)0x56, (byte)0x55, (byte)0xDC, (byte)0xB1,
530                 (byte)0x40, (byte)0x58, (byte)0xCF, (byte)0x67, (byte)0xBF, (byte)0x40, (byte)0x56, (byte)0xB1,
531                 (byte)0xBB, (byte)0x71, (byte)0x56, (byte)0x8F, (byte)0x75, (byte)0x40, (byte)0x56, (byte)0xB1,
532                 (byte)0x56, (byte)0xAD, (byte)0x63, (byte)0x8B, (byte)0x56, (byte)0x73, (byte)0xDC, (byte)0x62,
533                 (byte)0x40, (byte)0xCF, (byte)0x56, (byte)0xB1, (byte)0x56, (byte)0x67, (byte)0x63, (byte)0xBB,
534                 (byte)0x56, (byte)0x9A, (byte)0xDC, (byte)0x62, (byte)0x4B,
535             };
536             det.setText(bytes);
537             m = det.detect();
538             charsetMatch = m.getName();
539             CheckAssert(charsetMatch.equals("IBM420_rtl"));
540             
541          // Tests "public String getLanguage()"
542             CheckAssert(m.getLanguage().endsWith("ar"));
543         }
544
545         {
546             // We cannot rely on IBM420 converter in Sun Java
547             /*
548             StringBuffer ltrStrBuf = new StringBuffer(s);
549             ltrStrBuf = ltrStrBuf.reverse();
550             bytes = ltrStrBuf.toString().getBytes("IBM420");
551             */
552             bytes = new byte[] {
553                 (byte)0x4B, (byte)0x62, (byte)0xDC, (byte)0x9A, (byte)0x56, (byte)0xBB, (byte)0x63, (byte)0x67,
554                 (byte)0x56, (byte)0xB1, (byte)0x56, (byte)0xCF, (byte)0x40, (byte)0x62, (byte)0xDC, (byte)0x73,
555                 (byte)0x56, (byte)0x8B, (byte)0x63, (byte)0xAD, (byte)0x56, (byte)0xB1, (byte)0x56, (byte)0x40,
556                 (byte)0x75, (byte)0x8F, (byte)0x56, (byte)0x71, (byte)0xBB, (byte)0xB1, (byte)0x56, (byte)0x40,
557                 (byte)0xBF, (byte)0x67, (byte)0xCF, (byte)0x58, (byte)0x40, (byte)0xB1, (byte)0xDC, (byte)0x55,
558                 (byte)0x56, (byte)0x75, (byte)0x77, (byte)0x56, (byte)0x40, (byte)0x62, (byte)0xB1, (byte)0xCF,
559                 (byte)0x73, (byte)0x40, (byte)0xBD, (byte)0x56, (byte)0xAF, (byte)0x77, (byte)0xB1, (byte)0x40,
560                 (byte)0xBD, (byte)0x56, (byte)0xBB, (byte)0x49, (byte)0x40, (byte)0x62, (byte)0xAF, (byte)0x58,
561                 (byte)0x80, (byte)0x40, (byte)0xBD, (byte)0x56, (byte)0xBB, (byte)0x8D, (byte)0x40, (byte)0xAB,
562                 (byte)0x73, (byte)0xBF, (byte)0x58, (byte)0x40, (byte)0x62, (byte)0x77, (byte)0x77, (byte)0x52,
563                 (byte)0xBB, (byte)0xB1, (byte)0x56, (byte)0x40, (byte)0x63, (byte)0x75, (byte)0xCF, (byte)0x8F,
564                 (byte)0x63, (byte)0xCF, (byte)0x40, (byte)0x63, (byte)0x9A, (byte)0x77, (byte)0xCF, (byte)0x63,
565                 (byte)0x40, (byte)0x4B, (byte)0x62, (byte)0xB1, (byte)0xCF, (byte)0x73, (byte)0xB1, (byte)0xB1,
566                 (byte)0xCF, (byte)0x40, (byte)0x9A, (byte)0xBB, (byte)0x63, (byte)0x67, (byte)0xBB, (byte)0xB1,
567                 (byte)0xB1, (byte)0x40, (byte)0x62, (byte)0x75, (byte)0xDC, (byte)0x9E, (byte)0x63, (byte)0xBB,
568                 (byte)0xB1, (byte)0x56, (byte)0x40, (byte)0x63, (byte)0x56, (byte)0x67, (byte)0x56, (byte)0xDC,
569                 (byte)0x63, (byte)0x69, (byte)0x56, (byte)0xB1, (byte)0xB1, (byte)0x40, (byte)0x56, (byte)0xBB,
570                 (byte)0x55, (byte)0x56, (byte)0x73, (byte)0x40, (byte)0x56, (byte)0xBF, (byte)0x63, (byte)0xBB,
571                 (byte)0x55, (byte)0x56, (byte)0xB1, (byte)0xBB, (byte)0x40, (byte)0x9A, (byte)0xBB, (byte)0x40,
572                 (byte)0x6B, (byte)0xDC, (byte)0xBD, (byte)0x8F, (byte)0xCF, (byte)0xB1, (byte)0x56, (byte)0x40,
573                 (byte)0xBD, (byte)0xDC, (byte)0xBB, (byte)0x49, (byte)0x63, (byte)0xB1, (byte)0x56, (byte)0x40,
574                 (byte)0x62, (byte)0x77, (byte)0x77, (byte)0x52, (byte)0xBB, (byte)0x40, (byte)0xDC, (byte)0xAB,
575                 (byte)0x40, (byte)0x62, (byte)0x73, (byte)0xDC, (byte)0x73, (byte)0x9A, (byte)0x40, (byte)0xBD,
576                 (byte)0xDC, (byte)0xBB, (byte)0x49, (byte)0x63, (byte)0x40, (byte)0x67, (byte)0xBB, (byte)0x56,
577                 (byte)0x75, (byte)0x58, (byte)0x40, (byte)0x63, (byte)0x74, (byte)0xAB, (byte)0xBD, (byte)0xCF,
578                 (byte)0x40, (byte)0x63, (byte)0x9A, (byte)0x8D, (byte)0xCF,
579             };
580
581             det.setText(bytes);
582             m = det.detect();
583             charsetMatch = m.getName();
584             CheckAssert(charsetMatch.equals("IBM420_ltr"));
585         }
586     }
587
588     public void TestHebrew() throws Exception {
589         String  s =  "\u05D4\u05E4\u05E8\u05E7\u05DC\u05D9\u05D8 \u05D4\u05E6\u05D1\u05D0\u05D9 \u05D4" +
590             "\u05E8\u05D0\u05E9\u05D9, \u05EA\u05EA \u05D0\u05DC\u05D5\u05E3 \u05D0\u05D1\u05D9" + 
591             "\u05D7\u05D9 \u05DE\u05E0\u05D3\u05DC\u05D1\u05DC\u05D9\u05D8, \u05D4\u05D5\u05E8" + 
592             "\u05D4 \u05E2\u05DC \u05E4\u05EA\u05D9\u05D7\u05EA \u05D7\u05E7\u05D9\u05E8\u05EA " + 
593             "\u05DE\u05E6\"\u05D7 \u05D1\u05E2\u05E7\u05D1\u05D5\u05EA \u05E2\u05D3\u05D5\u05D9" + 
594             "\u05D5\u05EA \u05D7\u05D9\u05D9\u05DC\u05D9 \u05E6\u05D4\"\u05DC \u05DE\u05DE\u05D1" + 
595             "\u05E6\u05E2 \u05E2\u05D5\u05E4\u05E8\u05EA \u05D9\u05E6\u05D5\u05E7\u05D4 \u05D1+ " +
596             "\u05E8\u05E6\u05D5\u05E2\u05EA \u05E2\u05D6\u05D4. \u05DC\u05D3\u05D1\u05E8\u05D9 " + 
597             "\u05D4\u05E4\u05E6\"\u05E8, \u05DE\u05D4\u05E2\u05D3\u05D5\u05D9\u05D5\u05EA \u05E2" +
598             "\u05D5\u05DC\u05D4 \u05EA\u05DE\u05D5\u05E0\u05D4 \u05E9\u05DC \"\u05D4\u05EA\u05E0" + 
599             "\u05D4\u05D2\u05D5\u05EA \u05E4\u05E1\u05D5\u05DC\u05D4 \u05DC\u05DB\u05D0\u05D5\u05E8" + 
600             "\u05D4 \u05E9\u05DC \u05D7\u05D9\u05D9\u05DC\u05D9\u05DD \u05D1\u05DE\u05D4\u05DC\u05DA" + 
601             " \u05DE\u05D1\u05E6\u05E2 \u05E2\u05D5\u05E4\u05E8\u05EA \u05D9\u05E6\u05D5\u05E7\u05D4\"." + 
602             " \u05DE\u05E0\u05D3\u05DC\u05D1\u05DC\u05D9\u05D8 \u05E7\u05D9\u05D1\u05DC \u05D0\u05EA" +
603             " \u05D4\u05D7\u05DC\u05D8\u05EA\u05D5 \u05DC\u05D0\u05D7\u05E8 \u05E9\u05E2\u05D9\u05D9" +
604             "\u05DF \u05D1\u05EA\u05DE\u05DC\u05D9\u05DC \u05D4\u05E2\u05D3\u05D5\u05D9\u05D5\u05EA";
605         
606         CharsetMatch m = _test1255(s);
607         String charsetMatch = m.getName();
608         CheckAssert(charsetMatch.equals("ISO-8859-8"));
609         CheckAssert(m.getLanguage().equals("he"));
610         
611         m = _test1255_reverse(s);
612         charsetMatch = m.getName();
613         CheckAssert(charsetMatch.equals("ISO-8859-8"));
614         CheckAssert(m.getLanguage().equals("he"));
615         
616         m = _testIBM424_he_rtl(s);
617         charsetMatch = m.getName();
618         CheckAssert(charsetMatch.equals("IBM424_rtl"));
619         CheckAssert(m.getLanguage().equals("he"));
620         
621         m = _testIBM424_he_ltr(s);
622         charsetMatch = m.getName();
623         CheckAssert(charsetMatch.equals("IBM424_ltr"));
624         CheckAssert(m.getLanguage().equals("he"));
625     }
626     
627     private CharsetMatch _test1255(String s) throws Exception {
628         byte [] bytes = s.getBytes("ISO-8859-8");
629         CharsetDetector det = new CharsetDetector();
630         det.setText(bytes);
631         CharsetMatch m = det.detect();
632         return m;
633     }
634     
635     private CharsetMatch _test1255_reverse(String s) throws Exception {
636         StringBuffer reverseStrBuf = new StringBuffer(s);
637         reverseStrBuf = reverseStrBuf.reverse();
638         byte [] bytes = reverseStrBuf.toString().getBytes("ISO-8859-8");
639         
640         CharsetDetector det = new CharsetDetector();
641         det.setText(bytes);
642         CharsetMatch m = det.detect();
643         return m;
644     }
645     
646     private CharsetMatch _testIBM424_he_rtl(String s) throws Exception {
647         byte [] bytes = s.getBytes("IBM424");
648         CharsetDetector det = new CharsetDetector();
649         det.setText(bytes);
650         CharsetMatch m = det.detect();
651         return m;
652     }
653     
654     private CharsetMatch _testIBM424_he_ltr(String s) throws Exception {
655         /**
656          * transformation of input string to CP420 left to right requires reversing the string
657          */    
658         
659         StringBuffer ltrStrBuf = new StringBuffer(s);
660         ltrStrBuf = ltrStrBuf.reverse();
661         byte [] bytes = ltrStrBuf.toString().getBytes("IBM424");
662         
663         CharsetDetector det = new CharsetDetector();
664         det.setText(bytes);
665         CharsetMatch m = det.detect();
666         return m;
667     }
668     
669     /*
670      * Test the method int match(CharsetDetector det) in CharsetRecog_UTF_16_LE
671      */
672     public void TestCharsetRecog_UTF_16_LE_Match() {
673         byte[] in = { Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE };
674         CharsetDetector cd = new CharsetDetector();
675         // Tests when if (input.length>=4 && input[2] == 0x00 && input[3] == 0x00) is true inside the
676         // match(CharsetDetector) method of CharsetRecog_UTF_16_LE
677         try {
678             cd.setText(in);
679         } catch (Exception e) {
680             errln("CharsetRecog_UTF_16_LE.match(CharsetDetector) was not suppose to return an exception.");
681         }
682     }
683 }