0% found this document useful (0 votes)
147 views8 pages

Loop Coverage: I. Example of Loop Covergae Testing (Loop Coverage For)

The document discusses loop coverage testing and provides two examples. The first tests for prime numbers by iterating through numbers to check for factors. The second prints a pattern by nesting loops to output stars and spaces. Both examples count the total loops, executed loops, and calculate test coverage percentage. Output shows the results for best, average, and worst case scenarios.

Uploaded by

Mallika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views8 pages

Loop Coverage: I. Example of Loop Covergae Testing (Loop Coverage For)

The document discusses loop coverage testing and provides two examples. The first tests for prime numbers by iterating through numbers to check for factors. The second prints a pattern by nesting loops to output stars and spaces. Both examples count the total loops, executed loops, and calculate test coverage percentage. Output shows the results for best, average, and worst case scenarios.

Uploaded by

Mallika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4.

LOOP COVERAGE

LOOP COVERAGE TESTING


To implement the Loop coverage testing using various rule based approach in java.

I. EXAMPLE OF LOOP COVERGAE TESTING

(LOOP COVERAGE FOR FINDING PRIME NUMBER)

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

(LOOP COVERAGE FOR FINDING SINGLE PATTERN)

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)

You might also like