Test Array Max
Create a method named
test that accepts a single parameter: an instance of
ArrayMax provides a method
max that accepts an
IntArray and returns the maximum of the values as an
If the array is
null or empty
max should throw an
ArrayMax implementations are broken!
Your job is to identify all of them correctly.
To do this you should use
assert to test various inputs.
throw other kinds of exceptions on failure, or use
Here's an example:
Your function does not need to return a value. Instead, if the code is correct no assertion should fail, and if it is incorrect one should.
As you design test inputs, here are two conflicting objectives to keep in mind:
- Less is more. The fewer inputs you need to identify all broken cases, the more readable your test suite will be.
- Think defensively. At the same time, you want to anticipate all the different mistakes that a programmer
You've probably made many of these yourself!
Examples include forgetting to check for
null, off-by-one errors, not handling special cases properly, etc.
You'll also need to think about how to use
try-catch to handle places where the code should throw an
exception, how to ensure that it does, and how to make sure it throws the right type.
Good luck and have fun!