public void Test_equalsLjava_lang_Object() { // Test for method boolean // java.text.ChoiceFormat.equals(java.lang.Object) String patternString = "-2#Inverted Orange| 0#No Orange| 0<Almost No Orange| 1#Normal Orange| 2#Expensive Orange"; double[] appleLimits = { 1, 2, 3, 4, 5 }; String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple", "Large Apple", "Huge Apple" }; double[] orangeLimits = { -2, 0, ChoiceFormat.NextDouble(0), 1, 2 }; String[] orangeFormats = { "Inverted Orange", "No Orange", "Almost No Orange", "Normal Orange", "Expensive Orange" }; ChoiceFormat appleChoiceFormat = new ChoiceFormat(appleLimits, appleFormats); ChoiceFormat orangeChoiceFormat = new ChoiceFormat(orangeLimits, orangeFormats); ChoiceFormat orangeChoiceFormat2 = new ChoiceFormat(patternString); ChoiceFormat hybridChoiceFormat = new ChoiceFormat(appleLimits, orangeFormats); NUnit.Framework.Assert.IsTrue(!appleChoiceFormat.Equals(orangeChoiceFormat), "Apples should not equal oranges"); NUnit.Framework.Assert.IsTrue(!orangeChoiceFormat.Equals(hybridChoiceFormat), "Different limit list--should not appear as equal"); NUnit.Framework.Assert.IsTrue(!appleChoiceFormat.Equals(hybridChoiceFormat), "Different format list--should not appear as equal"); NUnit.Framework.Assert.IsTrue(appleChoiceFormat.Equals(appleChoiceFormat), "Should be equal--identical format"); NUnit.Framework.Assert.IsTrue(orangeChoiceFormat.Equals(orangeChoiceFormat2), "Should be equals--same limits, same formats"); ChoiceFormat f2 = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); NUnit.Framework.Assert.IsTrue(f1.Equals(f2), "Not equal"); }
public void Test_ConstructorLjava_lang_String() { // Test for method java.text.ChoiceFormat(java.lang.String) String formattedString; String patternString = "-2#Inverted Orange| 0#No Orange| 0<Almost No Orange| 1#Normal Orange| 2#Expensive Orange"; ChoiceFormat cf = new ChoiceFormat(patternString); formattedString = cf.Format(System.Double.NegativeInfinity); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Inverted Orange"), "a) Incorrect format returned: " + formattedString); formattedString = cf.Format(-3); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Inverted Orange"), "b) Incorrect format returned: " + formattedString); formattedString = cf.Format(-2); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Inverted Orange"), "c) Incorrect format returned: " + formattedString); formattedString = cf.Format(-1); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Inverted Orange"), "d) Incorrect format returned: " + formattedString); formattedString = cf.Format(-0); NUnit.Framework.Assert.IsTrue(formattedString.Equals("No Orange"), "e) Incorrect format returned: " + formattedString); formattedString = cf.Format(0); NUnit.Framework.Assert.IsTrue(formattedString.Equals("No Orange"), "f) Incorrect format returned: " + formattedString); formattedString = cf.Format(0.1d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Almost No Orange"), "g) Incorrect format returned: " + formattedString); formattedString = cf.Format(1); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Normal Orange"), "h) Incorrect format returned: " + formattedString); formattedString = cf.Format(1.5d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Normal Orange"), "i) Incorrect format returned: " + formattedString); formattedString = cf.Format(2); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Expensive Orange"), "j) Incorrect format returned: " + formattedString); formattedString = cf.Format(3); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Expensive Orange"), "k) Incorrect format returned: " + formattedString); formattedString = cf.Format(System.Double.PositiveInfinity); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Expensive Orange"), "l) Incorrect format returned: " + formattedString); }
public void Test_nextDoubleDZ() { // Test for method double java.text.ChoiceFormat.nextDouble(double, // boolean) NUnit.Framework.Assert.IsTrue(ChoiceFormat.NextDouble(0, true) > 0, "Not greater 0"); NUnit.Framework.Assert.IsTrue(ChoiceFormat.NextDouble(0, false) < 0, "Not less 0"); }
public void Test_toPattern() { // Regression for HARMONY-59 ChoiceFormat cf = new ChoiceFormat(""); NUnit.Framework.Assert.AreEqual("", cf.ToPattern()); cf = new ChoiceFormat("-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE"); // format for double issue : NUnit.Framework.Assert.AreEqual("-1.0#NEGATIVE_ONE|0.0#ZERO|1.0#ONE|1.0<GREATER_THAN_ONE", cf.ToPattern()); MessageFormat mf = new MessageFormat("CHOICE {1,choice}"); String ptrn = mf.ToPattern(); NUnit.Framework.Assert.AreEqual("CHOICE {1,choice,}", ptrn, "Unused message format returning incorrect pattern"); String pattern = f1.ToPattern(); // format for double issue : NUnit.Framework.Assert.IsTrue(pattern.Equals("0.0#Less than one|1.0#one|1.0<Between one and two|2.0<Greater than two"), "Wrong pattern: " + pattern); cf = new ChoiceFormat( "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+|2#is two |2<is more than 2."); String str = "org.apache.harmony.tests.java.lang.share.MyResources2"; cf.ApplyPattern(str); ptrn = cf.ToPattern(); NUnit.Framework.Assert.AreEqual(0, ptrn.Length, "Return value should be empty string for invalid pattern"); }
public void Test_hashCode() { // Test for method int java.text.ChoiceFormat.hashCode() ChoiceFormat f2 = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); NUnit.Framework.Assert.IsTrue(f1.GetHashCode() == f2.GetHashCode(), "Different hash"); }
public void Test_previousDoubleD() { // Test for method double java.text.ChoiceFormat.previousDouble(double) NUnit.Framework.Assert.IsTrue(ChoiceFormat.PreviousDouble(5) < 5, "Not less 5"); NUnit.Framework.Assert.IsTrue(ChoiceFormat.PreviousDouble(0) < 0, "Not less 0"); NUnit.Framework.Assert.IsTrue(ChoiceFormat.PreviousDouble(-5) < -5, "Not less -5"); NUnit.Framework.Assert.IsTrue(Double.IsNaN(ChoiceFormat.PreviousDouble(System.Double.NaN)), "Not NaN"); }
public ChoiceFormatTest() { this.limits = new double[] { 0, 1, ChoiceFormat.NextDouble(1), ChoiceFormat.NextDouble(2) }; this.formats = new String[] { "Less than one", "one", "Between one and two", "Greater than two" }; this.f1 = new ChoiceFormat(limits, formats); }
public void Test_nextDoubleD() { // Test for method double java.text.ChoiceFormat.nextDouble(double) NUnit.Framework.Assert.IsTrue(ChoiceFormat.NextDouble(5) > 5, "Not greater 5"); NUnit.Framework.Assert.IsTrue(ChoiceFormat.NextDouble(0) > 0, "Not greater 0"); NUnit.Framework.Assert.IsTrue(ChoiceFormat.NextDouble(-5) > -5, "Not greater -5"); NUnit.Framework.Assert.IsTrue(Double.IsNaN(ChoiceFormat.NextDouble(System.Double.NaN)), "Not NaN"); }
public void Test_applyPatternLjava_lang_String() { // Test for method void // java.text.ChoiceFormat.applyPattern(java.lang.String) ChoiceFormat f = (ChoiceFormat)f1.Clone(); f.ApplyPattern("0#0|1#1"); NUnit.Framework.Assert.IsTrue(ILOG.J2CsMapping.Collections.Arrays.Equals(f.GetLimits(), new double[] { 0, 1 }), "Incorrect limits"); NUnit.Framework.Assert.IsTrue(ILOG.J2CsMapping.Collections.Arrays.Equals(f.GetFormats(), new String[] { "0", "1" }), "Incorrect formats"); // Regression for Harmony 540 double[] choiceLimits = { -1, 0, 1, ChoiceFormat.NextDouble(1) }; String[] choiceFormats = { "is negative", "is zero or fraction", "is one", "is more than 1" }; f = new ChoiceFormat(""); f.ApplyPattern("-1#is negative|0#is zero or fraction|1#is one|1<is more than 1"); NUnit.Framework.Assert.IsTrue(ILOG.J2CsMapping.Collections.Arrays.Equals(f.GetLimits(), choiceLimits), "Incorrect limits"); NUnit.Framework.Assert.IsTrue(ILOG.J2CsMapping.Collections.Arrays.Equals(f.GetFormats(), choiceFormats), "Incorrect formats"); f = new ChoiceFormat(""); try { f.ApplyPattern("-1#is negative|0#is zero or fraction|-1#is one|1<is more than 1"); NUnit.Framework.Assert.Fail("Expected IllegalArgumentException"); } catch (ArgumentException e) { // Expected } f = new ChoiceFormat(""); try { f.ApplyPattern("-1is negative|0#is zero or fraction|1#is one|1<is more than 1"); NUnit.Framework.Assert.Fail("Expected IllegalArgumentException"); } catch (ArgumentException e_0) { // Expected } f = new ChoiceFormat(""); f.ApplyPattern("-1<is negative|0#is zero or fraction|1#is one|1<is more than 1"); choiceLimits[0] = ChoiceFormat.NextDouble(-1); NUnit.Framework.Assert.IsTrue(ILOG.J2CsMapping.Collections.Arrays.Equals(f.GetLimits(), choiceLimits), "Incorrect limits"); NUnit.Framework.Assert.IsTrue(ILOG.J2CsMapping.Collections.Arrays.Equals(f.GetFormats(), choiceFormats), "Incorrect formats"); f = new ChoiceFormat(""); f.ApplyPattern("-1#is negative|0#is zero or fraction|1#is one|1<is more than 1"); String str = "org.apache.harmony.tests.java.text.ChoiceFormat"; f.ApplyPattern(str); String ptrn = f.ToPattern(); NUnit.Framework.Assert.AreEqual(0, ptrn.Length, "Return value should be empty string for invalid pattern"); }
public void Test_clone() { // Test for method java.lang.Object java.text.ChoiceFormat.clone() ChoiceFormat f = (ChoiceFormat)f1.Clone(); NUnit.Framework.Assert.IsTrue(f.Equals(f1), "Not equal"); f.SetChoices(new double[] { 0, 1, 2 }, new String[] { "0", "1", "2" }); NUnit.Framework.Assert.IsTrue(!f.Equals(f1), "Equal"); }
public void Test_formatL() { ChoiceFormat fmt = new ChoiceFormat( "-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE"); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(Int64.MinValue)); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(-1)); NUnit.Framework.Assert.AreEqual("ZERO", fmt.Format(0)); NUnit.Framework.Assert.AreEqual("ONE", fmt.Format(1)); NUnit.Framework.Assert.AreEqual("GREATER_THAN_ONE", fmt.Format(Int64.MaxValue)); }
public void Test_setChoices_D_Ljava_lang_String() { // Test for method void java.text.ChoiceFormat.setChoices(double [], // java.lang.String []) ChoiceFormat f = (ChoiceFormat)f1.Clone(); double[] l = new double[] { 0, 1 }; String[] fs = new String[] { "0", "1" }; f.SetChoices(l, fs); NUnit.Framework.Assert.IsTrue(f.GetLimits() == l, "Limits copied"); NUnit.Framework.Assert.IsTrue((Object)f.GetFormats() == (Object)fs, "Formats copied"); }
public void Test4106660() { double[] limits = { 3, 1, 2 }; String[] formats = { "Three", "One", "Two" }; ChoiceFormat cf = new ChoiceFormat(limits, formats); double d = 5.0; String str = cf.Format(d); if (!str.Equals("Two")) { Errln("format(" + d + ") = " + cf.Format(d)); } }
public void Test4052223() { ParsePosition pos = new ParsePosition(0); if (pos.ErrorIndex != -1) { Errln("ParsePosition.getErrorIndex initialization failed."); } MessageFormat fmt = new MessageFormat("There are {0} apples growing on the {1} tree."); String str = "There is one apple growing on the peach tree."; Object[] objs = fmt.Parse(str, pos); Logln("unparsable string , should fail at " + pos.ErrorIndex); if (pos.ErrorIndex == -1) { Errln("Bug 4052223 failed : parsing string " + str); } pos.ErrorIndex = (4); if (pos.ErrorIndex != 4) { Errln("setErrorIndex failed, got " + pos.ErrorIndex + " instead of 4"); } if (objs != null) { Errln("objs should be null"); } ChoiceFormat f = new ChoiceFormat( "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2."); pos.Index = (0); pos.ErrorIndex = (-1); /*Number*/ object obj = f.Parse("are negative", pos); if (pos.ErrorIndex != -1 && Convert.ToDouble(obj) == -1.0) { Errln("Parse with \"are negative\" failed, at " + pos.ErrorIndex); } pos.Index = (0); pos.ErrorIndex = (-1); obj = f.Parse("are no or fraction ", pos); if (pos.ErrorIndex != -1 && Convert.ToDouble(obj) == 0.0) { Errln("Parse with \"are no or fraction\" failed, at " + pos.ErrorIndex); } pos.Index = (0); pos.ErrorIndex = (-1); obj = f.Parse("go postal", pos); if (pos.ErrorIndex == -1 && !double.IsNaN(Convert.ToDouble(obj))) { Errln("Parse with \"go postal\" failed, at " + pos.ErrorIndex); } }
public void Test4104976() { double[] limits = { 1, 20 }; String[] formats = { "xyz", "abc" }; ChoiceFormat cf = new ChoiceFormat(limits, formats); try { Log("Compares to null is always false, returned : "); Logln(cf.Equals(null) ? "TRUE" : "FALSE"); } catch (Exception foo) { Errln("ChoiceFormat.Equals(null) throws exception."); } }
public void Test_formatD() { ChoiceFormat fmt = new ChoiceFormat( "-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE"); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(System.Double.NegativeInfinity)); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(-999999999D)); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(-1.1d)); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(-1.0d)); NUnit.Framework.Assert.AreEqual("NEGATIVE_ONE", fmt.Format(-0.9d)); NUnit.Framework.Assert.AreEqual("ZERO", fmt.Format(0.0d)); NUnit.Framework.Assert.AreEqual("ZERO", fmt.Format(0.9d)); NUnit.Framework.Assert.AreEqual("ONE", fmt.Format(1.0d)); NUnit.Framework.Assert.AreEqual("GREATER_THAN_ONE", fmt.Format(1.1d)); NUnit.Framework.Assert.AreEqual("GREATER_THAN_ONE", fmt.Format(999999999D)); NUnit.Framework.Assert.AreEqual("GREATER_THAN_ONE", fmt.Format(System.Double.PositiveInfinity)); }
public void Test4105380() { String patternText1 = "The disk \"{1}\" contains {0}."; String patternText2 = "There are {0} on the disk \"{1}\""; MessageFormat form1 = new MessageFormat(patternText1); MessageFormat form2 = new MessageFormat(patternText2); double[] filelimits = { 0, 1, 2 }; String[] filepart = { "no files", "one file", "{0,number} files" }; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); form1.SetFormat(1, fileform); form2.SetFormat(0, fileform); Object[] testArgs = { 12373L, "MyDisk" }; Logln(form1.Format(testArgs)); Logln(form2.Format(testArgs)); }
public void Test4094906() { ChoiceFormat fmt = new ChoiceFormat( "-\u221E<are negative|0<are no or fraction|1#is one|1.0<is 1+|\u221E<are many."); if (!fmt.ToPattern().StartsWith("-\u221E<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|\u221E<are many.", StringComparison.Ordinal)) { Errln("Formatter Pattern : " + fmt.ToPattern()); } Logln("Format with -INF : " + fmt.Format(double.NegativeInfinity)); Logln("Format with -1.0 : " + fmt.Format(-1.0)); Logln("Format with 0 : " + fmt.Format(0)); Logln("Format with 0.9 : " + fmt.Format(0.9)); Logln("Format with 1.0 : " + fmt.Format(1)); Logln("Format with 1.5 : " + fmt.Format(1.5)); Logln("Format with 2 : " + fmt.Format(2)); Logln("Format with +INF : " + fmt.Format(double.PositiveInfinity)); }
public void Test4106661() { ChoiceFormat fmt = new ChoiceFormat( "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2."); Logln("Formatter Pattern : " + fmt.ToPattern()); Logln("Format with -INF : " + fmt.Format(double.NegativeInfinity)); Logln("Format with -1.0 : " + fmt.Format(-1.0)); Logln("Format with 0 : " + fmt.Format(0)); Logln("Format with 0.9 : " + fmt.Format(0.9)); Logln("Format with 1.0 : " + fmt.Format(1)); Logln("Format with 1.5 : " + fmt.Format(1.5)); Logln("Format with 2 : " + fmt.Format(2)); Logln("Format with 2.1 : " + fmt.Format(2.1)); Logln("Format with NaN : " + fmt.Format(Double.NaN)); Logln("Format with +INF : " + fmt.Format(double.PositiveInfinity)); }
public void Test4106659() { double[] limits = { 1, 2, 3 }; String[] formats = { "one", "two" }; ChoiceFormat cf = null; try { cf = new ChoiceFormat(limits, formats); } catch (Exception foo) { Logln("ChoiceFormat constructor should check for the array lengths"); cf = null; } if (cf != null) { Errln(cf.Format(5)); } }
public void TestChoicePatternQuote() { String[] DATA = { // Pattern 0 value 1 value "0#can''t|1#can", "can't", "can", "0#'pound(#)=''#'''|1#xyz", "pound(#)='#'", "xyz", "0#'1<2 | 1\u22641'|1#''", "1<2 | 1\u22641", "'", }; for (int i = 0; i < DATA.Length; i += 3) { try { ChoiceFormat cf = new ChoiceFormat(DATA[i]); for (int j = 0; j <= 1; ++j) { String @out = cf.Format(j); if ([email protected](DATA[i + 1 + j])) { Errln("Fail: Pattern \"" + DATA[i] + "\" x " + j + " -> " + @out + "; want \"" + DATA[i + 1 + j] + '"'); } } String pat = cf.ToPattern(); String pat2 = new ChoiceFormat(pat).ToPattern(); if (!pat.Equals(pat2)) { Errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"'); } else { Logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"'); } } catch (ArgumentException e) { Errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e); } } }
public void Test_setCurrencyLjava_util_Currency() { // Test for method void setCurrency(java.util.Currency) // a subclass that supports currency formatting Currency currA = Currency.GetInstance("ARS"); IBM.ICU.Text.NumberFormat format = IBM.ICU.Text.NumberFormat.GetInstance(new ILOG.J2CsMapping.Util.Locale("hu", "HU")); format.SetCurrency(currA); NUnit.Framework.Assert.AreSame(currA, format.GetCurrency(), "Returned incorrect currency"); // a subclass that doesn't support currency formatting ChoiceFormat cformat = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); try { //((IBM.ICU.Text.NumberFormat)cformat).SetCurrency(currA); //NUnit.Framework.Assert.Fail("Expected UnsupportedOperationException"); } catch (NotSupportedException e) { } }
public void Test_Constructor_D_Ljava_lang_String() { // Test for method java.text.ChoiceFormat(double [], java.lang.String // []) String formattedString; double[] appleLimits = { 1, 2, 3, 4, 5 }; String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple", "Large Apple", "Huge Apple" }; ChoiceFormat cf = new ChoiceFormat(appleLimits, appleFormats); formattedString = cf.Format(System.Double.NegativeInfinity); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Tiny Apple"), "a) Incorrect format returned: " + formattedString); formattedString = cf.Format(0.5d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Tiny Apple"), "b) Incorrect format returned: " + formattedString); formattedString = cf.Format(1d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Tiny Apple"), "c) Incorrect format returned: " + formattedString); formattedString = cf.Format(1.5d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Tiny Apple"), "d) Incorrect format returned: " + formattedString); formattedString = cf.Format(2d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Small Apple"), "e) Incorrect format returned: " + formattedString); formattedString = cf.Format(2.5d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Small Apple"), "f) Incorrect format returned: " + formattedString); formattedString = cf.Format(3d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Medium Apple"), "g) Incorrect format returned: " + formattedString); formattedString = cf.Format(4d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Large Apple"), "h) Incorrect format returned: " + formattedString); formattedString = cf.Format(5d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Huge Apple"), "i) Incorrect format returned: " + formattedString); formattedString = cf.Format(5.5d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Huge Apple"), "j) Incorrect format returned: " + formattedString); formattedString = cf.Format(6.0d); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Huge Apple"), "k) Incorrect format returned: " + formattedString); formattedString = cf.Format(System.Double.PositiveInfinity); NUnit.Framework.Assert.IsTrue(formattedString.Equals("Huge Apple"), "l) Incorrect format returned: " + formattedString); }
public void Test_parseLjava_lang_StringLjava_text_ParsePosition() { // Test for method java.lang.Number // java.text.ChoiceFormat.parse(java.lang.String, // java.text.ParsePosition) ChoiceFormat format = new ChoiceFormat("1#one|2#two|3#three"); NUnit.Framework.Assert.AreEqual(Double.NaN, System.Convert.ToDouble(format.Parse("One", new ParsePosition(0))), "Case insensitive"); ParsePosition pos = new ParsePosition(0); object result = f1.Parse("Greater than two", pos); NUnit.Framework.Assert.IsTrue(result is Double, "Not a Double1"); NUnit.Framework.Assert.IsTrue(Convert.ToDouble(result) == ChoiceFormat.NextDouble(2), "Wrong value ~>2"); NUnit.Framework.Assert.AreEqual(16, pos.GetIndex(), "Wrong position ~16"); pos = new ParsePosition(0); NUnit.Framework.Assert.IsTrue(Double.IsNaN(Convert.ToDouble(f1.Parse("12one", pos))), "Incorrect result"); NUnit.Framework.Assert.AreEqual(0, pos.GetIndex(), "Wrong position ~0"); pos = new ParsePosition(2); result = f1.Parse("12one and two", pos); NUnit.Framework.Assert.IsTrue(result is Double, "Not a Double2"); NUnit.Framework.Assert.AreEqual(1.0D, Convert.ToDouble(result), 0.0D, "Ignored parse position"); NUnit.Framework.Assert.AreEqual(5, pos.GetIndex(), "Wrong position ~5"); }
/// <exclude/> /// <summary> /// Attempt to parse the given string as a currency, either as a display name /// in the given locale, or as a 3-letter ISO 4217 code. If multiple display /// names match, then the longest one is selected. If both a display name and /// a 3-letter ISO code match, then the display name is preferred, unless /// it's length is less than 3. /// </summary> /// /// <param name="locale">the locale of the display names to match</param> /// <param name="text">the text to parse</param> /// <param name="pos">input-output position; on input, the position within text tomatch; must have 0 <= pos.getIndex() < text.length(); onoutput, the position after the last matched character. If theparse fails, the position in unchanged upon output.</param> /// <returns>the ISO 4217 code, as a string, of the best match, or null if /// there is no match</returns> public static String Parse(ULocale locale, String text, ILOG.J2CsMapping.Text.ParsePosition pos) { // TODO: There is a slight problem with the pseudo-multi-level // fallback implemented here. More-specific locales don't // properly shield duplicate entries in less-specific locales. // This problem will go away when real multi-level fallback is // implemented. We could also fix this by recording (in a // hash) which codes are used at each level of fallback, but // this doesn't seem warranted. int start = pos.GetIndex(); String fragment = text.Substring(start); String iso = null; int max = 0; // Look up the Currencies resource for the given locale. The // Currencies locale data looks like this: // |en { // | Currencies { // | USD { "US$", "US Dollar" } // | CHF { "Sw F", "Swiss Franc" } // | INR { "=0#Rs|1#Re|1<Rs", "=0#Rupees|1#Rupee|1<Rupees" } // | //... // | } // |} // In the future, resource bundles may implement multi-level // fallback. That is, if a currency is not found in the en_US // Currencies data, then the en Currencies data will be searched. // Currently, if a Currencies datum exists in en_US and en, the // en_US entry hides that in en. // We want multi-level fallback for this resource, so we implement // it manually. // Multi-level resource inheritance fallback loop while (locale != null) { UResourceBundle rb = IBM.ICU.Util.UResourceBundle.GetBundleInstance( IBM.ICU.Impl.ICUResourceBundle.ICU_BASE_NAME, locale); // We can't cast this to String[][]; the cast has to happen later try { UResourceBundle currencies = rb.Get("Currencies"); // Do a linear search for (int i = 0; i < currencies.GetSize(); ++i) { // String name = ((String[]) currencies[i][1])[0]; UResourceBundle item = currencies.Get(i); String name = item.GetString(0); if (name.Length < 1) { // Ignore zero-length names -- later, change this // when zero-length is used to mean something. continue; } else if (name[0] == '=') { name = name.Substring(1); if (name.Length > 0 && name[0] != '=') { ChoiceFormat choice = new ChoiceFormat(name); // Number n = choice.Parse(text, pos); int len = pos.GetIndex() - start; if (len > max) { iso = item.GetKey(); max = len; } pos.SetIndex(start); continue; } } if (name.Length > max && fragment.StartsWith(name)) { iso = item.GetKey(); max = name.Length; } } } catch (MissingManifestResourceException e) { } locale = locale.GetFallback(); } /* * 1. Look at the Currencies array from the locale 1a. Iterate through * it, and check each row to see if row[1] matches 1a1. If row[1] is a * pattern, use ChoiceFormat to attempt a parse 1b. Upon a match, return * the ISO code stored at row[0] 2. If there is no match, fall back to * "en" and try again 3. If there is no match, fall back to root and try * again 4. If still no match, parse 3-letter ISO {this code is probably * unchanged}. * * ICUResourceBundle rb = * (ICUResourceBundle)UResourceBundle.getBundleInstance * (UResourceBundle.ICU_BASE_NAME, locale); ICUResourceBundle currencies * = rb.get("Currencies"); */ // If display name parse fails or if it matches fewer than 3 // characters, try to parse 3-letter ISO. Do this after the // display name processing so 3-letter display names are // preferred. Consider /[A-Z]{3}/ to be valid ISO, and parse // it manually--UnicodeSet/regex are too slow and heavy. if (max < 3 && (text.Length - start) >= 3) { bool valid = true; for (int k = 0; k < 3; ++k) { char ch = text[start + k]; // 16-bit ok if (ch < 'A' || ch > 'Z') { valid = false; break; } } if (valid) { iso = text.Substring(start, (start + 3) - (start)); max = 3; } } pos.SetIndex(start + max); return(iso); }
public void TestBugTestsWithNamesArguments() { { // Taken from Test4031438(). String pattern1 = "Impossible {arg1} has occurred -- status code is {arg0} and message is {arg2}."; String pattern2 = "Double '' Quotes {ARG_ZERO} test and quoted '{ARG_ONE}' test plus 'other {ARG_TWO} stuff'."; MessageFormat messageFormatter = new MessageFormat(""); try { Logln("Apply with pattern : " + pattern1); messageFormatter.ApplyPattern(pattern1); IDictionary <string, object> paramsMap = new Dictionary <string, object>(); paramsMap["arg0"] = 7; String tempBuffer = messageFormatter.Format(paramsMap); if (!tempBuffer.Equals("Impossible {arg1} has occurred -- status code is 7 and message is {arg2}.")) { Errln("Tests arguments < substitution failed"); } Logln("Formatted with 7 : " + tempBuffer); ParsePosition status = new ParsePosition(0); var objs = messageFormatter.ParseToMap(tempBuffer, status); if (objs.Get("arg1") != null || objs.Get("arg2") != null) { Errln("Parse failed with more than expected arguments"); } //for (Iterator keyIter = objs.keySet().iterator(); // keyIter.hasNext();) //{ // String key = (String)keyIter.next(); foreach (var key in objs.Keys) { if (objs.Get(key) != null && !objs.Get(key).ToString().Equals(paramsMap.Get(key).ToString())) { Errln("Parse failed on object " + objs.Get(key) + " with argument name : " + key); } } tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Impossible {arg1} has occurred -- status code is {arg0} and message is {arg2}.")) { Errln("Tests with no arguments failed"); } Logln("Formatted with null : " + tempBuffer); Logln("Apply with pattern : " + pattern2); messageFormatter.ApplyPattern(pattern2); paramsMap.Clear(); paramsMap["ARG_ZERO"] = 7; tempBuffer = messageFormatter.Format(paramsMap); if (!tempBuffer.Equals("Double ' Quotes 7 test and quoted {ARG_ONE} test plus 'other {ARG_TWO} stuff'.")) { Errln("quote format test (w/ params) failed."); } Logln("Formatted with params : " + tempBuffer); tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Double ' Quotes {ARG_ZERO} test and quoted {ARG_ONE} test plus 'other {ARG_TWO} stuff'.")) { Errln("quote format test (w/ null) failed."); } Logln("Formatted with null : " + tempBuffer); Logln("toPattern : " + messageFormatter.ToPattern()); } catch (Exception foo) { Warnln("Exception when formatting in bug 4031438. " + foo.Message); } } { // Taken from Test4052223(). ParsePosition pos = new ParsePosition(0); if (pos.ErrorIndex != -1) { Errln("ParsePosition.getErrorIndex initialization failed."); } MessageFormat fmt = new MessageFormat("There are {numberOfApples} apples growing on the {whatKindOfTree} tree."); String str = "There is one apple growing on the peach tree."; var objs = fmt.ParseToMap(str, pos); Logln("unparsable string , should fail at " + pos.ErrorIndex); if (pos.ErrorIndex == -1) { Errln("Bug 4052223 failed : parsing string " + str); } pos.ErrorIndex = (4); if (pos.ErrorIndex != 4) { Errln("setErrorIndex failed, got " + pos.ErrorIndex + " instead of 4"); } if (objs != null) { Errln("unparsable string, should return null"); } } // ICU4N TODO: Serialization //{ // Taken from Test4111739(). // MessageFormat format1 = null; // MessageFormat format2 = null; // ObjectOutputStream ostream = null; // ByteArrayOutputStream baos = null; // ObjectInputStream istream = null; // try // { // baos = new ByteArrayOutputStream(); // ostream = new ObjectOutputStream(baos); // } // catch (IOException e) // { // Errln("Unexpected exception : " + e.getMessage()); // return; // } // try // { // format1 = new MessageFormat("pattern{argument}"); // ostream.writeObject(format1); // ostream.flush(); // byte bytes[] = baos.toByteArray(); // istream = new ObjectInputStream(new ByteArrayInputStream(bytes)); // format2 = (MessageFormat)istream.readObject(); // } // catch (Exception e) // { // Errln("Unexpected exception : " + e.Message); // } // if (!format1.Equals(format2)) // { // Errln("MessageFormats before and after serialization are not" + // " equal\nformat1 = " + format1 + "(" + format1.ToPattern() + ")\nformat2 = " + // format2 + "(" + format2.ToPattern() + ")"); // } // else // { // Logln("Serialization for MessageFormat is OK."); // } //} { // Taken from Test4116444(). String[] patterns = { "", "one", "{namedArgument,date,short}" }; MessageFormat mf = new MessageFormat(""); for (int i = 0; i < patterns.Length; i++) { String pattern = patterns[i]; mf.ApplyPattern(pattern); try { var objs = mf.ParseToMap(null, new ParsePosition(0)); Logln("pattern: \"" + pattern + "\""); Log(" parsedObjects: "); if (objs != null) { Log("{"); //for (Iterator keyIter = objs.keySet().iterator(); // keyIter.hasNext();) //{ // String key = (String)keyIter.next(); bool first = true; foreach (var key in objs.Keys) { if (first) { first = false; } else { Log(","); } if (objs.Get(key) != null) { Err("\"" + objs.Get(key).ToString() + "\""); } else { Log("null"); } //if (keyIter.hasNext()) //{ // Log(","); //} } Log("}"); } else { Log("null"); } Logln(""); } catch (Exception e) { Errln("pattern: \"" + pattern + "\""); Errln(" Exception: " + e.Message); } } } { // Taken from Test4114739(). MessageFormat mf = new MessageFormat("<{arg}>"); IDictionary <string, object> objs1 = null; IDictionary <string, object> objs2 = new Dictionary <string, object>(); IDictionary <string, object> objs3 = new Dictionary <string, object>(); objs3["arg"] = null; try { Logln("pattern: \"" + mf.ToPattern() + "\""); Log("format(null) : "); Logln("\"" + mf.Format(objs1) + "\""); Log("format({}) : "); Logln("\"" + mf.Format(objs2) + "\""); Log("format({null}) :"); Logln("\"" + mf.Format(objs3) + "\""); } catch (Exception e) { Errln("Exception thrown for null argument tests."); } } { // Taken from Test4118594(). String argName = "something_stupid"; MessageFormat mf = new MessageFormat("{" + argName + "}, {" + argName + "}, {" + argName + "}"); String forParsing = "x, y, z"; var objs = mf.ParseToMap(forParsing, new ParsePosition(0)); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + forParsing + "\""); if (!objs.Get(argName).ToString().Equals("z")) { Errln("argument0: \"" + objs.Get(argName) + "\""); } mf.SetLocale(new CultureInfo("en-us")); mf.ApplyPattern("{" + argName + ",number,#.##}, {" + argName + ",number,#.#}"); var oldobjs = new Dictionary <string, object>(); oldobjs[argName] = 3.1415d; String result = mf.Format(oldobjs); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + result + "\""); // result now equals "3.14, 3.1" if (!result.Equals("3.14, 3.1")) { Errln("result = " + result); } var newobjs = mf.ParseToMap(result, new ParsePosition(0)); // newobjs now equals {new Double(3.1)} if (Convert.ToDouble(newobjs.Get(argName)) != 3.1) // was (Double) [alan] { Errln("newobjs.get(argName) = " + newobjs.Get(argName)); } } { // Taken from Test4105380(). String patternText1 = "The disk \"{diskName}\" contains {numberOfFiles}."; String patternText2 = "There are {numberOfFiles} on the disk \"{diskName}\""; MessageFormat form1 = new MessageFormat(patternText1); MessageFormat form2 = new MessageFormat(patternText2); double[] filelimits = { 0, 1, 2 }; String[] filepart = { "no files", "one file", "{numberOfFiles,number} files" }; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); form1.SetFormat(1, fileform); form2.SetFormat(0, fileform); var testArgs = new Dictionary <string, object>(); testArgs["diskName"] = "MyDisk"; testArgs["numberOfFiles"] = 12373L; Logln(form1.Format(testArgs)); Logln(form2.Format(testArgs)); } { // Taken from test4293229(). MessageFormat format = new MessageFormat("'''{'myNamedArgument}'' '''{myNamedArgument}'''"); var args = new Dictionary <string, object>(); String expected = "'{myNamedArgument}' '{myNamedArgument}'"; String result = format.Format(args); if (!result.Equals(expected)) { throw new Exception("wrong format result - expected \"" + expected + "\", got \"" + result + "\""); } } }