Search code examples
javaexceptionjunitassert

What kind of exception it must be? JUnit test Assertion error


Please help me with understanding this case. I've been studying Java a while ago and I do not understand a lot of moments. My program has a Integer List input. It converts this list to 2d array and build inside of array a saw triangle like /\ with the smallest number at the top and from left to right order and returns it back. Also I need to create a new class for handling errors

private static int[] countSize(int listSize){
    int rows = 0;
    int columns = 0;
    for(int x=1, y=0; x+y<=listSize; x=x+y, y++, rows++);
    columns = 2*rows-1;
    return new int[] { rows, columns };
}


private static int[][] buildTriangle(List<Integer> list) throws myException{
    if(!list.isEmpty()&&!list.contains(null)&&(list.size()<Integer.MAX_VALUE - 8))
    {
        ArrayList<Integer> workList = new ArrayList<>();
        workList.addAll(list);
        Collections.sort(workList);
        int[] sizes = countSize(list.size());
        int[][] resArray = new int[sizes[0]][sizes[1]];

       //...sort logic...

        System.out.println();
            for(int x=0; x<sizes[0]; x++) {
                for (int y=0; y<sizes[1]; y++)
                    System.out.print("\t" + resArray[x][y] + " ");
                System.out.println();
            }

        return resArray;
    }
    else throw new myException("Input is incorrect");
    }

public int[][] buildPyramid(List<Integer> input) {
    try{
        return buildTriangle(input);
    } catch (myException e) {
        throw new myException();
    }
}

and exceptions handling class

public class myException extends RuntimeException {
    public myException(String message) {
    super(message);}
}

when x is 256 (or some other number like 10000) or bigger JUnit test shows an assertion error because it waiting for an exception. I don't understand what kind of exception can be caused by such data? I'm also glad to hear comments on the code

Here is a reslt for a 7 input elements

0 0 0 1 0 0 0
0 0 2 0 3 0 0
0 4 0 5 0 6 0
7 0 0 0 0 0 0


Solution

  • My guess is that 'able to build pyramid' means 'the base needs to be filled'. This is the case for 1, 3, 6, 10, and so on.

    Figure out a pattern to that series, and check if the list you're given fits ;otherwise throw the exception.

    Here's a list that doesn't fit : { 1,2,3,4,5,6,7}.

    Output array :

    00100

    02340

    56700

    See that's not a proper pyramid?