對【判斷閏年】函數的白盒測試


  最近學習了白盒測試。白盒測試的測試方法有很多種:代碼檢查法、靜態結構分析法、靜態質量度量法、邏輯覆蓋法、基本路徑測試法、域測試、符號測試、路徑覆蓋和程序變異。白盒測試法的覆蓋標准有邏輯覆蓋、循環覆蓋和基本路徑測試。其中邏輯覆蓋包括語句覆蓋、判定覆蓋、條件覆蓋、判定/條件覆蓋、條件組合覆蓋和路徑覆蓋。六種覆蓋標准發現錯誤的能力呈由弱到強的變化:

1.語句覆蓋每條語句至少執行一次。
2.判定覆蓋每個判定的每個分支至少執行一次。
3.條件覆蓋每個判定的每個條件應取到各種可能的值。
4.判定/條件覆蓋同時滿足判定覆蓋條件覆蓋。
5.條件組合覆蓋每個判定中各條件的每一種組合至少出現一次。
6.路徑覆蓋使程序中每一條可能的路徑至少執行一次。
 
下面對月初的【判斷閏年】的程序做白盒測試。
需要測試的源代碼:
 
改良前的代碼:

void LeapYear(int year); 

void main() { 

   int year=0; 

   LeapYear(year); 

}

void LeapYear(int year) { 

   cout<<"請輸入年份:"<<endl; 

   cin>>year; 

   while(!(year>=1811 && year<=2013)) { 

           cout<<"年份超過界限,請重新輸入"<<endl; 

           cin>>year; 

   }

   if((year%4==0 && year%100!=0) || (year%400==0)) { //檢查閏年

           cout<<"閏年"<<endl;

   }

   else{

           cout<<"不是閏年"<<endl;

   }

}

測試用例:

改良后的代碼:

public void handle(MouseEvent arg0) {
  boolean judge1 = true;
  boolean judge2 = true;
  String s = textfiled1.getText();
  if ((s.length() > 6) || (s.length() < 1))
  {
    text.setText(str2);
  return;
   for (int i = 0; i < s.length();i++)

  {
 char c = s.charAt(i);
 if (!((c >= 'A' && c <= 'Z')||(c >= 'a' && c <='z')||(c >= '0' && c <= '9')))
 {
 judge1 = false;
 break;
 }
  }
  
  if (!judge1)
  {
  text.setText(str2);
  return;
  }
  
  s = textfiled2.getText();
  if ((s.length() > 6) || (s.length() < 1))
  {
    text.setText(str2);
  return;
  }
  
  for (int i = 0; i < s.length();i++)
  {
 char c = s.charAt(i);
 if (!((c >= 'A' && c <= 'Z')||(c >= 'a' && c <='z')||(c >= '0' && c <= '9')))
 {
 judge2 = false;
 break;
 }
  }
  
  if (!judge2)
  {
  text.setText(str2);
  return;
  }
  
  s = textfiled3.getText();
  int i=Integer.parseInt(s);
  if (i != a)
  {
  text.setText(str2);
  }
  else
  {
  text.setText(str1);
  }
  
}
});*/

測試用例:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM