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 LongOperationsTest { 024 025 private LongOperations ops = new LongOperations(); 026 027 @Test 028 public void shouldReturnProperExponenentInScientificNotation() { 029 assertEquals(0l, ops.getExponentInScientificNotation(0l)); 030 assertEquals(0l, ops.getExponentInScientificNotation(1l)); 031 assertEquals(0l, ops.getExponentInScientificNotation(2l)); 032 assertEquals(0l, ops.getExponentInScientificNotation(9l)); 033 assertEquals(1l, ops.getExponentInScientificNotation(10l)); 034 assertEquals(1l, ops.getExponentInScientificNotation(20l)); 035 assertEquals(1l, ops.getExponentInScientificNotation(99l)); 036 assertEquals(2l, ops.getExponentInScientificNotation(100l)); 037 assertEquals(2l, ops.getExponentInScientificNotation(200l)); 038 assertEquals(2l, ops.getExponentInScientificNotation(999l)); 039 assertEquals(3l, ops.getExponentInScientificNotation(1000l)); 040 assertEquals(3l, ops.getExponentInScientificNotation(2000l)); 041 assertEquals(3l, ops.getExponentInScientificNotation(9999l)); 042 } 043 044 @Test 045 public void shouldRoundUpNumbersGreaterThan10() { 046 assertThat(ops.roundUp(-101l, 0), is(-101l)); 047 assertThat(ops.roundUp(-101l, 1), is(-101l)); 048 assertThat(ops.roundUp(-101l, 1), is(-101l)); 049 assertThat(ops.roundUp(-101l, -1), is(-100l)); 050 assertThat(ops.roundUp(-109l, -1), is(-110l)); 051 assertThat(ops.roundUp(101l, 0), is(101l)); 052 assertThat(ops.roundUp(101l, 0), is(101l)); 053 assertThat(ops.roundUp(101l, 1), is(101l)); 054 assertThat(ops.roundUp(101l, 1), is(101l)); 055 assertThat(ops.roundUp(109l, -1), is(110l)); 056 assertThat(ops.roundUp(101l, -1), is(100l)); 057 } 058 059 @Test 060 public void shouldRoundDownNumbersGreaterThan10() { 061 assertThat(ops.roundDown(-101l, 0), is(-101l)); 062 assertThat(ops.roundDown(-101l, 1), is(-101l)); 063 assertThat(ops.roundDown(-101l, 1), is(-101l)); 064 assertThat(ops.roundDown(-101l, -1), is(-100l)); 065 assertThat(ops.roundDown(-109l, -1), is(-100l)); 066 assertThat(ops.roundDown(101l, 0), is(101l)); 067 assertThat(ops.roundDown(101l, 0), is(101l)); 068 assertThat(ops.roundDown(101l, 1), is(101l)); 069 assertThat(ops.roundDown(101l, 1), is(101l)); 070 assertThat(ops.roundDown(109l, -1), is(100l)); 071 assertThat(ops.roundDown(101l, -1), is(100l)); 072 } 073 074 @Test 075 public void shouldKeepSignificantFigures() { 076 assertThat(ops.keepSignificantFigures(0l, 2), is(0l)); 077 assertThat(ops.keepSignificantFigures(1201234l, 5), is(1201200l)); 078 assertThat(ops.keepSignificantFigures(1201254l, 5), is(1201300l)); 079 assertThat(ops.keepSignificantFigures(1201234l, 4), is(1201000l)); 080 assertThat(ops.keepSignificantFigures(1201234l, 3), is(1200000l)); 081 assertThat(ops.keepSignificantFigures(1201234l, 2), is(1200000l)); 082 assertThat(ops.keepSignificantFigures(1201234l, 1), is(1000000l)); 083 assertThat(ops.keepSignificantFigures(-1320l, 2), is(-1300l)); 084 } 085}