Ad
  • Default User Avatar
  • Custom User Avatar

    refresh your browser: I just edited the tests so that the calls and the assertions are now concomitant. That should be good. Improved a bit the error messages too.

  • Default User Avatar
    public Matrix(double[][] elements) {   
      System.out.println("Entered the Constructor");
      try{
        print(elements);                      System.out.println("Passed Printing");
        matrix = clone(elements);             System.out.println("Passed Assignment");
      }catch(NullPointerException e){
        System.out.println("Error - Null \n" + "msg - " + e.getMessage() + "\n----------");
        throw new IllegalArgumentException();
      }catch(IndexOutOfBoundsException e){
        System.out.println("Error - Index\n" + "msg - " + e.getMessage() + "\n----------");
        //throw new RuntimeException();               // - Test Failed
        //throw new IndexOutOfBoundsException();      // - Test Failed
        //throw new NullPointerException();           // - Test Failed
        //throw new IllegalArgumentException();       // - should throw IllegalArgumentException if array is null: expected null, but was:<Matrix@2bbf180e>
        //throw new ArrayIndexOutOfBoundsException(); // - Test Failed
        // No Exception Thrown                        // - should throw IllegalArgumentException if array is null: expected null, but was:<Matrix@769e7ee8>
      }
      System.out.println("Reached Exit");
    }
    

    This is the Output Log

    Entered the Constructor
    Error - Null
    msg - null
    ----------

    Entered the Constructor
    size => rows = 2 cols = 2
    1.0 2.0
    Error - Null
    msg - null
    ----------

    Entered the Constructor
    size => rows = 2 cols = 3
    1.0 2.0 3.0
    1.0 2.0
    Passed Printing
    Error - Index
    msg - Index 2 out of bounds for length 2
    ----------

    Reached Exit

    The Error Message :-
    should throw IllegalArgumentException if array is null: expected null, but was:Matrix@769e7ee8

    Its always the same data. And the message is the above one or Test Failed. I tried different Exceptions but none worked. Also No Exception still doesn't works.
    Cannot use other than runtime Exceptions because the methods in the class creates the Matrix object by this constructor and then returns them. Like -> return new Matrix(someData);

    IndexOutOfBoundsException occurs due to the short length (2 rather than 3 as row 1) of 2nd row

    What to do.?

  • Custom User Avatar

    warning, the tests are "rather shity", about that part: there are actually several tests, some with the exact same assertion message but testing different things. I'll bet you're not failing the same every time, depending on your changes. Try to carefully print all the data you have to track doown the thing. If not enough, I can past the inputs of the tests.

    note: your class is called on 4 different inputs, and only then the 4 related assertions are made, so be very careful with what's happening...

  • Default User Avatar
    public Matrix(double[][] elements) {   
      try{
        matrix = clone(elements); 
      }catch(NullPointerException e){
        throw new IllegalArgumentException();
      }
    }
    

    yup throwing the exception but it still wont work.
    clone(element) a function to copy all the elements to new array and return.

  • Custom User Avatar

    should throw IllegalArgumentException if array is null

    -> ?

    (forget about the actual/expected part, it's not relevant, for those tests)

  • Default User Avatar

    I pass all the tests except one test1Init.

    It says:

    test1Init

    should throw IllegalArgumentException if array is null: expected null, but was:Matrix@14d3bc22


    can someone help.?

  • Default User Avatar

    Thanks! It worked!