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.

Systems with Complex State 161<br />

Set cities = new Set("Athens", "Rome", "Athens",<br />

"Paris", "New York", "Seattle");<br />

Assert.AreEqual(cities.Count, 5);<br />

You can also create a set from an IEnumerable object. Many .NET data types<br />

support enumeration by means of IEnumerable, and this can act as an interface<br />

between the data types used for modeling and system data types. Here is an example:<br />

static Set ConvertToSet(IEnumerable list)<br />

{<br />

return new Set(list);<br />

}<br />

The order of elements in the enumeration doesn’t matter, and duplicate elements<br />

will be ignored.<br />

Note that sets themselves implement the IEnumerable interface, so they can be<br />

used iteratively:<br />

static int CountOdd(Set s)<br />

{<br />

int result = 0;<br />

foreach(int i in s)<br />

if (i % 2 == 1) result += 1;<br />

return result;<br />

}<br />

You can expect iteration over sets to occur in an arbitrary order.<br />

Set properties and queries<br />

There are several properties defined for sets. You can use IsEmpty to determine if the<br />

set has no elements. The Count property returns the number of elements contained<br />

in a set.<br />

You can use the Contains method to see whether a given element is in the set.<br />

Set cities = new Set("Athens", "Rome", "Athens"};<br />

Assert.IsFalse(cities.IsEmpty);<br />

Assert.AreEqual(cities.Count, 2);<br />

Assert.IsTrue(cities.Contains("Rome"));<br />

Assert.IsFalse(cities.Contains("New York"));<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!