27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.7.2 Code<br />

Systems with Finite <strong>Model</strong>s 85<br />

The model program has two data state variables, which also appear in the implementation:<br />

buffer and previous, the most recent temperature sample and the<br />

preceding temperature sample. These are numbers, so they could generate an “infinite”<br />

state space. To finitize our model, we limit both variables to just two values<br />

that represent temperatures that are within-range and out-of-range (by the criteria<br />

explained in Chapter 3, Section 3.6: the out-of-range value differs too much from<br />

the in-range value).<br />

The reactive system model program appears in Figures 5.12–5.18. Compare this to<br />

the implementation code in Chapter 3, Figures 3.2–3.4. The model program includes<br />

code that models the environment (timer, sensor, and supervisor), which does not<br />

correspond to any code in the implementation Controller class. The following<br />

paragraphs explain some features that might not be obvious.<br />

Both data state variables represent numbers, but buffer is a string and previous<br />

is a double. The conversion is performed by the CheckMessage action method.<br />

The particular values of the two temperature values InRange and OutOfRange do<br />

not matter, but they must differ by more than tol in CheckMessage.<br />

There are two kinds of action methods in the model program. The action<br />

methods that handle events appear first. They also appear in the implementation.<br />

The other action methods raise events. They do not appear in the implementation.<br />

The two kinds of actions alternate, using the phase control state variable.<br />

The events are enabled when phase == Phase.WaitForEvent and assign phase =<br />

Phase.HandleEvent. The handlers are enabled when phase == Phase.HandleEvent<br />

and assign phase = Phase.WaitForEvent.<br />

There must be a dummy handler NoHandler to model what happens in the implementation<br />

when no handler is enabled. This handler’s enabling condition NoHandlerEnabled<br />

returns true in states where every other handler’s enabling condition<br />

returns false. The handler itself does nothing but assigns phase.<br />

The two control state variables TimeoutScheduled and MessageRequested, which<br />

do not appear in the implementation, model the timer and the sensor. TimeoutScheduled<br />

is in the enabling condition for every time-out event and MessageRequested is<br />

in the enabling condition for the message event.<br />

There is only one Timeout event in the implementation, but there are three timeout<br />

event action methods in the model. Timeout models a time-out that arrives<br />

when the controller is not waiting for a message; !MessageRequested appears in<br />

its enabling condition. TimeoutMsgLost, and TimeoutMsgLate model time-outs that<br />

arrive when a message is expected; MessageRequested appears in their enabling<br />

conditions. TimeoutMsgLost models what happens in the implementation when the<br />

message is lost; it assigns MessageRequested = false so the message will never be<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!