001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.common.math;
017
018import static org.junit.Assert.assertEquals;
019import org.junit.Test;
020
021public class DoubleOperationsTest {
022
023    private DoubleOperations ops = new DoubleOperations();
024
025    @Test
026    public void shouldReturnProperExponenentInScientificNotation() {
027        assertEquals(-3, ops.getExponentInScientificNotation(0.0010d));
028        assertEquals(-3, ops.getExponentInScientificNotation(0.0020d));
029        assertEquals(-3, ops.getExponentInScientificNotation(0.009999d));
030        assertEquals(-2, ops.getExponentInScientificNotation(0.010d));
031        assertEquals(-2, ops.getExponentInScientificNotation(0.020d));
032        assertEquals(-2, ops.getExponentInScientificNotation(0.09999d));
033        assertEquals(-1, ops.getExponentInScientificNotation(0.10d));
034        assertEquals(-1, ops.getExponentInScientificNotation(0.20d));
035        assertEquals(-1, ops.getExponentInScientificNotation(0.9999d));
036        assertEquals(0, ops.getExponentInScientificNotation(0.0d));
037        assertEquals(0, ops.getExponentInScientificNotation(1.0d));
038        assertEquals(0, ops.getExponentInScientificNotation(2.0d));
039        assertEquals(0, ops.getExponentInScientificNotation(9.999d));
040        assertEquals(1, ops.getExponentInScientificNotation(10.0d));
041        assertEquals(1, ops.getExponentInScientificNotation(20.0d));
042        assertEquals(1, ops.getExponentInScientificNotation(99.999d));
043        assertEquals(2, ops.getExponentInScientificNotation(100.0d));
044        assertEquals(2, ops.getExponentInScientificNotation(200.0d));
045        assertEquals(2, ops.getExponentInScientificNotation(999.999d));
046        assertEquals(3, ops.getExponentInScientificNotation(1000.0d));
047        assertEquals(3, ops.getExponentInScientificNotation(2000.0d));
048        assertEquals(3, ops.getExponentInScientificNotation(9999.999d));
049    }
050
051    @Test
052    public void shouldRoundNumbersGreaterThan10() {
053        assertEquals(101.0d, ops.roundUp(101.2523d, 0), 0.01d);
054        assertEquals(101.0d, ops.roundUp(101.2323d, 0), 0.01d);
055        assertEquals(101.3d, ops.roundUp(101.2523d, 1), 0.01d);
056        assertEquals(101.2d, ops.roundUp(101.2323d, 1), 0.01d);
057        assertEquals(110.0d, ops.roundUp(109.2323d, -1), 1d);
058        assertEquals(100.0d, ops.roundUp(101.2323d, -1), 1d);
059    }
060
061    @Test
062    public void shouldKeepSignificantFigures() {
063        assertEquals(12.012d, ops.keepSignificantFigures(12.0123456, 5), 0.0001d);
064        assertEquals(12.013d, ops.keepSignificantFigures(12.0125456, 5), 0.0001d);
065        assertEquals(12.01d, ops.keepSignificantFigures(12.0123456, 4), 0.0001d);
066        assertEquals(12.0d, ops.keepSignificantFigures(12.0123456, 3), 0.0001d);
067        assertEquals(12.0d, ops.keepSignificantFigures(12.0123456, 2), 0.0001d);
068        assertEquals(10.0d, ops.keepSignificantFigures(12.0123456, 1), 0.0001d);
069        assertEquals(1300.0d, ops.keepSignificantFigures(1320.0d, 2), 0.001d);
070    }
071}