Kotlinlearncs.online LogoJava
Return to List

Test Writing: Rotate Array

Created By: Justin Maier
/ Version: 2024.9.0

Write a function rotateArray that takes two arguments: first, an int array, and second, an int we will call steps, representing the number of steps to rotate the array by. The function should return a new array with the same values as the original argument, but with the elements rotated by steps spaces. You should not modify the original array.

If steps is positive, the returned array should be rotated to the right by that many places. For example, if the array is {1, 2, 3, 4, 5} and steps is 2, you should return the array {4, 5, 1, 2, 3}.

If steps is negative, the returned array should be a rotation to the left of the original. For example, if the original array is {1, 2, 3, 4, 5} and steps is -2, you should return the array {3, 4, 5, 1, 2}.

If steps is zero, the returned array should match the original.

Additionally, you should use an assert statement to check that the array is not empty.

Test Design Challenge

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

  • Provide a 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