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.

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

An exception will be thrown if the requested key is not in the map. Normally,<br />

you will want to use the ContainsKey method to check that the key is in the map<br />

before doing the lookup.<br />

You can combine the ContainsKey check and lookup into a single operation using<br />

TryGetValue.<br />

static string LookupState(string city)<br />

{<br />

Map cityState =<br />

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

string state;<br />

return (cityState.TryGetValue(city, out state) ? state<br />

: "Unknown state");<br />

}<br />

Iterating through maps<br />

Maps support iteration through keys, values, or key value pairs:<br />

Map cityLength =<br />

new Map("Athens", 6, "Paris", 5};<br />

foreach(string city in cityLength.Keys) /* ... */<br />

foreach(int length in cityLength.Values) /* ... */<br />

foreach(Pair keyValuePair in cityLength) /* ... */<br />

The Keys and Values properties return sets. This means that when iterating<br />

through values of a map, values associated with more than one key will only be<br />

encountered once. The Pair data type is decribed in Section 10.3.6.<br />

10.3.3 Sequences<br />

A sequence is an ordered collection of (possibly repeating) elements. This is similar<br />

to the .NET List type, except that a sequence is an immutable value. Operations to<br />

add and remove elements result in new sequences instead of making changes to the<br />

sequence given as the argument.<br />

Another difference between sequences and the .NET list type is structural equality.<br />

Two sequences are equal if they contain the same elements, in the same order. Lists<br />

in .NET use reference equality instead of structural equality.<br />

Appendix A.2.4 provides a reference of sequence operations provided by the<br />

N<strong>Model</strong> library.<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!