Kotlinlearncs.online LogoJava
Return to List

Test Writing: Recursive Range Sum

Created By: Geoffrey Challen
/ Version: 2021.10.0

Create method sum that accepts a single Int value and returns the sum of all the integers in the range 1..value as an Int. So, for example, given the input 10 you should return 55: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10. You can reject arguments less than or equal to 0 and ones greater than 128 by throwing an IllegalArgumentException.

You should submit a recursive solution. The range sum of 1 is 1, and this represents the base case. The range sum of n is n + the range sum of n - 1, and this represents the recursive step.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestRangeSum with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods