1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.tests.java.math; 19 20import java.math.BigDecimal; 21import java.math.MathContext; 22import java.math.RoundingMode; 23 24public class MathContextTest extends junit.framework.TestCase { 25 26 /** 27 * java.math.MathContext#MathContext(...) 28 */ 29 public void test_MathContextConstruction() { 30 String a = "-12380945E+61"; 31 BigDecimal aNumber = new BigDecimal(a); 32 MathContext mcIntRm6hd = new MathContext(6, RoundingMode.HALF_DOWN); 33 MathContext mcStr6hd = new MathContext("precision=6 roundingMode=HALF_DOWN"); 34 MathContext mcInt6 = new MathContext(6); 35 MathContext mcInt134 = new MathContext(134); 36 37 // getPrecision() 38 assertEquals("MathContext.getPrecision() returns incorrect value", 39 6, mcIntRm6hd.getPrecision() ); 40 assertEquals("MathContext.getPrecision() returns incorrect value", 41 134, mcInt134.getPrecision() ); 42 43 // getRoundingMode() 44 assertEquals("MathContext.getRoundingMode() returns incorrect value", 45 RoundingMode.HALF_UP, 46 mcInt6.getRoundingMode()); 47 assertEquals("MathContext.getRoundingMode() returns incorrect value", 48 RoundingMode.HALF_DOWN, mcIntRm6hd.getRoundingMode() ); 49 50 // toString() 51 assertEquals("MathContext.toString() returning incorrect value", 52 "precision=6 roundingMode=HALF_DOWN", mcIntRm6hd.toString() ); 53 assertEquals("MathContext.toString() returning incorrect value", 54 "precision=6 roundingMode=HALF_UP", mcInt6.toString() ); 55 56 // equals(.) 57 assertEquals("Equal MathContexts are not equal ", 58 mcIntRm6hd, mcStr6hd ); 59 assertFalse("Different MathContexts are reported as equal ", 60 mcInt6.equals(mcStr6hd) ); 61 assertFalse("Different MathContexts are reported as equal ", 62 mcInt6.equals(mcInt134) ); 63 64 // hashCode(.) 65 assertEquals("Equal MathContexts have different hashcodes ", 66 mcIntRm6hd.hashCode(), mcStr6hd.hashCode() ); 67 assertFalse("Different MathContexts have equal hashcodes ", 68 mcInt6.hashCode() == mcStr6hd.hashCode() ); 69 assertFalse("Different MathContexts have equal hashcodes ", 70 mcInt6.hashCode() == mcInt134.hashCode() ); 71 72 // other: 73 BigDecimal res = aNumber.abs(mcInt6); 74 assertEquals("MathContext Constructor with int precision failed", 75 new BigDecimal("1.23809E+68"), 76 res); 77 } 78 79} 80