aboutsummaryrefslogtreecommitdiff
path: root/luni/src/test/java/libcore/java/util/FormatterTest.java
blob: 4e4c4d5f741d46b2b1fb6666bf3c210f8e872660 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (C) 2009 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.util;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;

import static java.nio.charset.StandardCharsets.UTF_8;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;

import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
import libcore.test.annotation.NonMts;
import libcore.test.reasons.NonMtsReasons;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.FormatFlagsConversionMismatchException;
import java.util.Formatter;
import java.util.GregorianCalendar;
import java.util.IllegalFormatException;
import java.util.Locale;
import java.util.MissingFormatArgumentException;
import java.util.TimeZone;

@SuppressWarnings("FormatString")
public class FormatterTest {

    private File aFile;

    @Before
    public void setUp() throws Exception {
        aFile = File.createTempFile("libcore_java_util_FormatterTest-test-file", null);
        aFile.deleteOnExit();
    }

    @After
    public void cleanup() throws Exception {
        aFile.delete();
    }

    @NonMts(reason = NonMtsReasons.ICU_VERSION_DEPENDENCY,
        disabledUntilSdk = VersionCodes.VANILLA_ICE_CREAM)
    @Test
    public void test_numberLocalization() throws Exception {
        Locale arabic = new Locale("ar");

        // Check the fast path for %d:
        assertEquals("12 \u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669\u0660 34",
                String.format(arabic, "12 %d 34", 1234567890));
        // And the slow path too:
        assertEquals("12 \u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0666\u0667\u066c\u0668\u0669\u0660 34",
                String.format(arabic, "12 %,d 34", 1234567890));
        // And three localized floating point formats:
        assertEquals("12 \u0661\u066b\u0662\u0663\u0660\u0623\u0633+\u0660\u0660 34",
                String.format(arabic, "12 %.3e 34", 1.23));
        assertEquals("12 \u0661\u066b\u0662\u0663\u0660 34",
                String.format(arabic, "12 %.3f 34", 1.23));
        assertEquals("12 \u0661\u066b\u0662\u0663 34",
                String.format(arabic, "12 %.3g 34", 1.23));
        // And date/time formatting (we assume that all time/date number formatting is done by the
        // same code, so this is representative):
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-08:00"));
        c.setTimeInMillis(0);
        assertEquals("12 \u0661\u0666:\u0660\u0660:\u0660\u0660 34",
                String.format(arabic, "12 %tT 34", c));
        // These shouldn't get localized:
        assertEquals("1234", String.format(arabic, "1234"));
        assertEquals("1234", String.format(arabic, "%s", "1234"));
        assertEquals("1234", String.format(arabic, "%s", 1234));
        assertEquals("2322", String.format(arabic, "%o", 1234));
        assertEquals("4d2", String.format(arabic, "%x", 1234));
        assertEquals("0x1.0p0", String.format(arabic, "%a", 1.0));
    }

    // http://b/27566754
    @NonMts(reason = NonMtsReasons.ICU_VERSION_DEPENDENCY,
        disabledUntilSdk = VersionCodes.VANILLA_ICE_CREAM)
    @Test
    public void test_internationalizedExponent() {
        assertEquals("1E+02", String.format(Locale.ENGLISH, "%.0E", 100.0));
        assertEquals("1e+02", String.format(Locale.ENGLISH, "%.0e", 100.0));

        assertEquals("\u0661\u0623\u0633+\u0660\u0662", String.format(new Locale("ar"), "%.0E", 100.0));
        assertEquals("\u0661\u0623\u0633+\u0660\u0662", String.format(new Locale("ar"), "%.0e", 100.0));

        assertEquals("1\u00d710^+02", String.format(new Locale("et"), "%.0E", 100.0));
        assertEquals("1\u00d710^+02", String.format(new Locale("et"), "%.0e", 100.0));
    }

    // http://b/2301938
    @Test
    public void test_uppercaseConversions() throws Exception {
        // In most locales, the upper-case equivalent of "i" is "I".
        assertEquals("JAKOB ARJOUNI", String.format(Locale.US, "%S", "jakob arjouni"));
        // In Turkish-language locales, there's a dotted capital "i".
        assertEquals("JAKOB ARJOUN\u0130", String.format(new Locale("tr", "TR"), "%S", "jakob arjouni"));
    }

    // Creating a NumberFormat is expensive, so we like to reuse them, but we need to be careful
    // because they're mutable.
    @Test
    public void test_NumberFormat_reuse() throws Exception {
        assertEquals("7.000000 7", String.format("%.6f %d", 7.0, 7));
    }

    @Test
    public void test_grouping() throws Exception {
        // The interesting case is -123, where you might naively output "-,123" if you're just
        // inserting a separator every three characters. The cases where there are three digits
        // before the first separator may also be interesting.
        assertEquals("-1", String.format("%,d", -1));
        assertEquals("-12", String.format("%,d", -12));
        assertEquals("-123", String.format("%,d", -123));
        assertEquals("-1,234", String.format("%,d", -1234));
        assertEquals("-12,345", String.format("%,d", -12345));
        assertEquals("-123,456", String.format("%,d", -123456));
        assertEquals("-1,234,567", String.format("%,d", -1234567));
        assertEquals("-12,345,678", String.format("%,d", -12345678));
        assertEquals("-123,456,789", String.format("%,d", -123456789));
        assertEquals("1", String.format("%,d", 1));
        assertEquals("12", String.format("%,d", 12));
        assertEquals("123", String.format("%,d", 123));
        assertEquals("1,234", String.format("%,d", 1234));
        assertEquals("12,345", String.format("%,d", 12345));
        assertEquals("123,456", String.format("%,d", 123456));
        assertEquals("1,234,567", String.format("%,d", 1234567));
        assertEquals("12,345,678", String.format("%,d", 12345678));
        assertEquals("123,456,789", String.format("%,d", 123456789));
    }

    @Test
    public void test_formatNull() throws Exception {
        // We fast-path %s and %d (with no configuration) but need to make sure we handle the
        // special case of the null argument...
        assertEquals("null", String.format(Locale.US, "%s", (String) null));
        assertEquals("null", String.format(Locale.US, "%d", (Integer) null));
        // ...without screwing up conversions that don't take an argument.
        assertEquals("%", String.format(Locale.US, "%%"));
    }

    // Alleged regression tests for historical bugs. (It's unclear whether the bugs were in
    // BigDecimal or Formatter.)
    @Test
    public void test_BigDecimalFormatting() throws Exception {
        BigDecimal[] input = new BigDecimal[] {
            new BigDecimal("20.00000"),
            new BigDecimal("20.000000"),
            new BigDecimal(".2"),
            new BigDecimal("2"),
            new BigDecimal("-2"),
            new BigDecimal("200000000000000000000000"),
            new BigDecimal("20000000000000000000000000000000000000000000000000")
        };
        String[] output = new String[] {
            "20.00",
            "20.00",
            "0.20",
            "2.00",
            "-2.00",
            "200000000000000000000000.00",
            "20000000000000000000000000000000000000000000000000.00"
        };
        for (int i = 0; i < input.length; ++i) {
            String result = String.format("%.2f", input[i]);
            assertEquals("input=\"" + input[i] + "\", " + ",expected=" + output[i] + ",actual=" + result,
                    output[i], result);
        }
    }

    // https://code.google.com/p/android/issues/detail?id=42936
    @Test
    public void test42936() throws Exception {
        assertEquals("0.00000000000000", String.format("%.15g",0.0d));
    }

    // https://code.google.com/p/android/issues/detail?id=53983
    @Test
    public void test53983() throws Exception {
      checkFormat("00", "H", 00);
      checkFormat( "0", "k", 00);
      checkFormat("12", "I", 00);
      checkFormat("12", "l", 00);

      checkFormat("01", "H", 01);
      checkFormat( "1", "k", 01);
      checkFormat("01", "I", 01);
      checkFormat( "1", "l", 01);

      checkFormat("12", "H", 12);
      checkFormat("12", "k", 12);
      checkFormat("12", "I", 12);
      checkFormat("12", "l", 12);

      checkFormat("13", "H", 13);
      checkFormat("13", "k", 13);
      checkFormat("01", "I", 13);
      checkFormat( "1", "l", 13);

      checkFormat("00", "H", 24);
      checkFormat( "0", "k", 24);
      checkFormat("12", "I", 24);
      checkFormat("12", "l", 24);
    }

    private static void checkFormat(String expected, String pattern, int hour) {
      TimeZone utc = TimeZone.getTimeZone("UTC");

      Calendar c = new GregorianCalendar(utc);
      c.set(2013, Calendar.JANUARY, 1, hour, 00);

      assertEquals(expected, String.format(Locale.US, "%t" + pattern, c));
      assertEquals(expected, String.format(Locale.US, "%T" + pattern, c));
    }

    // http://b/33245708: Some locales have a group separator != '\0' but a default decimal format
    // pattern without grouping (e.g. a group size of zero). This would throw divide by zero when
    // working out where to place the separator.
    @Test
    public void testGroupingSizeZero() {
        Locale localeWithoutGrouping = new Locale("en", "US", "POSIX");
        DecimalFormat decimalFormat =
                (DecimalFormat) NumberFormat.getInstance(localeWithoutGrouping);

        // Confirm the locale is still a good example: it has a group separator, but no grouping in
        // the default decimal format.
        assertEquals(0, decimalFormat.getGroupingSize());
        assertFalse(decimalFormat.isGroupingUsed());
        DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();
        assertTrue(symbols.getGroupingSeparator() != '\0');

        Formatter formatter = new Formatter(localeWithoutGrouping);
        formatter.format("%,d", 123456789);
        // No exception expected
    }

    @Test
    public void testConstructor_Ljava_io_OutputStreamLjava_nio_charset_CharsetLjava_util_Locale()
            throws Exception {
        try {
            new Formatter((OutputStream) null, UTF_8, Locale.getDefault());
            fail("Should throw NPE");
        } catch (NullPointerException ignored) {
            // expected
        }

        try (var os = new FileOutputStream(aFile)) {
            new Formatter(os, (Charset) null, Locale.getDefault());
            fail("Should throw NPE");
        } catch (NullPointerException ignored) {
            // expected
        }

        try (var os = new FileOutputStream(aFile)) {
            var formatter = new Formatter(os, UTF_8, (Locale) null);
            assertNull(formatter.locale());
        }

        try (var os = new FileOutputStream(aFile)) {
            var locale = Locale.getDefault();
            var formatter = new Formatter(os, UTF_8, locale);
            assertEquals(locale, formatter.locale());
        }
    }

    @Test
    public void testConstructor_Ljava_io_FileLjava_nio_charset_CharsetLjava_util_Locale()
            throws Exception {
        try {
            new Formatter((File) null, UTF_8, Locale.getDefault());
            fail("Should throw NPE");
        } catch (NullPointerException ignored) {
            // expected
        }

        try {
            new Formatter(aFile, (Charset) null, Locale.getDefault());
            fail("Should throw NPE");
        } catch (NullPointerException ignored) {
            // expected
        }

        var formatter = new Formatter(aFile, UTF_8, (Locale) null);
        assertNull(formatter.locale());

        var locale = Locale.getDefault();
        formatter = new Formatter(aFile, UTF_8, locale);
        assertEquals(locale, formatter.locale());
    }

    @Test
    public void testConstructor_Ljava_lang_StringLjava_nio_charset_CharsetLjava_util_Locale()
            throws Exception {
        try {
            new Formatter((String) null, UTF_8, Locale.getDefault());
            fail("Should throw NPE");
        } catch (NullPointerException ignored) {
            // expected
        }

        try {
            new Formatter(aFile.getName(), (Charset) null, Locale.getDefault());
            fail("Should throw NPE");
        } catch (NullPointerException ignored) {
            // expected
        }
    }

    @Test
    public void test_floatWithAlternateForm() {
        // when # flag is set, decimal separator is always present.
        assertEquals("10.", new Formatter(Locale.US).format("%#.0f", 10.12).toString());
    }

    @Test
    public void test_numberSignIsNotAllowed_inGeneralFormat() {
        assertThrows(FormatFlagsConversionMismatchException.class,
                () -> new Formatter(Locale.US).format("%#.1g", 10.1).toString());
    }

    @Test
    @DisableCompatChanges({Formatter.ENABLE_STRICT_FORMATTER_VALIDATION})
    public void zerothIndexIsTreatedAsOrdinaryIndex_whenChecksAreDisabled() {
        assertEquals("x", new Formatter(Locale.US).format("%0$s", "x").toString());
        assertEquals("a a b", new Formatter(Locale.US).format("%s %1$s %0$s", "a", "b").toString());
        assertEquals(
                "string string",
                new Formatter(Locale.US).format("%s %2147483648$s", "string").toString());
        assertThrows(
                MissingFormatArgumentException.class,
                () -> new Formatter(Locale.US).format("%0$s %0$s %0$s", "x").toString());
    }

    @Test
    @DisableCompatChanges({Formatter.ENABLE_STRICT_FORMATTER_VALIDATION})
    public void invalidIndexIsTreatedAsOrdinaryIndex_whenChecksAreDisabled() {
        // In practice it makes no sense to use such a large index - there can't be that many
        // elements in an array anyway. OpenJDK 11 (and older) implementation swallowed exception
        // during these elements parsing and left default values. For index default value was -1,
        // which effectively means relative index a.k.a. re-use argument used for previous format
        // specifier.
        assertEquals(
                "string string",
                new Formatter(Locale.US).format("%s %2147483648$s", "string").toString());
        assertThrows(
                MissingFormatArgumentException.class,
                () -> new Formatter(Locale.US).format("%2147483648$s", "string").toString()
        );
    }

    @Test
    @EnableCompatChanges({Formatter.ENABLE_STRICT_FORMATTER_VALIDATION})
    public void invalidIndicesThrowsException_afterU_whenChecksAreEnabled() {
        var msg = "Checks are done only starting from V. Current SDK=" + VMRuntime.getSdkVersion();
        assumeTrue(msg, VMRuntime.getSdkVersion() >= VersionCodes.VANILLA_ICE_CREAM);
        var formatter = new Formatter(Locale.US);
        var exception = assertThrows(
                IllegalFormatException.class,
                () -> formatter.format("%0$s", "x").toString());

        assertEquals("IllegalFormatArgumentIndexException", exception.getClass().getSimpleName());

        exception = assertThrows(
                IllegalFormatException.class,
                () -> new Formatter(Locale.US).format("%s %2147483648$s", "string").toString());
        assertEquals("IllegalFormatArgumentIndexException", exception.getClass().getSimpleName());
    }

    @Test
    @EnableCompatChanges({Formatter.ENABLE_STRICT_FORMATTER_VALIDATION})
    public void invalidIndicesDontThrow_beforeV_whenChecksAreEnabled() {
        var msg = "Checks are done only starting from V. Current SDK=" + VMRuntime.getSdkVersion();
        assumeTrue(msg, VMRuntime.getSdkVersion() < VersionCodes.VANILLA_ICE_CREAM);

        assertEquals("x", new Formatter(Locale.US).format("%0$s", "x").toString());
        assertEquals("a a b", new Formatter(Locale.US).format("%s %1$s %0$s", "a", "b").toString());
    }

}