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

10.3.5 Bags or multisets<br />

A bag, or multiset, is an unordered collection of possibly repeating elements. Unlike<br />

sets, an element may appear in a bag more than once. Bags are immutable values,<br />

but you can construct new bags by operations such as union and intersection.<br />

Appendix A.2.6 provides a list of bag operations provided by the N<strong>Model</strong> library.<br />

Like sets, bags are unordered. Two bags are equal if they contain the number of<br />

occurrences of the same elements. The number of occurrences of a given element is<br />

called its multiplicity.<br />

Bags are useful as a way to abstract the order of a sequence while still allowing<br />

for duplicate values.<br />

For example, you might use a bag to model reference counting. In reference<br />

counting, operations track whether an element may be deleted based on the number<br />

of references.<br />

static class ReferenceCounting<br />

{<br />

static Bag references = Bag.EmptyBag;<br />

static Set allocated = Set.EmptySet;<br />

[Action]<br />

static void AddReference(string obj)<br />

{<br />

references = references.Add(obj);<br />

allocated = allocated.Add(obj);<br />

}<br />

static bool RemoveReferenceEnabled(string obj)<br />

{<br />

return references.Contains(obj);<br />

}<br />

[Action]<br />

static void RemoveReference(string obj)<br />

{<br />

references = references.Remove(obj);<br />

}<br />

static bool DeleteObjectEnabled(string obj)<br />

{<br />

return allocated.Contains(obj) && !references.Contains(obj);<br />

}<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!