对于一个项目,我在嵌入式系统上用c++实现了一个组件--通过FreeRTOS queue获取传感器,并将它们处理为FreeRTOS任务。
由于HW尚未到达&质量原因(TDD),我想模拟freeRTOS功能,并使用它们来模拟组件行为。
我提前感谢你。
发布于 2018-02-12 14:07:27
因此,我设法解决了我的问题,结合了不同的答案,从网站:How to use google test for C++ to run through combinations of data & Can gmock be used for stubbing C functions?。
我的答案有点大,但就像这样,如果你想使用它,你可以简单地使用拷贝和过去。
若要模拟freeRTOS元素,请在我的测试文件夹中:
FreeRTOS_mock.hpp
/* Include freeRTOS headers */
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
/* Include gTest mockup functionality */
#include "gmock/gmock.h"
/* Mock all functions needed from FreeRTOS */
namespace freertos {
class FreeRTOSInterface
{
public:
virtual ~FreeRTOSInterface() {}
virtual QueueHandle_t xQueueGenericCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType) = 0;
/* define other freeRTOS elements the same way */
};
class FreeRTOSMock : public FreeRTOSInterface
{
public:
virtual ~FreeRTOSMock() {}
MOCK_METHOD3(xQueueGenericCreate, QueueHandle_t(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType));
/* Align with what was defined above */
};
} /* namespace freertos */FreeRTOS_mock.cpp
#include "FreeRTOS_mock.hpp"
freertos::FreeRTOSMock FreeRTOSMockObj;
QueueHandle_t xQueueGenericCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType)
{
return FreeRTOSMockObj.xQueueGenericCreate(uxQueueLength, uxItemSize, ucQueueType);
}
/* Align with what is in the .hpp */TestSuiteXXX_unittest.cpp
#include "FreeRTOS_mock.hpp"
extern freertos::FreeRTOSMock FreeRTOSMockObj;
/* Write my TCs by using the FreeRTOS functions*/同样重要的是,您必须定义了一个有效的FreeRTOSConfig.h,并在makefile中定义了
INCLUDE_DIRS = \
-I$(FREERTOS_DIR)/Source/include \
-I$(FREERTOS_DIR)/Source/portable/GCC/ARM_CM4F \
-I$(PRJ_FREERTOS_CFG) \
-I$(UNITTEST_INCLUDE_DIR)
SRC_FILES = \
./test/FreeRTOS_mock.cpp \
./src/XXX.cpp
#Specify all unittest files
UNITTEST_SRC_FILES = \
./test/TestSuiteXXX_unittest.cpp为了模仿感官数据:
TestSuiteXXX_unittest.cpp
#include "FreeRTOS_mock.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <vector>
#include "Algo.hpp"
extern freertos::FreeRTOSMock FreeRTOSMockObj;
/* A sensor measurement */
std::vector<int32_t> input1 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
std::vector<int32_t> output1 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
/* Not very pretty adaptation function but it does the job */
std::vector<std::tuple<int32_t, int32_t>> genSet(std::vector<int32_t> a, std::vector<int32_t> b)
{
uint32_t i(0);
std::vector<std::tuple<int32_t int32_t>> vectorToReturn(a.size());
for (i = 0 ; i < a.size(); i++)
{
vectorToReturn[i] = std::make_tuple(a[i], b[i]);
}
return vectorToReturn;
}
/** Define the Value-Parameterized Tests class */
class AlgoToTest: public ::testing::TestWithParam<std::tuple<int32_t, int32_t>>
{
public:
/* SetUp() is run immediately before a test starts. */
virtual void SetUp()
{
algo = new Algo::Algo();
}
/* TearDown() is invoked immediately after a test finishes. */
virtual void TearDown()
{
delete algo;
}
Algo::Algorithm* algo = NULL;
};
/* The test-case used to loop on */
TEST_P(AlgoToTest, AlgoTestCase1)
{
int32_t outputValue(0);
outputValue = algo->run(std::get<0>(GetParam()), std::get<1>(GetParam()));
ASSERT_EQ(outputValue, std::get<3>(GetParam()));
}
INSTANTIATE_TEST_CASE_P(AlgoTestRun1, AlgoToTest, ::testing::ValuesIn(genSet(input1, output1)));如果你有改进的主张,请评论!
发布于 2018-02-07 09:20:25
一种选择是为您的主机构建应用程序,然后,当您的HW到达时,您可以为该HW重新编译它。
可以将FreeRTOS作为操作系统在主机PC上运行,但这并不是FreeRTOS的意图,因此在重新部署HW时可能会很棘手,或者导致一些不现实的问题。
在Windows上运行FreeRTOS有一些支持,但我对Linux不太确定。
另一种选择是freeRTOS模拟器。这需要注意的是,在模拟中,FreeRTOS不是实际的内核(就像在目标HW上那样),而是由Windows内核设置的线程来运行FreeRTOS代码。考虑到FreeRTOS是针对硬时间限制的,这个模拟远非理想,因为时间最终是由主机的内核决定的。
Windows模拟器可以使用Visual (免费版本)运行,并维护该端口。可能也有一些对Eclipse的支持。
Windows模拟器页面:http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
Linux模拟器页面:http://www.freertos.org/FreeRTOS-simulator-for-Linux.html
https://stackoverflow.com/questions/48659761
复制相似问题