Code stimmt nicht für vorgegebenen JUnit-Test

sailcomp

Neues Mitglied
Hallo Zusammen

Schreibe für die Uni einen Countingsort. JUnit-Tests sind gegeben (die möchte ich also nicht anpassen).
Ich verstehe nicht weshalb der Unit-Tests irgendwie immer noch mit dem Imput-Array vergleicht anstatt mit dem gemäss Konsole richtig sortierten output-Array.

Vielen Dank für die Hilfe!

Fehlermeldung
Java:
junit.framework.AssertionFailedError: expected:<1> but was:<2>
	at junit.framework.Assert.fail(Assert.java:50)
	at junit.framework.Assert.failNotEquals(Assert.java:287)
	at junit.framework.Assert.assertEquals(Assert.java:67)
	at junit.framework.Assert.assertEquals(Assert.java:199)
	at junit.framework.Assert.assertEquals(Assert.java:205)
	...

Konsole
--------------------------
buckets+: 2 buckets-: 0
input: [1, 2, 1]
temp occurrence: [2, 1]
temp incremented: [2, 3]
inputValue: 1
inputValue: 2
inputValue: 1
output: [1, 1, 2]
assertEquals: [1, 2, 1]
--------------------------

Unit-Test
Java:
import static junit.framework.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;

/**
 * Testet die Klasse {@link CountingSort}.
 */
public class CountingSortTest {
	
	private CountingSort countingSort;
	
	/** Prepares a test instance */
	@Before
	public void setupTestInstance(){
		countingSort = new CountingSort();
	}
...
	/**
	 * Range with one element works also.
	 */
	@Test
	public void testRangeWithOneElement() {
		int[] array = new int[] { 1, 2, 1 };
		countingSort.sort(array, 1, 2);
		System.out.println("assertEquals: "+Arrays.toString(array));
		assertEquals(1, array[0]);
		assertEquals(1, array[1]);
		assertEquals(2, array[2]);
	}

Main
Java:
import java.util.Random;

public class SorterComparison {

	public static void main(String... args) {
		CountingSort s1 = new CountingSort();
		int myArray[] = {-1, -3, -2};
		s1.sort(myArray, -4, 0);
...

CountingSort.java
Java:
import java.util.Arrays;

public class CountingSort implements SortAlgorithm {

	/**
	 * Sortierfunktion.
	 * 
	 * @param array
	 *            zu sortierender Array
	 * @param min
	 *            der Minimalwert des Arrays
	 * @param max
	 *            der Maximalwert des Arrays
	 */
	public void sort(int[] array, int min, int max) {
		// Positive count
		int bucketsPositive;
		if(max < 0){
			bucketsPositive = 0;
		}else if(min <= 0){
			bucketsPositive = max+1; // +1 for the bucket of "0"
		}else{
			bucketsPositive = max-min+1; // +1 for the number where we start to count
		}
		System.out.print("buckets+: "+bucketsPositive);
		
		// Negative count
		int bucketsNegative;
		if(min >= 0){
			bucketsNegative = 0;
		}else if(max >= 0){
			bucketsNegative = Math.abs(min);
		}else{
			bucketsNegative = Math.abs(min)-Math.abs(max)+1; // +1 for the number where we start to count
		}
		System.out.println(" buckets-: "+bucketsNegative);
		
		// Array declaration
		int input[] = array;
		int temporary[] = new int[bucketsPositive+bucketsNegative];
		int output[] = new int[array.length];
		System.out.println("input: "+Arrays.toString(input));
		
		// Counting occurrence
		for(int i = 0; i < input.length; i++){
			if(input[i] < 0){
				temporary[Math.abs(Math.abs(input[i])-bucketsNegative)]++;
			}else{
				temporary[input[i]+bucketsNegative-1]++;
			}
		}
		System.out.println("temp occurrence: "+Arrays.toString(temporary));
		
		// Incrementing
		for(int i=1; i<temporary.length; i++){
			temporary[i] = temporary[i-1]+temporary[i];
		}
		System.out.println("temp incremented: "+Arrays.toString(temporary));
		
		// Deploying and decrementing
		for(int i=input.length-1; i>-1; i--){
			int inputValue = input[i];
			System.out.println("inputValue: "+inputValue);
			int outputPos = temporary[inputValue-1]-1;
			temporary[inputValue-1] = temporary[inputValue-1]-1;
			//System.out.println("outputPos: "+outputPos);
			//System.out.println("temp decremented: "+Arrays.toString(temporary));
			output[outputPos] = inputValue;
			//System.out.println("output: "+Arrays.toString(output));
		}
		array = output;
		System.out.println("output: "+Arrays.toString(array));
		return;
	}

	@Override
	public void sort(int[] array) {
		if(array.length == 0){
			return;
		}
		
		int max = array[0];
		int min = array[0];
		for (int i = 1; i < array.length; ++i) {
			if (array[i] > max) max = array[i];
			if (array[i] < min) min = array[i];
		}
		sort(array, min, max);
	}

	@Override
	public String getName() {
		return "CountingSort";
	}

}
 
Das:
[JAVA=71]array = output;[/code]
macht keinen Sinn und hat null Effekt. Parameter werden in Java immer Call By Value übergeben, auch Objektreferenzen. Wenn du also der Variable eine andere Referenz zuweist, hat das auf die Referenz in der aufrufenden Methode keinen Einfluss. Du musst jedes Element innerhalb des übergebenen Arrays auf den entsprechenden Wert des Ouput-Arrays setzen. (z.B. mit System.arraycopy())
 
Ach so, das macht Sinn! 🙂
Vielen Dank, hat funktioniert, mit deinem Vorschlag:

System.arraycopy(output, 0, array, 0, output.length);
 

Neue Themen


Zurück
Oben