1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.math;
18
19import java.math.MathContext;
20import java.math.RoundingMode;
21
22public class MathContextTest extends junit.framework.TestCase {
23    public void testConstructor() throws Exception {
24        // All the rounding modes.
25        for (RoundingMode rm : RoundingMode.values()) {
26            MathContext mc = new MathContext("precision=1 roundingMode=" + rm);
27            assertEquals(1, mc.getPrecision());
28            assertEquals(rm, mc.getRoundingMode());
29        }
30
31        // A few precisions.
32        for (int p = 0; p < 10; ++p) {
33            MathContext mc = new MathContext("precision=" + p + " roundingMode=" + RoundingMode.UP);
34            assertEquals(p, mc.getPrecision());
35            assertEquals(RoundingMode.UP, mc.getRoundingMode());
36        }
37
38        // Case-sensitive keywords.
39        new MathContext("precision=1 roundingMode=UP");
40        try {
41            new MathContext("Precision=1 roundingMode=UP");
42            fail();
43        } catch (IllegalArgumentException expected) {
44        }
45        try {
46            new MathContext("precision=1 RoundingMode=UP");
47            fail();
48        } catch (IllegalArgumentException expected) {
49        }
50
51        // Case-sensitive rounding modes.
52        try {
53            new MathContext("precision=1 roundingMode=up");
54            fail();
55        } catch (IllegalArgumentException expected) {
56        }
57
58        // Exactly one space (U+0020).
59        try {
60            new MathContext("precision=1roundingMode=UP");
61            fail();
62        } catch (IllegalArgumentException expected) {
63        }
64        try {
65            new MathContext("precision=1  roundingMode=UP");
66            fail();
67        } catch (IllegalArgumentException expected) {
68        }
69        try {
70            new MathContext("precision=1\troundingMode=UP");
71            fail();
72        } catch (IllegalArgumentException expected) {
73        }
74
75        // No leading or trailing space.
76        try {
77            new MathContext(" precision=1 roundingMode=UP");
78            fail();
79        } catch (IllegalArgumentException expected) {
80        }
81        try {
82            new MathContext("precision=1 roundingMode=UP ");
83            fail();
84        } catch (IllegalArgumentException expected) {
85        }
86    }
87}
88