7 #ifndef _GENERIC_UNIT_TESTER_H_
8 #define _GENERIC_UNIT_TESTER_H_
13 #include <condition_variable>
14 #include <iface/UIIface.h>
15 #include <rtl/Dynamic_Library.h>
16 #include <rtl/hresult.h>
17 #include <utils/string_utils.h>
18 #include "../utils/TestFilter.h"
19 #include "../utils/Logger.h"
20 #include "FilterConfiguration.h"
21 #include "../utils/constants.h"
22 #include "../mappers/GuidFileMapper.h"
23 #include "../utils/EntityUtils.h"
31 std::mutex m_testMutex;
32 std::condition_variable m_testCv;
33 HRESULT m_lastTestResult = S_OK;
40 void executeTest(
const std::wstring& testName,
const std::function<HRESULT(
void)>& test);
46 HRESULT runTestInThread(
const std::function<HRESULT(
void)>& test);
47 virtual void runTest(
const std::function<HRESULT(
void)>& test);
56 static std::vector<std::string> s_testedModules;
58 static std::map<std::string, std::string> s_descriptorsToFactories;
60 std::map<std::string, std::function<HRESULT(
void)>> m_descriptorsToTests {
61 {
"do_get_filter_descriptors", std::bind(&ModuleUnitTester::filterCreationTest,
this) },
62 {
"do_get_signal_descriptors", std::bind(&ModuleUnitTester::signalCreationTest,
this) },
63 {
"do_get_metric_descriptors", std::bind(&ModuleUnitTester::metricCreationTest,
this) },
64 {
"do_get_solver_descriptors", std::bind(&ModuleUnitTester::solverCreationTest,
this) },
65 {
"do_get_approximator_descriptors", std::bind(&ModuleUnitTester::approxCreationTest,
this) }
68 std::map<std::string, std::function<HRESULT(
const std::string&)>> m_descriptorsToParamsTests {
69 {
"do_get_filter_descriptors", std::bind(&ModuleUnitTester::descriptorsParamsTest<scgms::TGet_Filter_Descriptors , scgms::TFilter_Descriptor>,
this, std::placeholders::_1) },
70 {
"do_get_signal_descriptors", std::bind(&ModuleUnitTester::descriptorsParamsTest<scgms::TGet_Signal_Descriptors , scgms::TSignal_Descriptor>,
this, std::placeholders::_1) },
71 {
"do_get_metric_descriptors", std::bind(&ModuleUnitTester::descriptorsParamsTest<scgms::TGet_Metric_Descriptors , scgms::TMetric_Descriptor>,
this, std::placeholders::_1) },
72 {
"do_get_solver_descriptors", std::bind(&ModuleUnitTester::descriptorsParamsTest<scgms::TGet_Solver_Descriptors , scgms::TSolver_Descriptor>,
this, std::placeholders::_1) },
73 {
"do_get_approximator_descriptors", std::bind(&ModuleUnitTester::descriptorsParamsTest<scgms::TGet_Approx_Descriptors , scgms::TApprox_Descriptor>,
this, std::placeholders::_1) },
76 std::string m_modulePath;
78 CDynamic_Library m_moduleLibrary;
80 std::vector<std::string> m_foundDescriptorsMethods;
114 bool loadModule(
const std::string& modulePath);
123 HRESULT shutDownTest()
override;
125 template<
typename T,
typename D>
126 HRESULT descriptorsParamsTest(
const std::string& symbolName);
127 HRESULT filterCreationTest();
128 HRESULT signalCreationTest();
129 HRESULT metricCreationTest();
130 HRESULT solverCreationTest();
131 HRESULT approxCreationTest();
142 const GUID m_entityGuid;
144 T* m_testedEntity =
nullptr;
146 CDynamic_Library m_entityLibrary;
148 std::wstring m_libraryPath;
151 explicit EntityUnitTester(
const GUID& entityGuid) : m_entityGuid(entityGuid) {}
161 return m_testedEntity !=
nullptr;
169 return m_entityLibrary;
184 if (m_libraryPath.empty()) {
186 m_libraryPath = std::wstring(file_name) + cnst::LIB_EXTENSION;
189 m_entityLibrary.Load(m_libraryPath);
191 if (!m_entityLibrary.Is_Loaded()) {
192 std::wcerr << L
"Couldn't load " << m_libraryPath <<
" library!\n";
193 Logger::getInstance().error(L
"Couldn't load " + std::wstring(m_libraryPath) + L
" library.");
200 template<
typename C,
typename D>
201 const wchar_t* getEntityName(
const std::string& symbolName) {
206 D* descriptor = tester::getEntityDescriptor<C, D>(m_entityLibrary, m_entityGuid, symbolName);
211 return descriptor->description;
215 void setEntityLib(
const std::wstring& libPath) {
216 if (m_entityLibrary.Is_Loaded()) {
217 Logger::getInstance().error(L
"Attempt to override loaded library! Forbidding...");
221 if (m_entityLibrary.Load(libPath)) {
222 m_libraryPath = libPath;
224 Logger::getInstance().error(std::wstring(L
"Error while loading library ") + libPath);
228 void setLibraryPath(
const std::wstring& libraryPath) noexcept {
229 m_libraryPath = libraryPath;
232 const std::wstring& getLibraryPath()
const {
233 return m_libraryPath;
236 T* getTestedEntity() noexcept {
237 return m_testedEntity;
240 void setTestedEntity(T *entity) {
241 if (m_testedEntity) {
242 m_testedEntity->Release();
245 m_testedEntity = entity;
249 void runTest(
const std::function<HRESULT ()> &test)
override {
250 if (!m_entityLibrary.Is_Loaded()) {
259 m_lastTestResult = test();
262 Logger::getInstance().error(L
"Filter is not loaded! Test will not be executed.");
263 m_lastTestResult = E_FAIL;
266 m_testCv.notify_all();
274 enum class EntityType : int16_t {
284 EntityType m_entityType;
327 void executeGenericTests();
352 HRESULT informativeEventsTest(
const scgms::NDevice_Event_Code& eventCode);
353 void loadEntity()
override;
355 HRESULT runConfigTestInThread(
const tester::FilterConfig& configuration, HRESULT expectedResult);
363 void executeModuleTests(
const std::string& modulePath);
367 #endif // !_GENERIC_UNIT_TESTER_H_