lundi 24 septembre 2018

J-unit with java

I'm trying to create j-unit tests for each function below, but I don't know how to get started. In my case, it's really hard to use the function itself and test it within the unit case. I only know how to test specific values/lists. I need help with understanding how to test the functions themselves.

public class FindValues {           
    /**
     * @param args
     */
    public static void main(String[] args) {        
        int[] x = new int[] {2,5,6};
        int y = 2;
        findLast(x, y);
        int[] x1 = new int[] {1,2,0};
        lastZero(x1,y);
        int[] x2  = new int[] {-3, 3, 5, 0};
        countPositive(x2,y);
        int[] x3  = new int[] {-6, 2, -1, 1};
        oddOrPos(x3);
    }   

    // findlast(int [] x, int y) takes an array of integers and should return the index of
    // the last element in the array that is equal to y.
    public static int findLast(int [] x, int y) {
        x = new int[]{2,5,6};
        y = 2;
        for (int i=x.length-1; i >= 0; i--) { 
            if (x[i] == y) { 
                // System.out.println(i);
                return i;
            } 
        }
        return -1;          
    } //end FindLast

// lastZero(int [] x) takes an array of integers and should return the index of the last//0 in x.
    public static int lastZero (int[] x, int y) {
        x = new int[]{1,2,0};
        for (int i = 0; i < x.length; i++)
        {
            if (x[i] == 0)
            {
                System.out.println(i);
            }
        }
        return -1;
    }

    public static int countPositive (int[] x, int y) {
        //Returns the number of positive elements in Xint count = 0;
        x = new int[]{-3, 3, 5, 0};
        int count = 0;
        for (int i=0; i < x.length; i++) {
            if (x[i] > 0) {
                count++;
            }
        }
        System.out.println(count);
        return count;           
    }

    public static int oddOrPos(int[] x) {
        //Return numbers in x that are either odd or positive or both int count = 0;
        int count = 0;
        x = new int[]{-6, 2, -1, 1};
        for (int i = 0; i < x.length; i++) {
            if (x[i]% 2 == -1 || x[i]% 2 == 1 || x[i] > 0) {
                count++;
            }
        }
        System.out.println(count);
        return count;
        }
    }

Aucun commentaire:

Enregistrer un commentaire