1/*
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 4092330 6246348
27 * @summary Tests for rounding mode in NumberFormat
28 */
29
30import java.math.*;
31import java.text.*;
32
33public class NumberFormatRounding {
34
35    static final String AE = "ArithmeticException";
36
37    static final double src[] = {5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5,
38                                 5.501, -5.501, 5.500, -5.500, 1.001, -1.001, 4.501, -4.501, 4.500, -4.500};
39    static final String up[] = {"6", "3", "2", "2", "1", "-1", "-2", "-2", "-3", "-6",
40                                "6", "-6", "6", "-6", "2", "-2", "5", "-5", "5", "-5"};
41    static final String down[] = {"5", "2", "1", "1", "1", "-1", "-1", "-1", "-2", "-5",
42                                  "5", "-5", "5", "-5", "1", "-1", "4", "-4", "4", "-4"};
43    static final String ceiling[] = {"6", "3", "2", "2", "1", "-1", "-1", "-1", "-2", "-5",
44                                     "6", "-5", "6", "-5", "2", "-1", "5", "-4", "5", "-4"};
45    static final String floor[] = {"5", "2", "1", "1", "1", "-1", "-2", "-2", "-3", "-6",
46                                   "5", "-6", "5", "-6", "1", "-2", "4", "-5", "4", "-5"};
47    static final String half_up[] = {"6", "3", "2", "1", "1", "-1", "-1", "-2", "-3", "-6",
48                                     "6", "-6", "6", "-6", "1", "-1", "5", "-5", "5", "-5"};
49    static final String half_down[] = {"5", "2", "2", "1", "1", "-1", "-1", "-2", "-2", "-5",
50                                       "6", "-6", "5", "-5", "1", "-1", "5", "-5", "4", "-4"};
51    static final String half_even[] = {"6", "2", "2", "1", "1", "-1", "-1", "-2", "-2", "-6",
52                                       "6", "-6", "6", "-6", "1", "-1", "5", "-5", "4", "-4"};
53    static final String unnecessary[] = {AE, AE, AE, AE, "1", "-1", AE, AE, AE, AE,
54                                         AE, AE, AE, AE, AE, AE, AE, AE, AE, AE};
55
56    public static void main(String[] args) {
57        basicTest();
58
59        roundTest(RoundingMode.UP, up);
60        roundTest(RoundingMode.DOWN, down);
61        roundTest(RoundingMode.CEILING, ceiling);
62        roundTest(RoundingMode.FLOOR, floor);
63        roundTest(RoundingMode.HALF_UP, half_up);
64        roundTest(RoundingMode.HALF_DOWN, half_down);
65        roundTest(RoundingMode.HALF_EVEN, half_even);
66        roundTest(RoundingMode.UNNECESSARY, unnecessary);
67    }
68
69    static void basicTest() {
70        NumberFormat nf = NumberFormat.getIntegerInstance();
71
72        if (nf.getRoundingMode() != RoundingMode.HALF_EVEN) {
73            throw new RuntimeException("default rounding is not HALF_EVEN");
74        }
75
76        try {
77            nf.setRoundingMode(null);
78            throw new RuntimeException(
79                "NullPointerException is not thrown by calling setRoundingMode(null)");
80        } catch (NullPointerException npe) {
81            // continue testing
82        }
83
84        ChoiceFormat cf = new ChoiceFormat("");
85
86        try {
87            cf.setRoundingMode(RoundingMode.HALF_EVEN);
88            throw new RuntimeException(
89                "UnsupportedOperationException is not thrown by calling setRoundingMode()");
90        } catch (UnsupportedOperationException uoe) {
91            // continue testing
92        }
93
94        try {
95            cf.getRoundingMode();
96            throw new RuntimeException(
97                "UnsupportedOperationException is not thrown by calling getRoundingMode()");
98        } catch (UnsupportedOperationException uoe) {
99            // continue testing
100        }
101    }
102
103    static void roundTest(RoundingMode rm, String[] expected) {
104        NumberFormat nf = NumberFormat.getIntegerInstance();
105        nf.setRoundingMode(rm);
106
107        if (nf.getRoundingMode() != rm) {
108            throw new RuntimeException("set rounding mode is not returned by get method");
109        }
110
111        for (int i = 0; i < src.length; i ++) {
112            String result = null;
113            try {
114                result = nf.parse(nf.format(src[i])).toString();
115                if (!result.equals(expected[i])) {
116                    throw new RuntimeException("rounding test #"+i+" failed. mode: "+rm+" src: "+src[i]+" expected: "+expected[i]+" result: "+result);
117                }
118            } catch (ArithmeticException ae) {
119                if (expected[i].equals(AE)) {
120                    continue;
121                } else {
122                    result = AE;
123                    throw new RuntimeException("rounding test #"+i+" failed. mode: "+rm+" src: "+src[i]+" expected: "+expected[i]+" result: "+result);
124                }
125            } catch (ParseException pe) {
126                throw new RuntimeException("ParseException ocurred.", pe);
127            }
128        }
129    }
130}
131