Add regression tests for string literals assigned to byte arrays

Check that string literals can be assigned to byte arrays. Check that
invalid target array types are reported as errors.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2026-05-02 15:59:19 -07:00
parent 272cf91eae
commit 6ffb4b9a3a
19 changed files with 294 additions and 0 deletions

View File

@ -0,0 +1,45 @@
// Check that string literals can be assigned to one-dimensional byte arrays.
module test;
bit failed = 1'b0;
`define check(val, exp) do \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %02h, got %02h", `__LINE__, \
`"val`", exp, val); \
failed = 1'b1; \
end \
while(0)
byte desc [3:0] = "AB\n";
byte asc [0:3];
byte unsized [4];
assign asc = "AB\n";
initial begin
#1;
`check(desc[3], 8'h41);
`check(desc[2], 8'h42);
`check(desc[1], 8'h0a);
`check(desc[0], 8'h00);
`check(asc[0], 8'h41);
`check(asc[1], 8'h42);
`check(asc[2], 8'h0a);
`check(asc[3], 8'h00);
unsized = "AB\n";
`check(unsized[0], 8'h41);
`check(unsized[1], 8'h42);
`check(unsized[2], 8'h0a);
`check(unsized[3], 8'h00);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,61 @@
// Check that string literals can be assigned to nested byte arrays using
// assignment patterns.
module test;
bit failed = 1'b0;
`define check(val, exp) do \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %02h, got %02h", `__LINE__, \
`"val`", exp, val); \
failed = 1'b1; \
end \
while(0)
byte desc [0:1][3:0] = '{"AB\n", "CD\t"};
byte asc [1:0][0:3];
byte unsized [2][4];
assign asc = '{"AB\n", "CD\t"};
initial begin
#1;
`check(desc[0][3], 8'h41);
`check(desc[0][2], 8'h42);
`check(desc[0][1], 8'h0a);
`check(desc[0][0], 8'h00);
`check(desc[1][3], 8'h43);
`check(desc[1][2], 8'h44);
`check(desc[1][1], 8'h09);
`check(desc[1][0], 8'h00);
`check(asc[0][0], 8'h43);
`check(asc[0][1], 8'h44);
`check(asc[0][2], 8'h09);
`check(asc[0][3], 8'h00);
`check(asc[1][0], 8'h41);
`check(asc[1][1], 8'h42);
`check(asc[1][2], 8'h0a);
`check(asc[1][3], 8'h00);
unsized = '{"AB\n", "CD\t"};
`check(unsized[0][0], 8'h41);
`check(unsized[0][1], 8'h42);
`check(unsized[0][2], 8'h0a);
`check(unsized[0][3], 8'h00);
`check(unsized[1][0], 8'h43);
`check(unsized[1][1], 8'h44);
`check(unsized[1][2], 8'h09);
`check(unsized[1][3], 8'h00);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,46 @@
// Check that string literals shorter than the target byte array are padded
// with null bytes.
module test;
bit failed = 1'b0;
`define check(val, exp) do \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %02h, got %02h", `__LINE__, \
`"val`", exp, val); \
failed = 1'b1; \
end \
while(0)
byte desc [3:0] = "AB";
byte asc [0:3];
byte unsized [4];
assign asc = "AB";
initial begin
#1;
`check(desc[3], 8'h41);
`check(desc[2], 8'h42);
`check(desc[1], 8'h00);
`check(desc[0], 8'h00);
`check(asc[0], 8'h41);
`check(asc[1], 8'h42);
`check(asc[2], 8'h00);
`check(asc[3], 8'h00);
unsized = "AB";
`check(unsized[0], 8'h41);
`check(unsized[1], 8'h42);
`check(unsized[2], 8'h00);
`check(unsized[3], 8'h00);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,39 @@
// Check that string literals longer than the target byte array are truncated.
module test;
bit failed = 1'b0;
`define check(val, exp) do \
if (val !== exp) begin \
$display("FAILED(%0d). '%s' expected %02h, got %02h", `__LINE__, \
`"val`", exp, val); \
failed = 1'b1; \
end \
while(0)
byte desc [1:0] = "ABC";
byte asc [0:1];
byte unsized [2];
assign asc = "ABC";
initial begin
#1;
`check(desc[1], 8'h41);
`check(desc[0], 8'h42);
`check(asc[0], 8'h41);
`check(asc[1], 8'h42);
unsized = "ABCD";
`check(unsized[0], 8'h41);
`check(unsized[1], 8'h42);
if (!failed) begin
$display("PASSED");
end
end
endmodule

View File

@ -0,0 +1,8 @@
// Check that string literals cannot be assigned to unpacked arrays whose
// element type is 4-state.
module test;
logic [7:0] value [0:3] = "AB"; // Error: target element type is 4-state
endmodule

View File

@ -0,0 +1,8 @@
// Check that string literals cannot be assigned directly to multi-dimensional
// byte arrays.
module test;
byte value [0:1][0:3] = "AB"; // Error: string is not nested
endmodule

View File

@ -0,0 +1,17 @@
// Check that string literals cannot be connected to output byte array ports.
module M (
output byte out [0:1]
);
initial begin
out = "CD";
end
endmodule
module test;
M i_m("AB"); // Error: output expression is not assignable
endmodule

View File

@ -0,0 +1,8 @@
// Check that string literals cannot be assigned to unpacked arrays whose
// element type is narrower than 8 bits.
module test;
bit [6:0] value [0:3] = "AB"; // Error: target element type is too narrow
endmodule

View File

@ -0,0 +1,8 @@
// Check that string literals cannot be assigned to unpacked arrays whose
// element type is wider than 8 bits.
module test;
bit [8:0] value [0:3] = "AB"; // Error: target element type is too wide
endmodule

View File

@ -226,6 +226,15 @@ sv_array_cassign6 vvp_tests/sv_array_cassign6.json
sv_array_cassign7 vvp_tests/sv_array_cassign7.json
sv_array_cassign8 vvp_tests/sv_array_cassign8.json
sv_automatic_2state vvp_tests/sv_automatic_2state.json
sv_byte_array_string1 vvp_tests/sv_byte_array_string1.json
sv_byte_array_string2 vvp_tests/sv_byte_array_string2.json
sv_byte_array_string3 vvp_tests/sv_byte_array_string3.json
sv_byte_array_string4 vvp_tests/sv_byte_array_string4.json
sv_byte_array_string_fail1 vvp_tests/sv_byte_array_string_fail1.json
sv_byte_array_string_fail2 vvp_tests/sv_byte_array_string_fail2.json
sv_byte_array_string_fail3 vvp_tests/sv_byte_array_string_fail3.json
sv_byte_array_string_fail4 vvp_tests/sv_byte_array_string_fail4.json
sv_byte_array_string_fail5 vvp_tests/sv_byte_array_string_fail5.json
sv_chained_constructor1 vvp_tests/sv_chained_constructor1.json
sv_chained_constructor2 vvp_tests/sv_chained_constructor2.json
sv_chained_constructor3 vvp_tests/sv_chained_constructor3.json

View File

@ -0,0 +1,5 @@
{
"type" : "normal",
"source" : "sv_byte_array_string1.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "normal",
"source" : "sv_byte_array_string2.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "normal",
"source" : "sv_byte_array_string3.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "normal",
"source" : "sv_byte_array_string4.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "sv_byte_array_string_fail1.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "sv_byte_array_string_fail2.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "sv_byte_array_string_fail3.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "sv_byte_array_string_fail4.v",
"iverilog-args" : [ "-g2005-sv" ]
}

View File

@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "sv_byte_array_string_fail5.v",
"iverilog-args" : [ "-g2005-sv" ]
}