在 VUnit 中测试断言失败

Test for Assertion Failure in VUnit

我可能有一些函数会在不满意时断言并失败。

如何使用 VUnit 测试此功能以确保这些功能确实在正确的条件下抛出故障?


例如,假设我想检查这个函数:

function to_normalised_float(slv: std_logic_vector) return half_float is
variable temp_hf: half_float;
begin
    assert slv'high = 15 report "SLV provided is incorrect length for an FP16";
    -- ConstructFloat
    return normalise_float(temp_hf);
end function;

如果我传入一个值,然后在我的测试台中对输出断言,我可以很容易地测试它 returns 预期值。

但是,我还希望能够使用 VUnit 进行测试,如果我传入 22 位 SLV,断言就会抛出。

这显然是一个非常简化的例子,但它应该能解释我的意思。

如果有帮助,C# 中的等价物是 Assert.Throws(function)

检查断言的能力将随着模拟器中 VHDL-2019 的支持而得到改善,但由于您使用的是 VUnit,我建议使用 VUnit 模拟 (http://vunit.github.io/logging/user_guide.html#mocking and https://github.com/VUnit/vunit/blob/f02c21452a505c527db575b10db94195ceb7ed2f/vunit/vhdl/logging/src/logger_pkg.vhd#L342),它正是为支持您的用例而提供的。

首先用 VUnit check 替换你的 assert:

check(slv'high = 15, "SLV provided is incorrect length for an FP16");

当该检查失败时,您将看到如下所示的错误消息:

0 ps - check - ERROR - SLV provided is incorrect length for an FP16

check 是管理此消息的 VUnit logger。您可以通过名称 (get_logger("check")) 获取此记录器并模拟它。模拟意味着所有输出消息(特定严重级别)将被放置在队列中而不是传递到标准输出。可以检查此队列中的消息以确定函数是否按预期工作。这是一个稍微修改过的示例测试台来展示原理

library vunit_lib;
context vunit_lib.vunit_context;

library ieee;
use ieee.std_logic_1164.all;

entity tb_example is
  generic (runner_cfg : string);
end entity;

architecture tb of tb_example is
begin
  main : process
    procedure dummy(slv : std_logic_vector) is
    begin
      check(slv'length = 16, "SLV provided is incorrect length for an FP16");
    end;

    constant logger : logger_t := get_logger("check");
  begin
    test_runner_setup(runner, runner_cfg);

    while test_suite loop
      if run("Test to see dummy fail") then
        dummy(x"17");
      elsif run("Test that dummy fails with the correct message") then
        mock(logger, error);
        dummy(x"17");
        check_log(logger, "SLV provided is incorrect length for an FP16", error);
        unmock(logger);
      elsif run("Test that dummy passes with 16 bit inputs") then
        mock(logger, error);
        dummy(x"1718");
        check_no_log;
        unmock(logger);
      end if;
    end loop;

    test_runner_cleanup(runner);
  end process;

end architecture;

第一个测试用例会失败(这是你的问题)但最后两个会通过

我还可以推荐使用 check_equal 以获得更多信息的输出。

check_equal(slv'length, 16, result("for input length"));

会给你以下错误输出:

0 fs - check - ERROR - Equality check failed for input length - Got 8. Expected 16.