samedi 25 janvier 2020

Problem with Junit test for sorting numbers in order class

I've got a class to sort numbers in proper order with my custom exception. I need to write Junit test for this class. But I am still learning and after some tutorials, I'm not sure how I should write this test. If somebody can help me I will be very thankful. This is my class code:


import java.util.Arrays;
import java.util.Scanner;
import java.util.InputMismatchException;


public class metoda1{
    private static Scanner sc;

    public static void main(String[] args) throws  myException1 {
        int size, i;
        sc = new Scanner(System.in);

        System.out.print("Enter numbers to sorting: ");
        size = sc.nextInt();

        int[] a = new int[size];

        System.out.print("Enter " + size + " numbers, which should be sorting: \n");
        for (i = 0; i < size; i++) {
            try {
                a[i] = sc.nextInt();
            } catch (InputMismatchException e) {
                throw new myException1(e);
            }
        }
        if (i == size) {
            Arrays.sort(a);

            System.out.println("\nNumbers after sorting:  ");
            for (int Number : a) {
                System.out.print(Number + "  ");
            }
        }
    }


    static class myException1 extends Exception {

        public myException1(InputMismatchException e) {
            System.out.println("My exception: " +e);
            System.exit(0);
        }
    }
}

And I try to do some Junit test:



import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


import java.util.Arrays;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.*;



public class metoda2Test {

    Integer[][] sorted = new Integer[6][];
    Integer[][] random = new Integer[6][];
    Integer[][] backwards = new Integer[6][];
    Integer[][] testSorted = new Integer[6][];
    Integer[][] testRandom = new Integer[6][];
    Integer[][] testBackwards = new Integer[6][];

    Random rand = new Random();

    @BeforeEach
    public void setUp() throws Exception {
        sorted[0] = new Integer[0];
        sorted[1] = new Integer[1];
        sorted[2] = new Integer[2];
        sorted[3] = new Integer[3];
        sorted[4] = new Integer[10];
        sorted[5] = new Integer[1000];

        // initialize arrays
        for (int i = 0; i < sorted.length; i++) {
            random[i] = Arrays.copyOf(sorted[i], sorted[i].length);
            backwards[i] = Arrays.copyOf(sorted[i], sorted[i].length);
            for (int j = 0; j < sorted[i].length; j++) {
                sorted[i][j] = j;
                backwards[i][j] = sorted[i].length - j - 1;
                int k = rand.nextInt(j+1); // place for j in random[i]
                if (k < j) {
                    random[i][j] = random[i][k];
                    random[i][k] = j;
                } else random[i][j] = j;
            }
            testSorted[i] = Arrays.copyOf(sorted[i], sorted[i].length);
            testRandom[i] = Arrays.copyOf(random[i], random[i].length);
            testBackwards[i] = Arrays.copyOf(backwards[i], backwards[i].length);
        }
    }

    private void copy(Integer[][] source, Integer[][] dest) {
        for (int i = 0; i < source.length; i++) {
            System.arraycopy(source[i], 0, dest[i], 0, source[i].length);
        }
    }

    private void copyAll() {
        copy(sorted, testSorted);
        copy(random, testRandom);
        copy(backwards, testBackwards);
    }

    private void sort(sorter<Integer> s) {
        for (Integer[] arr : testSorted) s.sort(arr);
        for (Integer[] arr : testRandom) s.sort(arr);
        for (Integer[] arr : testBackwards) s.sort(arr);
    }

    private boolean sorted(Integer[][] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays[i].length; j++) {
                if (!arrays[i][j].equals(sorted[i][j])) return false;
            }
        }
        return true;
    }


    @AfterEach
    void tearDown() {
    }

    @Test
    public void insertionSortTest() {
        copyAll();
        sort(new insertionSort<Integer>());
        assertTrue(sorted(testRandom));
        assertTrue(sorted(testSorted));
        assertTrue(sorted(testBackwards));
    }

    @Test
    public void selectionSortTest() {
        copyAll();
        sort(new selectionSortTest<Integer>());
        assertTrue(sorted(testRandom));
        assertTrue(sorted(testSorted));
        assertTrue(sorted(testBackwards));
    }

    @Test
    public void mergeSortTest() {
        copyAll();
        sort(new MergeSort<Integer>());
        assertTrue(sorted(testRandom));
        assertTrue(sorted(testSorted));
        assertTrue(sorted(testBackwards));
    }

    private void sort(MergeSort<Integer> integerMergeSort) {
    }

    @Test
    public void quickSortTest() {
        copyAll();
        sort(new quickSort<Integer>());
        assertTrue(sorted(testRandom));
        assertTrue(sorted(testSorted));
        assertTrue(sorted(testBackwards));
    }

    @Test
    void main() {
    }

//    @Test(expected = metoda1.myException1.class)
//    public void divisionWithException() {
//        int i = 1/0;
//    }

    private class sorter<T> {
        public void sort(Integer[] arr) {
        }
    }

    private class insertionSort<T> extends sorter<Integer> {
    }

    private class selectionSortTest<T> extends sorter<Integer> {
    }

    private class quickSort<T> extends sorter<Integer> {
    }

    private class MergeSort<T> {
    }
}


Aucun commentaire:

Enregistrer un commentaire