在 VUnit 中测试断言失败

时间:2021-03-07 21:18:40

标签: vhdl vunit

如果它们不满意,我可能有一些函数会断言并失败。

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


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

with open(fileName, mode='rb') as f:
    x = os.stat(fileName).st_size
    y = int(x/4)
    print('x,y:', x, y)
    for i in range(0, y, 4):
        x = st.unpack('<I', f.read(4))
        if x == int("00000050", 16):
            print("loc:")

如果我传入一个值然后在我的测试平台中对输出进行断言,我可以很容易地测试它是否返回了预期值。

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

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

如果有帮助,C# 中的等效项将是 with open(filename, mode='rb') as f: b = f.read() np_data = np.frombuffer(b, dtype=np.uint16) findIndex = np.where(np_data == int("00000050", 16))

1 个答案:

答案 0 :(得分:0)

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

首先将您的 assert 替换为 VUnit check

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")) 获取此记录器并模拟它。 Mocking 意味着所有输出消息(具有特定严重性级别)将被放置在队列中而不是传递到 stdout。可以检查此队列中的消息以确定该函数是否按预期工作。这是一个稍微修改的示例测试平台以展示原理

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;

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

enter image description here

我也可以推荐使用 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.
相关问题