1// DESCRIPTION: Verilator: Verilog Test module
2//
3// This file ONLY is placed under the Creative Commons Public Domain, for
4// any use, without warranty, 2019 by Wilson Snyder.
5// SPDX-License-Identifier: CC0-1.0
6
7`define stop $stop
8`define checkh(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d:  got='h%x exp='h%x\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
9`define checks(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d:  got='%s' exp='%s'\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
10
11module t (/*AUTOARG*/);
12   initial begin
13      int q[5];
14      int qv[$];  // Value returns
15      int qi[$];  // Index returns
16      int i;
17      string v;
18
19      q = '{1, 2, 2, 4, 3};
20      v = $sformatf("%p", q); `checks(v, "'{1, 2, 2, 4, 3} ");
21
22      // NOT tested: with ... selectors
23
24      q.sort;
25      v = $sformatf("%p", q); `checks(v, "'{1, 2, 2, 3, 4} ");
26      q.sort with (item == 2);
27      v = $sformatf("%p", q); `checks(v, "'{4, 3, 1, 2, 2} ");
28      q.sort(x) with (x == 3);
29      v = $sformatf("%p", q); `checks(v, "'{2, 1, 2, 4, 3} ");
30
31      q.rsort;
32      v = $sformatf("%p", q); `checks(v, "'{4, 3, 2, 2, 1} ");
33      q.rsort with (item == 2);
34      v = $sformatf("%p", q); `checks(v, "'{2, 2, 4, 1, 3} ");
35
36      qv = q.unique;
37      v = $sformatf("%p", qv); `checks(v, "'{2, 4, 1, 3} ");
38      qi = q.unique_index; qi.sort;
39      v = $sformatf("%p", qi); `checks(v, "'{0, 2, 3, 4} ");
40      q.reverse;
41      v = $sformatf("%p", q); `checks(v, "'{3, 1, 4, 2, 2} ");
42      q.shuffle(); q.sort;
43      v = $sformatf("%p", q); `checks(v, "'{1, 2, 2, 3, 4} ");
44
45      // These require an with clause or are illegal
46      // TODO add a lint check that with clause is provided
47      qv = q.find with (item == 2);
48      v = $sformatf("%p", qv); `checks(v, "'{2, 2} ");
49      qv = q.find_first with (item == 2);
50      v = $sformatf("%p", qv); `checks(v, "'{2} ");
51      qv = q.find_last with (item == 2);
52      v = $sformatf("%p", qv); `checks(v, "'{2} ");
53
54      qv = q.find with (item == 20);
55      v = $sformatf("%p", qv); `checks(v, "'{}");
56      qv = q.find_first with (item == 20);
57      v = $sformatf("%p", qv); `checks(v, "'{}");
58      qv = q.find_last with (item == 20);
59      v = $sformatf("%p", qv); `checks(v, "'{}");
60
61      qi = q.find_index with (item == 2); qi.sort;
62      v = $sformatf("%p", qi); `checks(v, "'{1, 2} ");
63      qi = q.find_first_index with (item == 2);
64      v = $sformatf("%p", qi); `checks(v, "'{1} ");
65      qi = q.find_last_index with (item == 2);
66      v = $sformatf("%p", qi); `checks(v, "'{2} ");
67
68      qi = q.find_index with (item == 20); qi.sort;
69      v = $sformatf("%p", qi); `checks(v, "'{}");
70      qi = q.find_first_index with (item == 20);
71      v = $sformatf("%p", qi); `checks(v, "'{}");
72      qi = q.find_last_index with (item == 20);
73      v = $sformatf("%p", qi); `checks(v, "'{}");
74
75      qv = q.min;
76      v = $sformatf("%p", qv); `checks(v, "'{1} ");
77      qv = q.max;
78      v = $sformatf("%p", qv); `checks(v, "'{4} ");
79
80      // Reduction methods
81
82      i = q.sum; `checkh(i, 32'hc);
83      i = q.sum with (item + 1); `checkh(i, 32'h11);
84      i = q.product; `checkh(i, 32'h30);
85      i = q.product with (item + 1); `checkh(i, 32'h168);
86
87      q = '{32'b1100, 32'b1010, 32'b1100, 32'b1010, 32'b1010};
88      i = q.and; `checkh(i, 32'b1000);
89      i = q.and with (item + 1); `checkh(i, 32'b1001);
90      i = q.or; `checkh(i, 32'b1110);
91      i = q.or with (item + 1); `checkh(i, 32'b1111);
92      i = q.xor; `checkh(i, 32'ha);
93      i = q.xor with (item + 1); `checkh(i, 32'hb);
94
95      $write("*-* All Finished *-*\n");
96      $finish;
97   end
98endmodule
99