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.

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

Structural equality may be contrasted with reference equality where two objects<br />

are equal if they occupy the same location of the computer’s memory. In objectoriented<br />

programming languages, reference equality is the default. However, structual<br />

equality may be explicitly provided by an implementation. For example, the<br />

string type in .NET languages provides structural equality.<br />

Sometimes we want to use data structures that go beyond the built-in value types. 3<br />

<strong>We</strong> can do this with custom data types. But in order to preserve the desirable property<br />

of state comparison, we need to use custom data types with structural equality.<br />

The C# language provides a keyword struct that may be used to declare data types<br />

with structural equality. There are some technical limitations here – for example, C#<br />

structs must be of a fixed length in memory. The N<strong>Model</strong> framework uses classes<br />

instead of structs for implementing structural data types.<br />

In order for equality of states to be well defined, we use compound values.<br />

Compound values are data types distinguished by their use of structural equality and<br />

their ability to handle variable-length and tree-structured data in addition to records<br />

of fixed size.<br />

The N<strong>Model</strong> modeling framework includes a mechanism that makes it easy to<br />

define custom structural types with a built-in base class called CompoundValue. This<br />

data type is a useful building block for many kinds of data structured data values.<br />

For example, a binary tree of integers could be defined in this way:<br />

class IntTree : CompoundValue<br />

{<br />

public readonly int value;<br />

public readonly IntTree left;<br />

public readonly IntTree right;<br />

}<br />

public IntTree(int value, IntTree left, IntTree right)<br />

{<br />

this.value = value;<br />

this.left = left;<br />

this.right = right;<br />

}<br />

All fields of a type that derives from CompoundValue must be marked readonly.<br />

This is because structural data types are immutable values, like strings and integers.<br />

3 The string type isn’t technically a value type in .NET terminology, but for simplicity we will<br />

use that term because strings are immutable and use structural equality. <strong>We</strong> ignore the issue of<br />

whether a value is stack-allocated.<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!