90 min

Test design: certify the chamber controller

The full controller: a switch picks the limit, an if chain picks the state. Decision tables, repairs proved by a failing test, reading a test log, acceptance tests and the team mission.

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:

modetemperaturebatterystatewhy
N50401limit 80; 50 is below 70
N70402limit 80; 70 reaches 80 - 10
T70403limit 70; 70 reaches the limit
X20400no 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.

Run each of the four cases and follow the path through the switch and the if chain.Open full screen

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.
modelimitRUNWARNSHUTDOWN
N80below 7070 to 7980 and above
T70below 6060 to 6970 and above
S60below 5050 to 5960 and above

A battery below 15 gives SHUTDOWN in every mode, whatever the temperature.

Guided practice: complete the decision table

modelimittemperaturebatterystate
S604970RUN
S605070WARN
S606070SHUTDOWN
T706514SHUTDOWN
Xnone2070INVALID

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;
FaultFailing testExpectedObserved
no break after case 'N': N falls into T and gets limit 70N, 75 °CWARNSHUTDOWN
no default: for an unknown mode limit is never setX, 20 °CINVALIDanything
> instead of >= on both state linesT, 70 °CSHUTDOWNWARN

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:

TestInputExpectedObserved
D1N, 79 °C, 50%WARNWARN
D2N, 80 °C, 50%SHUTDOWNWARN
D3T, 70 °C, 50%SHUTDOWNSHUTDOWN
D4X, 20 °C, 50%INVALIDRUN

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 guesses appear one by one. Check each against all four rows.Open full screen

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

RuleRequirement
Valid inputmode N, T or S; temperature -40 to 120; battery 0 to 100 (all inclusive)
LimitN: 80 °C, T: 70 °C, S: 60 °C
Invalid inputany input out of range gives INVALID
Critical batterybattery below 15 gives SHUTDOWN
Heatat or above the limit: SHUTDOWN; within 10 °C below it: WARN; otherwise RUN
Outputexactly 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:

IDmodetemperaturebatteryexpected
A1N2050RUN
A2N7050WARN
A3N8050SHUTDOWN
A4S5050WARN
A5T3014SHUTDOWN
A6X2050INVALID
A7N12150INVALID
A8N-400SHUTDOWN

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).

The reference controller. Write your prediction first, then set the inputs and compare.Open full screen

How to work

  1. Model. Complete the decision table and write down the priority. Do not start coding until a teammate can explain the table.
  2. Implement. switch for the mode, if for validity and the ranges.
  3. 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

EnglishHebrewMeaning
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.