0% found this document useful (0 votes)
5 views16 pages

Assignment#03

Uploaded by

Subramanian A
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)
5 views16 pages

Assignment#03

Uploaded by

Subramanian A
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/ 16

Assignment: 03

Name: Jay Ganesh


ASU ID: 1220405901
ASURITE: jganesh2

Part 1:

1. Tool Used: EclEmma Java Code Coverage 3.1.3.


Description:
It gives a coverage overview (instructions, lines, methods, branches, classes, cyclomatic
complexity) that boils down to method-level coverage and cyclomatic complexity. We
can also export the coverage report data to HTML, XML, CSV, etc.
EclEmma is a great tool that can be easily and directly used with Eclipse workbench
with just a few clicks. All you need to do is to install this tool from “Eclipse Marketplace”
available under “Help”. This code coverage tool also supports various unit testing
frameworks and suites, such as JUnit, TestNG, etc. EclEmma is based on the JaCoCo
library.

List of Coverage it provides:


It is a free Java Code Coverage tool that provides various coverage metrics such as:
​• Statement coverage
• Decision coverage
• Decision Condition (Branch) coverage
• It also provides the missed branches and statements with a color tagging (Green:
Executed, Yellow: Possibly Executed/May or May not be executed, Red: Not Executed)
that which lines or branches are executed/possibly executed/not executed.

2. Source Listing of the Code:

import java.util.*;

public class HeapSort{


public static void main(String[] args){
int n;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the number of elements you want to sort: ");
//reading the number of elements from the that we want to enter
n=sc.nextInt();
//creates an array in the memory of length n
int[] array = new int[n];
System.out.println("\nEnter the elements of the array to be sorted: ");
for(int i=0; i<n; i++)
{
//reading array elements from the user
array[i]=sc.nextInt();
}
System.out.println("\nArray elements are: ");
// accessing array elements using the for loop
for (int i=0; i<n; i++){
System.out.println(array[i]);
}
int[] toReturn = new int[n];
toReturn = heapsort(array);
System.out.println("\nThe sorted elements are (using HeapSort algorithm): " +
Arrays.toString(toReturn) + "\n");
}

public static int[] heapsort(int[] input) {


// convert input to heap
int leastParent = (input.length - 1) / 2;
for (int i = leastParent; i >= 0; i--) {
moveDown(input, i, input.length - 1);
}
// flatten heap into sorted array
for (int i = input.length - 1; i > 0; i--) {
if (input[0] > input[i]) {
swap(input, 0, i);
moveDown(input, 0, i - 1);
}
}
int[] output = new int[input.length];
output = input;
return output;
}

public static void moveDown(int[] input, int first, int last) {


int largest = 2 * first + 1;
while (largest <= last) {
// right child exists and is larger than left child
if (largest < last && input[largest] < input[largest + 1]) {
largest++;
}
// right child is larger than parent
if (input[largest] > input[first]) {
swap(input, largest, first);
// move down to largest child
first = largest;
largest = 2 * first + 1;
} else {
return;// force exit
}
}
}

public static void swap(int[] input, int a, int b) {


int tmp = input[a];
input[a] = input[b];
input[b] = tmp;
}
}

3. Test Cases: (JUnit 4)

import org.junit.Test;
import org.junit.Assert;

public class HeapTest {


@Test
public void testHeapsort1() {
System.out.println("test-case-1: heapsort using JUnit 4");
int[] input = {8, 5, 3, 1, 9, 6, 0, 7, 4, 2, 5};
int[] expected = {0,1,2,3,4,5,5,6,7,8,9};
HeapSort instance = new HeapSort();
instance.main(new String[] {"arg1", "arg2", "arg3"});
Assert.assertArrayEquals(expected, instance.heapsort(input));
}

@Test
public void testHeapsort2() {
System.out.println("test-case-2: heapsort using JUnit 4");
int[] input = {8, 5, 3, 1, 9, 6, 0, 7, 4, 2, 5};
HeapSort instance = new HeapSort();
instance.main(new String[] {"arg1", "arg2", "arg3"});
Assert.assertNotSame(new int[]{1,0,2,3,4,5,5,6,7,8,9}, instance.heapsort(input));
}

@Test
public void testHeapsort3() {
System.out.println("test-case-4: heapsort using JUnit 4");
int[] input = {8, 5, 3, 1, 9};
HeapSort instance = new HeapSort();
Assert.assertArrayEquals(new int[]{1,3,5,8,9}, instance.heapsort(input));
}

@Test
public void testHeapsort4() { //Test case for empty input array
System.out.println("test-case-6: heapsort using JUnit 4");
int[] input = {};
HeapSort instance = new HeapSort();
Assert.assertArrayEquals(new int[]{}, instance.heapsort(input));
}
}

4. Screenshot of Coverage of Each Test Case:

All Test Cases run together: Coverage achieved (Branch/Decision Coverage): 100%
Individual Test Case:

Test Case#01:
Coverage achieved (Branch/Decision Coverage): 100%
Test Case#02:
Coverage achieved (Branch/Decision Coverage): 100%
Test Case#03:
Coverage achieved (Branch/Decision Coverage): 100%
Test Case#04:
Coverage achieved (Branch/Decision Coverage): 94.4%
5. Evaluation of the tool’s usefulness:

EclEmma is extremely easy to use, which makes it outstanding. It can generate the output
report of the code coverage in a variety of formats, such as HTML, XML, etc. It is free to use. It
has several counters like branch counters, line counters, etc. which makes it a very good
analytical tool in terms of code coverage. This tool has no external dependencies and is pure
100% java and works in almost every version of JVM (1.2.x or higher). It also helps in merging
the test runs and the coverage data.
Report items will be highlighted with coverage levels below user-specified thresholds. The
consequence of a source highlighting is additionally straightforwardly apparent in the Java
source editors. An adaptable shading color code features completely, mostly, and not covered
lines. This works for your own source code just as for source appended to instrumented
outside libraries.
Thus, EclEmma is extremely useful in terms of the Java Code Coverage tool.

PART-2:

1. Tool used: FindBugs 3.0.1.


Description: It is easily integrated with Eclipse IDE and is very in demand in Java
Static Source Code Analysis.

FindBugs uses special bytecode scanning strategies to analyze static Java code. It
uses the concept of bug patterns. It is free to use.

Types of Analysis: All bugs/warnings are classified in four positions: (i) scariest, (ii) scary, (iii)
troubling, and (iv) of concern. This can be an indication to the developer of the conceivable
impact/severity of the warnings. The current adaptation of FindBugs reports around 400
warnings within the nine categories:

CATEGORY NUMBER SAMPLES

Illegal format string

Correctness 142 Null value is guaranteed to be dereferenced

Call to equals() comparing unrelated class and interface

Confusing method names

Bad practice 84 Method may fail to close stream

Comparison of String parameter using == or !=

Useless control flow

Dodgy code 71 Integer remainder modulo 1

Redundant null check of value known to be null

A thread was created using the default empty run method

Multithreaded Correctness 45
Class's writeObject() method is synchronized but nothing
else is
Method concatenates strings using + in a loop
Performance 27
Method invokes inefficient Boolean constructor

Finalizer should be protected, not public


Malicious Code
15
Vulnerability
Field isn't final and can't be protected from malicious code

Hardcoded constant database password


Security 11
A prepared statement is generated from a variable String

Experimental 3 Method may fail to clean up stream or resource

Consider using Locale parameterized version of invoked


Internationalization 2
method

Three different data flow anomalies:


(i) Variable defined and then redefined without being referenced.
(ii) Referencing an undefined variable.
(iii) Defining a variable and never using it.
There could be many more.

2. SOURCE CODE LISTING:

HeapSort.java

import java.util.*;
public class HeapSort{

public static void main(String[] args){


int n;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the number of elements you want to sort: ");
//reading the number of elements from the that we want to enter
n=sc.nextInt();
//creates an array in the memory of length n
int[] array = new int[n];
System.out.println("\nEnter the elements of the array to be sorted: ");
for(int i=0; i<n; i++)
{
//reading array elements from the user
array[i]=sc.nextInt();
}
System.out.println("\nArray elements are: ");
// accessing array elements using the for loop
for (int i=0; i<n; i++){
System.out.println(array[i]);
}
int[] toReturn = new int[n];
toReturn = heapsort(array);
System.out.println("\nThe sorted elements are (using HeapSort algorithm): " +
Arrays.toString(toReturn) + "\n");
}

public static int[] heapsort(int[] input) {


// convert input to heap
int leastParent = (input.length - 1) / 2;
for (int i = leastParent; i >= 0; i--) {
moveDown(input, i, input.length - 1);
}
// flatten heap into sorted array
for (int i = input.length - 1; i > 0; i--) {
if (input[0] > input[i]) {
swap(input, 0, i);
moveDown(input, 0, i - 1);
}
}
int[] output = new int[input.length];
output = input;
return output;
}

public static void moveDown(int[] input, int first, int last) {


int largest = 2 * first + 1;
while (largest <= last) {
// right child exists and is larger than left child
if (largest < last && input[largest] < input[largest + 1]) {
largest++;
}
// right child is larger than parent
if (input[largest] > input[first]) {
swap(input, largest, first);
// move down to largest child
first = largest;
largest = 2 * first + 1;
} else {
return;// force exit
}
}
}

public static void swap(int[] input, int a, int b) {


int tmp = input[a];
input[a] = input[b];
input[b] = tmp;
}
}
HeapTest.java

import org.junit.Test;
import org.junit.Assert;

public class HeapTest {


@Test
public void testHeapsort1() {
System.out.println("test-case-1: heapsort using JUnit 4");
int[] input = {8, 5, 3, 1, 9, 6, 0, 7, 4, 2, 5};
int[] expected = {0,1,2,3,4,5,5,6,7,8,9};
HeapSort instance = new HeapSort();
instance.main(new String[] {"arg1", "arg2", "arg3"});
Assert.assertArrayEquals(expected, instance.heapsort(input));
}

@Test
public void testHeapsort2() {
System.out.println("test-case-2: heapsort using JUnit 4");
int[] input = {8, 5, 3, 1, 9, 6, 0, 7, 4, 2, 5};
HeapSort instance = new HeapSort();
instance.main(new String[] {"arg1", "arg2", "arg3"});
Assert.assertNotSame(new int[]{1,0,2,3,4,5,5,6,7,8,9}, instance.heapsort(input));
}

@Test
public void testHeapsort3() {
System.out.println("test-case-4: heapsort using JUnit 4");
int[] input = {8, 5, 3, 1, 9};
HeapSort instance = new HeapSort();
instance.main(new String[] {"arg1", "arg2", "arg3"});
Assert.assertArrayEquals(new int[]{1,3,5,8,9}, instance.heapsort(input));
}

@Test
public void testHeapsort4() { //Test case for empty input array
System.out.println("test-case-6: heapsort using JUnit 4");
int[] input = {};
HeapSort instance = new HeapSort();
instance.main(new String[] {"arg1", "arg2", "arg3"});
Assert.assertArrayEquals(new int[]{}, instance.heapsort(input));
}
}

3. Screenshots of FindBugs:

Bugs Reported = 04 in HeapTest.java as defining a variable and never using it.


4. Evaluation of the tool’s usefulness:
FindBugs is precious for each novice and skilled programmer. If you simply begin to
write Java applications, the device will help you to learn better programming practices
by way of explaining to you the motives why the code must be written in an exclusive
way. FindBugs helps experienced builders to keep the code smooth and to evaluate
other's code by way of virtually jogging the analysis over the Java projects.

FindBugs works similar to the Java compiler: as quickly as you hit CTRL+S, Eclipse will
collect the code and begin bug analysis incrementally. FindBugs can find an
outstanding quantity of exclusive bug patterns, which are occasionally difficult to
reproduce or debug. FindBugs has a very small wide variety of false positives. Even if
you suppose that the code will never run in the highlighted issue, in many instances
the tool opens your eyes to "dirty" places in your code.

In the long term, FindBugs has a lot of categories of warnings that inculcate a good
practice for Java Development and hence, is extensively used by giant firms.

REFERENCES:
• http://www.geekviewpoint.com/java/sorting/heapsort

• https://www.methodsandtools.com/tools/findbugs.php
• https://www.eclemma.org/

You might also like