samedi 27 avril 2019

How can I implement JUnit testing into this program for a binary search?

How can I implement JUnit testing into this program for a binary search, i am quite overwhelmed by this task because I am really unfamiliar with JUnit and also I have all these packages and path that you see in code that I need to use.

I have already tried implementing Testing for just a normal Java class made by me but that did not work either, it would be great if someone could also explain me the syntax of some tests for my code.

package de.hska.iwi.ads.solution.search;

import java.security.acl.LastOwnerException;

import de.hska.iwi.ads.search.Search;

public class BinarySearch<E extends Comparable<E>> implements Search<E> {


@Override
public int search(E[] a, E key, int lower, int upper) {
    // TODO Auto-generated method stub

    this.lower = lower;
    this.upper = upper;

    if(upper > a.length) {
        throw new ArrayIndexOutOfBoundsException();
    }
    int ret = binarySearch(a, key, lower, upper);

    return returnValue;
}

int lower;
int upper;
int returnValue;



/**
 * 
 * @param a Array, der durchsucht werden soll.
 * @param key Element, nach dem gesucht wird.
 * @param lower untere Grenze des zu durchsuchenden Bereiches.
 * @param upper obere Grenze des zu durchsuchenden Bereiches.
 * @return index der Stelle wo daa Elemnt ist.
 */
private int binarySearch(E[] a, E key, int lower, int upper) {

    if (lower < upper) {

        int middle = (lower / 2) + (upper / 2);
        int tempInt = a[middle].compareTo(key);

        if (tempInt > 0) {

            return binarySearch(a, key, lower, middle - 1);
        }
        if (tempInt < 0) {
            return binarySearch(a, key, middle + 1, upper);
        }

        this.returnValue = middle;
        if (key.equals(a[middle]) && !key.equals(a[middle-1])) {
            return middle;
        } else {
            return binarySearch(a, key, lower, middle-1);
        }
    }

    if (key.equals(a[lower])) {
        this.returnValue = lower;

        int temp = checkForDuplicates(a, key, 0, upper-1);

        return returnValue;
    }

    int temp = key.compareTo(a[this.upper]);
    if(temp > 0) {
        this.returnValue = (this.upper + 1);
        return (this.upper + 1);
    }
    temp = key.compareTo(a[this.lower]);
    if(temp < 0) {
        this.returnValue = this.lower - 1;
        return (this.lower - 1);
    } else {
        this.returnValue = upper + 1;
    }


    return returnValue;

}

int lastIndex;

private int checkForDuplicates(E[] a, E key, int lower, int upper) {

    if (lower < upper) {

        int middle = (lower / 2) + (upper / 2);
        lastIndex = middle;
        int tempInt = a[middle].compareTo(key);

        if (tempInt < 0) {
            return checkForDuplicates(a, key, middle + 1, upper);
        }

        this.returnValue = middle;
        if (key.equals(a[lower])) {
            this.returnValue = lower;
            checkForDuplicates(a, key, 0, middle-1);
            return returnValue;
        }

        return -1;
    }

    if (key.equals(a[lower])) {
        this.returnValue = lower;
        return returnValue;
    } 

    return -1;
}


}

Aucun commentaire:

Enregistrer un commentaire