procedure assert_equal (
prefix'event : string;
got, expected : integer;
level : severity_level := error) is
begin
assert got = expected
report prefix & " wrong. Got " & integer'image(got) & " expected "
& integer'image(expected)
& "(difference=" & integer'image(got-expected) &")"
severity level; end procedure assert_equal;
In addition, the same API can be used for comparing more complex types (like the integer_vector type which is also part of libv):
procedure assert_equal ( prefix : string;
got, expected : integer_vector;
level : severity_level := error) is
variable g,e : integer;
begin -- procedure assert_equal
assert got'length = expected'length
report prefix & " length wrong. Got " & integer'image(got'length)
& " expected " & integer'image(expected'length)
& "(difference=" & integer'image(got'length-expected'length) &")"
severity level;
for i in 0 to got'length-1 loop
g := got(got'low+i);
e := expected(expected'low+i);
assert g = e
report prefix & " got(" & integer'image(got'low+i) & ") = "
& integer'image(g) & CR
& " expected(" & integer'image(expected'low+i)
& ") = " & integer'image(e)
severity level;
end loop; -- i
end procedure assert_equal;
or indeed a similar function for `pixel_array`:
procedure assert_equal (
prefix : string;
expected, got : pixel_array;
level : severity_level := error) is
begin -- procedure assert_equal
assert_equal(prefix & "(width)", expected'length(1), got'length(1), level);
assert_equal(prefix & "(height)", expected'length(2), got'length(2), level);
for y in expected'range(2) loop
for x in expected'range(1) loop
assert expected(x,y)=got(x,y)
report prefix &"(" & str(x) & "," & str(y) &")" &
str(expected(x,y)) & "/=" & str(got(x,y))
severity level;
end loop; -- x
end loop; -- y
end procedure assert_equal;
You’ll notice I got bored of writing integer'image
and wrote a str
function which takes an integer and an optional length argument and returns a string, left-padded with spaces if the length argument is longer than the integer requires:
function str (val : integer; length : natural := 0) return string is
constant chars_needed : natural := num_chars(val);
variable s : string(1 to max(length, chars_needed)) := (others => ' ');
begin -- function str
if length = 0 then
return integer'image(val);
end if;
if chars_needed > length then
report "Can't fit " & integer'image(val) & " into "
& integer'image(length) & " character(s) - returning full width"
severity warning;
return integer'image(val);
end if;
s(s'high-(chars_needed-1) to s'high) := integer'image(val);
return s;
end function str;
Again, this can be a standard API and we can have a str
function for other types; For example, boolean
:
function str (val : boolean; length:natural range 0 to 1 := 0) return string is
begin -- function str
if length = 0 then
return boolean'image(val);
end if;
if length = 1 then
if val then
return "T";
else
return "F";
end if;
end if;
end function str;