1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2004-2013, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.format; 11 12 import java.util.Locale; 13 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.junit.runners.JUnit4; 17 18 import android.icu.dev.test.TestFmwk; 19 import android.icu.text.RuleBasedNumberFormat; 20 import android.icu.util.ULocale; 21 import android.icu.testsharding.MainTestShard; 22 23 @MainTestShard 24 @RunWith(JUnit4.class) 25 public class RBNFParseTest extends TestFmwk { 26 @Test 27 public void TestParse() { 28 29 // these rules make no sense but behave rationally 30 String[] okrules = { 31 "random text", 32 "%foo:bar", 33 "%foo: bar", 34 "0:", 35 "0::", 36 "%%foo:;", 37 "-", 38 "-1", 39 "-:", 40 ".", 41 ".1", 42 "[", 43 "]", 44 "[]", 45 "[foo]", 46 "[[]", 47 "[]]", 48 "[[]]", 49 "[][]", 50 "<", 51 ">", 52 "=", 53 "==", 54 "===", 55 "=foo=", 56 }; 57 58 String[] exceptrules = { 59 "", 60 ";", 61 ";;", 62 ":", 63 "::", 64 ":1", 65 ":;", 66 ":;:;", 67 "<<", 68 "<<<", 69 "10:;9:;", 70 ">>", 71 ">>>", 72 "10:", // formatting any value with a one's digit will fail 73 "11: << x", // formating a multiple of 10 causes rollback rule to fail 74 "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;", 75 }; 76 77 String[][] allrules = { 78 okrules, 79 exceptrules, 80 }; 81 82 for (int j = 0; j < allrules.length; ++j) { 83 String[] tests = allrules[j]; 84 boolean except = tests == exceptrules; 85 for (int i = 0; i < tests.length; ++i) { 86 logln("----------"); 87 logln("rules: '" + tests[i] + "'"); 88 boolean caughtException = false; 89 try { 90 RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(tests[i], Locale.US); 91 logln("1.23: " + fmt.format(20)); 92 logln("-123: " + fmt.format(-123)); 93 logln(".123: " + fmt.format(.123)); 94 logln(" 123: " + fmt.format(123)); 95 } 96 catch (Exception e) { 97 if (!except) { 98 errln("Unexpected exception: " + e.getMessage()); 99 } else { 100 caughtException = true; 101 } 102 } 103 if (except && !caughtException) { 104 errln("expected exception but didn't get one!"); 105 } 106 } 107 } 108 } 109 110 private void parseFormat(RuleBasedNumberFormat rbnf, String s, String target) { 111 try { 112 Number n = rbnf.parse(s); 113 String t = rbnf.format(n); 114 assertEquals(rbnf.getLocale(ULocale.ACTUAL_LOCALE) + ": " + s + " : " + n, target, t); 115 } catch (java.text.ParseException e){ 116 fail("exception:" + e); 117 } 118 } 119 120 private void parseList(RuleBasedNumberFormat rbnf_en, RuleBasedNumberFormat rbnf_fr, String[][] lists) { 121 for (int i = 0; i < lists.length; ++i) { 122 String[] list = lists[i]; 123 String s = list[0]; 124 String target_en = list[1]; 125 String target_fr = list[2]; 126 127 parseFormat(rbnf_en, s, target_en); 128 parseFormat(rbnf_fr, s, target_fr); 129 } 130 } 131 132 @Test 133 public void TestLenientParse() throws Exception { 134 RuleBasedNumberFormat rbnf_en, rbnf_fr; 135 136 // TODO: this still passes, but setLenientParseMode should have no effect now. 137 // Did it ever test what it was supposed to? 138 rbnf_en = new RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT); 139 rbnf_en.setLenientParseMode(true); 140 rbnf_fr = new RuleBasedNumberFormat(Locale.FRENCH, RuleBasedNumberFormat.SPELLOUT); 141 rbnf_fr.setLenientParseMode(true); 142 143 Number n = rbnf_en.parse("1,2 million"); 144 logln(n.toString()); 145 146 String[][] lists = { 147 { "1,2", "twelve", "un virgule deux" }, 148 { "1,2 million", "twelve million", "un virgule deux" }, 149 { "1,2 millions", "twelve million", "un million deux cent mille" }, 150 { "1.2", "one point two", "douze" }, 151 { "1.2 million", "one million two hundred thousand", "douze" }, 152 { "1.2 millions", "one million two hundred thousand", "douze millions" }, 153 }; 154 155 Locale.setDefault(Locale.FRANCE); 156 logln("Default locale:" + Locale.getDefault()); 157 logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName()); 158 logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName()); 159 parseList(rbnf_en, rbnf_fr, lists); 160 161 Locale.setDefault(Locale.US); 162 logln("Default locale:" + Locale.getDefault()); 163 logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName()); 164 logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName()); 165 parseList(rbnf_en, rbnf_fr, lists); 166 } 167 } 168