首页 > 代码库 > 黑盒测试等价划分实例

黑盒测试等价划分实例

NextDay1:

#include <stdio.h>
int main(){
/* day:1~27
* day:28,month=2,year=平年
* day:29,month=2,year=闰年
* day:30,month=4,6,9,11
* day:31,month=1,3,5,7,8,10
* day:31,month=12
*/

int year,month,day,y,m,d;
printf("请输入一个日期(yyyy-mm-dd):");
scanf("%d-%d-%d",&year,&month,&day);
y = year;
m = month;
d = day;

if((day >= 1 && day <= 31) && (month >= 1 && month <= 12) && (year >= 1800 && year <= 2050)){
if(day <= 27){
day++;
}
else if(month == 2){
if(day == 28){
if(isLeapYear(year) == 0){
month++;
day = 1;
}
else{
day++;
}
}
else if(day == 29){
if(isLeapYear(year) == 1){
month++;
day = 1;
}
else{
printf("%d-%d-%d是一个非法日期.\n",y,m,d);
return;
}
}
}
else if(month == 4 || month == 6 || month == 9 || month == 11){
if(day < 30){
day++;
}
else if(day == 30){
month++;
day = 1;
}
else{
printf("%d-%d-%d是一个非法日期.\n",y,m,d);
return;
}
}
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day < 31){
day++;
}
else if(day == 31){
if(month == 12){
year++;
month = 1;
day = 1;
}
else{
month++;
day = 1;
}
}
}
else{
printf("%d-%d-%d是一个非法日期.\n",y,m,d);
return;
}
}
else{
printf("%d-%d-%d是一个非法日期.\n",y,m,d);
return;
}
printf("NextDay(%d-%d-%d) = %d-%d-%d\n",y,m,d,year,month,day);

return 0;
}

/*
* isLeadYear:判断是否为闰年
* return:int
* 1:闰年,0:平年
*/
int isLeapYear(int year){
if(year%4 ==0 && year%100 != 0 || year%400 == 0){
return 1;
}
return 0;
}

NextDay2:

 

#include <stdio.h>
int main(){
/* day:1~27
* day:28,month=2,year=平年
* day:29,month=2,year=闰年
* day:30,month=4,6,9,11
* day:31,month=1,3,5,7,8,10
* day:31,month=12
*/

 

int year,month,day,y,m,d;
printf("请输入一个日期(yyyy-mm-dd):");
scanf("%d-%d-%d",&year,&month,&day);
y = year;
m = month;
d = day;

 

int has30Day = month == 4 || month == 6 || month == 9 || month == 11;
int has31Day = month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10;
if((day >= 1 && day <= 31) && (month >= 1 && month <= 12) && (year >= 1800 && year <= 2050)){
if(isLeapYear(year) == 0 && month == 2 && day < 28
|| isLeapYear(year) == 1 && month == 2 && day < 29
|| has30Day && day < 30
|| has31Day || month == 12) && day < 31){
day++;
printf("NextDay(%d-%d-%d) = %d-%d-%d\n",y,m,d,year,month,day);
}
else if(day == 28 && month == 2 && isLeapYear(year) == 0
|| day == 29 && month == 2 && isLeapYear(year) == 1
|| day == 30 && has30Day
|| day == 31 && has31Day){
month++;
day = 1;
printf("NextDay(%d-%d-%d) = %d-%d-%d\n",y,m,d,year,month,day);
}
else if(day == 31 && month == 12){
year++;
month = 1;
day = 1;
printf("NextDay(%d-%d-%d) = %d-%d-%d\n",y,m,d,year,month,day);
}
else{
printf("%d-%d-%d是一个非法日期.\n",y,m,d);
}
}
else{
printf("%d-%d-%d是一个非法日期.\n",y,m,d);
}

 

return 0;
}

 

/*
* isLeadYear:判断是否为闰年
* return:int
* 1:闰年,0:平年
*/
int isLeapYear(int year){
if(year%4 ==0 && year%100 != 0 || year%400 == 0){
return 1;
}
return 0;
}

黑盒测试等价划分实例