תכנון בדיקות: מאשרים את בקר תא החום
הבקר המלא: switch בוחר את הגבול ושרשרת if בוחרת את המצב. טבלאות החלטה, תיקון שמוכח בבדיקה שנכשלת, קריאת יומן בדיקות, בדיקות קבלה ומשימת הצוות.
השיעור הזה כתוב באנגלית בלבד.
The whole controller
Lessons 1 and 2 gave all the pieces. Now they work together: a switch turns the mode into a temperature limit, and an if chain turns the sensors into a state.
switch (mode) {
case 'N': limit = 80; break;
case 'T': limit = 70; break;
case 'S': limit = 60; break;
default: limit = -1;
}
if (limit == -1)
state = 0; /* INVALID */
else if (battery < 15 || temperature >= limit)
state = 3; /* SHUTDOWN */
else if (temperature >= limit - 10)
state = 2; /* WARN */
else
state = 1; /* RUN */State codes: 0 invalid, 1 run, 2 warn, 3 shutdown. Predict before you read the answers:
| mode | temperature | battery | state | why |
|---|---|---|---|---|
| N | 50 | 40 | 1 | limit 80; 50 is below 70 |
| N | 70 | 40 | 2 | limit 80; 70 reaches 80 - 10 |
| T | 70 | 40 | 3 | limit 70; 70 reaches the limit |
| X | 20 | 40 | 0 | no case: default sets limit to -1 |
The invalid mode is checked first. If it were checked later, the limit -1 would reach the comparisons: 20 >= -1 is true, and an unknown mode would shut the chamber down for no reason, or worse, hide a fault.
One rule, three modes
The rule is the same in every mode; only the limit moves:
- SHUTDOWN from the limit up:
temperature >= limit. - WARN in the 10 °C below the limit:
temperature >= limit - 10. - RUN below that.
| mode | limit | RUN | WARN | SHUTDOWN |
|---|---|---|---|---|
| N | 80 | below 70 | 70 to 79 | 80 and above |
| T | 70 | below 60 | 60 to 69 | 70 and above |
| S | 60 | below 50 | 50 to 59 | 60 and above |
A battery below 15 gives SHUTDOWN in every mode, whatever the temperature.
Guided practice: complete the decision table
| mode | limit | temperature | battery | state |
|---|---|---|---|---|
| S | 60 | 49 | 70 | RUN |
| S | 60 | 50 | 70 | WARN |
| S | 60 | 60 | 70 | SHUTDOWN |
| T | 70 | 65 | 14 | SHUTDOWN |
| X | none | 20 | 70 | INVALID |
Say in one sentence why the state changes from one row to the next: at 50 the temperature reaches limit - 10, so RUN becomes WARN; at 60 it reaches the limit, so WARN becomes SHUTDOWN; a battery of 14 is critical whatever the temperature.
Three faults, three failing tests
This version has three independent faults:
switch (mode) {
case 'N': limit = 80;
case 'T': limit = 70; break;
case 'S': limit = 60; break;
}
if (temperature > limit)
state = 3;
else if (temperature > limit - 10)
state = 2;
else
state = 1;| Fault | Failing test | Expected | Observed |
|---|---|---|---|
no break after case 'N': N falls into T and gets limit 70 | N, 75 °C | WARN | SHUTDOWN |
no default: for an unknown mode limit is never set | X, 20 °C | INVALID | anything |
> instead of >= on both state lines | T, 70 °C | SHUTDOWN | WARN |
Notice the third test uses mode T. With mode N, fault 1 changes the limit to 70 and hides fault 3. That is why every fault needs its own test, chosen so that no other fault gets in the way.
The rule for repairs: first the test that fails, then the fix, then the same test passes. A repair without a failing test is a guess. Visual Studio also helps here: it warns that limit may be used without being initialised.
Reading a test log
A test log lists each test with its expected and observed output. This one came back from the lab:
| Test | Input | Expected | Observed |
|---|---|---|---|
| D1 | N, 79 °C, 50% | WARN | WARN |
| D2 | N, 80 °C, 50% | SHUTDOWN | WARN |
| D3 | T, 70 °C, 50% | SHUTDOWN | SHUTDOWN |
| D4 | X, 20 °C, 50% | INVALID | RUN |
Work like a detective. Every guess must fit every row:
- D2, guess 1:
>instead of>=at the limit. But D3 is exactly at T's limit and passes. A wrong operator would fail there too, so this guess does not fit the whole log. - D2, guess 2: the limit for N is wrong, for example 81 instead of 80. That fits D1, D2 and D3. Confirming test: N at 70 °C should give WARN; with a limit of 81 it gives RUN.
- D4: an invalid mode is not caught: a missing
default, or a limit of -1 that is never checked. Confirming test: Z at 100 °C should give INVALID.
A log narrows the suspects. It does not prove which line is wrong: different bugs can produce the same log. Each guess needs one more test, and then a look at the code.
The team mission: certify the controller
Teams of 2 or 3: a specification reader, an implementation driver and a test lead. The deliverable is a C program, a completed decision table, and a test report with expected and observed results.
Specification
| Rule | Requirement |
|---|---|
| Valid input | mode N, T or S; temperature -40 to 120; battery 0 to 100 (all inclusive) |
| Limit | N: 80 °C, T: 70 °C, S: 60 °C |
| Invalid input | any input out of range gives INVALID |
| Critical battery | battery below 15 gives SHUTDOWN |
| Heat | at or above the limit: SHUTDOWN; within 10 °C below it: WARN; otherwise RUN |
| Output | exactly one of INVALID, RUN, WARN, SHUTDOWN |
If several inputs are invalid at once, the answer is still just INVALID.
Order of the checks. The table turns into an order: (1) is the input valid? (2) the switch picks the limit, (3) is the battery critical? (4) which heat band? Each step runs only if the steps before it did not already decide.
Acceptance tests. The program must pass all eight:
| ID | mode | temperature | battery | expected |
|---|---|---|---|---|
| A1 | N | 20 | 50 | RUN |
| A2 | N | 70 | 50 | WARN |
| A3 | N | 80 | 50 | SHUTDOWN |
| A4 | S | 50 | 50 | WARN |
| A5 | T | 30 | 14 | SHUTDOWN |
| A6 | X | 20 | 50 | INVALID |
| A7 | N | 121 | 50 | INVALID |
| A8 | N | -40 | 0 | SHUTDOWN |
A8 is the interesting one: -40 °C and a battery of 0 are both valid. The battery is critical, so the answer is SHUTDOWN, not INVALID. Then add at least four tests of your own, each with its purpose (which boundary, which side of it).
How to work
- Model. Complete the decision table and write down the priority. Do not start coding until a teammate can explain the table.
- Implement.
switchfor the mode,iffor validity and the ranges. - Certify. Run the acceptance tests and your own boundary tests. Record expected and observed output side by side.
Grading (20 points): decision table covers priorities and boundaries 4; correct structure and exact output 6; acceptance tests pass 4; four tests of your own, each with a purpose 3; readable names, braces and explanation 3. A program that still fails can earn the table and testing points.
Complication card (for teams that finish early): a new input sensor_ok is 1 when the temperature reading can be trusted and 0 when the sensor reports a fault. When it is 0, the answer is INVALID whatever the other values are. Where does this check go? (First, before everything else.) Which test proves it? (For example sensor_ok 0, N, 95 °C, battery 10: everything else says SHUTDOWN, the answer must be INVALID.)
Debrief questions
- Where does each solution check that the input is valid?
- Can a later assignment overwrite a safety state?
- Which boundaries can you see in the code?
- Would a technician understand why switch was used?
- Which test gives the strongest evidence that exactly 80 °C is right?
Key words
| English | Hebrew | Meaning |
|---|---|---|
| specification | מפרט | what the program must do |
| input validation | בדיקת תקינות קלט | rejecting impossible input first |
| priority | סדר עדיפויות | which rule wins when several apply |
| acceptance test | בדיקת קבלה | a test the customer requires the program to pass |
| test log | יומן בדיקות | each test with its expected and observed output |
| failing test | בדיקה שנכשלת | a test that shows a fault before it is repaired |
| fault | תקלה | the wrong part of the code |
| hypothesis | השערה | a guess that one more test can confirm |
Practice
All 14 challenges and 32 coding problems, with answers, are on the practice page. Challenges 9, 12, 13 and 14 and problems 25-32 fit this lesson.