软件测试第四周 - 闰年测试

本文介绍了一个简单的iOS应用,用于判断输入年份是否为闰年。通过正则表达式验证输入的有效性,并根据世纪年和平年不同的规则进行闰年判断。

闰年测试

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

062055554462090.png

062056061026469.png

062056187431573.png

062056275408552.png

062056361802047.png



//
//  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

转载于:https://www.cnblogs.com/triggerlin/p/4396586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值