Testing Generic Compilation Units

Component Testing for Ada

Types and objects in a generic unit depend on generic formal parameters that are not known by the Test Script Compiler. Therefore,  Component Testing for Ada cannot directly test a generic package.

To test a generic package, you must first instanciate the package and then call the instance. Such instances must appear in compilation units or at the beginning of the test script (in any case before the BEGIN statement), as follows:

WITH <generic>;

PACKAGE <instance> IS NEW <generic> (...);

Depending on the nature of the source code under test, there are two ways to test an instanciation of a generic package:

In the .ptu test script, after the generic instanciation, add the WITH <instance>; statement before the  BEGIN keyword. For example:

WITH <Generic_Package>;

PACKAGE <Instance> IS NEW <Generic_Package> (...);

WITH <Instance>;

BEGIN

where <Generic_Package> is the name of the generic unit under test, and <Instance> is the name of the instanciated unit from the generic.

In the .ptu test script, specify the generic package, the instance package and the test procedure on the BEGIN line. For example:

WITH <Generic_Package>;

PACKAGE <Instance> IS NEW <Generic_Package> (...);

BEGIN GENERIC(<Generic_Package>, <Instance>), <Procedure_Name>

where <Generic_Package> is the name of the generic unit under test, and <Instance> is the name of the instanciated unit from the generic. The <Procedure_Name> parameter is not mandatory. Component Testing uses Attol_Test by default.

This instruction generates the test body into <Procedure_Name> as a separate unit of the Generic package as well as the WITH to this instance, as requested by the test body.

If specified, <Procedure_Name> must be part of the generic package as separate procedure.

Example

Consider the following Ada compilation unit:

Generic

Type t is private ;

Procedure swap(x,y :in out t) ;

Procedure swap(x,y :in out t) is

Z :t ;

Begin

  Z := x ;

  X := y;

  Y := z;

End swap ;

With swap ;

Procedure swap_integer is new swap(integer) ;

 

You can test the swap_integer procedure just like any other procedure:

HEADER swap_integer,,

#with swap_integer;

BEGIN

SERVICE swap_integer

  #x,y:integer;

  TEST 1

    FAMILY nominal

    ELEMENT

      VAR x , init = 1 , ev = 4

      Var Y , init=4 ,ev = 1

      #swap_integer(x,y) ;

    END ELEMENT

  END TEST

END SERVICE

 

Related Topics

Simulating Generic UnitsAdvanced Ada Testing