2020-10-17 23:23:27 +00:00
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2019 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0
`define stop $stop
`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);
`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);
module t ( /*AUTOARG*/ ) ;
2023-07-04 13:11:15 +00:00
typedef struct { int x , y ; } point ;
2020-10-17 23:23:27 +00:00
initial begin
int q [ int ] ;
2020-10-24 22:00:40 +00:00
int qe [ int ] ; // Empty
int qv [ $ ] ; // Value returns
int qi [ $ ] ; // Index returns
2023-07-04 13:11:15 +00:00
point points_q [ int ] ;
point points_qv [ $ ] ;
2020-10-17 23:23:27 +00:00
int i ;
string v ;
q = ' { 10 : 1 , 11 : 2 , 12 : 2 , 13 : 4 , 14 : 3 } ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , q ) ; `checks ( v , " '{'ha:'h1, 'hb:'h2, 'hc:'h2, 'hd:'h4, 'he:'h3} " ) ;
2020-10-17 23:23:27 +00:00
// NOT tested: with ... selectors
//q.sort; // Not legal on assoc - see t_assoc_meth_bad
//q.rsort; // Not legal on assoc - see t_assoc_meth_bad
//q.reverse; // Not legal on assoc - see t_assoc_meth_bad
//q.shuffle; // Not legal on assoc - see t_assoc_meth_bad
2020-10-24 22:00:40 +00:00
v = $sformatf ( " %p " , qe ) ; `checks ( v , " '{} " ) ;
2020-10-17 23:23:27 +00:00
qv = q . unique ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{'h1, 'h2, 'h4, 'h3} " ) ;
2020-10-17 23:23:27 +00:00
qv = qe . unique ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
2020-10-24 22:00:40 +00:00
qi = q . unique_index ; qi . sort ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'ha, 'hb, 'hd, 'he} " ) ;
qi = qe . unique_index ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{} " ) ;
2020-10-17 23:23:27 +00:00
2023-07-04 13:11:15 +00:00
points_q [ 0 ] = point ' { 1 , 2 } ;
points_q [ 1 ] = point ' { 2 , 4 } ;
points_q [ 5 ] = point ' { 1 , 4 } ;
points_qv = points_q . unique ( p ) with ( p . x ) ;
`checkh ( points_qv . size , 2 ) ;
qi = points_q . unique_index ( p ) with ( p . x + p . y ) ;
qi . sort ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'h0, 'h1, 'h5} " ) ;
2020-10-17 23:23:27 +00:00
// These require an with clause or are illegal
// TODO add a lint check that with clause is provided
qv = q . find with ( item = = 2 ) ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{'h2, 'h2} " ) ;
2020-10-17 23:23:27 +00:00
qv = q . find_first with ( item = = 2 ) ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{'h2} " ) ;
2020-10-17 23:23:27 +00:00
qv = q . find_last with ( item = = 2 ) ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{'h2} " ) ;
2020-10-17 23:23:27 +00:00
qv = q . find with ( item = = 20 ) ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
qv = q . find_first with ( item = = 20 ) ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
qv = q . find_last with ( item = = 20 ) ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
2020-10-24 22:00:40 +00:00
qi = q . find_index with ( item = = 2 ) ; qi . sort ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'hb, 'hc} " ) ;
2020-10-24 22:00:40 +00:00
qi = q . find_first_index with ( item = = 2 ) ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'hb} " ) ;
2020-10-24 22:00:40 +00:00
qi = q . find_last_index with ( item = = 2 ) ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'hc} " ) ;
2020-10-24 22:00:40 +00:00
qi = q . find_index with ( item = = 20 ) ; qi . sort ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{} " ) ;
qi = q . find_first_index with ( item = = 20 ) ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{} " ) ;
qi = q . find_last_index with ( item = = 20 ) ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{} " ) ;
2020-10-17 23:23:27 +00:00
2020-11-24 04:18:58 +00:00
qi = q . find_index with ( item . index = = 12 ) ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'hc} " ) ;
qi = q . find with ( item . index = = 12 ) ;
v = $sformatf ( " %p " , qi ) ; `checks ( v , " '{'h2} " ) ;
2020-10-17 23:23:27 +00:00
qv = q . min ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{'h1} " ) ;
2023-07-04 13:11:15 +00:00
points_qv = points_q . min ( p ) with ( p . x + p . y ) ;
if ( points_qv [ 0 ] . x ! = 1 | | points_qv [ 0 ] . y ! = 2 ) $stop ;
2020-10-17 23:23:27 +00:00
qv = q . max ;
2020-11-01 15:56:07 +00:00
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{'h4} " ) ;
2023-07-04 13:11:15 +00:00
points_qv = points_q . max ( p ) with ( p . x + p . y ) ;
if ( points_qv [ 0 ] . x ! = 2 | | points_qv [ 0 ] . y ! = 4 ) $stop ;
2020-10-17 23:23:27 +00:00
qv = qe . min ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
2023-07-04 13:11:15 +00:00
qv = qe . min ( x ) with ( x + 1 ) ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
2020-10-17 23:23:27 +00:00
qv = qe . max ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
2023-07-04 13:11:15 +00:00
qv = qe . max ( x ) with ( x + 1 ) ;
v = $sformatf ( " %p " , qv ) ; `checks ( v , " '{} " ) ;
2020-10-17 23:23:27 +00:00
// Reduction methods
2024-03-02 14:06:54 +00:00
i = q . sum ;
2020-11-01 15:56:07 +00:00
`checkh ( i , 32 'hc ) ;
i = q . sum with ( item + 1 ) ;
`checkh ( i , 32 'h11 ) ;
i = q . product ;
`checkh ( i , 32 'h30 ) ;
i = q . product with ( item + 1 ) ;
`checkh ( i , 32 'h168 ) ;
2020-10-17 23:23:27 +00:00
2020-11-01 15:56:07 +00:00
i = qe . sum ;
`checkh ( i , 32 'h0 ) ;
i = qe . product ;
`checkh ( i , 32 'h0 ) ;
2020-10-17 23:23:27 +00:00
q = ' { 10 : 32 'b1100 , 11 : 32 'b1010 } ;
2020-11-01 15:56:07 +00:00
i = q . and ;
`checkh ( i , 32 'b1000 ) ;
i = q . and with ( item + 1 ) ;
`checkh ( i , 32 'b1001 ) ;
i = q . or ;
`checkh ( i , 32 'b1110 ) ;
i = q . or with ( item + 1 ) ;
`checkh ( i , 32 'b1111 ) ;
i = q . xor ;
`checkh ( i , 32 'b0110 ) ;
i = q . xor with ( item + 1 ) ;
`checkh ( i , 32 'b0110 ) ;
i = qe . and ;
`checkh ( i , 32 'b0 ) ;
i = qe . or ;
`checkh ( i , 32 'b0 ) ;
i = qe . xor ;
`checkh ( i , 32 'b0 ) ;
2020-10-17 23:23:27 +00:00
2021-10-26 16:20:45 +00:00
i = q . and ( ) ;
`checkh ( i , 32 'b1000 ) ;
i = q . and ( ) with ( item + 1 ) ;
`checkh ( i , 32 'b1001 ) ;
i = q . or ( ) ;
`checkh ( i , 32 'b1110 ) ;
i = q . or ( ) with ( item + 1 ) ;
`checkh ( i , 32 'b1111 ) ;
i = q . xor ( ) ;
`checkh ( i , 32 'b0110 ) ;
i = q . xor ( ) with ( item + 1 ) ;
`checkh ( i , 32 'b0110 ) ;
i = qe . and ( ) ;
`checkh ( i , 32 'b0 ) ;
i = qe . or ( ) ;
`checkh ( i , 32 'b0 ) ;
i = qe . xor ( ) ;
`checkh ( i , 32 'b0 ) ;
2023-05-26 22:01:11 +00:00
q = ' { 10 : 1 , 11 : 2 } ;
qe = ' { 10 : 1 , 11 : 2 } ;
`checkh ( q = = qe , 1 'b1 ) ;
`checkh ( q ! = qe , 1 'b0 ) ;
2020-10-17 23:23:27 +00:00
$write ( " *-* All Finished *-* \n " ) ;
$finish ;
end
endmodule