编写程序,完成以下功能:实现 NextDate 函数,获得当前输入日期的前一个日期。 例如:输入日期 day 变量值大于 1,则把 day 变量的值加1;如果输入日期是1–11 月份中某月的最后一天,把day 变量的值置为下个月的一个月的第一天,month 变量的值加1; 如果输入日期是12月的最后一天,则 day 变量的值置为 1,month 变量的值置为 1,year 变量的值加 1。 关于最后一天的判断:如果是有31天的月份(1,3,5,7,8,10,12),day 变量值为 31,如果是有30天的月份(4,6,9,11),day 变量值为 30;如果是有29天的月份(闰年的2月),day 变量值为29;如果是有 28 天的月份(非闰年的 2 月),day 变量值为 28。 设计测试用例进行白盒测试,使其分别满足语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖以及路径覆盖。
时间: 2023-11-22 13:55:31 浏览: 113
下面是一个 Python 实现的 NextDate 函数:
```Python
def next_date(year, month, day):
if not (1 <= year <= 9999 and 1 <= month <= 12 and 1 <= day <= 31):
return "Invalid date!"
if month in [1, 3, 5, 7, 8, 10, 12]:
last_day = 31
elif month in [4, 6, 9, 11]:
last_day = 30
elif year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
last_day = 29
else:
last_day = 28
if day == last_day:
if month == 12:
return (year + 1, 1, 1)
else:
return (year, month + 1, 1)
elif day < last_day:
return (year, month, day + 1)
else:
return "Invalid date!"
```
下面是 White-box testing 设计的测试用例:
语句覆盖:
| Test Case | Input | Expected Output |
| --- | --- | --- |
| 1 | year=2021, month=6, day=18 | (2021, 6, 19) |
| 2 | year=2021, month=2, day=28 | (2021, 3, 1) |
判定覆盖:
| Test Case | Input | Expected Output |
| --- | --- | --- |
| 1 | year=2000, month=2, day=29 | (2000, 3, 1) |
| 2 | year=2100, month=2, day=28 | (2100, 3, 1) |
| 3 | year=2021, month=6, day=31 | "Invalid date!" |
条件覆盖:
| Test Case | Input | Expected Output |
| --- | --- | --- |
| 1 | year=2021, month=6, day=18 | (2021, 6, 19) |
| 2 | year=2021, month=2, day=28 | (2021, 3, 1) |
| 3 | year=2000, month=2, day=29 | (2000, 3, 1) |
| 4 | year=2100, month=2, day=28 | (2100, 3, 1) |
| 5 | year=2021, month=6, day=31 | "Invalid date!" |
判定/条件覆盖:
| Test Case | Input | Expected Output |
| --- | --- | --- |
| 1 | year=2000, month=2, day=29 | (2000, 3, 1) |
| 2 | year=2100, month=2, day=28 | "Invalid date!" |
组合覆盖:
| Test Case | Input | Expected Output |
| --- | --- | --- |
| 1 | year=2021, month=6, day=18 | (2021, 6, 19) |
| 2 | year=2021, month=2, day=28 | (2021, 3, 1) |
| 3 | year=2000, month=2, day=29 | (2000, 3, 1) |
| 4 | year=2100, month=2, day=28 | "Invalid date!" |
| 5 | year=2021, month=6, day=31 | "Invalid date!" |
路径覆盖:
| Test Case | Input | Expected Output |
| --- | --- | --- |
| 1 | year=2021, month=6, day=18 | (2021, 6, 19) |
| 2 | year=2021, month=2, day=28 | (2021, 3, 1) |
| 3 | year=2000, month=2, day=29 | (2000, 3, 1) |
| 4 | year=2100, month=2, day=28 | "Invalid date!" |
| 5 | year=2021, month=6, day=31 | "Invalid date!" |
阅读全文