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.

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

You can construct a sequence from any value that supports IEnumerable:<br />

static Sequence ConvertToSequence(IEnumerable list)<br />

{<br />

return new Sequence(list);<br />

}<br />

Sequence queries<br />

You can access the first and last element of a sequence using the Head and Last<br />

properties.<br />

The subsequence of all elements except the first is the Tail. The subsequence of<br />

all elements except the last is the Front of the sequence.<br />

Sequence squares = new Sequence(0, 1, 4, 9, 16, 25, 36);<br />

Assert.AreEqual(squares.Head, 0);<br />

Assert.AreEqual(squares.Last, 36);<br />

Assert.AreEqual(squares.Tail.Head, 1);<br />

Assert.AreEqual(squares.Front.Last, 25);<br />

Assert.AreEqual(squares.Tail.Tail.Head, 4);<br />

Sequences also provide for random access using the C# indexer. However, if your<br />

primary mode of access is random, you should consider using the ValueArray type<br />

instead of a sequence.<br />

10.3.4 Value arrays<br />

A value array is an ordered collection of (possibly repeating) elements. It is similar<br />

to the .NET Array type, except that a value array is immutable. Unlike a .NET array,<br />

a value array uses structural equality.<br />

Sequences and value arrays are similar in the way that .NET lists and arrays are<br />

similar. In other words, sequences are best for list-like iterative construction and<br />

access. Value arrays work best when random access is the most common form of<br />

access and when incrementally adding elements is not common.<br />

Sequences appear more often than value arrays in most models, especially when<br />

small numbers of elements are involved. Value arrays are useful for larger data sets<br />

that require a lot of random access. Most models will not require them.<br />

Appendix A.2.5 lists value array operations provided by the N<strong>Model</strong> library.<br />

Value arrays are created from a .NET array:<br />

string[] ratingsArr = new string[]{"Poor", "Average", "Excellent"};<br />

ValueArray ratings = new ValueArray(ratingsArr);<br />

Assert.AreEqual(ratings[2], "Excellent");<br />

Random access is supported through the C# indexer.<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!