Saturday, February 19, 2011

In java how do you check to see if a function returns and exact negative value?

Can't believe I don't know the answer to this, but getting late and SO is my only hope of getting some decent sleep ;)

So far I know these are not it: assert solver.solveArray(array3) == -1 assert solver.solveArray(array3) == (-1)

From stackoverflow
  • assertEquals("message", expectedNegativeValue, func() ); ??

    Chris Conway : Oh, are we using JUnit?
  • In what way do you know those aren't it? This short but complete program works fine:

    public class Solver
    {
        public static void main(String[] args)
        {
            Solver solver = new Solver();
            assert solver.solveArray(-1) == -1; // Pass
            assert solver.solveArray(-1) == 2;  // Fail
        }
    
        int solveArray(int x)
        {
            return x;
        }
    }
    
  • Hard to say based on what you've written, but you may need parentheses

    assert( solver.solveArray(array3) == -1 )
    
  • My algorithm was fubar.... thanks for the help anyways

0 comments:

Post a Comment