软件测试_白盒测试
作业内容:使用自己熟悉的编程语言完成话费计算小程序
我的答案:
1 def validDate(year,month,day,hour,minute,second,i): 2 3 day31 = [1, 3, 5, 7, 8, 10, 12] 4 # day30=[4,6,9,11] 5 # print(year,month,day,hour,minute,seconds) 6 flag=0 7 year = int(year) 8 month = int(month) 9 day = int(day) 10 print("第", i, "个测试用例",year,month,hour,minute,second,end=" ") 11 hour = int(hour) 12 minute = int(minute) 13 seconds = int(second) 14 if month < 10: 15 month = str(month)[-1] 16 month = int(month) 17 if day < 10: 18 if day<0: 19 day=int(day) 20 else: 21 day = str(day)[-1] 22 day = int(day) 23 if hour < 10: 24 if hour<0: 25 hour=int(hour) 26 else: 27 hour = str(hour)[-1] 28 hour = int(hour) 29 if minute < 10: 30 if minute<0: 31 minute=int(minute) 32 else: 33 minute = str(minute)[-1] 34 minute = int(minute) 35 if seconds < 10: 36 if seconds<0: 37 seconds=int(seconds) 38 else: 39 seconds = str(seconds)[-1] 40 seconds = int(seconds) 41 flag = 0 42 if year < 1970 or year > 3000 or month < 1 or month > 12 or hour < 0 or hour > 23 or minute < 0 or minute > 59 or seconds < 0 or seconds > 59: 43 print("输入时间错误") 44 flag=0 45 46 else: 47 if month == 2: 48 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): 49 if day < 1 or day > 29: 50 print("2月份输入时间错误") 51 flag = 0 52 else: 53 flag = 1 54 print("输入时间有效") 55 else: 56 if day < 1 or day > 28: 57 print("2月份输入时间错误") 58 flag = 0 59 else: 60 flag = 1 61 print("输入时间有效") 62 elif month in day31: 63 64 if day < 1 or day > 31: 65 print("输入天数小于1或者大于31") 66 flag = 0 67 else: 68 flag = 1 69 print("输入时间有效") 70 else: 71 if day < 1 or day > 30: 72 73 print("输输入天数小于1或者大于30") 74 flag = 0 75 else: 76 flag = 1 77 print("输入时间有效") 78 return flag 79 80 def cityCost(duration,i): 81 print("第",i,"个测试用例",duration) 82 dur_time=duration 83 if dur_time <= 180 and dur_time>=6: 84 spend = 0.2 85 print("市内小于/等于3分钟的费用:", spend) 86 elif dur_time<6: 87 spend=0 88 print("市内小于6秒的费用:", spend) 89 else: 90 m = int((dur_time - 180) / 60) 91 #print("m:", m) 92 if (dur_time - 180) % 60 == 0: 93 spend = m * 0.1 + 0.2 94 print("市内整分钟大于3的费用:", round(spend, 3)) 95 else: 96 spend = (m + 1) * 0.1 + 0.2 97 print("市内不整分钟大于3的费用:", round(spend, 3)) 98 return round(spend,3) 99 100 def countryCost(duration,hour,minute,second,i): 101 print("第",i,"个测试用例:",duration,hour,minute,second) 102 dur_time=duration 103 hour = int(hour) 104 minute = int(minute) 105 seconds = int(second) 106 #print("通话开始时间:", hour, " ", minute, " ", seconds) 107 h = int(dur_time / 3600) # 通话小时数 108 m = int(dur_time / 60) # 通话分钟数 109 s = int(dur_time % 60) # 通话秒钟数 110 # print("通话时间:", h, " ", m, " ", s) 111 hour_n = hour + h # 通话时间+开始时间 112 minute_n = minute + m # 通话分钟+开始分钟 113 seconds_n = seconds + s # 通话秒钟+开始秒钟 114 f_m = 59 - minute # 通话边界的分钟数 115 if s>=0 and s<=59: 116 f_m=f_m+1 117 #print("****",f_m) 118 # print("通话边界的分钟数:",f_m) 119 if seconds_n >= 60: 120 minute_n = minute_n + 1 121 seconds_n = seconds_n - 60 122 if minute_n >= 60: 123 hour_n = hour_n + 1 124 minute_n = minute_n - 60 125 #print("开始时间+通话时间:", hour_n, " ", minute_n, " ", seconds_n) 126 # 全优惠 127 if hour >= 23 or hour_n < 7 or hour_n == 7 and minute_n == 0 and seconds_n == 0: 128 if s == 0: 129 spend = m * 0.1 130 print("全优惠整分钟:", spend) 131 else: 132 spend = (m + 1) * 0.1 133 print("全优惠不整分钟:", spend) 134 #elif hour==6 and f_m*60 >= dur_time: 135 #spend = f_m*0.1 136 #print("最后一分钟的第一秒在优惠区间:", spend)''' 137 # 全非优惠 138 elif 7 <= hour and hour_n < 23 and seconds >= 0: 139 if s == 0: 140 spend = m * 0.2 141 print("全非优惠整分钟:", spend) 142 else: 143 spend = (m + 1) * 0.2 144 print("全非优惠不整分钟:", spend) 145 #elif hour_n == 22 and minute_n == 59 and seconds + s > 60: 146 #spend = (m + 1) * 0.2 147 #print("最后一分钟的第一秒不在优惠区间:", spend) 148 # 优惠向非优惠和非优惠向优惠 149 else: 150 if hour == 6 and seconds >= 0: 151 if dur_time <= f_m * 60: 152 spend = f_m * 0.1 153 print("优惠向非优惠的最后一分钟优惠:", spend) 154 else: 155 d_time = dur_time - f_m * 60 156 d_m = int(d_time / 60) 157 d_s = int(d_time % 60) 158 if d_s == 0: 159 spend = f_m * 0.1 + d_m * 0.2 160 print("优惠向非优惠的整分钟最后一分钟不优惠:", spend) 161 else: 162 spend = f_m * 0.1 + (d_m + 1) * 0.2 163 print("优惠向非优惠的不整分钟最后一分钟不优惠:", spend) 164 else: 165 if hour == 22 and seconds >= 0: 166 if dur_time <= f_m * 60: 167 spend = f_m * 0.2 168 print("非优惠向优惠的最后一分钟不优惠:", spend) 169 else: 170 d_time = dur_time - f_m * 60 171 d_m = int(d_time / 60 ) 172 d_s = int(d_time % 60) 173 if d_s == 0: 174 spend = f_m * 0.2 + d_m * 0.1 175 print("非优惠向优惠的整分钟最后一分钟优惠:", spend) 176 else: 177 spend = f_m * 0.2 + (d_m + 1) * 0.1 178 print("非优惠向优惠的不整分钟最后一分钟优惠:", spend) 179 return round(spend,3) 180 181 def telephone(data_z,data_b,data_startTime,data_dur,i): 182 #print("输入要拨叫的主区号:") 183 #quhao_z=input() 184 print("第",i,"个测试用例:",data_z,data_b,data_startTime,data_dur,end=" ") 185 quhao_z=data_z 186 187 if not quhao_z.isdigit(): 188 print("主区号非法输入,请重新拨号") 189 flag=0 190 return flag 191 else: 192 quhao_z=int(quhao_z) 193 if quhao_z<10 or quhao_z>999: 194 print("主区号拨号错误") 195 flag = 0 196 else: 197 #print("输入要拨叫的被区号:") 198 #quhao_b=input() 199 quhao_b=data_b 200 if not quhao_b.isdigit(): 201 print("被区号非法输入,请重新拨号") 202 flag = 0 203 else: 204 quhao_b=int(quhao_b) 205 if quhao_b<10 or quhao_b>999: 206 print("被区号拨号错误") 207 flag = 0 208 else: 209 #print("输入通话开始时间:") 210 #time=input() 211 time=data_startTime 212 if len(time)!=14 or not time.isdigit(): 213 print("通话开始时间输入错误") 214 flag=0 215 else: 216 year = time[0:4] 217 month = time[4:6] 218 day = time[6:8] 219 hour = time[8:10] 220 minute = time[10:12] 221 seconds = time[12:14] 222 #fla=validDate(year,month,day,hour,minute,seconds) 223 fla=1 224 if fla==1: 225 #print("输入通话时长:") 226 #dur_time=input() 227 dur_time=data_dur 228 if not dur_time.isdigit(): 229 print("通话时长非法输入") 230 flag=0 231 else: 232 dur_time=int(dur_time) 233 if dur_time<0 or dur_time>3600: 234 print("通话时长输入错误") 235 flag = 0 236 else: 237 flag=1 238 print("通话时间正确") 239 #print(dur_time) 240 spend=0 241 ''' 242 if(dur_time<6): 243 spend=0 244 print("通话时间小于6s:",spend) 245 else: 246 #市区通话 247 flag=1 248 249 if quhao_b==quhao_z: 250 spend=cityCost(dur_time) 251 #国内长途 252 else: 253 spend=countryCost(dur_time,hour,minute,seconds) 254 255 else: 256 return 257 ''' 258 return flag 259 260 #print(spend) 261 #return spend 262 263 #telephone() 264 265 #测试代码 266 267 #测试有效为1,无效为0 268 def expect_dat(): 269 expect_data=[0,1,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0] 270 return expect_data 271 272 def testValidData(): 273 print("开始测试输入时间") 274 expect=[] 275 expect=expect_dat() 276 true=[] 277 true_data0 = validDate(1969, 12, 1, 12, 12, 12, 0) 278 true_data1 = validDate(1970, 12, 1, 12, 12, 12, 1) 279 true_data2 = validDate(2000, 2, 29, 0, 0, 0, 2) 280 true_data3 = validDate(2100, 2, 29, 12, 12, 12, 3) 281 true_data4 = validDate(2020, 2, 29, 12, 12, 12, 4) 282 true_data5 = validDate(2020, 2, 30, 12, 12, 12, 5) 283 true_data6 = validDate(2019, 2, 28, 23, 59, 59, 6) 284 true_data7 = validDate(2019, 2, 29, 23, 59, 59, 7) 285 true_data8 = validDate(2020, 1, 31, 23, 59, 59, 8) 286 true_data9 = validDate(2020, 1, 32, 23, 59, 59, 9) 287 true_data10 = validDate(2020, 6, 30, 12, 30, 30,10) 288 true_data11 = validDate(2020, 6, 31, 12, 30, 30, 11) 289 true_data12 = validDate(3000, 12, 1, 12, 12, 12, 12) 290 true_data13 = validDate(3001, 12, 1, 12, 12, 12, 13) 291 true_data14 = validDate(2020, 0, 28, 12, 12, 12, 14) 292 true_data15 = validDate(2020, 12, 31, 0, 0, 0, 15) 293 true_data16 = validDate(2020, 13, 28, 12, 12, 12, 16) 294 true_data17 = validDate(2020, 12, -1, 0, 0, 0, 17) 295 true_data18 = validDate(2020, 12, 32, 0, 0, 0, 18) 296 true_data19 = validDate(2020, 1, 28, -1, 12, 12, 19) 297 true_data20 = validDate(2020, 1, 28, 24, 12, 12, 20) 298 true_data21 = validDate(2020, 1, 28, 0, -1, 12, 21) 299 true_data22 = validDate(2020, 1, 28, 0, 60, 12, 22) 300 true_data23 = validDate(2020, 1, 28, 0, 59, -1, 23) 301 true_data24 = validDate(2020, 1, 28, 0, 59, 60, 24) 302 303 true.append(true_data0) 304 true.append(true_data1) 305 true.append(true_data2) 306 true.append(true_data3) 307 true.append(true_data4) 308 true.append(true_data5) 309 true.append(true_data6) 310 true.append(true_data7) 311 true.append(true_data8) 312 true.append(true_data9) 313 true.append(true_data10) 314 true.append(true_data11) 315 true.append(true_data12) 316 true.append(true_data13) 317 true.append(true_data14) 318 true.append(true_data15) 319 true.append(true_data16) 320 true.append(true_data17) 321 true.append(true_data18) 322 true.append(true_data19) 323 true.append(true_data20) 324 true.append(true_data21) 325 true.append(true_data22) 326 true.append(true_data23) 327 true.append(true_data24) 328 329 for i in range(0,len(expect)): 330 if true[i]==expect[i]: 331 print("测试数据",i,"通过") 332 else: 333 print("测试数据",i,"不通过") 334 335 def testTelephone(): 336 print("开始测试区号和通话时间") 337 expect1 = [0,0,0,0,0,0,0,0,0,0,0,1,1] 338 339 true1 = [] 340 true_data0 = telephone("abc","20","20200403223014","120",0) 341 true_data1 = telephone("20","def","20200403223014","120",1) 342 true_data2 = telephone("9", "20", "20200403223014", "120",2) 343 true_data3 = telephone("1000", "20", "20200403223014", "120",3) 344 true_data4 = telephone("20", "9", "20200403223014", "120",4) 345 true_data5 = telephone("20", "1000", "20200403223014", "120",5) 346 true_data6 = telephone("20", "20", "202004032230", "120",6) 347 true_data7 = telephone("20", "20", "abc", "120",7) 348 true_data8 = telephone("20", "20", "20200403223014", "def",8) 349 true_data9 = telephone("20", "20", "20200403223014", "-1",9) 350 true_data10 = telephone("20", "20", "20200403223014", "3601",10) 351 true_data11 = telephone("20","20", "20200403223014", "0",11) 352 true_data12 = telephone("20", "20", "20200403223014", "3600",12) 353 true_data13 = telephone("20", "20", "20200403223014", "120",13) 354 355 356 true1.append(true_data0) 357 true1.append(true_data1) 358 true1.append(true_data2) 359 true1.append(true_data3) 360 true1.append(true_data4) 361 true1.append(true_data5) 362 true1.append(true_data6) 363 true1.append(true_data7) 364 true1.append(true_data8) 365 true1.append(true_data9) 366 true1.append(true_data10) 367 true1.append(true_data11) 368 true1.append(true_data12) 369 true1.append(true_data13) 370 371 for i in range(0,len(expect1)): 372 if true1[i]==expect1[i]: 373 print("测试数据",i,"通过") 374 else: 375 print("测试数据",i,"不通过") 376 377 def testCitycost(): 378 print("开始测试市内通话") 379 expect2=[0.2,0.2,0.3,0.3,0.3,0.4,0,5.9] 380 true2=[] 381 true_data0=cityCost(179,0) 382 true_data1 = cityCost(180,1) 383 true_data2 = cityCost(181,2) 384 true_data3 = cityCost(239,3) 385 true_data4 = cityCost(240,4) 386 true_data5 = cityCost(241,5) 387 true_data6 = cityCost(5,6) 388 true_data7 = cityCost(3600,7) 389 390 true2.append(true_data0) 391 true2.append(true_data1) 392 true2.append(true_data2) 393 true2.append(true_data3) 394 true2.append(true_data4) 395 true2.append(true_data5) 396 true2.append(true_data6) 397 true2.append(true_data7) 398 399 for i in range(0,len(expect2)): 400 if true2[i]==expect2[i]: 401 print("测试数据",i,"通过") 402 else: 403 print("测试数据",i,"不通过") 404 405 def testCountrycost(): 406 print("开始国内通话测试") 407 expect3=[0.4,0.4,0.5,0.5,0.4,0.4,0.2,0.2,0.4,0.4,1.0,1.0] 408 true3=[] 409 true_data0=countryCost(90,22,58,59,0) 410 true_data1=countryCost(120,22,58,59,1) 411 true_data2 = countryCost(150, 22, 58, 59,2) 412 true_data3 = countryCost(180, 22, 58, 59,3) 413 true_data4 = countryCost(150, 6, 58, 45,4) 414 true_data5 = countryCost(180, 6, 58, 45,5) 415 true_data6 = countryCost(120, 6, 58, 45,6) 416 true_data7 = countryCost(90, 6, 58, 45,7) 417 true_data8 = countryCost(100, 7, 1, 1,8) 418 true_data9 = countryCost(120, 7, 1, 1,9) 419 true_data10 = countryCost(600, 23, 1, 1,10) 420 true_data11 = countryCost(599, 23, 1, 1,11) 421 422 true3.append(true_data0) 423 true3.append(true_data1) 424 true3.append(true_data2) 425 true3.append(true_data3) 426 true3.append(true_data4) 427 true3.append(true_data5) 428 true3.append(true_data6) 429 true3.append(true_data7) 430 true3.append(true_data8) 431 true3.append(true_data9) 432 true3.append(true_data10) 433 true3.append(true_data11) 434 435 for i in range(0,len(expect3)): 436 if true3[i]==expect3[i]: 437 print("测试数据",i,"通过") 438 else: 439 print("测试数据",i,"不通过") 440 441 442 443 testValidData() 444 print() 445 testTelephone() 446 print() 447 testCitycost() 448 print() 449 450 testCountrycost()