天天看點

0330 複利計算的單元測試情況

1.本金為100萬,利率或者投資回報率為3%,投資年限為30年,那麼,30年後所獲得的利息收入:按複利計算公式來計算就是:1,000,000×(1+3%)^30

2.如果按照單利計算,本息的最終收益

3.假如30年之後要籌措到300萬元的養老金,平均的年回報率是3%,那麼,現在必須投入的本金是多少呢?

4.利率這麼低,複利計算收益都這麼厲害了,如果拿100萬元去買年報酬率10%的股票,若一切順利,過多長時間,100萬元就變成200萬元呢? 

5.如果我希望在十年内将100萬元變成200萬元,應該找到報酬率在多少的投資工具來幫助我達成目标?如果想在5年後本金翻倍,報酬率就應至少為多少才行呢?

6.如果每年都将積蓄的3萬元進行投資,每年都能獲得3%的回報,然後将這些本利之和連同年金再投入新一輪的投資,那麼,30年後資産總值将變為多少?如果換成每月定投3000呢?(定額定投收益計算辦法)

7. 如果向銀行貸款10萬元,年利率6.5%,期限為10年,那麼每月等額本息還款多少?(算複利條件下等額還款金額)

二.單元測試預期結果及其代碼。

測試子產品 測試輸入的資料 預期結果 誤差允許的範圍 最後測試結果
複利計算終值      ( 本金,年利率,年限) 終值
測試運算結果        (10000  ,0.03,  10) 13439.163793441223         1.0             ture
 單利計算 ( 本金,年利率,年限)
(10000,0.03,10) 13000.0 0.0 
利率計算                             ( 終值,本金,年限)
 測試運算結果 (13439,10000,10)  0.029998744652774967 0.001              ture

三.單元測試代碼

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;


public class compoundingTest {
    compounding compound;

    @Before
    public void setUp() throws Exception {
        compound=new compounding();
    }

    @Test
    public void testPrincipal_formula() {
        int Year=10;
        double i=0.03,P,S=13439;
         P = S/ Math.pow((1 + i ), Year);
         assertEquals(9999.878122297087,10000,0.5);
    }

    @Test
    public void testCompound_amount_formula() {
        int Year=10;
        double i=0.03,P,S=10000;
        P = S/ Math.pow((1 + i ), Year);
        assertEquals(13439.163793441223,13440,1.0);
    }

    @Test
    public void testSingle_interest_formula() {
         int Year=10;
          double F,i=0.03,P=10000;
            F = P *(1+i*Year);
            assertEquals(13439.163793441223,13440,1.0);
        
    }

    @Test
    public void testYears_formula() {
        int year;
        double i=0.03, F=13439, P=10000;
        year = (int) (Math.log(F / P) / Math.log(1 + i ));
        assertEquals(9,10,1.0);
    }

    @Test
    public void testRate_formula() {
        int Year=10;
        double rate;
        double F=13439, P=10000;
        rate = (Math.pow(F / P, 1.0 / Year) - 1);
        assertEquals(0.029998744652774967,0.03,0.01);
    }

    @Test
    public void testEqual_investment_years() {
        int Year=10;
        double i=0.03, F=13439, P=10000,final_value;
        final_value = P * (Math.pow(1 + i, Year) - 1) / i;
        assertEquals(114638.79311470741,114639,0.5);
    }

    @Test
    public void testEqual_investment_months() {
        int Year=10;
        double i=0.003,P=10000,final_value;
        final_value = P * 12 * (1 + i) * (Math.pow(1 + i, Year) - 1) / i;
        assertEquals(1219979.2737040932,1219980,1.0);
    }

    @Test
    public void testEqual_repayment_formula() {
        int Year=10;
        double refund;
        double i=0.03, F=13439, P=10000;
        refund = F * i / (12 * (1 + i) * (Math.pow(1 + i, Year) - 1));
        assertEquals(94.84553222222804,95,0.5);
    }

}      
0330 複利計算的單元測試情況

四.感受與體會

       剛開始單元測試是什麼都不懂,慢慢了解後,才逐漸懂得了一點,不想該開始那樣迷茫,因為不知道c語言的單元測試的,是以匆忙下改了java的版本,因為時間不夠,接下來會不斷完善程式,我相信我會慢慢進步。

複利程式java版源代碼網址:http://www.cnblogs.com/RSTART/p/5339132.html