Search in sources :

Example 1 with FieldContainer

use of android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer in project j2objc by google.

the class NumberFormatTest method CompareAttributedCharacterFormatOutput.

private void CompareAttributedCharacterFormatOutput(AttributedCharacterIterator iterator, List<FieldContainer> expected, String formattedOutput) {
    List<FieldContainer> result = new ArrayList<FieldContainer>();
    while (iterator.getIndex() != iterator.getEndIndex()) {
        int start = iterator.getRunStart();
        int end = iterator.getRunLimit();
        Iterator it = iterator.getAttributes().keySet().iterator();
        AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) it.next();
        Object value = iterator.getAttribute(attribute);
        result.add(new FieldContainer(start, end, attribute, value));
        iterator.setIndex(end);
    }
    assertEquals("Comparing vector length for " + formattedOutput, expected.size(), result.size());
    if (!expected.containsAll(result)) {
        // Print information on the differences.
        for (int i = 0; i < expected.size(); i++) {
            System.out.println("     expected[" + i + "] =" + expected.get(i).start + " " + expected.get(i).end + " " + expected.get(i).attribute + " " + expected.get(i).value);
            System.out.println(" result[" + i + "] =" + result.get(i).start + " " + result.get(i).end + " " + result.get(i).attribute + " " + result.get(i).value);
        }
    }
// TODO: restore when #11914 is fixed.
// assertTrue("Comparing vector results for " + formattedOutput,
// expected.containsAll(result));
}
Also used : ArrayList(java.util.ArrayList) TokenIterator(android.icu.impl.data.TokenIterator) Iterator(java.util.Iterator) AttributedCharacterIterator(java.text.AttributedCharacterIterator) FieldContainer(android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 2 with FieldContainer

use of android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer in project j2objc by google.

the class NumberFormatTest method TestNPEIssue11914.

// Testing for Issue 11914, missing FieldPositions for some field types.
@Test
public void TestNPEIssue11914() {
    // First test: Double value with grouping separators.
    List<FieldContainer> v1 = new ArrayList<FieldContainer>(7);
    v1.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
    v1.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR));
    v1.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER));
    v1.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR));
    v1.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER));
    v1.add(new FieldContainer(11, 12, NumberFormat.Field.DECIMAL_SEPARATOR));
    v1.add(new FieldContainer(12, 15, NumberFormat.Field.FRACTION));
    Number number = new Double(123456789.9753);
    ULocale usLoc = new ULocale("en-US");
    DecimalFormatSymbols US = new DecimalFormatSymbols(usLoc);
    NumberFormat outFmt = NumberFormat.getNumberInstance(usLoc);
    String numFmtted = outFmt.format(number);
    AttributedCharacterIterator iterator = outFmt.formatToCharacterIterator(number);
    CompareAttributedCharacterFormatOutput(iterator, v1, numFmtted);
    // Second test: Double with scientific notation formatting.
    List<FieldContainer> v2 = new ArrayList<FieldContainer>(7);
    v2.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER));
    v2.add(new FieldContainer(1, 2, NumberFormat.Field.DECIMAL_SEPARATOR));
    v2.add(new FieldContainer(2, 5, NumberFormat.Field.FRACTION));
    v2.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL));
    v2.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SIGN));
    v2.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT));
    DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
    numFmtted = fmt2.format(number);
    iterator = fmt2.formatToCharacterIterator(number);
    CompareAttributedCharacterFormatOutput(iterator, v2, numFmtted);
    // Third test. BigInteger with grouping separators.
    List<FieldContainer> v3 = new ArrayList<FieldContainer>(7);
    v3.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
    v3.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(6, 7, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(7, 10, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(10, 11, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(11, 14, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(14, 15, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(15, 18, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(18, 19, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(19, 22, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(22, 23, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(23, 26, NumberFormat.Field.INTEGER));
    BigInteger bigNumberInt = new BigInteger("-1234567890246813579");
    String fmtNumberBigInt = outFmt.format(bigNumberInt);
    iterator = outFmt.formatToCharacterIterator(bigNumberInt);
    CompareAttributedCharacterFormatOutput(iterator, v3, fmtNumberBigInt);
    // Fourth test: BigDecimal with exponential formatting.
    List<FieldContainer> v4 = new ArrayList<FieldContainer>(7);
    v4.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
    v4.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER));
    v4.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR));
    v4.add(new FieldContainer(3, 6, NumberFormat.Field.FRACTION));
    v4.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL));
    v4.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN));
    v4.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT));
    java.math.BigDecimal numberBigD = new java.math.BigDecimal(-123456789);
    String fmtNumberBigDExp = fmt2.format(numberBigD);
    iterator = fmt2.formatToCharacterIterator(numberBigD);
    CompareAttributedCharacterFormatOutput(iterator, v4, fmtNumberBigDExp);
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ArrayList(java.util.ArrayList) BigDecimal(android.icu.math.BigDecimal) AttributedCharacterIterator(java.text.AttributedCharacterIterator) BigInteger(java.math.BigInteger) FieldContainer(android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

FieldContainer (android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer)2 AttributedCharacterIterator (java.text.AttributedCharacterIterator)2 ArrayList (java.util.ArrayList)2 TokenIterator (android.icu.impl.data.TokenIterator)1 BigDecimal (android.icu.math.BigDecimal)1 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)1 DecimalFormat (android.icu.text.DecimalFormat)1 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)1 NumberFormat (android.icu.text.NumberFormat)1 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)1 ULocale (android.icu.util.ULocale)1 BigInteger (java.math.BigInteger)1 Iterator (java.util.Iterator)1 Test (org.junit.Test)1