/* Copyright 2012 Tim Garrett, Mothsoft LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mothsoft.alexis.domain;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
private static final double MAX_ERROR = 0.00001;
@Test
public void testCalculatePercentChange() {
assertEquals(0.0, Calculator.calculatePercentChange(0, 0, 100), MAX_ERROR);
assertEquals(-0.10, Calculator.calculatePercentChange(100, 0, 1000), MAX_ERROR);
assertEquals(0.20, Calculator.calculatePercentChange(-5, -4, 20), MAX_ERROR);
assertEquals(-0.25, Calculator.calculatePercentChange(-4, -5, 20), MAX_ERROR);
assertEquals(0, Calculator.calculatePercentChange(3, 3, 100), MAX_ERROR);
assertEquals(-0.03, Calculator.calculatePercentChange(3, 0, 100), MAX_ERROR);
assertEquals(0.03, Calculator.calculatePercentChange(0, 3, 100), MAX_ERROR);
assertEquals(3.66666666, Calculator.calculatePercentChange(-3, 8, 100), MAX_ERROR);
assertEquals(-0.01407837, Calculator.calculatePercentChange(137.8, 135.86, 1000), MAX_ERROR);
assertEquals(0.01427940, Calculator.calculatePercentChange(135.86, 137.8, 1000), MAX_ERROR);
}
}