复利计算——单元测试
对我们和复利计算程序,写单元测试。
有哪些场景?
期待的返回值
写测试程序。
运行测试。
测试计算是否正确:
@Test public void test0() {//测单利终值 double money=1000000.0; int years=30; double r=0.03; double sum; sum = money + money * years * r; Assert.assertEquals(1900000,sum, 1.0); } @Test public void test1(){//测复利终值 double money=1000000.0; int years=30; double r=0.03; double N=1; double sum; for (int i = 1; i <= years; i++) { N = (1 + r) * N; } sum = N * money; Assert.assertEquals(2427262,sum, 1.0); } @Test public void test2(){//测本金 double N = 1, money; double sum=3000000; int years=30; double r=0.03; for (int i = 1; i <= years; i++) { N = (1 + r) * N; } money = sum / N; Assert.assertEquals(1235960,money, 1.0); }