This checkpoint turns a written rule into a function you design yourself, and then proves the function follows the rule. The study report will award bonus points: one point for every full 10 minutes studied, at most 5 points, and a negative duration earns 0. A contract like this says what the function promises for every input, not just for the example you happened to try.
The evidence is a set of assertions. A single positive example cannot prove much: a function that always returns 2 passes assert(bonus_points(25) == 2). Good tests are chosen so that each plausible mistake breaks at least one of them.
Predict and run
#include <stdio.h>
int accepted(int n) {
return n >= 1 && n <= 10;
}
int main(void) {
printf("%d %d\n", accepted(0), accepted(10));
return 0;
}Check your prediction
0 10 is just below the range and 10 is its top edge. Change the upper limit to 9 and the second number becomes 0, while a middle value such as 5 would not notice. The values that tell two versions of a rule apart are the ones at the edges.
What you will use
All of it is from Days 1 to 3; nothing here is new.
- A function with a contract. One
intparameter, oneintresult, and a rule that covers every input. - Integer division.
minutes / 10counts full tens, because the remainder is dropped. Remember from Day 1 that it truncates toward zero. - Early returns for the cases that do not follow the ordinary calculation.
assertwith expected values you calculated yourself, one per case, each written from the rule, not from the function's output.
Plan before you type
- List the edges of the rule on paper, and the expected points for each: where the first point starts, where the cap starts, and what "negative" means. Include at least one value on each side of each edge.
- Write those assertions and a success
printfinmain, before the function body. - Write the simplest function that could work, run it, and let a failing assertion tell you which part of the rule is still missing.
- Add one case of your own that you think could catch a different mistake.
Prove it can fail
When every assertion passes, break the function on purpose, one mistake at a time, and check that some assertion fails. Remove the cap. Round up instead of down with (minutes + 9) / 10. Remove the negative check. If removing the negative check does not fail, your negative case is too small: work out what -5 / 10 is in C and what -15 / 10 is. Then change the cap test from > 5 to >= 5 (or whatever your version of it is): everything still passes, and that is correct, because returning 5 when the points are exactly 5 changes nothing. Not every change is a bug, and a good test suite tells you which ones are.
Mistakes worth checking for
- 9 minutes earns a point. The division rounds up, or the function adds 1 somewhere. Only full tens count.
- 120 minutes earns 12. The cap is missing, or it checks the minutes against 5 instead of the points.
- A negative duration earns a negative score. The negative check is missing. It hides behind small negatives:
-5 / 10is 0, so only a case like -15 exposes it. - The compiler warns that control reaches the end of a non-void function. One path through the function has no
return. - All checks pass for a function that is plainly wrong. The expected values were copied from the function's own output. Recalculate them from the rule.
Practice
Your turn
Write int bonus_points(int minutes) for the rule above, with at least seven assertions: below the first point, at it, just below the cap, at the cap, far above it, zero, and a negative duration that would catch a missing negative check. Then print:
Bonus checks passed#include <stdio.h>
#include <assert.h>
int main(void) {
// Write your solution here.
return 0;
}Hint 1: the idea
There are three regimes: negative, ordinary and capped. Integer division already gives the ordinary answer; the other two need their own check, and each check needs a test that sits right at its edge.
Hint 2: a first step
Write only return minutes / 10; as the body, with all your assertions in main. Run it: one of your failures should be your far-above-the-cap case (for 120 it returns 12). If nothing fails, your cases are missing an edge.
Show one solution
#include <stdio.h>
#include <assert.h>
int bonus_points(int minutes) {
if (minutes < 0) {
return 0;
}
int points = minutes / 10;
if (points > 5) {
return 5;
}
return points;
}
int main(void) {
assert(bonus_points(0) == 0);
assert(bonus_points(9) == 0);
assert(bonus_points(10) == 1);
assert(bonus_points(49) == 4);
assert(bonus_points(50) == 5);
assert(bonus_points(120) == 5);
assert(bonus_points(-15) == 0);
printf("Bonus checks passed\n");
return 0;
}Bonus checks passed9 and 10 pin the first point, 49 and 50 pin the cap, and 120 proves the cap holds far above it. -15 is chosen with care: in C, -5 / 10 is already 0, so a test with -5 would pass even without the negative check, while -15 / 10 is -1. Capping the points rather than the minutes keeps the rule in the units it was written in. Tomorrow the report holds many sessions at once, in an array, and the empty collection becomes one of the boundaries you test.
Completion is saved in this browser only. It does not measure mastery.