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.

Systems with Complex State 163<br />

system. For example, a server may have an operation that assigns integer priorities<br />

to its active clients:<br />

static Map priority = Map.EmptyMap;<br />

[Action]<br />

static void SetPriority(string client, int p)<br />

{<br />

priority = priority.Override(client, p);<br />

}<br />

The Override method produces a new map by substituting a key/value pair. If the<br />

key given as the argument is not in the map, then a new key/value pair is added. In<br />

this example we set the variable priority to contain the value of the new map.<br />

Creating maps<br />

If your map contains five or fewer key/value pairs, you can use a map constructor<br />

that takes keys and values as arguments:<br />

Map cityId = new Map("Athens", 1, "Rome", 2,<br />

"Paris", 3, "New York", 4);<br />

Assert.AreEqual(cityId["Paris"], 3);<br />

A common way to build a map is to start with the empty map and add elements<br />

programmatically:<br />

Map idCity = Map.EmptyMap;<br />

string[] cities =<br />

new string[]{"Athens", "Rome", "Paris", "New York"};<br />

for(int i = 0; i < cities.Length; i += 1)<br />

idCity = idCity.Add(i + 100, city[i]);<br />

Assert.AreEqual(idCity[102], "Paris");<br />

Map lookup operations<br />

The lookup operations for a map are very similar to those of a .NET dictionary.<br />

If you need to look up the value of a key, you may use the C# indexer syntax:<br />

Map cityState =<br />

new Map("Athens", "GA", "Paris", "TX"};<br />

Assert.AreEqual(cityState["Paris"], "TX");<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!