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.

104 Exploring and Analyzing Finite <strong>Model</strong> Programs<br />

<strong>Model</strong>ProgramSimple m = <strong>Model</strong>Name.Factory.Create(); // factory method<br />

Sequence frontier = new Sequence(m.InitialState);<br />

Sequence explored = new Sequence(); // empty<br />

Set transitions = new Set(); // empty<br />

while (!frontier.IsEmpty)<br />

{<br />

State current = frontier.Head; // choose first element of frontier<br />

frontier = frontier.Tail; // all but first element of frontier<br />

explored = explored.AddLast(current); // append current to explored<br />

foreach (Action a in m.GetActions(current))<br />

{<br />

State next = m.GetNextState(current, a);<br />

if (!frontier.Contains(next) && !explored.Contains(next))<br />

{<br />

frontier = frontier.AddLast(next); // append for breadth-first<br />

}<br />

transitions = transitions.Add(Transition(current, a, next));<br />

}<br />

}<br />

Figure 6.7. An exhaustive exploration algorithm.<br />

The exploration algorithm also uses these types, which are defined in the modeling<br />

library (Chapter 10):<br />

Set The unordered collection of elements of type T, with constructors<br />

Set(), Set(x,y,z), and so on, and method Add, where s.Add(x) returns<br />

a new set that contains all the elements of set s and the element x.<br />

Sequence The ordered collection of elements of type T, with properties Head<br />

(the first element), Tail (all but the first element), and methods AddFirst (which<br />

pushes an element on the head) , and AddLast (which appends an element to<br />

end).<br />

Figure 6.7 shows an exhaustive exploration algorithm. (This simplified version<br />

is not the actual code used by the mpv tool.) The frontier is the collection of states<br />

that have been reached but whose enabled transitions have not yet been executed.<br />

(In Figure 6.5, states 1 and 2 are on the frontier; in Figure 6.6, states 1, 3, and 4<br />

are on the frontier.) When exploration begins, only the initial state is on the frontier.<br />

Exploration proceeds by executing transitions that are enabled on the frontier.<br />

When executing, a transition reaches a state that has not already been reached, that<br />

state is added to the frontier. When all of the transitions that are enabled in a state<br />

have been executed, that state is removed from the frontier and added to the collection<br />

of explored states. (In Figure 6.5, state 0 has been explored; in Figure 6.6,<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!