Question

I want to define a method that returns a sized lists. E.g.

my_method(): list of my_struct is { ... };

will obviously return a list of unknown size. The online documentation has no syntax definition for a sized list as return value.

Was it helpful?

Solution

List sizes are specified like:

my_list : list of int;
keep my_list.size() == 4;

You could wrapper such a list in a template struct and constrain the list inside the struct to a number like this:

<'

struct my_struct {
    data : int;
};

template struct FourElemWrpr of ( <first'type> ) {
    d : list of <first'type>;
    keep d.size() == 4;
};

extend sys {
    foo() : FourElemWrpr of int is {
        gen result;
        print result.d;
    };
    run() is also {
        var wrpr := foo();
        print wrpr.d;
    };
};

'>

When run on Specman 10, this yields:

Welcome to Specman Elite 
[...]

Generating the test with Pgen using seed 1...

Starting the test ...
Running the test ...
  result.d =
0.      -208970122
1.      -1768025704
2.      -65377588
3.      -723567746
  wrpr.d =
0.      -208970122
1.      -1768025704
2.      -65377588
3.      -723567746
No actual running requested.
Checking the test ...
Checking is complete - 0 DUT errors, 0 DUT warnings.

OTHER TIPS

When you define a struct field of a list type, you can also specifiy its size as follows:

struct list_wrapper {
    my_list[4]: list of int;
};

which can be used instead of:

struct list_wrapper {
    my_list: list of int;
    keep my_list.size() == 4;
};

This syntax is only available for fields. Not for local variables, methods return types, etc.

Also, notice that it's not a "fixed sized" list as it could seem. The declared size (4 in the above example) only means the initial size. Once created, elements can be added, removed, etc. like with any other list.

There is one important difference between the [size] notation and using the constraint. While a constraint only takes effect for generated fields when the containing struct is generated, the [size] notation takes effect also for fields of structs created by new, or for non-generateable fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top