Professional Documents
Culture Documents
Loop Coverage: I. Example of Loop Covergae Testing (Loop Coverage For)
Loop Coverage: I. Example of Loop Covergae Testing (Loop Coverage For)
Loop Coverage: I. Example of Loop Covergae Testing (Loop Coverage For)
LOOP COVERAGE
1. SOURCE CODE
(JPrimeNumber.java)
package lt;
import java.io.*;
public class JPrimeNumber {
DataInputStream ds=new DataInputStream(System.in);
int n=0,c=0;
int lc=0;
public void findPrime()throws Exception
{
System.out.print("Enter a number : ");
n=Integer.parseInt(ds.readLine());
if(n!=0)
{
for(int i=0;i<n;i++)
{
if(n%(i+1)==0)
c++;
}
++lc;
if(c==2)
System.out.println("Given Number "+new Integer(n).toString()+" is a prime number");
else
System.out.println("Given Number "+new Integer(n).toString()+" is not a prime number");
}
else
System.out.println("Plz enter a valid number");
}
int findExecutedLoops()
{
return lc;
}
public static void main(String[] args)throws Exception
{
1
JPrimeNumber obj=new JPrimeNumber();
obj.findPrime();
}
}
(JLoopPrime.java)
package lt;
import java.io.*;
public class JLoopPrime {
File f;
BufferedReader br;
String str;
String fp="C:\\Users\\shiva\\Documents\\NetBeansProjects\\LoopTesting
24.07.2014\\src\\lt\\JPrimeNumber.java";
int tl=0,el=0,lp=0;
public void findLoops()
{
try
{
f=new File(fp);
if(f.exists())
{
br=new BufferedReader(new FileReader(f));
while((str=br.readLine())!=null)
{
if(str.trim().startsWith("for"))
++tl;
}
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void main(String[] args)throws Exception
{
String lc="";
DataInputStream ds=new DataInputStream(System.in);
do
{
System.out.println("----------------------------------");
System.out.println("\tManual Loop Testing");
System.out.println("----------------------------------");
JLoopPrime obj=new JLoopPrime();
2
obj.findLoops();
JPrimeNumber jp=new JPrimeNumber();
jp.findPrime();
obj.el=jp.findExecutedLoops();
obj.lp=((obj.el)/(obj.tl)*100);
System.out.println("Total Number of Loops : "+obj.tl);
System.out.println("Number of Loops are executed : "+obj.el);
System.out.println("Loop Testing Percentage : "+new Integer(obj.lp).toString()+" %");
System.out.println("----------------------------------");
System.out.print("Do u want to continue : press Yes or No : ");
lc=ds.readLine();
}while(lc.equalsIgnoreCase("Yes"));
}
}
3
2. OUTPUT (BEST | AVERAGE | WORST CASE)
4
II. EXAMPLE OF LOOP COVERGAE TESTING
1. SOURCE CODE
(JSinglePattern.java)
package lt;
import java.io.*;
public class JSinglePattern {
DataInputStream ds=new DataInputStream(System.in);
int n=0,c=0;
int lc=0;
public void findPattern()throws Exception
{
System.out.print("Enter a number : ");
n=Integer.parseInt(ds.readLine());
if(n!=0)
{
for(int i=0;i<n;i++)
{
for(int j=n-1;j>i;j--)
{
System.out.print(" ");
}
for(int k=0;k<i;k++)
{
System.out.print("*");
}
System.out.println("");
}
// increment the loops covered value by 3
lc+=3;
}
else
System.out.println("Plz enter a valid number");
}
int findExecutedLoops()
{
return lc;
}
public static void main(String[] args)throws Exception
{
JSinglePattern obj=new JSinglePattern();
obj.findPattern();
}
5
}
(JLoopPattern.java)
package lt;
import java.io.*;
public class JLoopPattern {
File f;
BufferedReader br;
String str;
// path of implementation file
String fp="C:\\Users\\shiva\\Documents\\NetBeansProjects\\LoopTesting
24.07.2014\\src\\lt\\JSinglePattern.java";
int tl=0,el=0,lp=0;
public void findLoops()
{
try
{
f=new File(fp);
// check the whether file exists or not
if(f.exists())
{
br=new BufferedReader(new FileReader(f));
while((str=br.readLine())!=null)
{
if(str.trim().startsWith("for")||str.trim().startsWith("while"))
++tl;
//System.out.println(str);
}
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void main(String[] args)throws Exception
{
String lc="";
DataInputStream ds=new DataInputStream(System.in);
do
{
System.out.println("----------------------------------");
System.out.println("\tManual Loop Testing for Pattern");
System.out.println("----------------------------------");
JLoopPattern obj=new JLoopPattern();
obj.findLoops();
6
JSinglePattern sp=new JSinglePattern();
sp.findPattern();
// return the total no of loops are covered in the program
obj.el=sp.findExecutedLoops();
// calculate loop testing percentage
obj.lp=((obj.el)/(obj.tl)*100);
System.out.println("Total Number of Loops : "+obj.tl);
System.out.println("Number of Loops are executed : "+obj.el);
System.out.println("Loop Testing Percentage : "+new
Integer(obj.lp).toString()+" %");
System.out.println("----------------------------------");
System.out.print("Do u want to continue : press Yes or No : ");
lc=ds.readLine();
}while(lc.equalsIgnoreCase("Yes"));
}
}
7
2. OUTPUT (BEST | AVERAGE | WORST CASE)