闰年测试
- 仍在之前的等价类测试 testApp 上进行功能上的修改
- 将正则表达式改为 [0-9]{1-4},不足之处在于非数字和负数都归为了同一类「非法输入」,有待改进
- buttomPress 方法下先使用正则表达式判断函数判断输入是否合法,如果符合要求再进行下一步计算验证
- 先算是否为世纪年(模 100),若是则模 400 判断是否闰年
- 非世纪年则模 4 判断是否闰年,最后由 UIAlertView 显示判断结果弹窗
测试用例
编号 |
输入 |
输出 |
1 |
2000 |
2000 是闰年 |
2 |
1900 |
1900 不是闰年 |
3 |
eadhwij |
非法输入 |
4 |
-100 |
非法输入 |
测试截图





//
// ViewController.m
// testApp
//
// Created by trigger on 15/3/22.
// Copyright (c) 2015年 trigger. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textBoxText;
- (IBAction)buttonPress:(id)sender;
@end
@implementation ViewController
@synthesize textBoxText;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self textCheckWithRegex:@""];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPress:(id)sender {
NSString *message;
if (![self textCheckWithRegex:textBoxText.text]) {
message = @"非法输入";
}else{
int year = [textBoxText.text intValue];
if (year % 100 == 0) {
if (year % 400 == 0){
message = [[NSString alloc]initWithFormat:@"%d 是闰年", year];
}else{
message = [[NSString alloc]initWithFormat:@"%d 不是闰年", year];
}
}else{
if (year % 4 == 0) {
message = [[NSString alloc]initWithFormat:@"%d 是闰年", year];
}else{
message = [[NSString alloc]initWithFormat:@"%d 不是闰年", year];
}
}
}
[[[UIAlertView alloc]initWithTitle:@"闰年检查" message:message delegate: nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil]show];
}
- (BOOL)textCheckWithRegex:(NSString*)str{
NSString *searchText = [NSString stringWithString:str];
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]{1,4}" options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:searchText options:0 range:NSMakeRange(0, [searchText length])];
searchText = [searchText substringWithRange:result.range];
if ([str isEqualToString:searchText] && ![str isEqualToString:@""]) {
return true;
}
else{
return false;
}
}
@end