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 165<br />

Sequences appear in models where it is important to distinguish elements based on<br />

order. For example, you could model a variable-length stack (LIFO) with operations<br />

Push and Pop using a sequence:<br />

static class Stack<br />

{<br />

static Sequence contents = Sequence.EmptySequence;<br />

}<br />

[Action]<br />

static void Push(int value)<br />

{<br />

contents = contents.AddFirst(value);<br />

}<br />

static bool PopEnabled() { return !contents.IsEmpty; }<br />

[Action]<br />

static int Pop()<br />

{<br />

int result = contents.Head;<br />

contents = contents.Tail;<br />

return result;<br />

}<br />

Order matters in this example: Pop must produce the value given by the most<br />

recent Push action.<br />

Creating sequences<br />

You can create a sequence by listing its elements as arguments to the constructor:<br />

Sequence ratings = new Sequence("Poor", "Average",<br />

"Excellent")<br />

You can also start with the empty sequence and add elements to the beginning or<br />

end programmatically:<br />

Sequence squares = Map.EmptySequence;<br />

for(int i = 0; i < 5; i += 1)<br />

squares = squares.AddLast(i * i);<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!