public void CheckIncrementalAux(Transliterator t, String input) { IReplaceable test = new ReplaceableString(input); TransliterationPosition pos = new TransliterationPosition(0, test.Length, 0, test.Length); t.Transliterate(test, pos); bool gotError = false; // we have a few special cases. Any-Remove (pos.start = 0, but also = limit) and U+XXXXX?X? if (pos.Start == 0 && pos.Limit != 0 && !t.ID.Equals("Hex-Any/Unicode")) { Errln("No Progress, " + t.ID + ": " + UtilityExtensions.FormatInput(test, pos)); gotError = true; } else { Logln("PASS Progress, " + t.ID + ": " + UtilityExtensions.FormatInput(test, pos)); } t.FinishTransliteration(test, pos); if (pos.Start != pos.Limit) { Errln("Incomplete, " + t.ID + ": " + UtilityExtensions.FormatInput(test, pos)); gotError = true; } if (!gotError) { //Errln("FAIL: Did not get expected error"); } }
/// <summary> /// For debugging purposes; format the given text in the form /// aaa{bbb|ccc|ddd}eee, where the {} indicate the context start /// and limit, and the || indicate the start and limit. /// </summary> /// <param name="appendTo"></param> /// <param name="input"></param> /// <param name="pos"></param> /// <returns></returns> public static StringBuffer FormatInput(StringBuffer appendTo, ReplaceableString input, Transliterator.Position pos) { if (0 <= pos.ContextStart && pos.ContextStart <= pos.Start && pos.Start <= pos.Limit && pos.Limit <= pos.ContextLimit && pos.ContextLimit <= input.Length) { string b, c, d; //a = input.substring(0, pos.contextStart); b = input.Substring(pos.ContextStart, pos.Start - pos.ContextStart); // ICU4N: Corrected 2nd parameter c = input.Substring(pos.Start, pos.Limit - pos.Start); // ICU4N: Corrected 2nd parameter d = input.Substring(pos.Limit, pos.ContextLimit - pos.Limit); // ICU4N: Corrected 2nd parameter //e = input.substring(pos.contextLimit, input.length()); appendTo. //Append(a). Append('{').Append(b). Append('|').Append(c).Append('|').Append(d). Append('}') //.Append(e) ; } else { appendTo.Append("INVALID Position {cs=" + pos.ContextStart + ", s=" + pos.Start + ", l=" + pos.Limit + ", cl=" + pos.ContextLimit + "} on " + input); } return(appendTo); }
internal TestReplaceable(String text, String styles) { Chars = new ReplaceableString(text); StringBuffer s = new StringBuffer(); for (int i = 0; i < text.Length; ++i) { if (styles != null && i < styles.Length) { s.Append(styles[i]); } else { if (text[i] == NO_STYLE_MARK) { s.Append(NO_STYLE); } else { s.Append((char)(i + '1')); } } } this.Styles = new ReplaceableString(s.ToString()); }
/// <summary> /// For debugging purposes; format the given text in the form /// aaa{bbb|ccc|ddd}eee, where the {} indicate the context start and limit, /// and the || indicate the start and limit. /// </summary> /// public static StringBuilder FormatInput(StringBuilder appendTo, ReplaceableString input, Transliterator.Position pos) { if (0 <= pos.contextStart && pos.contextStart <= pos.start && pos.start <= pos.limit && pos.limit <= pos.contextLimit && pos.contextLimit <= input.Length()) { String b, c, d; // a = input.substring(0, pos.contextStart); b = input.Substring(pos.contextStart, pos.start); c = input.Substring(pos.start, pos.limit); d = input.Substring(pos.limit, pos.contextLimit); // e = input.substring(pos.contextLimit, input.length()); appendTo.// append(a). Append('{').Append(b).Append('|').Append(c).Append('|').Append(d) .Append('}') // .append(e) ; } else { appendTo.Append("INVALID Position {cs=" + pos.contextStart + ", s=" + pos.start + ", l=" + pos.limit + ", cl=" + pos.contextLimit + "} on " + input); } return(appendTo); }
/// <summary> /// For debugging purposes; format the given text in the form /// aaa{bbb|ccc|ddd}eee, where the {} indicate the context start /// and limit, and the || indicate the start and limit. /// </summary> public static string FormatInput(ReplaceableString input, Transliterator.Position pos) { StringBuffer appendTo = new StringBuffer(); FormatInput(appendTo, input, pos); return(Utility.Escape(appendTo.ToString())); }
private void expect(Transliterator t, String source, String expectedResult) { String result = t.Transliterate(source); expectAux(t.ID + ":String", source, result, expectedResult); ReplaceableString rsource = new ReplaceableString(source); t.Transliterate(rsource); result = rsource.ToString(); expectAux(t.ID + ":Replaceable", source, result, expectedResult); // Test keyboard (incremental) transliteration -- this result // must be the same after we finalize (see below). rsource.Replace(0, rsource.Length, ""); TransliterationPosition index = new TransliterationPosition(); StringBuffer log = new StringBuffer(); for (int i = 0; i < source.Length; ++i) { if (i != 0) { log.Append(" + "); } log.Append(source[i]).Append(" -> "); t.Transliterate(rsource, index, source[i] + ""); // Append the string buffer with a vertical bar '|' where // the committed index is. String s = rsource.ToString(); log.Append(s.Substring(0, index.Start)). // ICU4N: Checked 2nd parameter Append('|'). Append(s.Substring(index.Start)); } // As a final step in keyboard transliteration, we must call // transliterate to finish off any pending partial matches that // were waiting for more input. t.FinishTransliteration(rsource, index); result = rsource.ToString(); log.Append(" => ").Append(rsource.ToString()); expectAux(t.ID + ":Keyboard", log.ToString(), result.Equals(expectedResult), expectedResult); }
public void TestTransliterate() { Logln("Testing the handleTransliterate() API of CompoundTransliterator"); Transliterator ct1 = null; try { ct1 = Transliterator.GetInstance("Any-Hex;Hex-Any"); } catch (ArgumentException iae) { Errln("FAIL: construction using CompoundTransliterator(String ID) failed for " + "Any-Hex;Hex-Any"); throw iae; } String s = "abcabc"; expect(ct1, s, s); TransliterationPosition index = new TransliterationPosition(); ReplaceableString rsource2 = new ReplaceableString(s); String expectedResult = s; ct1.Transliterate(rsource2, index); ct1.FinishTransliteration(rsource2, index); String result = rsource2.ToString(); expectAux(ct1.ID + ":ReplaceableString, index(0,0,0,0)", s + "->" + rsource2, result.Equals(expectedResult), expectedResult); TransliterationPosition index2 = new TransliterationPosition(1, 3, 2, 3); ReplaceableString rsource3 = new ReplaceableString(s); ct1.Transliterate(rsource3, index2); ct1.FinishTransliteration(rsource3, index2); result = rsource3.ToString(); expectAux(ct1.ID + ":String, index2(1,2,2,3)", s + "->" + rsource3, result.Equals(expectedResult), expectedResult); String[] Data = { //ID, input string, transliterated string "Any-Hex;Hex-Any;Any-Hex", "hello", "\\u0068\\u0065\\u006C\\u006C\\u006F", "Any-Hex;Hex-Any", "hello! How are you?", "hello! How are you?", "Devanagari-Latin;Latin-Devanagari", "\u092D\u0948'\u0930'\u0935", "\u092D\u0948\u0930\u0935", // quotes lost "Latin-Cyrillic;Cyrillic-Latin", "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h", "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h", "Latin-Greek;Greek-Latin", "ABGabgAKLMN", "ABGabgAKLMN", //"Latin-Arabic;Arabic-Latin", "Ad'r'a'b'i'k'dh'dd'gh", "Adrabikdhddgh", "Hiragana-Katakana", "\u3041\u308f\u3099\u306e\u304b\u3092\u3099", "\u30A1\u30f7\u30ce\u30ab\u30fa", "Hiragana-Katakana;Katakana-Hiragana", "\u3041\u308f\u3099\u306e\u304b\u3051", "\u3041\u308f\u3099\u306e\u304b\u3051", "Katakana-Hiragana;Hiragana-Katakana", "\u30A1\u30f7\u30ce\u30f5\u30f6", "\u30A1\u30f7\u30ce\u30ab\u30b1", "Latin-Katakana;Katakana-Latin", "vavivuvevohuzizuzoninunasesuzezu", "vavivuvevohuzizuzoninunasesuzezu", }; Transliterator ct2 = null; for (int i = 0; i < Data.Length; i += 3) { try { ct2 = Transliterator.GetInstance(Data[i + 0]); } catch (ArgumentException iae2) { Errln("FAIL: CompoundTransliterator construction failed for " + Data[i + 0]); throw iae2; } expect(ct2, Data[i + 1], Data[i + 2]); } }
public void TestTransliteratorErrors() { String trans = "Latin-Greek"; String bogusID = "LATINGREEK-GREEKLATIN"; String newID = "Bogus-Latin"; String newIDRules = "zzz > Z; f <> ph"; String bogusRules = "a } [b-g m-p "; ReplaceableString testString = new ReplaceableString("A quick fox jumped over the lazy dog."); String insertString = "cats and dogs"; int stoppedAt = 0, len; Transliterator.Position pos = new Transliterator.Position(); Transliterator t = Transliterator.GetInstance(trans, Transliterator.FORWARD); if (t == null) { Errln("FAIL: construction of Latin-Greek"); return; } len = testString.Length; stoppedAt = t.Transliterate(testString, 0, 100); if (stoppedAt != -1) { Errln("FAIL: Out of bounds check failed (1)."); } else if (testString.Length != len) { testString = new ReplaceableString("A quick fox jumped over the lazy dog."); Errln("FAIL: Transliterate fails and the target string was modified."); } stoppedAt = t.Transliterate(testString, 100, testString.Length - 1); if (stoppedAt != -1) { Errln("FAIL: Out of bounds check failed (2)."); } else if (testString.Length != len) { testString = new ReplaceableString("A quick fox jumped over the lazy dog."); Errln("FAIL: Transliterate fails and the target string was modified."); } pos.Start = 100; pos.Limit = testString.Length; try { t.Transliterate(testString, pos); Errln("FAIL: Start offset is out of bounds, error not reported."); } catch (ArgumentException e) { Logln("Start offset is out of bounds and detected."); } pos.Limit = 100; pos.Start = 0; try { t.Transliterate(testString, pos); Errln("FAIL: Limit offset is out of bounds, error not reported.\n"); } catch (ArgumentException e) { Logln("Start offset is out of bounds and detected."); } len = pos.ContextLimit = testString.Length; pos.ContextStart = 0; pos.Limit = len - 1; pos.Start = 5; try { t.Transliterate(testString, pos, insertString); if (len == pos.Limit) { Errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected."); } } catch (ArgumentException e) { Errln("Insertion test with string failed for some reason."); } pos.ContextStart = 0; pos.ContextLimit = testString.Length; pos.Limit = testString.Length - 1; pos.Start = 5; try { t.Transliterate(testString, pos, 0x0061); if (len == pos.Limit) { Errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected."); } } catch (ArgumentException e) { Errln("FAIL: Insertion test with UTF-16 code point failed for some reason."); } len = pos.Limit = testString.Length; pos.ContextStart = 0; pos.ContextLimit = testString.Length - 1; pos.Start = 5; try { t.Transliterate(testString, pos, insertString); Errln("FAIL: Out of bounds check failed (3)."); if (testString.Length != len) { Errln("FAIL: The input string was modified though the offsets were out of bounds."); } } catch (ArgumentException e) { Logln("Insertion test with out of bounds indexes."); } Transliterator t1 = null; try { t1 = Transliterator.GetInstance(bogusID, Transliterator.FORWARD); if (t1 != null) { Errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\""); } } catch (ArgumentException e) { } //try { // unneeded - Exception cannot be thrown Transliterator t2 = Transliterator.CreateFromRules( newID, newIDRules, Transliterator.FORWARD); try { Transliterator t3 = t2.GetInverse(); Errln("FAIL: The newID transliterator was not registered so createInverse should fail."); if (t3 != null) { Errln("FAIL: The newID transliterator was not registered so createInverse should fail."); } } catch (Exception e) { } //} catch (Exception e) { } try { Transliterator t4 = Transliterator.CreateFromRules( newID, bogusRules, Transliterator.FORWARD); if (t4 != null) { Errln("FAIL: The rules is malformed but error was not reported."); } } catch (Exception e) { } }