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.

160 <strong>Model</strong>ing Systems with Structured State<br />

[Action]<br />

static void RemoveClient(string client)<br />

{<br />

clients = clients.Remove(client);<br />

}<br />

This model program shows the basic operation of sets. The expression Set.<br />

EmptySet is a way to denote the empty set of strings. This is analogous to the<br />

expression string.Empty provided by the .NET framework that returns the empty<br />

string. <strong>We</strong> say “the” empty string because all empty strings are equal. 4 It is useful<br />

to think that there is just one empty string value, just as there is only one value for<br />

0 or 1. Similarly, there is just one empty set of strings.<br />

<strong>We</strong> should point out that in the set implementation provided by N<strong>Model</strong>, the<br />

values Set.EmptySet and Set.EmptySet are not equal. You can think<br />

of each set as having a subscript based on the type parameter inside the angle<br />

brackets. This is true even for subtypes: values of type Set and Set<br />

are never equal, even if they contain the same elements.<br />

Another thing to point out is that the Add and Remove operations return new set<br />

values. This is because set values, like .NET strings, are immutable. There is no way<br />

to change their values. However, we can update a state variable with the new value<br />

that results from adding or removing an element:<br />

clients = clients.Add(client);<br />

If you were to explore the model program shown above for the client/server<br />

system, you would notice that the following two traces result in the same end<br />

state:<br />

Trace 1 Trace 2<br />

AddClient("alice") AddClient("bob")<br />

AddClient("bob") AddClient("alice")<br />

<strong>Model</strong>ing complex state often needs mathematical data structures like sets. Using<br />

sets whenever order doesn’t matter can dramatically reduce the number of states you<br />

will have to consider for design analysis and testing. Sets are the bread and butter<br />

of experienced software modelers. They appear very often.<br />

Creating sets<br />

The easiest way to create a set is to list its elements in the constructor. The order in<br />

which arguments appear doesn’t matter and duplicates will be ignored:<br />

4 When we use the word “equals,” we mean equality in the sense of the Object.Equals method.<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!