'JUNIT not runnig
I am new to Intellij IDEA and junit testing. Right now I am writting a quicksort code and writting tests for the classes. When I try to run the test they turn grey and does not go through. How should I fix this?
Down under I have included Test classes and the classes for Quicksort.
// TODO: Help me Understand how to fix the testclasses so the test goes through.
Please be kind, I am new to JUNIT testing.
Here is my codes:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
public class QuicksortFixedPivotTest extends IntSorterTest{
/**
* Returns an implementation of the IntSorter interface. Extending classes
* must override this method.
*
* @return An implementation of IntSorter.
*/
/* An instance of the quicksortFixedPivot */
private QuicksortFixedPivot quicksortFixedPivot;
private InsertionSort quickSort;
/* An array of even length */
private int[] evenArray;
/* An array of odd length */
private int[] oddArray;
private Random random;
@Before
public void setUp() throws IOException {
quicksortFixedPivot = new QuicksortFixedPivot();
evenArray = new int[]{1,5,3,8};
oddArray = new int[]{1,5,2,4,3};
}
/**
* Returns an implementation of the IntSorter interface. Extending classes
* must override this method.
*
* @return An implementation of IntSorter.
*/
@Override
protected IntSorter getIntSorter() {
return new IntSorter() {
@Override
public void sort(int[] v) {
Arrays.sort(v);
}
};
}
//TESTs for arrays of even and odd length
@Test
public void QuickSortFindsAllElementsInEvenArray() {
// Arrange
Arrays.sort(evenArray);
String result = new String();
for (int i = 0; i > evenArray.length; i++) {
result = result + " " + evenArray[i];
// Act, Assert
assertEquals("1 3 5 8", result);
assertThat(evenArray.length,equalTo(i));
}
}
/**
* Test of generateBigArray method.
*/
@Test
public void testGenerateBigArray() throws IOException {
int n = 1000;
InsertionSort i = new InsertionSort();
int[] expResult = i.generateBigArray(n);
int[] result = expResult;
assertArrayEquals(expResult, result);
}
/**
* Assert that binarySearch returns the correct index
* for all elements in an odd length array with multiple elements.
*/
@Test
public void QuickSortFindsAllElementsInOddArray() {
//arrange
Arrays.sort(oddArray);
String result = new String();
for (int i = 0; i > oddArray.length; i++) {
result = result + " " + oddArray[i];
// Act, Assert
assertEquals(" 1 2 3 4 5", result);
assertThat(oddArray.length, equalTo(i));
}
}
/**
*
*/
@Test
public void test1() {
int[] list = {};
String result = new String();
quicksortFixedPivot.sort(list);
for (int i = 0; i < list.length; i++)
result = result + " " + list[i];
assertEquals("", result);
}
}
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.junit.Before;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* Abstract test class for implementations.
*
* Implementing test classes must override the getIntSorter method.
*/
public abstract class IntSorterTest {
private Data data;
private InsertionSort sorter;
int [] emptyArray = new int[] {}; // An empty array
/* An array of even length */
private int[] evenArray;
/* An array of odd length */
private int[] oddArray;
private Random random;
/**
/**
* Returns an implementation of the IntSorter interface. Extending classes
* must override this method.
*
* @return An implementation of IntSorter.
*/
protected abstract IntSorter getIntSorter();
@Before
public void setUp() throws IOException {
sorter = new InsertionSort();
evenArray = new int[]{1,5,3,8};
oddArray = new int[]{1,5,2,4,3};
}
/**
* Assert that the size of an empty Array is exactly 0.
*/
@Test
public void sizeIsZeroWhenArrayIsEmpty() {
int size = emptyArray.length;
assertThat(size,equalTo(0));
assertTrue(emptyArray.length == 0);
}
/**
* Assert that sorting an empty array has no effects.
*/
@Test
public void sortHasNoEffectWhenArrayIsEmpty() {
// Arrange
int [] emptyArray = new int[] {};
// Act
Arrays.sort(emptyArray);
// Assert
assertThat(emptyArray.length, equalTo(0));
}
/**
* Assert that sorting an array with a single element has no effects.
*/
@Test
public void sortHasNoEffectWhenArrayHasSingleElement() {
//Arrange
int[] array = new int[1];
//act
Arrays.sort(array);
//assert
assertThat(array.length,equalTo(1));
}
/**
* Assert that arrayA is the reverse of arrayB.
*/
@Test
public void assertReversed() {
int [] arrayA = {1, 3, 4};
int [] arrayB = {4, 3, 1};
Integer len = arrayA.length;
for (Integer i = 0; i < len; i++) {
assertThat(arrayB[len-i-1], equalTo(arrayA[i]));
}
}
/**
* Assert that sorting a multiple element array results in a correct
* ordering of the elements.
*/
@Test
public void sortMultipleElementArrayGivesCorrectOrdering() {
// Arrange
int[] actual = evenArray;
int[] expected = actual.clone();
// Act
Arrays.sort(expected);
Arrays.sort(actual);
// Assert
assertThat(actual, equalTo(expected));
}
/**
* Test case number: 5
* Pre-condition: given integer list {1,2,3 }
* Test case values: call quickSort to sort it
* Expected output (Post-state): "1 2 3"
*/
@Test
public void test5() {
int[] list = { 1, 2, 3 };
String result = new String();
sorter.sort(list);
for (int i = 0; i < list.length; i++)
result = result + " " + list[i];
assertEquals(" 1 2 3", result);
}
/**
* Test sorting a negative array.
*/
@Test
public void NegativeArray() {
int[] list = { -1, 2, -3 };
String result = new String();
sorter.sort(list);
for (int i = 0; i < list.length; i++)
result = result + " " + list[i];
assertEquals(" -3 -1 2", result);
}
/**
* Sorting a reverse sorted array.
*/
@Test
public void testReverseSorted() {
int[] array1 = new int[10];
int[] array2 = new int[10];
int[] array3 = new int[10];
for (int i = 0; i < 10; i++) {
array1[i] = (100-i);
array2[i] = (100-i);
array3[i] = (100-i);
}
//sort array3
Arrays.sort(array3);
// run QS1()
sorter.sort(array1);
assertArrayEquals(array1,array3);
// run QS2()
sorter.sort(array2);
assertArrayEquals(array2,array3);
}
/**
* Test that all elements are equal in two different arrays
* when sorted. Ascending order
*/
@Test
public void testSort() {
int[] array = {0, 5, 6, 8, 7, 1, 11, 100, 2};
sorter.sort(array);
int[] expected = {0, 1, 2, 5, 6, 7, 8, 11, 100};
System.out.println(Arrays.toString(array));
assertEquals(true, Arrays.equals(expected, array));
}
@Test
public void testSort2() {
int[] array = {0, 5, 6, 8, 7, 1, 11, 100, 2};
sorter.sort(array);
System.out.println(Arrays.toString(array));
assertEquals(true, isSorted(array));
}
/**
* helper method for tests that is in ascending order.
* @param array
* @return
*/
private boolean isSorted(int[] array) {
boolean sorted = true;
for(int i = 0; i < array.length-1; i++) {
sorted = sorted && array[i] <= array[i+1];
if(!sorted) {
System.out.println(array[i] +" >" +array[i+1]);
return false;
}
}
return sorted;
}
}
/**
* An interface for sorting int arrays.
*/
public interface IntSorter {
/**
* Sorts the array in-place into ascending numerical order.
*
* @param v An array of ints.
*/
void sort(int[] v);
}
import java.io.IOException;
public class QuicksortFixedPivot implements IntSorter{
/**
* Sorts the array in-place into ascending numerical order.
*
* @param v An array of ints.
*/
@Override
public void sort(int[] v) {
if (v != null && v.length > 1)
qsort(v, 0, v.length - 1);
}
public void qsort(int[] v, int start, int end) {
if(start < end) {
int[] mid = partition(v, start, end);
qsort(v, start, mid[0] - 1);
qsort(v, mid[1] + 1, end);
}
}
public int[] partition(int[] v, int start, int end) {
int pivot = v[choosePivot(v, start, end)];
int low = start, mid = start, high = end;
while (mid <= high) {
if (v[mid] < pivot) {
swap(v, mid, low);
low++;
mid++;
} else if (v[mid] == pivot) {
mid++;
} else {
swap(v, mid, high);
high--;
}
}
return new int[]{low, high};
}
public int choosePivot(int[] v, int start, int end) {
return start + (end - start)/2;
}
public void swap(int[] v, int i, int j) {
if(i == j) return;
int t = v[i];
v[i] = v[j];
v[j] = t;
}
public QuicksortFixedPivot() throws IOException {
Kattio io = new Kattio(System.in, System.out);
int n = io.getInt(); // number of elements to sort
int[] v = new int[n];
int i = 0;
// read in problem array
while (io.hasMoreTokens()) {
v[i++] = io.getInt();
}
sort(v);
// output sorted array as solution
for (int k : v) {
io.println(k);
}
io.close();
System.exit(0);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
