Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ryszard Rozak <[email protected]>
  • Loading branch information
RRozak committed Nov 22, 2024
1 parent 8a26ebc commit d5070e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
47 changes: 44 additions & 3 deletions test_regress/t/t_constraint_state.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,50 @@
// any use, without warranty, 2023 by Antmicro Ltd.
// SPDX-License-Identifier: CC0-1.0


`define check_rand(cl, field, cond) \
begin \
longint prev_result; \
int ok = 0; \
if (!bit'(cl.randomize())) $stop; \
prev_result = longint'(field); \
if (!(cond)) $stop; \
repeat(9) begin \
longint result; \
if (!bit'(cl.randomize())) $stop; \
result = longint'(field); \
if (!(cond)) $stop; \
if (result != prev_result) ok = 1; \
prev_result = result; \
end \
if (ok != 1) $stop; \
end

class Foo;
int x;
endclass

class Bar;
rand int y;
endclass

class Packet;
rand int rf;
int state;
rand int a;
rand Foo foo;
Bar bar;

constraint c { rf == state; }
constraint c1 { rf == state; }
constraint c2 { a > foo.x; a < bar.y; }

function new(int s, int x, int y);
state = s;
foo = new;
foo.x = x;
bar = new;
bar.y = y;
endfunction
endclass

module t (/*AUTOARG*/);
Expand All @@ -19,12 +57,15 @@ module t (/*AUTOARG*/);
int v;

initial begin
p = new;
p.state = 123;
p = new(123, 10, 20);
v = p.randomize();
if (v != 1) $stop;
if (p.rf != 123) $stop;

`check_rand(p, p.a, p.a > 10 && p.a < 20)
if (p.foo.x != 10) $stop;
if (p.bar.y != 20) $stop;

p.state = 234;
v = p.randomize();
if (v != 1) $stop;
Expand Down
19 changes: 11 additions & 8 deletions test_regress/t/t_randomize_method_types_unsup.out
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:17:17: Unsupported: randomizing this expression, treating as state
17 | dynarr[1].size < 10;
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:23:17: Unsupported: randomizing this expression, treating as state
23 | dynarr[1].size < 10;
| ^~~~
... For warning description see https://verilator.org/warn/CONSTRAINTIGN?v=latest
... Use "/* verilator lint_off CONSTRAINTIGN */" and lint_on around source to disable this message.
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:21:9: Size constraint combined with element constraint may not work correctly
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:27:9: Size constraint combined with element constraint may not work correctly
: ... note: In instance 't'
21 | q.size < 5;
27 | q.size < 5;
| ^~~~
%Error-UNSUPPORTED: t/t_randomize_method_types_unsup.v:11:13: Unsupported: random member variable with the type of the containing class
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:31:10: Global constraints ignored (unsupported)
31 | foo.x < y;
| ^
%Error-UNSUPPORTED: t/t_randomize_method_types_unsup.v:15:13: Unsupported: random member variable with the type of the containing class
: ... note: In instance 't'
11 | rand Cls cls;
15 | rand Cls cls;
| ^~~
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:33:43: Unsupported: randomizing this expression, treating as state
33 | res = obj.randomize() with { dynarr.size > 2; };
%Warning-CONSTRAINTIGN: t/t_randomize_method_types_unsup.v:43:43: Unsupported: randomizing this expression, treating as state
43 | res = obj.randomize() with { dynarr.size > 2; };
| ^~~~
%Error: Exiting due to
10 changes: 10 additions & 0 deletions test_regress/t/t_randomize_method_types_unsup.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
// any use, without warranty, 2020 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0

class Foo;
rand int x;
endclass

class Cls;
rand int assocarr[string];
rand int dynarr[][];
rand int q[$];
rand Cls cls;
rand int i;
rand Foo foo;
rand int y;
int st;
constraint dynsize {
dynarr.size < 20;
Expand All @@ -21,6 +27,9 @@ class Cls;
q.size < 5;
q[i] < 10;
}
constraint global_constraint {
foo.x < y;
}
endclass

module t (/*AUTOARG*/);
Expand All @@ -29,6 +38,7 @@ module t (/*AUTOARG*/);

initial begin
obj = new;
obj.foo = new;
res = obj.randomize();
res = obj.randomize() with { dynarr.size > 2; };
end
Expand Down

0 comments on commit d5070e4

Please sign in to comment.