hello, I suppose to create simple test case(white box and black box) using a Jun
ID: 3555549 • Letter: H
Question
hello, I suppose to create simple test case(white box and black box) using a Junit test
But i keep getting error, I dont know exact problem , could you f help me please
COde
package select;
public class selectionSort2 {
public static void main(String[] args)
{
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
printArray(numArray,n);
selectSort2(numArray, n);
printArray(numArray, n);
}
//Generate data for the selection sort array
public static int[] unsortedArray(int n) {
int[] a = new int[n];
for (int index = 0; index < n; index++) {
a[index] = (n - index);
}
return a;
}
//print the array
public static void printArray(int[] data, int n)
{
for (int index = 0; index < n; index++) {
System.out.print(data[index] + " ");
}
System.out.println();
} public static void selectSort2(int[] data, int n)
{
for (int numUnsorted = n; numUnsorted > 0; numUnsorted--) {
int maxIndex = 0;
for (int index = 1; index < numUnsorted; index++) {
if (data[maxIndex] < data[index])
maxIndex = index;
//swap the element
int temp = data[numUnsorted-1];
data[numUnsorted-1] = data[maxIndex];
data[maxIndex] = temp;
}
}
}
}
Junit test code
package select;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author secc
*
*/
public class selectionSort2Test {
public static void test(){
}
public static void test_clean()
{
}
/**
* {@l#selectSort2(int[], int)}.
*/
@Test
public void testSelectSort2() {
selectionSort2 tester= new selectionSort2();
tester.selectSort2(null, 0);
fail(" Error");
}
@Test
void testselctsort() throws Exception
{
int n=20;
int []data= new int [20];
int []result= new int[]{20,19,18,17,16,15,24,13,12,11,10,9,8,7,6,5,4,3,2,1};
selectionSort2 selection = new selectionSort2();
assertArrayEquals(result,selection.selectSort2(data,0));
}
}
Explanation / Answer
hey,
you can use this
package select;
public class selectionSort2 {
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
//Generate data for the selection sort array
public static int[] unsortedArray(int n) {
int[] a = new int[n];
for (int index = 0; index < n; index++) {
a[index] = (n - index);
}
return a;
}
//print the array
public static void printArray(int[] data, int n)
{
for (int index = 0; index < n; index++) {
System.out.print(data[index] + " ");
}
System.out.println();
}
public static int [] selectSort2(int[] data, int n)
{
for (int numUnsorted = n; numUnsorted > 0; numUnsorted--) {
int maxIndex = 0;
for (int index = 1; index < numUnsorted; index++) {
if (data[maxIndex] < data[index])
maxIndex = index;
//swap the element
int temp = data[numUnsorted-1];
data[numUnsorted-1] = data[maxIndex];
data[maxIndex] = temp;
}
}
return data;
}
}
package select;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
/**
* @author secc
*
*/
public class testSelectSort2{
public static void main(String args[]) {
int[] num = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,6, 5, 4, 3, 2, 1 };
int n=num.length;
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20 };
num=selectionSort2.selectSort2(num, n);
selectionSort2.printArray(num, n);
assertArrayEquals(expected, num);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.