Back to TTCN-3 Refactoring Catalog
Subtype Basic Types
Use subtypes on basic type declarations to improve code flaw detection.
Motivation
It is possible to restrict types in TTCN-3 by using subtypes (such as lists, ranges and length restrictions) on basic types. This is good for two reasons. Firstly, it forces the test engineer to think about what he wants to achieve and what values he expects the type instantiations to have. Secondly, it allows early detection of code flaws either in the test suite or the SUT. A tool making a thorough semantic analysis of the code can partially detect when an assignment is outside its declared range and report this error for example. Other violations (e.g. range violations) can only be detected at test execution, but entirely undetected range violations can possibly lead to unexpected behavior that may lead to false test verdicts which is, needless to say, a bad thing for a test.
Mechanics
- Copy the source type declaration and give it a carefully decided subtype. Give it a temporary name. This is the target type declaration.
- Compile.
- Remove the source type declaration and rename the name of the subtyped target type declaration to the name of the source type declaration.
- Compile and validate.
- Consider if applying the Rename refactoring to the target type declaration may improve the meaningfulness of its name now that it is subtyped.
Example
The example uses subtyping for early detection when values are out of range. In the next listing, a variable v_byte of type integer is declared and then assigned 1024. A byte is of course only 8 bits long and its range is between 0 and 255. Hence, assigning 1024 to a variable that is supposed to be within this range is illegal and will probably result in unexpected behavior when not detected early by the compiler.
// ...
control {
// ...
var integer v_byte;
v_byte := 1024;
}
The following listing improves this weak point by introducing a type byte with a range restriction which is then used instead of type integer. The compiler is now able to detect this flaw at compilation.
// ...
type integer byte ( 0 .. 255 );
control {
// ...
var byte v_byte;
v_byte := 1024;
}
