一个小程序分支覆盖(Branch Coverage)需要哪些用例?

christie1988 2013-03-14 02:17:25

// incomeList[]: the array recording the individual income items
// childList[]: the array recording the ages of children afforded by this person
// parentList[]: the array recording the ages of parents afforded by this person
public double computeTax(double[] incomeList, int[] parentList, int[] childList) {
double taxAmount = 0.0;
double incomeAmount = 0.0;

// calculate the income amount
for (int i = 0; i < incomeList.length; i++) {
incomeAmount = incomeAmount + incomeList[i];
}

// calculate the basic tax
if (incomeAmount <= 40000) {
taxAmount = incomeAmount * 0.02;
} else if (incomeAmount > 40000 && incomeAmount <= 80000) {
taxAmount = 800 + incomeAmount * 0.07;
} else if (incomeAmount > 80000 && incomeAmount <= 120000) {
taxAmount = 800 + 2800 + incomeAmount * 0.12;
} else if (incomeAmount > 120000) {
taxAmount = 800 + 2800 + 4800 + incomeAmount * 0.17;
}

// calculate the tax exemption from having children
int taxExemption = 0;
int numOfChild = childList.length;
while (numOfChild > 0) {
if (childList[numOfChild - 1] < 18) {
taxAmount = taxAmount - 4000;

taxExemption = taxExemption + 4000;
}
numOfChild--;
}

// calculate the tax exemption from having parents
for (int j = 0; j < parentList.length; j++) {
if (parentList[j] > 60) {
taxAmount = taxAmount - 2000;
taxExemption = taxExemption + 2000;
}
}

// the maximum tax exemption is 8000 each person
if (taxExemption <= 8000) {
if (taxAmount >= 0) {
return taxAmount;
} else { // i.e., taxAmount <0
return 0;
}
} else { // i.e., taxExemption > 8000
taxAmount = taxAmount + (taxExemption - 8000);
return taxAmount;
}
}
...全文
598 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
christie1988 2013-03-14
  • 打赏
  • 举报
回复
引用 2 楼 sp1234 的回复:
如果是一个培训学校的教师,我建议就不要用这种题目来坑害学生了。这里不是理论化去地套用八股概念,而是尽可能地简单化、扩展化,首先将代码重构,而不是弄一个有着一堆else的烂代码来“分析”。
唉,就是一个课程的一个小作业的一个问题。。估计这代码也是拼凑的
  • 打赏
  • 举报
回复
如果是一个培训学校的教师,我建议就不要用这种题目来坑害学生了。这里不是理论化去地套用八股概念,而是尽可能地简单化、扩展化,首先将代码重构,而不是弄一个有着一堆else的烂代码来“分析”。
  • 打赏
  • 举报
回复
晕死! 仅凭经验就知道,这是很坏的味道,需要重构了。 重构的方法有几十种,其根本地是重新考虑领域模型,学会“抽象、继承和多态”,删除大部分 else if 语句。

5,179

社区成员

发帖
与我相关
我的任务
社区描述
软件工程/管理 质量管理/软件测试
功能测试压力测试安全性测试 个人社区 湖南省·长沙市
社区管理员
  • 软件测试
  • 虫无涯
  • 小博测试成长之路
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

欢迎大家加入到软件测试的社区,在这里,希望大家勇于发表自己的看法,欢迎大家分享自己在软件测试工作过程中遇到的问题以及工作经验分享。

1.想转行的小伙伴,遇到问题没有及时回复的,可以私聊小博进行反馈

2.大家对社区有好的建议,都可以在社区发帖进行反馈

推荐大家学习的软件测试入门笔记:软件测试入门学习笔记

试试用AI创作助手写篇文章吧