Kotlinlearncs.online LogoJava
Return to List

Test Writing: clamp

Created By: Justin Maier
/ Version: 2024.8.0

A "clamp" function is a function that takes three inputs: a lower bound, an upper bound, and the value that will be clamped. If this value is between the lower and upper bound, the original value is simply returned. If it is greater than the upper bound, the upper bound should be returned. Similarly, if it is less than the lower bound, the lower bound should be returned.

Write a function clamp that takes three int arguments: the value to be clamped, a lower bound, and an upper bound, in this order. It should return the "clamped" value of the first argument. You can assume that the lower bound will never be greater than the upper bound.

For example, clamp(6, 5, 10) should return 6 while clamp(11, 5, 10) should return 10.

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

Related Lessons

Stuck? You may find these lessons helpful: