1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 /* 3 ******************************************************************************* 4 * Copyright (C) 2009-2014, Google, International Business Machines Corporation and 5 * others. All Rights Reserved. 6 ******************************************************************************* 7 */ 8 package android.icu.dev.test.translit; 9 10 import android.icu.dev.test.TestFmwk; 11 import android.icu.lang.UScript; 12 import android.icu.text.Transliterator; 13 import android.icu.text.UTF16; 14 import android.icu.text.UnicodeSet; 15 import android.icu.text.UnicodeSetIterator; 16 import android.icu.util.ULocale; 17 import org.junit.runner.RunWith; 18 import android.icu.junit.IcuTestFmwkRunner; 19 20 /** 21 * @author markdavis 22 * 23 */ 24 @RunWith(IcuTestFmwkRunner.class) 25 public class AnyScriptTest extends TestFmwk { 26 public static void main(String[] args) throws Exception { 27 new AnyScriptTest().run(args); 28 } 29 30 public void TestContext() { 31 Transliterator t = Transliterator.createFromRules("foo", "::[bc]; a{b}d > B;", Transliterator.FORWARD); 32 String sample = "abd abc b"; 33 assertEquals("context works", "aBd abc b", t.transform(sample)); 34 } 35 36 public void TestScripts(){ 37 // get a couple of characters of each script for testing 38 39 StringBuffer testBuffer = new StringBuffer(); 40 for (int script = 0; script < UScript.CODE_LIMIT; ++script) { 41 UnicodeSet test = new UnicodeSet().applyPropertyAlias("script", UScript.getName(script)); 42 int count = Math.min(20, test.size()); 43 for (int i = 0; i < count; ++i){ 44 testBuffer.append(UTF16.valueOf(test.charAt(i))); 45 } 46 } 47 String test = testBuffer.toString(); 48 logln("Test line: " + test); 49 50 int inclusion = getInclusion(); 51 boolean testedUnavailableScript = false; 52 53 for (int script = 0; script < UScript.CODE_LIMIT; ++script) { 54 if (script == UScript.COMMON || script == UScript.INHERITED) { 55 continue; 56 } 57 // if the inclusion rate is not 10, skip all but a small number of items. 58 // Make sure, however, that we test at least one unavailable script 59 if (inclusion < 10 && script != UScript.LATIN 60 && script != UScript.HAN 61 && script != UScript.HIRAGANA 62 && testedUnavailableScript 63 ) { 64 continue; 65 } 66 67 String scriptName = UScript.getName(script); // long name 68 ULocale locale = new ULocale(scriptName); 69 if (locale.getLanguage().equals("new") || locale.getLanguage().equals("pau")) { 70 if (logKnownIssue("11171", 71 "long script name loosely looks like a locale ID with a known likely script")) { 72 continue; 73 } 74 } 75 Transliterator t; 76 try { 77 t = Transliterator.getInstance("any-" + scriptName); 78 } catch (Exception e) { 79 testedUnavailableScript = true; 80 logln("Skipping unavailable: " + scriptName); 81 continue; // we don't handle all scripts 82 } 83 logln("Checking: " + scriptName); 84 if (t != null) { 85 t.transform(test); // just verify we don't crash 86 } 87 String shortScriptName = UScript.getShortName(script); // 4-letter script code 88 try { 89 t = Transliterator.getInstance("any-" + shortScriptName); 90 } catch (Exception e) { 91 errln("Transliterator.getInstance() worked for \"any-" + scriptName + 92 "\" but not for \"any-" + shortScriptName + '\"'); 93 } 94 t.transform(test); // just verify we don't crash 95 } 96 } 97 98 /** 99 * Check to make sure that wide characters are converted when going to narrow scripts. 100 */ 101 public void TestForWidth(){ 102 Transliterator widen = Transliterator.getInstance("halfwidth-fullwidth"); 103 Transliterator narrow = Transliterator.getInstance("fullwidth-halfwidth"); 104 UnicodeSet ASCII = new UnicodeSet("[:ascii:]"); 105 String lettersAndSpace = "abc def"; 106 final String punctOnly = "( )"; 107 108 String wideLettersAndSpace = widen.transform(lettersAndSpace); 109 String widePunctOnly = widen.transform(punctOnly); 110 assertContainsNone("Should be wide", ASCII, wideLettersAndSpace); 111 assertContainsNone("Should be wide", ASCII, widePunctOnly); 112 113 String back; 114 back = narrow.transform(wideLettersAndSpace); 115 assertEquals("Should be narrow", lettersAndSpace, back); 116 back = narrow.transform(widePunctOnly); 117 assertEquals("Should be narrow", punctOnly, back); 118 119 Transliterator latin = Transliterator.getInstance("any-Latn"); 120 back = latin.transform(wideLettersAndSpace); 121 assertEquals("Should be ascii", lettersAndSpace, back); 122 123 back = latin.transform(widePunctOnly); 124 assertEquals("Should be ascii", punctOnly, back); 125 126 // Han-Latin is now forward-only per CLDR ticket #5630 127 //Transliterator t2 = Transliterator.getInstance("any-Han"); 128 //back = t2.transform(widePunctOnly); 129 //assertEquals("Should be same", widePunctOnly, back); 130 131 132 } 133 134 public void TestCommonDigits() { 135 UnicodeSet westernDigitSet = new UnicodeSet("[0-9]"); 136 UnicodeSet westernDigitSetAndMarks = new UnicodeSet("[[0-9][:Mn:]]"); 137 UnicodeSet arabicDigitSet = new UnicodeSet("[[:Nd:]&[:block=Arabic:]]"); 138 Transliterator latin = Transliterator.getInstance("Any-Latn"); 139 Transliterator arabic = Transliterator.getInstance("Any-Arabic"); 140 String westernDigits = getList(westernDigitSet); 141 String arabicDigits = getList(arabicDigitSet); 142 143 String fromArabic = latin.transform(arabicDigits); 144 assertContainsAll("Any-Latin transforms Arabic digits", westernDigitSetAndMarks, fromArabic); 145 if (false) { // we don't require conversion to Arabic digits 146 String fromLatin = arabic.transform(westernDigits); 147 assertContainsAll("Any-Arabic transforms Western digits", arabicDigitSet, fromLatin); 148 } 149 } 150 151 // might want to add to TestFmwk 152 private void assertContainsAll(String message, UnicodeSet set, String string) { 153 handleAssert(set.containsAll(string), message, set, string, "contains all of", false); 154 } 155 156 private void assertContainsNone(String message, UnicodeSet set, String string) { 157 handleAssert(set.containsNone(string), message, set, string, "contains none of", false); 158 } 159 160 // might want to add to UnicodeSet 161 private String getList(UnicodeSet set) { 162 StringBuffer result = new StringBuffer(); 163 for (UnicodeSetIterator it = new UnicodeSetIterator(set); it.next();) { 164 result.append(it.getString()); 165 } 166 return result.toString(); 167 } 168 } 169