Skip to content

Checkpoint: Tested Progress

The Course
•
35 min study

A progress display shows two numbers: what percentage of the goal is done, and how many more sessions you need to book to reach it. Both are easy to get almost right. Forget to cap the percentage and a big day reports 150%. Divide the minutes left by the session length and 50 minutes left in 25-minute sessions is fine, but 60 minutes left becomes 2 sessions, 10 minutes short. In this checkpoint you write two functions you have not seen before, from their contracts alone, and you write the tests first so they can catch exactly those mistakes.

The contracts, for a positive goal, a positive session length and a done value of zero or more:

  • sessions_needed(goal, done, length) is how many sessions of length minutes you still need to reach goal. A partial session counts as a whole one, and once done reaches goal the answer is 0.
  • percent_done(goal, done) is the whole percentage of the goal completed, rounded down, and never more than 100.

Predict and run

Predict before running C++
#include <iostream>

bool at_goal(int goal, int done) {
    return done >= goal;
}

int main() {
    std::cout << at_goal(60, 60) << " " << at_goal(60, 59) << "\n";
    return 0;
}
Check your prediction
Text
1 0

Now imagine at_goal used == instead of >=. Both of these calls would still print the same thing. Only a case above the goal, such as at_goal(60, 70), exposes the difference. A test set that never goes past the goal cannot tell the two versions apart.

What you will use

  • Functions that return values. Each helper calculates and returns; only main prints.
  • An early return. Handle a reached or exceeded goal before subtracting or dividing.
  • Integer arithmetic in a safe order. Integer division discards the remainder: decide, for each function, whether that is the rounding the contract asks for.
  • assert from <cassert>. One assertion per case, with the expected value worked out by hand.
  • Boundary cases. Zero done, an exact multiple of the session length, just below the goal, exactly the goal, and past the goal.

Plan before you type

  1. For each function, work out the expected answers from the contract by hand, before writing any body. Use the cases listed in the exercise.
  2. Write the assertions in main and give each function a deliberately simple body that returns 0. Run and note which cases fail.
  3. Implement sessions_needed and run until its assertions pass.
  4. Implement percent_done and run until all assertions pass.
  5. Print Progress checks passed only after the last assertion.
Prove it can fail

With everything passing, break each function on purpose and check that an assertion stops the run. Delete the cap in percent_done: the 90-minute case should fail. Change the order to done / goal * 100: the 30-minute case is the first to fail, and assert stops there. In sessions_needed, return the minutes left divided by the length with no rounding up: the case with 60 minutes left should fail. Then try adding 1 after dividing instead: now the exact-multiple case with 50 minutes left should fail. If no assertion fails for a broken version, your tests are missing a case.

Mistakes worth checking for

  • sessions_needed(60, 0, 25) returns 2: the division rounded down, so the last 10 minutes got no session.
  • sessions_needed(60, 10, 25) returns 3: you always added one session, even when the minutes left were an exact multiple of the length.
  • sessions_needed(60, 120, 25) returns -1: the subtraction ran without the reached-goal check, so 60 minutes over the goal became -60 left, and (-60 + 24) / 25 is -1. A case only slightly past the goal can hide this: with done 90, (-30 + 24) / 25 truncates toward zero and happens to give 0. That is why the list goes well past the goal.
  • percent_done(60, 30) returns 0: the division happens before the multiplication, so 30 / 60 was truncated to 0.
  • percent_done(60, 40) returns 67: you rounded to nearest (66.7 becomes 67), or used floating point and rounded. The contract says whole percent, rounded down, which is 66. A result of 99 for 59 minutes means you rounded up: 5900 / 60 is 98.33, and rounded down that is 98.
  • The program prints nothing and the confirmation never appears: an assertion failed. Read which expression it names before changing code.
  • Every assertion passes even with a broken body: the expected values were copied from the function's own output instead of worked out from the contract.

Practice

Exercise

Your turn

Implement sessions_needed(goal, done, length) and percent_done(goal, done) from the contracts above. Assert, with a goal of 60:

  • sessions_needed with 25-minute sessions: done 0 gives 3, 10 gives 2, 35 gives 1, 59 gives 1, 60 gives 0, 120 gives 0
  • percent_done: done 0 gives 0, 30 gives 50, 40 gives 66, 59 gives 98, 60 gives 100, 90 gives 100

When all of them pass, print Progress checks passed.

Your workspace C++
#include <iostream>
#include <cassert>

int main() {
    // Write both functions above main and your assertions here.
    return 0;
}
Hint 1: the idea

Both functions share the same first step: if the goal is reached or exceeded, return the capped answer at once. Only the in-progress case needs arithmetic. For sessions, you met the round-up problem in the previous lesson, with hours instead of a session length.

Hint 2: a first step

Write both functions with the body return 0; and all twelve assertions in main. Run it: the first failure is sessions_needed(60, 0, 25) == 3. Then add the reached-goal check to sessions_needed and work on that one function until its six assertions pass.

Show one solution
Run the solution C++
#include <iostream>
#include <cassert>

int sessions_needed(int goal, int done, int length) {
    if (done >= goal) {
        return 0;
    }
    int left = goal - done;
    return (left + length - 1) / length;
}

int percent_done(int goal, int done) {
    if (done >= goal) {
        return 100;
    }
    return done * 100 / goal;
}

int main() {
    assert(sessions_needed(60, 0, 25) == 3);
    assert(sessions_needed(60, 10, 25) == 2);
    assert(sessions_needed(60, 35, 25) == 1);
    assert(sessions_needed(60, 59, 25) == 1);
    assert(sessions_needed(60, 60, 25) == 0);
    assert(sessions_needed(60, 120, 25) == 0);

    assert(percent_done(60, 0) == 0);
    assert(percent_done(60, 30) == 50);
    assert(percent_done(60, 40) == 66);
    assert(percent_done(60, 59) == 98);
    assert(percent_done(60, 60) == 100);
    assert(percent_done(60, 90) == 100);
    std::cout << "Progress checks passed\n";
    return 0;
}
Text
Progress checks passed

The early returns enforce the caps, and they also protect the arithmetic: past the goal, goal - done would be negative. For sessions, left + length - 1 is the same round-up trick as the hours exercise: any remainder from 1 to length - 1 crosses into one more whole session, while an exact multiple does not. For the percentage, multiplying first keeps the precision until the final division: 40 times 100 is 4000, and 4000 divided by 60 is 66 with the remainder discarded, which is exactly the rounding down the contract asks for. Negative values and a session length of 0 are outside the contract, which is why a real program validates input before calling helpers like these. On Day 4 you will apply the same habits to whole collections of durations.

Was this article helpful?

Your response is saved on this device.