diff --git a/src.wsjcpp/CMakeLists.txt b/src.wsjcpp/CMakeLists.txt index a10b694..1fa6aea 100644 --- a/src.wsjcpp/CMakeLists.txt +++ b/src.wsjcpp/CMakeLists.txt @@ -1,7 +1,7 @@ -# Automaticly generated by wsjcpp@v0.1.5 +# Automaticly generated by wsjcpp@v0.2.0 cmake_minimum_required(VERSION 3.0) -add_definitions(-DWSJCPP_APP_VERSION="v0.0.1") +add_definitions(-DWSJCPP_APP_VERSION="v0.0.2") add_definitions(-DWSJCPP_APP_NAME="wsjcpp-async-jobs-pool") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") @@ -17,14 +17,9 @@ set (WSJCPP_SOURCES "") find_package(Threads REQUIRED) list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) -# wsjcpp-core:v0.1.7 +# wsjcpp-core:v0.2.1 list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/") list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp") list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h") -list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h") -list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.cpp") - -# required-libraries -list (APPEND WSJCPP_LIBRARIES "-lpthread") diff --git a/src.wsjcpp/wsjcpp_core/generate.Class b/src.wsjcpp/wsjcpp_core/generate.Class.wsjcpp-script similarity index 100% rename from src.wsjcpp/wsjcpp_core/generate.Class rename to src.wsjcpp/wsjcpp_core/generate.Class.wsjcpp-script diff --git a/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest.wsjcpp-script b/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest.wsjcpp-script new file mode 100644 index 0000000..fdf4902 --- /dev/null +++ b/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest.wsjcpp-script @@ -0,0 +1,70 @@ +#!/usr/bin/wsjcpp-safe-scripting + +# log_info rootdir +# log_info script_filename + +make_dir "./unit-tests.wsjcpp" +make_dir "./unit-tests.wsjcpp/src" + +var class_name +set_value class_name arg1 +normalize_class_name class_name + +var filename_cpp +set_value filename_cpp arg2 + +var content_source +concat content_source " +#include +#include + +// --------------------------------------------------------------------- +// " class_name " + +class " class_name " : public WsjcppUnitTestBase { + public: + " class_name "(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; +}; + +REGISTRY_WSJCPP_UNIT_TEST(" class_name ") + +" class_name "::" class_name "() + : WsjcppUnitTestBase(\"" class_name "\") { +} + +// --------------------------------------------------------------------- + +bool " class_name "::doBeforeTest() { + // do something before test + return true; +} + +// --------------------------------------------------------------------- + +void " class_name "::executeTest() { + compare(\"Not implemented\", true, false); + // TODO unit test source code here +} + +// --------------------------------------------------------------------- + +bool " class_name "::doAfterTest() { + // do somethig after test + return true; +} + +" + +write_file filename_cpp content_source + +log_info " +====== +Generated class: + - " class_name " +Generated files: + - " filename_cpp " +====== +" diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml b/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml index 32119e7..183d7b6 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml +++ b/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml @@ -3,7 +3,7 @@ cmake_cxx_standard: 11 cmake_minimum_required: 3.0 name: wsjcpp-core -version: v0.1.7 +version: v0.2.1 description: Basic Utils for wsjcpp issues: https://github.com/wsjcpp/wsjcpp-core/issues repositories: @@ -16,33 +16,36 @@ keywords: authors: - name: Evgenii Sopov email: mrseakg@gmail.com - distribution: - source-file: src/wsjcpp_core.cpp target-file: wsjcpp_core.cpp type: "source-code" + sha1: "09ef821bbc090fc1cd8a15bc4a57a9a2ce8ae00d" - source-file: src/wsjcpp_core.h target-file: wsjcpp_core.h type: "source-code" # todo must be header-file + sha1: "e6e4ab2067d3c942db08e3b79862486eaf851e4b" - source-file: "src/wsjcpp_unit_tests.cpp" target-file: "wsjcpp_unit_tests.cpp" type: "unit-tests" + sha1: "fd5989d1a83c8b90bdc4d5e9bc9c3051eaa1e6d2" - source-file: "src/wsjcpp_unit_tests.h" target-file: "wsjcpp_unit_tests.h" type: "unit-tests" + sha1: "83d4b6e046b6b58c42882ccae4be413e03c401c1" - source-file: "src/wsjcpp_unit_tests_main.cpp" target-file: "wsjcpp_unit_tests_main.cpp" type: "unit-tests" - - source-file: "scripts.wsjcpp/generate.Class" - target-file: "generate.Class" + sha1: "388ae269b325c5e161f6c3a5c598575714a4bffc" + - source-file: "scripts.wsjcpp/generate.WsjcppUnitTest.wsjcpp-script" + target-file: "generate.WsjcppUnitTest.wsjcpp-script" + type: "safe-scripting-generate" + sha1: "a7c9c2d19bf81c5b00e659384b0b92a99319a4c1" + - source-file: "scripts.wsjcpp/generate.Class.wsjcpp-script" + target-file: "generate.Class.wsjcpp-script" type: "safe-scripting-generate" - - source-file: "src/wsjcpp_resources_manager.h" - target-file: "wsjcpp_resources_manager.h" - type: "source-code" - - source-file: "src/wsjcpp_resources_manager.cpp" - target-file: "wsjcpp_resources_manager.cpp" - type: "source-code" + sha1: "de1799907c685d606b93e08b821b540c2faa2db1" unit-tests: cases: - name: CoreNormalizePath @@ -79,3 +82,9 @@ unit-tests: description: "Test basic resources" - name: "ListOfDirs" description: "Check list of directories" + - name: "FilePermissions" + description: "" + - name: "StringPadding" + description: "" + - name: "DateTimeFormat" + description: "" diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp index 188fda4..5e937f3 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp @@ -1,6 +1,7 @@ #include "wsjcpp_core.h" #include #include +#include #include #include #include @@ -16,10 +17,237 @@ #include #include #include -#include -#include +// #include #include #include +#include + +// --------------------------------------------------------------------- +// WsjcppFilePermissions + +WsjcppFilePermissions::WsjcppFilePermissions() { + // default permissions + m_bOwnerReadFlag = true; + m_bOwnerWriteFlag = true; + m_bOwnerExecuteFlag = false; + m_bGroupReadFlag = false; + m_bGroupWriteFlag = false; + m_bGroupExecuteFlag = false; + m_bOtherReadFlag = true; + m_bOtherWriteFlag = false; + m_bOtherExecuteFlag = false; +} + +WsjcppFilePermissions::WsjcppFilePermissions( + bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, + bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag, + bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag +) { + m_bOwnerReadFlag = bOwnerReadFlag; + m_bOwnerWriteFlag = bOwnerWriteFlag; + m_bOwnerExecuteFlag = bOwnerExecuteFlag; + m_bGroupReadFlag = bGroupReadFlag; + m_bGroupWriteFlag = bGroupWriteFlag; + m_bGroupExecuteFlag = bGroupExecuteFlag; + m_bOtherReadFlag = bOtherReadFlag; + m_bOtherWriteFlag = bOtherWriteFlag; + m_bOtherExecuteFlag = bOtherExecuteFlag; +} + +WsjcppFilePermissions::WsjcppFilePermissions(uint16_t nFilePermission) { + + // owner + m_bOwnerReadFlag = nFilePermission & 0x0400; + m_bOwnerWriteFlag = nFilePermission & 0x0200; + m_bOwnerExecuteFlag = nFilePermission & 0x0100; + + // group + m_bGroupReadFlag = nFilePermission & 0x0040; + m_bGroupWriteFlag = nFilePermission & 0x0020; + m_bGroupExecuteFlag = nFilePermission & 0x0010; + + // for other + m_bOtherReadFlag = nFilePermission & 0x0004; + m_bOtherWriteFlag = nFilePermission & 0x0002; + m_bOtherExecuteFlag = nFilePermission & 0x0001; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerReadFlag(bool bOwnerReadFlag) { + m_bOwnerReadFlag = bOwnerReadFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOwnerReadFlag() const { + return m_bOwnerReadFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerWriteFlag(bool bOwnerWriteFlag) { + m_bOwnerWriteFlag = bOwnerWriteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOwnerWriteFlag() const { + return m_bOwnerWriteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerExecuteFlag(bool bOwnerExecuteFlag) { + m_bOwnerExecuteFlag = bOwnerExecuteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOwnerExecuteFlag() const { + return m_bOwnerExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag) { + m_bOwnerReadFlag = bOwnerReadFlag; + m_bOwnerWriteFlag = bOwnerWriteFlag; + m_bOwnerExecuteFlag = bOwnerExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupReadFlag(bool bGroupReadFlag) { + m_bGroupReadFlag = bGroupReadFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getGroupReadFlag() const { + return m_bGroupReadFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupWriteFlag(bool bGroupWriteFlag) { + m_bGroupWriteFlag = bGroupWriteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getGroupWriteFlag() const { + return m_bGroupWriteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupExecuteFlag(bool bGroupExecuteFlag) { + m_bGroupExecuteFlag = bGroupExecuteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getGroupExecuteFlag() const { + return m_bGroupExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag) { + m_bGroupReadFlag = bGroupReadFlag; + m_bGroupWriteFlag = bGroupWriteFlag; + m_bGroupExecuteFlag = bGroupExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherReadFlag(bool bOtherReadFlag) { + m_bOtherReadFlag = bOtherReadFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOtherReadFlag() const { + return m_bOtherReadFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherWriteFlag(bool bOtherWriteFlag) { + m_bOtherWriteFlag = bOtherWriteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOtherWriteFlag() const { + return m_bOtherWriteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherExecuteFlag(bool bOtherExecuteFlag) { + m_bOtherExecuteFlag = bOtherExecuteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOtherExecuteFlag() const { + return m_bOtherExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag) { + m_bOtherReadFlag = bOtherReadFlag; + m_bOtherWriteFlag = bOtherWriteFlag; + m_bOtherExecuteFlag = bOtherExecuteFlag; +} + +// --------------------------------------------------------------------- + +std::string WsjcppFilePermissions::toString() const { + std::string sRet = "-"; + + // owner + sRet += m_bOwnerReadFlag ? "r" : "-"; + sRet += m_bOwnerWriteFlag ? "w" : "-"; + sRet += m_bOwnerExecuteFlag ? "x" : "-"; + + // group + sRet += m_bGroupReadFlag ? "r" : "-"; + sRet += m_bGroupWriteFlag ? "w" : "-"; + sRet += m_bGroupExecuteFlag ? "x" : "-"; + + // for other + sRet += m_bOtherReadFlag ? "r" : "-"; + sRet += m_bOtherWriteFlag ? "w" : "-"; + sRet += m_bOtherExecuteFlag ? "x" : "-"; + + return sRet; +} + +// --------------------------------------------------------------------- + +uint16_t WsjcppFilePermissions::toUInt16() const { + uint16_t nRet = 0x0; + // owner + nRet |= m_bOwnerReadFlag ? 0x0400 : 0x0; + nRet |= m_bOwnerWriteFlag ? 0x0200 : 0x0; + nRet |= m_bOwnerExecuteFlag ? 0x0100 : 0x0; + + // group + nRet += m_bGroupReadFlag ? 0x0040 : 0x0; + nRet += m_bGroupWriteFlag ? 0x0020 : 0x0; + nRet += m_bGroupExecuteFlag ? 0x0010 : 0x0; + + // for other + nRet += m_bOtherReadFlag ? 0x0004 : 0x0; + nRet += m_bOtherWriteFlag ? 0x0002 : 0x0; + nRet += m_bOtherExecuteFlag ? 0x0001 : 0x0; + return nRet; +} + // --------------------------------------------------------------------- // WsjcppCore @@ -139,28 +367,35 @@ std::string WsjcppCore::getCurrentDirectory() { // --------------------------------------------------------------------- -long WsjcppCore::currentTime_milliseconds() { +long WsjcppCore::getCurrentTimeInMilliseconds() { long nTimeStart = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); return nTimeStart; } // --------------------------------------------------------------------- -long WsjcppCore::currentTime_seconds() { +long WsjcppCore::getCurrentTimeInSeconds() { long nTimeStart = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); return nTimeStart; } // --------------------------------------------------------------------- -std::string WsjcppCore::currentTime_logformat() { - long nTimeStart = WsjcppCore::currentTime_milliseconds(); +std::string WsjcppCore::getCurrentTimeForFilename() { + long nTimeStart = WsjcppCore::getCurrentTimeInSeconds(); + return WsjcppCore::formatTimeForFilename(nTimeStart); +} + +// --------------------------------------------------------------------- + +std::string WsjcppCore::getCurrentTimeForLogFormat() { + long nTimeStart = WsjcppCore::getCurrentTimeInMilliseconds(); std::string sMilliseconds = std::to_string(int(nTimeStart % 1000)); nTimeStart = nTimeStart / 1000; std::time_t tm_ = long(nTimeStart); - // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -171,11 +406,14 @@ std::string WsjcppCore::currentTime_logformat() { // --------------------------------------------------------------------- -std::string WsjcppCore::threadId() { +std::string WsjcppCore::getThreadId() { + + static_assert(sizeof(std::thread::id)==sizeof(uint64_t),"this function only works if size of thead::id is equal to the size of uint_64"); std::thread::id this_id = std::this_thread::get_id(); + uint64_t val = *((uint64_t*) &this_id); std::stringstream stream; - stream << std::hex << this_id; - return "0x" + std::string(stream.str()); + stream << "0x" << std::setw(16) << std::setfill('0') << std::hex << val; + return std::string(stream.str()); } // --------------------------------------------------------------------- @@ -183,8 +421,8 @@ std::string WsjcppCore::threadId() { std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) { std::time_t tm_ = long(nTimeInSec); // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); - + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -194,19 +432,15 @@ std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) { return std::string(buf); } -// --------------------------------------------------------------------- -std::string WsjcppCore::currentTime_forFilename() { - long nTimeStart = WsjcppCore::currentTime_seconds(); - return WsjcppCore::formatTimeForFilename(nTimeStart); -} // --------------------------------------------------------------------- std::string WsjcppCore::formatTimeForFilename(long nTimeInSec) { std::time_t tm_ = long(nTimeInSec); // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -221,7 +455,8 @@ std::string WsjcppCore::formatTimeUTC(int nTimeInSec) { // datetime std::time_t tm_ = long(nTimeInSec); // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -723,8 +958,6 @@ bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::s return true; } - - // --------------------------------------------------------------------- bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) { @@ -755,6 +988,93 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) { return true; } +// --------------------------------------------------------------------- + +bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError) { + + mode_t m; + + // owner + m |= filePermissions.getOwnerReadFlag() ? S_IRUSR : 0x0; + m |= filePermissions.getOwnerWriteFlag() ? S_IWUSR : 0x0; + m |= filePermissions.getOwnerExecuteFlag() ? S_IXUSR : 0x0; + + // group + m |= filePermissions.getGroupReadFlag() ? S_IRGRP : 0x0; + m |= filePermissions.getGroupWriteFlag() ? S_IWGRP : 0x0; + m |= filePermissions.getGroupExecuteFlag() ? S_IXGRP : 0x0; + + // for other + m |= filePermissions.getOtherReadFlag() ? S_IROTH : 0x0; + m |= filePermissions.getOtherWriteFlag() ? S_IWOTH : 0x0; + m |= filePermissions.getOtherExecuteFlag() ? S_IXOTH : 0x0; + + if (chmod(sFilePath.c_str(), m) != 0) { + sError = "Could not change permissions for: '" + sFilePath + "'"; + return false; + } + return true; +} + +// --------------------------------------------------------------------- + +bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError) { + if (!WsjcppCore::fileExists(sFilePath)) { + sError = "File '" + sFilePath + "' - not found"; + return false; + } + + struct stat fileStat; + if (stat(sFilePath.c_str(), &fileStat) < 0) { + sError = "Could not get info about file '" + sFilePath + "'."; + return false; + } + + mode_t m = fileStat.st_mode; + + // S_ISDIR(fileStat.st_mode)) ? "d" : "-" + + // owner + filePermissions.setOwnerReadFlag(m & S_IRUSR); + filePermissions.setOwnerWriteFlag(m & S_IWUSR); + filePermissions.setOwnerExecuteFlag(m & S_IXUSR); + + + // group + filePermissions.setGroupReadFlag(m & S_IRGRP); + filePermissions.setGroupWriteFlag(m & S_IWGRP); + filePermissions.setGroupExecuteFlag(m & S_IXGRP); + + // for other + filePermissions.setOtherReadFlag(m & S_IROTH); + filePermissions.setOtherWriteFlag(m & S_IWOTH); + filePermissions.setOtherExecuteFlag(m & S_IXOTH); + + return true; +} + +// --------------------------------------------------------------------- + +std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, int nLength) { + std::string sRet; + int nPadLen = nLength - sIn.length(); + for (int i = 0; i < nPadLen; i++) { + sRet += cWhat; + } + return sRet + sIn; +} + +// --------------------------------------------------------------------- + +std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, int nLength) { + std::string sRet; + int nPadLen = nLength - sIn.length(); + for (int i = 0; i < nPadLen; i++) { + sRet += cWhat; + } + return sIn + sRet; +} + // --------------------------------------------------------------------- // WsjcppLog @@ -771,7 +1091,7 @@ WsjcppLogGlobalConf::WsjcppLogGlobalConf() { // --------------------------------------------------------------------- void WsjcppLogGlobalConf::doLogRotateUpdateFilename(bool bForce) { - long t = WsjcppCore::currentTime_seconds(); + long t = WsjcppCore::getCurrentTimeInSeconds(); long nEverySeconds = logRotationPeriodInSeconds; // rotate log if started now or if time left more then 1 day if (logStartTime == 0 || t - logStartTime > nEverySeconds || bForce) { logStartTime = t; @@ -869,7 +1189,7 @@ void WsjcppLog::add(WsjcppColorModifier &clr, const std::string &sType, const st std::lock_guard lock(WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logMutex); WsjcppColorModifier def(WsjcppColorCode::FG_DEFAULT); - std::string sLogMessage = WsjcppCore::currentTime_logformat() + ", " + WsjcppCore::threadId() + std::string sLogMessage = WsjcppCore::getCurrentTimeForLogFormat() + ", " + WsjcppCore::getThreadId() + " [" + sType + "] " + sTag + ": " + sMessage; std::cout << clr << sLogMessage << def << std::endl; @@ -893,4 +1213,122 @@ void WsjcppLog::add(WsjcppColorModifier &clr, const std::string &sType, const st } } +// --------------------------------------------------------------------- +// WsjcppResourceFile + +WsjcppResourceFile::WsjcppResourceFile() { + WsjcppResourcesManager::add(this); +} + +// --------------------------------------------------------------------- + + +// --------------------------------------------------------------------- +// WsjcppResourcesManager + +std::vector *g_pWsjcppResourceFiles = nullptr; + +void WsjcppResourcesManager::initGlobalVariables() { + if (g_pWsjcppResourceFiles == nullptr) { + g_pWsjcppResourceFiles = new std::vector(); + } +} + +// --------------------------------------------------------------------- + +void WsjcppResourcesManager::add(WsjcppResourceFile* pStorage) { + WsjcppResourcesManager::initGlobalVariables(); + g_pWsjcppResourceFiles->push_back(pStorage); +} + +// --------------------------------------------------------------------- + +bool WsjcppResourcesManager::has(const std::string &sFilename) { + WsjcppResourcesManager::initGlobalVariables(); + for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) { + if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) { + return true; + } + } + return false; +} + +// --------------------------------------------------------------------- + +WsjcppResourceFile* WsjcppResourcesManager::get(const std::string &sFilename) { + WsjcppResourcesManager::initGlobalVariables(); + for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) { + if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) { + return WsjcppResourcesManager::list()[i]; + } + } + return nullptr; +} + +// --------------------------------------------------------------------- + +const std::vector &WsjcppResourcesManager::list() { + return *g_pWsjcppResourceFiles; +} + +// --------------------------------------------------------------------- + +/* +bool WsjcppResourcesManager::make(const std::string &sWorkspace) { + if (!WsjcppResourcesManager::createFolders(sWorkspace)) { + return false; + } + return WsjcppResourcesManager::extractFiles(sWorkspace); +} + +// --------------------------------------------------------------------- + +bool WsjcppResourcesManager::createFolders(const std::string &sWorkspace) { + // prepare folders + std::vector vCreateDirs; + vCreateDirs.push_back(sWorkspace + "/logs"); + vCreateDirs.push_back(sWorkspace + "/teams"); + vCreateDirs.push_back(sWorkspace + "/checkers"); + vCreateDirs.push_back(sWorkspace + "/html"); + vCreateDirs.push_back(sWorkspace + "/html/css"); + vCreateDirs.push_back(sWorkspace + "/html/js"); + vCreateDirs.push_back(sWorkspace + "/html/images"); + vCreateDirs.push_back(sWorkspace + "/html/images/teams"); + vCreateDirs.push_back(sWorkspace + "/html/images/states"); + + for(int i = 0; i < vCreateDirs.size(); i++) { + std::string sPath = vCreateDirs[i]; + // check dir existing + if (!FS::dirExists(sPath)) { + // try make dir + if (!FS::makeDir(sPath)) { + std::cout << "Could not create folder " << sPath << std::endl; + return false; + } else { + std::cout << "Created folder " << sPath << std::endl; + } + } + } + return true; +} + +// --------------------------------------------------------------------- + +bool WsjcppResourcesManager::extractFiles(const std::string &sWorkspace) { + // TODO mkdir -p for files + const std::vector list = WsjcppResourcesManager::list(); + for(int i = 0; i < list.size(); i++) { + std::string sFilename = sWorkspace + "/" + list[i]->filename(); + if (!FS::fileExists(sFilename)) { + if (!FS::writeFile(sFilename, list[i]->buffer(), list[i]->bufferSize())) { + std::cout << "Could not write file " << sFilename << std::endl; + return false; + } else { + std::cout << "Created file " << sFilename << std::endl; + } + } + } + return true; +} +*/ diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_core.h b/src.wsjcpp/wsjcpp_core/wsjcpp_core.h index b990460..249c693 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_core.h +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_core.h @@ -8,6 +8,58 @@ #include #include +class WsjcppFilePermissions { + public: + WsjcppFilePermissions(); + WsjcppFilePermissions( + bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, + bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag, + bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag + ); + WsjcppFilePermissions(uint16_t nFilePermission); + + // owner flags + void setOwnerReadFlag(bool bOwnerReadFlag); + bool getOwnerReadFlag() const; + void setOwnerWriteFlag(bool bOwnerWriteFlag); + bool getOwnerWriteFlag() const; + void setOwnerExecuteFlag(bool bOwnerExecuteFlag); + bool getOwnerExecuteFlag() const; + void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag); + + // group flags + void setGroupReadFlag(bool bGroupReadFlag); + bool getGroupReadFlag() const; + void setGroupWriteFlag(bool bGroupWriteFlag); + bool getGroupWriteFlag() const; + void setGroupExecuteFlag(bool bGroupExecuteFlag); + bool getGroupExecuteFlag() const; + void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag); + + // for other flags + void setOtherReadFlag(bool bOtherReadFlag); + bool getOtherReadFlag() const; + void setOtherWriteFlag(bool bOtherWriteFlag); + bool getOtherWriteFlag() const; + void setOtherExecuteFlag(bool bOtherExecuteFlag); + bool getOtherExecuteFlag() const; + void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag); + + std::string toString() const; + uint16_t toUInt16() const; + + private: + bool m_bOwnerReadFlag; + bool m_bOwnerWriteFlag; + bool m_bOwnerExecuteFlag; + bool m_bGroupReadFlag; + bool m_bGroupWriteFlag; + bool m_bGroupExecuteFlag; + bool m_bOtherReadFlag; + bool m_bOtherWriteFlag; + bool m_bOtherExecuteFlag; +}; + class WsjcppCore { public: static bool init( @@ -22,11 +74,11 @@ class WsjcppCore { static std::string extractFilename(const std::string &sPath); static std::string getCurrentDirectory(); - static long currentTime_milliseconds(); - static long currentTime_seconds(); - static std::string currentTime_forFilename(); - static std::string currentTime_logformat(); - static std::string threadId(); + static long getCurrentTimeInMilliseconds(); + static long getCurrentTimeInSeconds(); + static std::string getCurrentTimeForFilename(); + static std::string getCurrentTimeForLogFormat(); + static std::string getThreadId(); static std::string formatTimeForWeb(long nTimeInSec); static std::string formatTimeForFilename(long nTimeInSec); static std::string formatTimeUTC(int nTimeInSec); @@ -71,6 +123,13 @@ class WsjcppCore { static bool recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir); static bool recoursiveRemoveDir(const std::string& sDir); + + static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError); + static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError); + + static std::string doPadLeft(const std::string& sIn, char cWhat, int nLength); + static std::string doPadRight(const std::string& sIn, char cWhat, int nLength); + }; @@ -135,6 +194,42 @@ class WsjcppLog { static void add(WsjcppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage); }; +// --------------------------------------------------------------------- +// WsjcppResourceFile + +class WsjcppResourceFile { + public: + WsjcppResourceFile(); + virtual const std::string &getFilename() const = 0; + virtual const std::string &getPackAs() const = 0; + virtual int getBufferSize() const = 0; + virtual const char *getBuffer() const = 0; +}; + + +// --------------------------------------------------------------------- +// WsjcppResourcesManager + +extern std::vector *g_pWsjcppResourceFiles; + +class WsjcppResourcesManager { + public: + static void initGlobalVariables(); + static void add(WsjcppResourceFile*); + static const std::vector &list(); + static bool has(const std::string &sFilename); + static WsjcppResourceFile* get(const std::string &sFilename); + static bool make(const std::string &sWorkspace); + // static bool createFolders(const std::string &sWorkspace); + // static bool extractFiles(const std::string &sWorkspace); +}; + +// --------------------------------------------------------------------- +// Registry WsjcppResourceFile +#define REGISTRY_WSJCPP_RESOURCE_FILE( classname ) \ + static classname * pRegistryWsjcppResourceFile ## classname = new classname(); \ + + #endif // WSJCPP_CORE_H diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.cpp deleted file mode 100644 index 062b617..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include "wsjcpp_resources_manager.h" - -// --------------------------------------------------------------------- - -WsjcppResourceFile::WsjcppResourceFile() { - WsjcppResourcesManager::add(this); -} - -// --------------------------------------------------------------------- - -std::vector *g_pWsjcppResourceFiles = nullptr; - -// --------------------------------------------------------------------- - -void WsjcppResourcesManager::initGlobalVariables() { - if (g_pWsjcppResourceFiles == nullptr) { - g_pWsjcppResourceFiles = new std::vector(); - } -} - -// --------------------------------------------------------------------- - -void WsjcppResourcesManager::add(WsjcppResourceFile* pStorage) { - WsjcppResourcesManager::initGlobalVariables(); - g_pWsjcppResourceFiles->push_back(pStorage); -} - -// --------------------------------------------------------------------- - -bool WsjcppResourcesManager::has(const std::string &sFilename) { - WsjcppResourcesManager::initGlobalVariables(); - for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) { - if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) { - return true; - } - } - return false; -} - -// --------------------------------------------------------------------- - -WsjcppResourceFile* WsjcppResourcesManager::get(const std::string &sFilename) { - WsjcppResourcesManager::initGlobalVariables(); - for (int i = 0; i < WsjcppResourcesManager::list().size(); i++) { - if (WsjcppResourcesManager::list()[i]->getFilename() == sFilename) { - return WsjcppResourcesManager::list()[i]; - } - } - return nullptr; -} - -// --------------------------------------------------------------------- - -const std::vector &WsjcppResourcesManager::list() { - return *g_pWsjcppResourceFiles; -} - -// --------------------------------------------------------------------- - -/* -bool WsjcppResourcesManager::make(const std::string &sWorkspace) { - if (!WsjcppResourcesManager::createFolders(sWorkspace)) { - return false; - } - return WsjcppResourcesManager::extractFiles(sWorkspace); -} - -// --------------------------------------------------------------------- - -bool WsjcppResourcesManager::createFolders(const std::string &sWorkspace) { - // prepare folders - std::vector vCreateDirs; - vCreateDirs.push_back(sWorkspace + "/logs"); - vCreateDirs.push_back(sWorkspace + "/teams"); - vCreateDirs.push_back(sWorkspace + "/checkers"); - vCreateDirs.push_back(sWorkspace + "/html"); - vCreateDirs.push_back(sWorkspace + "/html/css"); - vCreateDirs.push_back(sWorkspace + "/html/js"); - vCreateDirs.push_back(sWorkspace + "/html/images"); - vCreateDirs.push_back(sWorkspace + "/html/images/teams"); - vCreateDirs.push_back(sWorkspace + "/html/images/states"); - - for(int i = 0; i < vCreateDirs.size(); i++) { - std::string sPath = vCreateDirs[i]; - // check dir existing - if (!FS::dirExists(sPath)) { - // try make dir - if (!FS::makeDir(sPath)) { - std::cout << "Could not create folder " << sPath << std::endl; - return false; - } else { - std::cout << "Created folder " << sPath << std::endl; - } - } - } - return true; -} - -// --------------------------------------------------------------------- - -bool WsjcppResourcesManager::extractFiles(const std::string &sWorkspace) { - // TODO mkdir -p for files - const std::vector list = WsjcppResourcesManager::list(); - for(int i = 0; i < list.size(); i++) { - std::string sFilename = sWorkspace + "/" + list[i]->filename(); - if (!FS::fileExists(sFilename)) { - if (!FS::writeFile(sFilename, list[i]->buffer(), list[i]->bufferSize())) { - std::cout << "Could not write file " << sFilename << std::endl; - return false; - } else { - std::cout << "Created file " << sFilename << std::endl; - } - } - } - return true; -} -*/ \ No newline at end of file diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h b/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h deleted file mode 100644 index efba363..0000000 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef WSJCPP_RESOURCES_MANAGER_H -#define WSJCPP_RESOURCES_MANAGER_H - -#include -#include -#include -#include - -class WsjcppResourceFile { - public: - WsjcppResourceFile(); - virtual const std::string &getFilename() = 0; - virtual const std::string &getPackAs() = 0; - virtual const int getBufferSize() = 0; - virtual const char *getBuffer() = 0; -}; - -extern std::vector *g_pWsjcppResourceFiles; - -class WsjcppResourcesManager { - public: - static void initGlobalVariables(); - static void add(WsjcppResourceFile*); - static const std::vector &list(); - static bool has(const std::string &sFilename); - static WsjcppResourceFile* get(const std::string &sFilename); - static bool make(const std::string &sWorkspace); - // static bool createFolders(const std::string &sWorkspace); - // static bool extractFiles(const std::string &sWorkspace); -}; - -// --------------------------------------------------------------------- -// Registry WsjcppResourceFile -#define REGISTRY_WSJCPP_RESOURCE_FILE( classname ) \ - static classname * pRegistryWsjcppResourceFile ## classname = new classname(); \ - -#endif // WSJCPP_RESOURCES_MANAGER_H \ No newline at end of file diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp index d675dac..bd00383 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp @@ -1,56 +1,74 @@ #include "wsjcpp_unit_tests.h" +#include + +// --------------------------------------------------------------------- +// WsjcppUnitTestBase WsjcppUnitTestBase::WsjcppUnitTestBase(const std::string &sTestName) { m_sTestName = sTestName; TAG = m_sTestName; + m_bTestResult = true; WsjcppUnitTests::addUnitTest(sTestName, this); } // --------------------------------------------------------------------- -std::string WsjcppUnitTestBase::name() { +std::string WsjcppUnitTestBase::getName() { return m_sTestName; } // --------------------------------------------------------------------- -void WsjcppUnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint, - const std::string &sValue, const std::string &sExpected) { - if (sValue != sExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + sExpected + "', but got '" + sValue + "'"); - bTestSuccess = false; - } +void WsjcppUnitTestBase::ok(const std::string &sSuccessMessage) { + // print obly success message + WsjcppLog::ok(TAG, sSuccessMessage); } // --------------------------------------------------------------------- -bool WsjcppUnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) { - if (nValue != nExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); - bTestSuccess = false; - return false; - } - return true; +void WsjcppUnitTestBase::fail(const std::string &sFailedMessage) { + WsjcppLog::err(TAG, sFailedMessage); + m_bTestResult = false; } // --------------------------------------------------------------------- -bool WsjcppUnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected) { - if (nValue != nExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); - bTestSuccess = false; +bool WsjcppUnitTestBase::runTest() { + WsjcppLog::info(TAG, "Start unit-test"); + WsjcppLog::info(TAG, "Do before unit-test"); + if (!doBeforeTest()) { + fail("Problem with before unit-test"); return false; } - return true; + WsjcppLog::info(TAG, "Execute unit-test"); + try { + executeTest(); + } catch(const std::exception& e) { + fail(e.what()); + } catch(...) { + + } + if (m_bTestResult) { + ok("Test passed."); + } else { + fail("Test failed."); + } + WsjcppLog::info(TAG, "Do after unit-test"); + if (!doAfterTest()) { + fail("Problem with after unit-test"); + } + WsjcppLog::info(TAG, "End unit-test"); + return m_bTestResult; } // --------------------------------------------------------------------- -void WsjcppUnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) { - if (bValue != bExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'"); - bTestSuccess = false; +bool WsjcppUnitTestBase::compareD(const std::string &sMark, double nValue, double nExpected) { + if (abs(nValue - nExpected) > std::numeric_limits::epsilon()) { + fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); + return false; } + return true; } // --------------------------------------------------------------------- @@ -71,7 +89,7 @@ void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBa bool bFound = false; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* p = g_pWsjcppUnitTests->at(i); - if (p->name() == sTestName) { + if (p->getName() == sTestName) { bFound = true; } } diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h index b3213db..c2d8eff 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h @@ -1,25 +1,35 @@ #ifndef WSJCPP_UNIT_TESTS_H #define WSJCPP_UNIT_TESTS_H -#include -#include #include +#include class WsjcppUnitTestBase { public: WsjcppUnitTestBase(const std::string &sTestName); - std::string name(); - virtual void init() = 0; - virtual bool run() = 0; + std::string getName(); + void ok(const std::string &sSuccessMessage); + void fail(const std::string &sFailedMessage); + bool runTest(); + + virtual bool doBeforeTest() = 0; + virtual void executeTest() = 0; + virtual bool doAfterTest() = 0; protected: std::string TAG; - - void compareS(bool &bTestSuccess, const std::string &sPoint, const std::string &sValue, const std::string &sExpected); - bool compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected); - bool compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected); - void compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected); - + + bool compareD(const std::string &sMark, double nValue, double nExpected); + template bool compare(const std::string &sMark, T1 tGotValue, T2 tExpectedValue) { + if (tGotValue != tExpectedValue) { + std::stringstream ss; + ss << " {mark: " << sMark << "} Expected '" << tExpectedValue << "', but got '" << tGotValue << "'"; + fail(ss.str()); + return false; + } + return true; + } private: + bool m_bTestResult; std::string m_sTestName; }; diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp index 3d66672..c66e9d2 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp @@ -36,16 +36,17 @@ int main(int argc, char** argv) { int nSuccess = 0; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - std::string sTestName = pUnitTest->name(); - WsjcppLog::info(TAG, "Run test " + sTestName); - if (pUnitTest->run()) { - WsjcppLog::ok(sTestName, "Test passed"); + if (pUnitTest->runTest()) { nSuccess++; - } else { - WsjcppLog::err(sTestName, "Test failed"); } } - WsjcppLog::info(TAG, "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); + if (nSuccess == nAll) { + WsjcppLog::ok(TAG, "All unit-tests passed " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); + } else { + WsjcppLog::warn(TAG, "Passed unit-tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); + WsjcppLog::err(TAG, std::to_string(nAll - nSuccess) + " unit-test(s) failed."); + } + bool bResult = nSuccess == nAll; return bResult ? 0 : -1; } else if (argc == 2) { @@ -54,7 +55,7 @@ int main(int argc, char** argv) { std::string sOutput = "\nList of unit-tests:\n"; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - sOutput += " - " + pUnitTest->name() + "\n"; + sOutput += " - " + pUnitTest->getName() + "\n"; } WsjcppLog::info(TAG, sOutput); return -1; @@ -70,15 +71,11 @@ int main(int argc, char** argv) { bool bTestFound = false; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - if (pUnitTest->name() == sArg3) { + if (pUnitTest->getName() == sArg3) { bTestFound = true; - WsjcppLog::info(TAG, "Run test " + pUnitTest->name()); - bool bResult = pUnitTest->run(); + bool bResult = pUnitTest->runTest(); if (bResult) { - WsjcppLog::ok(TAG, pUnitTest->name() + " Test passed"); nSuccess++; - } else { - WsjcppLog::err(TAG, pUnitTest->name() + " Test failed"); } } } diff --git a/unit-tests.wsjcpp/.gitkeeper b/unit-tests.wsjcpp/.gitkeeper deleted file mode 100644 index e69de29..0000000 diff --git a/unit-tests.wsjcpp/CMakeLists.txt b/unit-tests.wsjcpp/CMakeLists.txt index f0100f4..099e88c 100644 --- a/unit-tests.wsjcpp/CMakeLists.txt +++ b/unit-tests.wsjcpp/CMakeLists.txt @@ -1,8 +1,8 @@ -# Automaticly generated by wsjcpp@v0.1.5 +# Automaticly generated by wsjcpp@v0.2.0 cmake_minimum_required(VERSION 3.0) project(unit-tests C CXX) -add_definitions(-DWSJCPP_APP_VERSION="ut-v0.0.1") +add_definitions(-DWSJCPP_APP_VERSION="ut-v0.0.2") add_definitions(-DWSJCPP_APP_NAME="unit-tests-wsjcpp-async-jobs-pool") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") @@ -19,29 +19,23 @@ set (WSJCPP_SOURCES "") find_package(Threads REQUIRED) list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) -# wsjcpp-core:v0.1.7 +# wsjcpp-core:v0.2.1 list (APPEND WSJCPP_INCLUDE_DIRS "../src.wsjcpp/wsjcpp_core/") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_core.h") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp") -list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h") -list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.cpp") -# wsjcpp-async-jobs-pool:v0.0.1 +# wsjcpp-async-jobs-pool:v0.0.2 list (APPEND WSJCPP_INCLUDE_DIRS "../src") list (APPEND WSJCPP_SOURCES "../src/wsjcpp_async_jobs_pool.cpp") list (APPEND WSJCPP_SOURCES "../src/wsjcpp_async_jobs_pool.h") # unit-tests list (APPEND WSJCPP_INCLUDE_DIRS "src") -list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_async_jobs_pool.h") list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_async_jobs_pool.cpp") -# required-libraries -list (APPEND WSJCPP_LIBRARIES "-lpthread") - include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt) @@ -51,11 +45,3 @@ add_executable ("unit-tests" ${WSJCPP_SOURCES}) target_link_libraries("unit-tests" -lpthread ${WSJCPP_LIBRARIES} ) -install( - TARGETS - "unit-tests" - RUNTIME DESTINATION - /usr/bin -) - - diff --git a/unit-tests.wsjcpp/build_simple.sh b/unit-tests.wsjcpp/build_simple.sh index 5c3dfd7..8b35d10 100755 --- a/unit-tests.wsjcpp/build_simple.sh +++ b/unit-tests.wsjcpp/build_simple.sh @@ -1,7 +1,7 @@ #!/bin/bash if [ ! -d tmp ]; then - mkdir -p tmp + mkdir -p tmp fi cd tmp diff --git a/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.cpp b/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.cpp index e1ac7cb..1d59387 100644 --- a/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.cpp +++ b/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.cpp @@ -1,8 +1,14 @@ -#include "unit_test_async_jobs_pool.h" -#include -#include +#include #include +class UnitTestAsyncJobsPool : public WsjcppUnitTestBase { + public: + UnitTestAsyncJobsPool(); + virtual bool doBeforeTest(); + virtual void executeTest(); + virtual bool doAfterTest(); +}; + REGISTRY_WSJCPP_UNIT_TEST(UnitTestAsyncJobsPool) UnitTestAsyncJobsPool::UnitTestAsyncJobsPool() @@ -12,12 +18,6 @@ UnitTestAsyncJobsPool::UnitTestAsyncJobsPool() // ---------------------------------------------------------------------- -void UnitTestAsyncJobsPool::init() { - // nothing -} - -// ---------------------------------------------------------------------- - class JobWaiterResult { public: JobWaiterResult() { @@ -54,6 +54,7 @@ class JobAsyncWaiter : public WsjcppAsyncJob { std::this_thread::sleep_for(std::chrono::milliseconds(m_nMilliseconds)); WsjcppLog::info(sWorkerId, "end job " + std::to_string(m_nNumber)); m_pJobWaiterResult->onDone(); + return true; } private: @@ -64,7 +65,14 @@ class JobAsyncWaiter : public WsjcppAsyncJob { // ---------------------------------------------------------------------- -bool UnitTestAsyncJobsPool::run() { +bool UnitTestAsyncJobsPool::doBeforeTest() { + // do nothing + return true; +} + +// ---------------------------------------------------------------------- + +void UnitTestAsyncJobsPool::executeTest() { WsjcppAsyncJobsPool::start(); // TEST waitForDone @@ -76,11 +84,13 @@ bool UnitTestAsyncJobsPool::run() { } WsjcppAsyncJobsPool::waitForDone(); - if (pJobWaiterResult->finishedJobs() != nCountJobs) { - WsjcppLog::err(TAG, "Test waitForDone FAILED expected " + std::to_string(nCountJobs) + ", but got " + std::to_string(pJobWaiterResult->finishedJobs())); - return false; - } + compare("Test waitForDone FAILED", pJobWaiterResult->finishedJobs(), nCountJobs); +} + +// ---------------------------------------------------------------------- +bool UnitTestAsyncJobsPool::doAfterTest() { + // do nothing return true; } diff --git a/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.h b/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.h deleted file mode 100644 index 21bb254..0000000 --- a/unit-tests.wsjcpp/src/unit_test_async_jobs_pool.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef UNIT_TEST_ASYNC_JOBS_POOL_H -#define UNIT_TEST_ASYNC_JOBS_POOL_H - -#include - -class UnitTestAsyncJobsPool : public WsjcppUnitTestBase { - public: - UnitTestAsyncJobsPool(); - virtual void init(); - virtual bool run(); -}; - -#endif // UNIT_TEST_ASYNC_JOBS_POOL_H diff --git a/unit-tests.wsjcpp/src/unit_tests.cpp b/unit-tests.wsjcpp/src/unit_tests.cpp deleted file mode 100644 index a5a09de..0000000 --- a/unit-tests.wsjcpp/src/unit_tests.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include "unit_tests.h" - -UnitTestBase::UnitTestBase(const std::string &sTestName) { - m_sTestName = sTestName; - TAG = m_sTestName; - UnitTests::addUnitTest(sTestName, this); -} - -// --------------------------------------------------------------------- - -std::string UnitTestBase::name() { - return m_sTestName; -} - -// --------------------------------------------------------------------- - -void UnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint, - const std::string &sValue, const std::string &sExpected) { - if (sValue != sExpected) { - WSJCppLog::err(TAG, sPoint + ", expeceted '" + sExpected + "', but got '" + sValue + "'"); - bTestSuccess = false; - } -} - -// --------------------------------------------------------------------- - -bool UnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) { - if (nValue != nExpected) { - WSJCppLog::err(TAG, " {" + sPoint + "} Expeceted '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); - bTestSuccess = false; - return false; - } - return true; -} - -// --------------------------------------------------------------------- - -void UnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) { - if (bValue != bExpected) { - WSJCppLog::err(TAG, sPoint + ", expeceted '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'"); - bTestSuccess = false; - } -} - -// --------------------------------------------------------------------- - -std::vector *g_pUnitTests = NULL; - -void UnitTests::initGlobalVariables() { - if (g_pUnitTests == NULL) { - // WSJCppLog::info(std::string(), "Create handlers map"); - g_pUnitTests = new std::vector(); - } -} - -// --------------------------------------------------------------------- - -void UnitTests::addUnitTest(const std::string &sTestName, UnitTestBase* pUnitTest) { - UnitTests::initGlobalVariables(); - bool bFound = false; - for (int i = 0; i < g_pUnitTests->size(); i++) { - UnitTestBase* p = g_pUnitTests->at(i); - if (p->name() == sTestName) { - bFound = true; - } - } - - if (bFound) { - WSJCppLog::err(sTestName, "Already registered"); - } else { - g_pUnitTests->push_back(pUnitTest); - // WSJCppLog::info(sCmd, "Registered"); - } -} - -// --------------------------------------------------------------------- - -bool UnitTests::runUnitTests() { - UnitTests::initGlobalVariables(); - int nAll = g_pUnitTests->size(); - WSJCppLog::info("runUnitTests", "All tests count " + std::to_string(nAll)); - int nSuccess = 0; - for (int i = 0; i < g_pUnitTests->size(); i++) { - UnitTestBase* pUnitTest = g_pUnitTests->at(i); - std::string sTestName = pUnitTest->name(); - WSJCppLog::info("runUnitTests", "Run test " + sTestName); - if (pUnitTest->run()) { - WSJCppLog::ok(sTestName, "Test passed"); - nSuccess++; - } else { - WSJCppLog::err(sTestName, "Test failed"); - } - } - WSJCppLog::info("runUnitTests", "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); - return nSuccess == nAll; -} - -// --------------------------------------------------------------------- \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_tests.h b/unit-tests.wsjcpp/src/unit_tests.h deleted file mode 100644 index abfc334..0000000 --- a/unit-tests.wsjcpp/src/unit_tests.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef UNIT_TESTS_H -#define UNIT_TESTS_H - -#include -#include -#include - -class UnitTestBase { - public: - UnitTestBase(const std::string &sTestName); - std::string name(); - virtual void init() = 0; - virtual bool run() = 0; - protected: - std::string TAG; - - void compareS(bool &bTestSuccess, const std::string &sPoint, const std::string &sValue, const std::string &sExpected); - bool compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected); - void compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected); - - private: - std::string m_sTestName; -}; - -extern std::vector *g_pUnitTests; - -class UnitTests { - public: - static void initGlobalVariables(); - static void addUnitTest(const std::string &sTestName, UnitTestBase* pCmdHandler); - static bool runUnitTests(); -}; - -// RegistryCmdHandler -#define REGISTRY_UNIT_TEST( classname ) \ - static classname * pRegistryUnitTest ## classname = new classname(); \ - - -#endif // UNIT_TESTS_H \ No newline at end of file diff --git a/wsjcpp.yml b/wsjcpp.yml index 9b78988..1b2095a 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -2,7 +2,7 @@ wsjcpp_version: v0.0.1 cmake_minimum_required: 3.0 cmake_cxx_standard: 11 name: wsjcpp-async-jobs-pool -version: v0.0.1 +version: v0.0.2 description: Multithreading jobs pool (asyncron tasks) issues: https://github.com/wsjcpp/wsjcpp-async-jobs-pool/issues keywords: @@ -12,24 +12,21 @@ keywords: - "async" - "jobs" -required-libraries: - - pthread - -dependencies: - - name: "wsjcpp-core" - version: "v0.1.7" - url: "https://github.com/wsjcpp/wsjcpp-core:master" - origin: "https://github.com/" - installation-dir: "./src.wsjcpp/wsjcpp_core" - repositories: - type: main url: "https://github.com/wsjcpp/wsjcpp-async-jobs-pool" - authors: - name: "Evgenii Sopov" email: "mrseakg@gmail.com" +dependencies: + - name: "wsjcpp-core" + version: "v0.2.1" + url: "https://github.com/wsjcpp/wsjcpp-core:master" + origin: "https://github.com/" + installation-dir: "./src.wsjcpp/wsjcpp_core" + installation-datetime: "Sun, 11 Oct 2020 12:26:32 GMT" + distribution: - source-file: "src/wsjcpp_async_jobs_pool.cpp" target-file: "wsjcpp_async_jobs_pool.cpp"