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.hamcrest.core.Is.is;
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertThat;
021import org.junit.Test;
022
023public class IntegerOperationsTest {
024
025    private IntegerOperations ops = new IntegerOperations();
026
027    @Test
028    public void shouldReturnProperExponenentInScientificNotation() {
029        assertEquals(0, ops.getExponentInScientificNotation(0));
030        assertEquals(0, ops.getExponentInScientificNotation(1));
031        assertEquals(0, ops.getExponentInScientificNotation(2));
032        assertEquals(0, ops.getExponentInScientificNotation(9));
033        assertEquals(1, ops.getExponentInScientificNotation(10));
034        assertEquals(1, ops.getExponentInScientificNotation(20));
035        assertEquals(1, ops.getExponentInScientificNotation(99));
036        assertEquals(2, ops.getExponentInScientificNotation(100));
037        assertEquals(2, ops.getExponentInScientificNotation(200));
038        assertEquals(2, ops.getExponentInScientificNotation(999));
039        assertEquals(3, ops.getExponentInScientificNotation(1000));
040        assertEquals(3, ops.getExponentInScientificNotation(2000));
041        assertEquals(3, ops.getExponentInScientificNotation(9999));
042    }
043
044    @Test
045    public void shouldRoundNumbersGreaterThan10() {
046        assertThat(ops.roundUp(-101, 0), is(-101));
047        assertThat(ops.roundUp(-101, 1), is(-101));
048        assertThat(ops.roundUp(-101, 1), is(-101));
049        assertThat(ops.roundUp(-101, -1), is(-100));
050        assertThat(ops.roundUp(-109, -1), is(-110));
051        assertThat(ops.roundUp(101, 0), is(101));
052        assertThat(ops.roundUp(101, 0), is(101));
053        assertThat(ops.roundUp(101, 1), is(101));
054        assertThat(ops.roundUp(101, 1), is(101));
055        assertThat(ops.roundUp(109, -1), is(110));
056        assertThat(ops.roundUp(101, -1), is(100));
057    }
058
059    @Test
060    public void shouldKeepSignificantFigures() {
061        assertThat(ops.keepSignificantFigures(0, 2), is(0));
062        assertThat(ops.keepSignificantFigures(1201234, 5), is(1201200));
063        assertThat(ops.keepSignificantFigures(1201254, 5), is(1201300));
064        assertThat(ops.keepSignificantFigures(1201234, 4), is(1201000));
065        assertThat(ops.keepSignificantFigures(1201234, 3), is(1200000));
066        assertThat(ops.keepSignificantFigures(1201234, 2), is(1200000));
067        assertThat(ops.keepSignificantFigures(1201234, 1), is(1000000));
068        assertThat(ops.keepSignificantFigures(-1320, 2), is(-1300));
069    }
070}