## Test Sequence and Assessment Syntax

This topic describes syntax used within Test Sequence and Test Assessment blocks, and Stateflow® charts. In the blocks, you use this syntax for test step actions, transitions, and assessments. In charts, you use this syntax in states and transitions.

For information on using the command-line interface to create and edit test sequence steps, transitions, and data symbols, see the functions listed under Test Sequences on the Test Scripts page.

Test Sequence and Test Assessment blocks use MATLAB® as the action language. You can also use strings, including string comparisons, in test sequence steps and transitions. You define actions, transitions, assessments with assessment operators, temporal operators, transition operators, signal generation functions, logical operators, and relational operators. Except for `verify`, Stateflow charts can use all operators in MATLAB or C as the action language. `verify` can be used only with MATLAB language and you cannot use strings in `verify` statements. For example:

• To output a square wave with a period of `10` sec:

`square(et/10)`
• To transition when `h` changes to `0`:

`hasChangedTo(h,0)`
• To verify that x is greater than y:

`verify(x > y)`

### Assessment Statements

To verify simulation, stop simulation, and return verification results, use assessment statements.

KeywordStatement SyntaxDescriptionExample
verify

`verify(expression)`

```verify(expression, errorMessage)```

```verify(expression, identifier, errorMessage)```

Assesses a logical expression. Optional arguments label results in the Test Manager and diagnostic viewer.

```verify(x > y,... 'SimulinkTest:greaterThan',... 'x and y values are %d, %d',... x,y)```
assert

`assert(expression)`

```assert(expression, errorMessage)```

Evaluates a logical expression. Failure stops simulation and returns an error. Optional arguments return an error message.

```assert(h==0 && k==0,... 'h and k must '... 'initialize to 0')```

Syntax in the table uses these arguments:

### Temporal Operators

To create an expression that evaluates the simulation time, use temporal operators. Variables used in signal conditions must be inputs, parameters, or constants in the Test Sequence block.

OperatorSyntaxDescriptionExample
et

`et(TimeUnits)`

The elapsed time of the test step in `TimeUnits`. Omitting `TimeUnits` returns the value in seconds.

The elapsed time of the test sequence step in milliseconds:

`et(msec)`
t

`t(TimeUnits)`

The elapsed time of the simulation in `TimeUnits`. Omitting `TimeUnits` returns the value in seconds.

The elapsed time of the simulation in microseconds:

`t(usec)`
after

`after(n, TimeUnits)`

Returns `true` if `n` specified units of time in `TimeUnits` elapse since the beginning of the current test step.

After 4 seconds:

`after(4,sec)`
before

`before(n, TimeUnits)`

Returns `true` until `n` specified units of time in `TimeUnits` elapse, beginning with the current test step.

Before 4 seconds:

`before(4,sec)`
duration

`ElapsedTime = duration (Condition, TimeUnits)`

Returns `ElapsedTime` in `TimeUnits` for which `Condition` has been `true`. `ElapsedTime` is reset when the test step is re-entered or when `Condition` is no longer `true`.

Return `true` if the time in milliseconds since `Phi > 1` is greater than 550:

`duration(Phi>1,msec) > 550`

Syntax in the table uses these arguments:

### Transition Operators

To create expressions that evaluate signal events, use transition operators. Common transition operators include:

OperatorSyntaxDescriptionExample
hasChanged
```hasChanged(u) ```

Returns `true` if `u` changes in value since the beginning of the test step, otherwise returns `false`.

`u` must be an input data symbol.

Transition when `h` changes:

`hasChanged(h)`
hasChangedFrom
```hasChangedFrom(u,A) ```

Returns true if `u` changes from the value `A`, otherwise returns false.

`u` must be an input data symbol.

Transition when `h` changes from `1`:

`hasChangedFrom(h,1)`
hasChangedTo
```hasChangedTo(u,B) ```

Returns true if `u` changes to the value `B`, otherwise returns false.

`u` must be an input data symbol.

Transition when `h` changes to `0`:

`hasChangedTo(h,0)`

### Signal Generation Functions

The following table lists common functions you can use in the Test Sequence block to create test signals, random number values, and natural exponents. It also describes the `latch` function, which saves and returns a specific value evaluated within a test sequence step.

Some signal generation functions use the temporal operator `et`, which is the elapsed time of the test step in seconds. For additional operators related to `et` that you can use in test sequence steps, see Temporal Operators

Note

Scaling, rounding, and other approximations of argument values can affect function outputs.

FunctionSyntaxDescriptionExample
`sin``sin(x)`

Returns the sine of `x`, where `x` is in radians.

A sine wave with a period of 10 sec:

`sin(et*2*pi/10)`
`cos``cos(x)`

Returns the cosine of `x`, where `x` is in radians.

A cosine wave with a period of 10 sec:

`cos(et*2*pi/10)`
square`square(x)`

Square wave output with a period of `1` and range `–1` to `1`.

Within the interval `0 <= x < 1`, `square(x)` returns the value `1` for `0 <= x < 0.5`and `–1` for ```0.5 <= x < 1```.

`square` is not supported in Stateflow charts.

Output a square wave with a period of `10` sec:

`square(et/10)`
sawtooth`sawtooth(x)`

Sawtooth wave output with a period of `1` and range `–1` to `1`.

Within the interval `0 <= x < 1`, `sawtooth(x)` increases.

`sawtooth` is not supported in Stateflow charts.

Output a sawtooth wave with a period of `10` sec:

`sawtooth(et/10)`
triangle`triangle(x)`

Triangle wave output with a period of `1` and range `–1` to `1`.

Within the interval `0 <= x < 0.5`, `triangle(x)` increases.

`triangle` is not supported in Stateflow charts.

Output a triangle wave with a period of `10` sec:

`triangle(et/10)`
ramp`ramp(x)`

Ramp signal of slope `1`, returning the value of the ramp at time `x`.

`ramp(et)` effectively returns the elapsed time of the test step.

`ramp` is not supported in Stateflow charts.

Ramp one unit for every 5 seconds of test step elapsed time:

`ramp(et/5)`
heaviside`heaviside(x)`

Heaviside step signal, returning `0` for `x < 0` and `1` for ```x >= 0```.

`heaviside` is not supported in Stateflow charts.

Output a heaviside signal after `5` seconds:

`heaviside(et-5)`
`exp``exp(x)`

Returns the natural exponential function, ${e}^{x}$.

An exponential signal progressing at one tenth of the test step elapsed time:

`exp(et/10)`
`rand``rand`

Uniformly distributed pseudorandom values

Generate new random values for each simulation by declaring `rand` extrinsic with `coder.extrinsic`. Assign the random number to a local variable. For example:

```coder.extrinsic('rand') nr = rand sg = a + (b-a)*nr```
`randn``randn`

Normally distributed pseudorandom values

Generate new random values for each simulation by declaring `randn` extrinsic with `coder.extrinsic`. Assign the random number to a local variable. For example:

```coder.extrinsic('randn') nr = randn sg = nr*2```
latch`latch(x)`

Saves the value of `x` at the first time `latch(x)` evaluates in a test step, and subsequently returns the saved value of `x`. Resets the saved value of `x` when the step exits. Reevaluates `latch(x)` when the step is next active.

`latch` is not supported in Stateflow charts.

Latch `b` to the value of `torque`:

`b = latch(torque)`

### Logical Operators

You can use logical connectives in actions, transitions, and assessments. In these examples, `p` and `q` represent Boolean signals or logical expressions.

OperationSyntaxDescriptionExample

Negation

`~p`

not `p`

`verify(~p)`

Conjunction

`p && q`

`p` and `q`

`verify(p && q)`

Disjunction

`p || q`

`p` or `q`

`verify(p || q)`

Implication

`~p || q`

if `p`, `q`. Logically equivalent to implication `p``q`.

`verify(~p || q)`

Biconditional

`(p && q) || (~p && ~q)`

`p` and `q`, or not `p` and not `q`. Logically equivalent to biconditional `p``q`.

`verify((p && q) || (~p && ~q))`

### Relational Operators

You can use relational operators in actions, transitions, and assessments. In these examples, `x` and `y` represent numeric-type variables.

Using `==` or `~=` operators in a `verify` statement returns a warning when comparing floating-point data. Consider the precision limitations associated with floating-point numbers when implementing `verify` statements. See Floating-Point Numbers. If you use floating-point data, consider defining a tolerance for the assessment. For example, instead of `verify(x == 5)`, verify `x` within a tolerance of `0.001`:

`verify(abs(x-5) < 0.001)`

Operator and SyntaxDescriptionExample
`x > y`Greater than`verify(x > y)`
`x < y`Less than`verify(x < y)`
`x >= y`Greater than or equal to`verify(x >= y)`
`x <= y`Less than or equal to`verify(x <= y)`
`x == y`Equal to`verify(x == y)`
`x ~= y`Not equal to`verify(x ~= y)`