Kotlinlearncs.online LogoJava
Return to List

Test Writing: Cache Handler

Created By: Pranav Narayanan
/ Version: 2024.9.0

Write a method cacheHandler that takes a 2D String array of cache entries, and returns a Map<String, Int> containing every cache key and its most recent value.

The String array has 3 columns: the cache id, the timestamp of that entry(hh:mm:ss), and the Int value at that time, in that order. Your Map should contain only the most recent entry for any given key based on the timestamps. In the case where you re-encounter the same timestamp for the same key, do NOT change the value.

You may assume that the input follows this format, although it may be empty. You will need the String.toInt() method to convert a String to an Int value.

Hint: How can you keep track of the most recent time for every key?

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 Kotlin's assert or check methods
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/kotlin/cache-handler/[email protected]