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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

184 Analyzing Systems with Complex State<br />

}<br />

{<br />

}<br />

static int counter = 0;<br />

[Action]<br />

static void ModularIncrement(int x)<br />

{ counter = (counter + x) % 5; }<br />

This model program is not explorable because the parameter x could take an “infinite”<br />

(very large) number of values. <strong>We</strong> sometimes call a model program like this a<br />

contract model program because it defines all possible behaviors of a system, even<br />

when those behaviors are too complex to be encoded as a finite state machine. In<br />

fact, many contract model programs will not be explorable.<br />

After thinking about the problem, we might say that it would be sufficient to<br />

explore the Counter model program with just five input values 0, 1, 2, 3, and 4. <strong>We</strong><br />

might justify this by saying that these values are representative of typical inputs and<br />

that after examining the logic, we see that other values would not produce additional<br />

states.<br />

If we are willing to restrict the scope of our analysis in this way then we can use<br />

the Domain attribute that was introduced on page 81 to create a finitely branching<br />

model program:<br />

namespace Counter<br />

{<br />

static class Contract<br />

{<br />

static int counter = 0;<br />

}<br />

}<br />

static readonly Set inputs = new Set(0, 1, 2, 3, 4);<br />

[Action]<br />

static void ModularIncrement([Domain("inputs")] int x)<br />

{ counter = (counter + x) % 5; }<br />

Adding a Domain attribute gives us an explorable model program with five possible<br />

actions in each state. 2 Unfortunately, this approach introduces a new problem. What<br />

2 Although we don’t make use of it in this example, it is possible to make the domain depend on<br />

the current state. To do this, you would just give the domain as a static method that references<br />

one or more state variables to compute the set of values returned.<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!