summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/text/NumberFormatTest.java
blob: 87fe96d273dde9686dd23a0d5e6626e60fa06b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.java.text;

import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Currency;
import java.util.Locale;

public class NumberFormatTest extends junit.framework.TestCase {
    // NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls doubleValue for
    // custom Number subclasses.
    public void test_custom_Number_gets_longValue() throws Exception {
        class MyNumber extends Number {
            public byte byteValue() { throw new UnsupportedOperationException(); }
            public double doubleValue() { return 123; }
            public float floatValue() { throw new UnsupportedOperationException(); }
            public int intValue() { throw new UnsupportedOperationException(); }
            public long longValue() { throw new UnsupportedOperationException(); }
            public short shortValue() { throw new UnsupportedOperationException(); }
            public String toString() { throw new UnsupportedOperationException(); }
        }
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
        assertEquals("123", nf.format(new MyNumber()));
    }

    // NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls longValue for
    // any BigInteger with a bitLength strictly less than 64.
    public void test_small_BigInteger_gets_longValue() throws Exception {
        class MyNumberFormat extends NumberFormat {
            public StringBuffer format(double value, StringBuffer b, FieldPosition f) {
                b.append("double");
                return b;
            }
            public StringBuffer format(long value, StringBuffer b, FieldPosition f) {
                b.append("long");
                return b;
            }
            public Number parse(String string, ParsePosition p) {
                throw new UnsupportedOperationException();
            }
        }
        NumberFormat nf = new MyNumberFormat();
        assertEquals("long", nf.format(BigInteger.valueOf(Long.MAX_VALUE)));
        assertEquals("double", nf.format(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)));
        assertEquals("long", nf.format(BigInteger.valueOf(Long.MIN_VALUE)));
        assertEquals("double", nf.format(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)));
    }

    public void test_getIntegerInstance_ar() throws Exception {
        NumberFormat numberFormat = NumberFormat.getNumberInstance(new Locale("ar"));
        assertEquals("#,##0.###", ((DecimalFormat) numberFormat).toPattern());
        NumberFormat integerFormat = NumberFormat.getIntegerInstance(new Locale("ar"));
        assertEquals("#,##0", ((DecimalFormat) integerFormat).toPattern());
    }

    public void test_numberLocalization() throws Exception {
        Locale arabic = new Locale("ar");
        NumberFormat nf = NumberFormat.getNumberInstance(arabic);
        assertEquals('\u0660', new DecimalFormatSymbols(arabic).getZeroDigit());
        assertEquals("١٬٢٣٤٬٥٦٧٬٨٩٠", nf.format(1234567890));
    }

    // Formatting percentages is confusing but deliberate.
    // Ensure we don't accidentally "fix" this.
    // https://code.google.com/p/android/issues/detail?id=10333
    public void test_10333() throws Exception {
        NumberFormat nf = NumberFormat.getPercentInstance(Locale.US);
        assertEquals("15%", nf.format(0.15));
        assertEquals("1,500%", nf.format(15));
        try {
            nf.format("15");
            fail();
        } catch (IllegalArgumentException expected) {
        }
    }

    public void testPercentageRounding() throws Exception {
        NumberFormat nf = NumberFormat.getPercentInstance(Locale.US);
        assertEquals("15%", nf.format(0.149));
        assertEquals("14%", nf.format(0.142));

        nf.setRoundingMode(RoundingMode.UP);
        assertEquals("15%", nf.format(0.142));

        nf.setRoundingMode(RoundingMode.DOWN);
        assertEquals("14%", nf.format(0.149));

        nf.setMaximumFractionDigits(1);
        assertEquals("14.9%", nf.format(0.149));
    }

    // https://code.google.com/p/android/issues/detail?id=62269
    public void test_62269() throws Exception {
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
        try {
            nf.parse(null);
            fail();
        } catch (NullPointerException expected) {
        }
    }

    public void test_nullLocales() {
        try {
            NumberFormat.getInstance(null);
            fail();
        } catch (NullPointerException expected) {}

        try {
            NumberFormat.getIntegerInstance(null);
            fail();
        } catch (NullPointerException expected) {}

        try {
            NumberFormat.getCurrencyInstance(null);
            fail();
        } catch (NullPointerException expected) {}

        try {
            NumberFormat.getPercentInstance(null);
            fail();
        } catch (NullPointerException expected) {}

        try {
            NumberFormat.getNumberInstance(null);
            fail();
        } catch (NullPointerException expected) {}
    }

    // https://code.google.com/p/android/issues/detail?id=79925
    public void test_setCurrency() throws Exception {
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
        nf.setCurrency(Currency.getInstance("AMD"));
        assertEquals("AMD50.00", nf.format(50.0));

        DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols();
        decimalFormatSymbols.setCurrencySymbol("");
        ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols);
        assertEquals("50.00", nf.format(50.0));

        nf.setCurrency(Currency.getInstance("AMD"));
        assertEquals("AMD50.00", nf.format(50.0));

        nf.setCurrency(Currency.getInstance("AMD"));
        assertEquals("AMD50.00", nf.format(50.0));

        nf.setCurrency(Currency.getInstance("USD"));
        assertEquals("$50.00", nf.format(50.0));

        nf.setCurrency(Currency.getInstance("AMD"));
        assertEquals("AMD50.00", nf.format(50.0));
    }
}