[軟體測試學習]考慮到測試的代碼編寫/int.parse的非法輸入—由一個簡單的c#閏年檢測程式說起

來源:互聯網
上載者:User

標籤:

一個簡單的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         else11             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內建了異常處理。

[軟體測試學習]考慮到測試的代碼編寫/int.parse的非法輸入—由一個簡單的c#閏年檢測程式說起

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.