Write the Test
Stardate 55163.2 ยท UES Magnanimous
Briefing
Commander Raghunathan has four implementations of the shield clamp from Season 1, written by four different people, and she does not want to read them. She wants a test: a function that takes a clamp implementation, tries it, and says whether it is correct. Then she can run it on all four and only read the ones that fail.
A test is only worth anything if it can fail. So ARCHIE will hand your test a correct clamp and expect a yes, and then several broken ones, each broken in a specific way, and expect a no for each. If your test says yes to a broken clamp, it did not check the thing that was broken. That is the whole discipline of testing, in one mission.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขAccepts a correct clamp
- โขRejects a clamp with no lower bound
- โขRejects a clamp whose upper bound is off by one
- โขRejects a clamp that does nothing at all
- โขAccepts a second correct clamp written a different way
Objective
Write test_clamp(clamp): clamp is a function of one integer
argument. Return True if it correctly clamps to 0..100 inclusive (values
below 0 become 0, above 100 become 100, others unchanged), and False
otherwise.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Write test_clamp(clamp: fn(i32) -> i32) -> bool: return
true if clamp correctly limits values to 0..=100 (below 0
becomes 0, above 100 becomes 100, others unchanged), and false otherwise.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Call the clamp with a handful of chosen inputs and compare against what a correct clamp returns: something negative, zero, something in range, exactly 100, something above 100. Return whether every one matched. The inputs you choose are the test.
Debrief
Two of the four clamps failed your test. One was Ensign Tannenbaum's, which had no lower bound. The other was Chief T'Kala's, which returned 99 for anything over 100, because, she says, "nothing on this ship is ever actually at 100 percent". She has been asked to fix it and has said she will "think about it".