将现有的单元测试框架与 SystemC 结合使用
我正在使用 SystemC 开发一个项目,并且想要合并单元测试。是否可以将现有的单元测试框架与 SystemC 一起使用?
我问这个问题是因为 SystemC 模块似乎只能通过模拟内核执行,并且我想对模块本身使用单元测试。
I am working on a project in SystemC and want to incorporate unit testing. Is it possible to use existing unit test frameworks with SystemC?
I ask this because it seems like the SystemC modules only get executed with the simulation kernel, and I want to use unit tests on the modules themselves.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 GTest 中运行任何测试之前,您必须创建所有必需的 SystemC 信号、SystemC 模块并在它们之间建立连接。这需要创建自己的
gtest_main.cc
实现。当然,在 SystemC 中,您必须将所有内容放入sc_main
函数中。为此,我将使用注册表设计模式。
首先创建注册表类(注册表+工厂+单例)。该类将负责使用 lambda 表达式中的 new 和智能指针进行动态分配来存储已注册的构造函数(请参阅factory::add 类)。在运行所有测试之前,使用
factory::create()
方法创建所有对象。然后您可以在测试执行中使用factory::get()
方法获取对象。factory.hpp
factory.cpp
创建您自己的
gtest_main.cc
实现版本。在运行任何测试RUN_ALL_TESTS()
之前,调用factory::create()
方法创建所有SystemC信号和SystemC模块。由于工厂类是单例设计模式,因此在完成所有测试后调用factory::destroy()方法来销毁所有创建的SystemC对象。main.cpp
然后在测试中定义 dut 类,它将创建 SystemC 信号和 SystemC 模块。在构造函数中,在创建的 SystemC 信号和模块之间进行连接。使用全局构造函数将定义的 dut 类注册到注册表对象,例如 factory::add g。之后,您可以使用简单的 factory::get() 方法获取 dut 对象。
test.cpp
my_module.h
CMakeLists.txt
如需更多灵感,您可以查看我的用于 SystemC 验证的 logic 库: https://github.com/tymonx/logic
You must create all necessary SystemC signals, SystemC modules and make connection between them before you run any test in GTest. This requires to create own
gtest_main.cc
implementation. Naturally in SystemC you must put everything insc_main
function.For this, I would use registry design pattern.
First create registry class (registry + factory + singleton). This class will be responsible for storing registered constructors using dynamic allocation with new and smart pointer in lambda expression (see
factory::add
class). Create all objects usingfactory::create()
method before running all tests. Then you can get object usingfactory::get()
method in you test execution.factory.hpp
factory.cpp
Create your own version of
gtest_main.cc
implementation. Callfactory::create()
method to create all SystemC signals and SystemC modules before running any testsRUN_ALL_TESTS()
. Because factory class is a singleton design pattern, callfactory::destroy()
method after finishing all tests to destroy all created SystemC objects.main.cpp
Then define dut class in your test than will create SystemC signals and SystemC modules. In constructor do connection between created SystemC signals and modules. Register defined dut class to registry object using global constructor like this factory::add g. After than you can get your dut object using simple factory::get() method.
test.cpp
my_module.h
CMakeLists.txt
For more inspiration, you can check my logic library for SystemC verification: https://github.com/tymonx/logic
我能够使用 fork 系统调用运行 2 个 SystemC 测试。我使用了 doulos.com 和 Google 测试框架。我能够运行测试两次,但 SystemC 模拟器打印出一条有关在调用 sc_stop 后开始测试的错误。然而,无论出现什么错误,模拟器第二次都能正常运行。
更新:按照要求的代码示例:
I was able to run 2 SystemC tests using the fork system call. I used the tutorial example on doulos.com and the Google Test framework. I was able to run the test twice, but I get an error printed out by the SystemC simulator about starting the test after calling sc_stop. However, regardless of the error, the simulator runs fine the second time around.
UPDATE: Code sample as requested:
我对这个问题有第二个解决方案,它使用 CMkae 和 CTest (http://cmake.org/)。我使用的设置为每个测试创建一个二进制文件。这是我使用的
CMakeLists.txt
文件:每个
test_*.cxx
文件都有一个sc_main
方法来执行测试,返回值指示是否或测试未通过或失败。要运行测试,只需执行以下操作:如果您不想运行模拟器,则可以简单地跳过对
sc_start
的调用,并在对特定模块进行所需的任何特定测试后退出应用程序。I have a second solution to this question that uses CMkae and CTest (http://cmake.org/). The setup I used creates a binary for each test. Here's the
CMakeLists.txt
file I used:Each
test_*.cxx
file has asc_main
method that executes the test and the return value indicates whether or not the test passed or failed. To run the tests simply do:If you don't want to run the simulator, you can simply skip the call to
sc_start
and exit the application after doing whatever specific testing you want on a particular module.通常,SystemC 被测设备 (DUT) 可以通过断言某些信号来重置为初始状态。您可以利用这一事实并启用您想要的任何 C++ 单元测试框架。只需在运行每个测试之前重置您的 DUT,因此您无需对其进行两次详细说明。
以下是 Google Test 的示例,以及来自
sc_main
的简单“累加器”DUT::testing::InitGoogleTest(&argc, argv);
)RUN_ALL_TESTS()
从sc_module
内的某个线程运行测试来源:
CMakeLists.txt(需要安装 SystemC 2.3.2 )
Very often SystemC Device-under-test (DUT) can be resetted to initial state by asserting some signal. You can utilize this fact and enable any C++ unit testing framework you want. Just reset you DUT before running each test, so you don't need to elaborate it twice.
Here is an example with Google Test, and a simple "Accumulator" DUT
::testing::InitGoogleTest(&argc, argv);
) fromsc_main
sc_module
by callingRUN_ALL_TESTS()
Source:
CMakeLists.txt (requires installed SystemC 2.3.2 )