zoukankan      html  css  js  c++  java
  • [软件测试学习]考虑到测试的代码编写/int.parse的非法输入—由一个简单的c#闰年检测程序说起

    一个简单的C#的闰年检测程序

    1.闰年检测的函数编写

    当提起检测平年闰年时候,第一反应写出的代码

    1 public static bool isLeapYear(int year){
    2   return ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
    3 }

    但是这个并不易于测试和出现错后的修改,更改代码如下

     1 public static bool isLeapYear(int year){
     2     bool check = new bool();
     3     check = false;
     4     if (year % 4 == 0)
     5         check = true;
     6     if (year % 100 == 0)
     7         check = false;
     8     if (year % 400 == 0)
     9         check = true;
    10     return check;
    11 }             

    2.完整代码和测试结果

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int year = 3;
                String years = textBox1.Text;
                int year = int.Parse(textBox1.Text);
                if (isLeapYear(year))
                    label2.Text = year + " is leap year!";
                else
                    label2.Text = year + " is not leap year!";
             }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            public static bool isLeapYear(int year)
            {
                bool check = new bool();
                check = false;
                if (year % 4 == 0)
                    check = true;
                if (year % 100 == 0)
                    check = false;
                if (year % 400 == 0)
                    check = true;
                return check;
    
            }
    
            private void label2_Click(object sender, EventArgs e)
            {
    
            }
    
        }
    }

    测试结果

    3.非法输入测试

    输入用例: #

    结果:

    4.用Convert.ToInt/TryParse函数的结果

    5.对TryParse的改进

    通过如上测试,发现只有int.TryParse没有显示异常,出现了0。为了判断这个0是初始化的year还是函数遇到error后赋值的0。把year的初始值改成了3。结果显示表明是遇到error后该函数会将其赋值为0。

    通过查阅资料发现函数返回值是bool类型。于是对代码进行了改动。

     1 private void button1_Click(object sender, EventArgs e)
     2 {
     3     int year = 3;
     4     String years = textBox1.Text;
     5     bool isNum = Int32.TryParse(years, out year);
     6     if (isNum)
     7     {
     8         if (isLeapYear(year))
     9             label2.Text = year + " is leap year!";
    10         else
    11             label2.Text = year + " is not leap year!";
    12     }
    13     else label2.Text = "input error!";
    14 }                    

    结果如下:

    5.比较与反思

    Convert.ToInt32、int.Parse、int.TryParse 都可以将非int数据类型转换为int数据类型,三者各有不同。其中Convert.ToInt32最为强大,可以将多种不同的类型,如byte short string 等转换为int数据类型。int.Parse和int.TryParse差不多,都是将string字符串类型转换为int,区别是int.TryParse内置了异常处理。

  • 相关阅读:
    bShare一个强大的网页分享插件
    免费软件,到底是谁在获益?
    波西的小球——优化日志
    CSDN无故删除东西,强烈抗议 枯木
    网站排障分析常用的命令 枯木
    KVM在线迁移(动态迁移) 枯木
    RHEL6 KVM安装备忘 枯木
    MySQL备份和恢复具体实施(上) 枯木
    Nginx支持php相关配置 枯木
    关于RHEL6中ulimit的nproc限制 枯木
  • 原文地址:https://www.cnblogs.com/RabbitHole/p/4395179.html
Copyright © 2011-2022 走看看