Kotlinlearncs.online LogoJava
Return to List

Test Writing: Mergesort

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create a public class named Mergesort that provides a single instance method (this is required for testing) named mergesort. mergesort accepts an array of ints and returns a sorted (ascending) array. You should not modify the passed array. If the array that is passed is null you should throw an IllegalArgumentException.

Mergesort should extend Merge, and its parent provides several helpful methods:

  • int[] merge(int[] first, int[] second): this merges two sorted arrays into a second sorted array. If either array is null it throws an IllegalArgumentException, so don't call it on null arrays.
  • int[] copyOfRange(int[] original, int from, int to): this acts as a wrapper on java.util.Arrays.copyOfRange, accepting the same arguments and using them in the same way.

(You can't use java.util.Arrays in this problem for reasons that will become obvious if you inspect the rest of the documentation...)

Note that you do need to use merge and call it the correct number of times. This will be tested during grading. You should use an array of size 1 or 0 as your base case.

Test Design Challenge

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

  • Provide a public class named TestMergesort 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 Java's assert method