Contracts, tests and decomposition
Write the contract of a function, test it on its boundaries, repair a broken function, split a program into one-job functions, and build the charging station controller.
A function contract
A battery charging station may charge only when two things are true: the battery voltage is between 12000 and 14500 millivolts, and the battery is not hotter than 45 °C. We put this rule in one function, is_voltage_safe.
Before writing the code, write the function's contract (חוזה פונקציה): the agreed input, output and rule.
is_voltage_safe | |
|---|---|
| Input | int voltage_mV, int temperature_c |
| Rule | 12000 <= voltage_mV <= 14500 and temperature_c <= 45 |
| Output | 1 when safe, 0 when not safe |
Every caller uses the same rule, and anyone who calls the function knows what to send and what comes back without reading its body. The rule can also be tested by itself.
The rule in C
int is_voltage_safe(int voltage_mV,
int temperature_c)
{
int voltage_ok;
int temperature_ok;
voltage_ok = voltage_mV >= 12000 &&
voltage_mV <= 14500;
temperature_ok = temperature_c <= 45;
return voltage_ok && temperature_ok;
}Two local variables hold the two parts of the rule. Each part can be read and checked on its own, and the return line joins them with &&.
Trace the call is_voltage_safe(13000, 46):
| Variable | Value | Why |
|---|---|---|
voltage_mV | 13000 | copy of the first argument |
temperature_c | 46 | copy of the second argument |
voltage_ok | 1 | 13000 is between 12000 and 14500 |
temperature_ok | 0 | 46 is more than 45 |
| returned value | 0 | 1 && 0 is 0 |
The voltage is fine, but the battery is too hot: the function returns 0.
Boundary tests
Mistakes in rules hide on the edges. If you write < where the rule needs <=, or 1450 instead of 14500, a test in the middle (13000 mV, 25 °C) still passes. So we test each edge three ways: just below, on, and just above. These are boundary tests (בדיקות גבול).
| voltage_mV | temperature_c | expected | what it checks |
|---|---|---|---|
| 11999 | 25 | 0 | just below the low edge |
| 12000 | 45 | 1 | on the low edge, and on the temperature edge |
| 14500 | 45 | 1 | on the high edge |
| 14501 | 25 | 0 | just above the high edge |
| 13000 | 46 | 0 | temperature just above 45 |
For a function that accepts 10 through 20, the smallest useful set is 9, 10, 20 and 21, plus one value in the middle such as 15.
Guided practice: temperature conversion
Fill in the return expression. The formula is F = C × 9/5 + 32.
double celsius_to_fahrenheit(double c)
{
return ______________________;
}
int main(void)
{
double result;
result = celsius_to_fahrenheit(25.0);
printf("%.1f\n", result);
return 0;
}Checks: 0.0 gives 32.0, 25.0 gives 77.0, and -40.0 gives -40.0.
Try it yourself first, then open the solution step by step.
Guided practice: larger value
int larger(int a, int b)
{
if (a >= b) {
return a;
}
return b;
}Trace three calls:
| Call | a >= b | Line that runs | Result |
|---|---|---|---|
larger(4, 9) | false | return b; | 9 |
larger(9, 4) | true | return a; | 9 |
larger(7, 7) | true | return a; | 7 |
A return ends the function at once: when a >= b, the line return b; never runs. The tie returns a, but the value is the same.
Debugging: average
This program should print 7.5. It has two separate faults. Find both before you edit.
int average(int a, int b);
int main(void)
{
double result = average(7, 8);
printf("%.1f\n", result);
return 0;
}
double average(int a, int b)
{
return (a + b) / 2;
}Try it yourself first, then open the solution step by step.
One function, one responsibility
The charging station controller has four jobs: read the input, check safety, select the charging current, and print a report. Written all in main, the jobs are tangled together, and to test the safety rule you must run everything.
Split into functions, each job gets a name and one place to change:
| Job | Function |
|---|---|
| reads input | read_input |
| checks safety | is_voltage_safe |
| selects current | charge_current_ma |
| prints report | print_report |
Splitting a program into functions is called decomposition (חלוקת תוכנית). The number of functions is not the goal. A separate function helps when it names one meaningful job, removes repetition, or makes testing easier.
A good name says what the function returns. Compare check, thing and is_temperature_safe: only the last one tells you what 1 means. A name that starts with is_ reads like a yes or no question, so the call reads like a sentence: if (is_temperature_safe(t)).
The charging station mission
This is the team mission of the unit (practice question 30).
- Write
int is_voltage_safe(int voltage_mV, int temperature_c): 1 for 12000 to 14500 mV and at most 45 °C, otherwise 0. - Write
int charge_current_ma(int voltage_mV): 2000 below 13500 mV; 1000 from 13500 through 14500; otherwise 0. mainreads the voltage and the temperature once.- Print
SAFE <current>when safe. Otherwise printSTOP 0. - Submit the code and the output of every acceptance test.
The acceptance tests (בדיקות קבלה) decide when the mission is done. Each boundary proves one part of the contract:
| voltage | temperature | safe | current | output |
|---|---|---|---|---|
| 11999 | 25 | 0 | 0 | STOP 0 |
| 12000 | 45 | 1 | 2000 | SAFE 2000 |
| 13500 | 25 | 1 | 1000 | SAFE 1000 |
| 14500 | 45 | 1 | 1000 | SAFE 1000 |
| 14501 | 25 | 0 | 0 | STOP 0 |
| 13000 | 46 | 0 | 0 | STOP 0 |
Key words
| English | Hebrew | Meaning |
|---|---|---|
| contract | חוזה פונקציה | the agreed input, output and rule of a function |
| boundary test | בדיקת גבול | a test just below, on or just above an edge |
| acceptance test | בדיקת קבלה | a required test the program must pass |
| decomposition | חלוקת תוכנית | splitting a program into functions |
| responsibility | אחריות | the one job a function does |
| integer division | חילוק שלמים | int / int drops the fraction: 15 / 2 is 7 |
| conflicting declarations | הצהרות סותרות | a prototype and a definition that do not match |
Practice
The full set of questions with answers is on the practice page. Homework: questions 15 to 22, each with its tests.