Kotlinlearncs.online LogoJava
Return to List

Test Writing: Insertion Sort (int)

Created By: Geoffrey Challen
/ Version: 2021.4.0

Create a public class named InsertionSorter. It should provide one class method sort. sort accepts an array of ints and sorts them in ascending order. You should sort the array in place, meaning that you modify the original array, and return the number of swaps required to sort the array as an int. That's how we'll know that you've correctly implemented insertion sort. If the array is null you should throw an IllegalArgumentException.

To receive credit implement insertion sort as follows. Have the sorted part start at the left and grow to the right. Each step takes the left-most value from the unsorted part of the array and move it leftward, swapping elements until it is in the correct place. Do not swap equal values. This will make your sort unstable and cause you to fail the test suites.

Test Design Challenge

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

  • Provide a public class named TestInsertionSorter 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