diff --git a/slsDetectorSoftware/CMakeLists.txt b/slsDetectorSoftware/CMakeLists.txt index aaf0d2bf9..120b3644e 100644 --- a/slsDetectorSoftware/CMakeLists.txt +++ b/slsDetectorSoftware/CMakeLists.txt @@ -1,6 +1,6 @@ set(SOURCES multiSlsDetector/multiSlsDetector.cpp - sharedMemory/SharedMemory.cpp + # sharedMemory/SharedMemory.cpp slsDetector/slsDetectorUsers.cpp slsDetector/slsDetectorCommand.cpp slsDetector/slsDetector.cpp @@ -49,6 +49,10 @@ if(DOXYGEN_FOUND) ) endif() +if (SLS_USE_TESTS) + add_subdirectory(tests) +endif(SLS_USE_TESTS) + install(TARGETS slsDetectorShared EXPORT "${TARGETS_EXPORT_NAME}" diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 9ee47eca2..0c551799e 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -259,7 +259,7 @@ void multiSlsDetector::freeSharedMemory(int multiId, int detPos) { // sharedMultiSlsDetector *mdet = // (sharedMultiSlsDetector *)shm.OpenSharedMemory( // sizeof(sharedMultiSlsDetector)); - shm.OpenSharedMemory(sizeof(sharedMultiSlsDetector)); + shm.OpenSharedMemory(); numDetectors = shm()->numberOfDetectors; shm.UnmapSharedMemory(); shm.RemoveSharedMemory(); @@ -328,13 +328,13 @@ void multiSlsDetector::initSharedMemory(bool verify) { try { // shared memory object with name sharedMemory = new SharedMemory(detId, -1); - size_t sz = sizeof(sharedMultiSlsDetector); + // size_t sz = sizeof(sharedMultiSlsDetector); // create if (!sharedMemory->IsExisting()) { // thisMultiDetector = // (sharedMultiSlsDetector *)sharedMemory->CreateSharedMemory(sz); - sharedMemory->CreateSharedMemory(sz); + sharedMemory->CreateSharedMemory(); thisMultiDetector= (*sharedMemory)(); //TODO remove line initializeDetectorStructure(); } @@ -342,7 +342,7 @@ void multiSlsDetector::initSharedMemory(bool verify) { else { // thisMultiDetector = // (sharedMultiSlsDetector *)sharedMemory->OpenSharedMemory(sz); - sharedMemory->OpenSharedMemory(sz); + sharedMemory->OpenSharedMemory(); thisMultiDetector = (*sharedMemory)(); if (verify && thisMultiDetector->shmversion != MULTI_SHMVERSION) { FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch " diff --git a/slsDetectorSoftware/sharedMemory/SharedMemory.cpp b/slsDetectorSoftware/sharedMemory/SharedMemory.cpp index 97b76f903..f752e5e36 100644 --- a/slsDetectorSoftware/sharedMemory/SharedMemory.cpp +++ b/slsDetectorSoftware/sharedMemory/SharedMemory.cpp @@ -1,178 +1,178 @@ -#include "SharedMemory.h" -#include "sls_detector_exceptions.h" -#include "ansi.h" -#include "logger.h" +// #include "SharedMemory.h" +// #include "sls_detector_exceptions.h" +// #include "ansi.h" +// #include "logger.h" -#include "slsDetector.h" -#include "multiSlsDetector.h" +// #include "slsDetector.h" +// #include "multiSlsDetector.h" -#include -#include // printf -#include // errno -#include // strerror -#include -#include // O_CREAT, O_TRUNC.. -#include // fstat -#include // shared memory -#include -#include "stdlib.h" +// #include +// #include // printf +// #include // errno +// #include // strerror +// #include +// #include // O_CREAT, O_TRUNC.. +// #include // fstat +// #include // shared memory +// #include +// #include "stdlib.h" -#define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_" -#define SHM_SLS_PREFIX "_sls_" -#define SHM_ENV_NAME "SLSDETNAME" +// #define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_" +// #define SHM_SLS_PREFIX "_sls_" +// #define SHM_ENV_NAME "SLSDETNAME" -template -SharedMemory::SharedMemory(int multiId, int slsId): - fd(-1), - shmSize(0) -{ - name = ConstructSharedMemoryName(multiId, slsId); -} +// template +// SharedMemory::SharedMemory(int multiId, int slsId): +// fd(-1), +// shmSize(0) +// { +// name = ConstructSharedMemoryName(multiId, slsId); +// } -template -SharedMemory::~SharedMemory(){ - if (fd >= 0) - close(fd); -} +// template +// SharedMemory::~SharedMemory(){ +// if (fd >= 0) +// close(fd); +// } -template -bool SharedMemory::IsExisting() { - bool ret = true; - int tempfd = shm_open(name.c_str(), O_RDWR, 0); - if ((tempfd < 0) && (errno == ENOENT)) { - ret = false; - } - close(tempfd); - return ret; -} +// template +// bool SharedMemory::IsExisting() { +// bool ret = true; +// int tempfd = shm_open(name.c_str(), O_RDWR, 0); +// if ((tempfd < 0) && (errno == ENOENT)) { +// ret = false; +// } +// close(tempfd); +// return ret; +// } -template -std::string SharedMemory::GetName() { - return name; -} +// template +// std::string SharedMemory::GetName() { +// return name; +// } -template -void SharedMemory::CreateSharedMemory(size_t sz){ - // create - fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); - if (fd < 0) { - FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno); - throw SharedMemoryException(); - } +// template +// void SharedMemory::CreateSharedMemory(size_t sz){ +// // create +// fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); +// if (fd < 0) { +// FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno); +// throw SharedMemoryException(); +// } - // resize - if (ftruncate(fd, sz) < 0) { - FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno); - close(fd); - RemoveSharedMemory(); - throw SharedMemoryException(); - } +// // resize +// if (ftruncate(fd, sz) < 0) { +// FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno); +// close(fd); +// RemoveSharedMemory(); +// throw SharedMemoryException(); +// } - // map - // void* addr = MapSharedMemory(sz); - shared_struct = MapSharedMemory(sz); - FILE_LOG(logINFO) << "Shared memory created " << name; +// // map +// // void* addr = MapSharedMemory(sz); +// shared_struct = MapSharedMemory(sz); +// FILE_LOG(logINFO) << "Shared memory created " << name; - // return addr; -} +// // return addr; +// } -template -void SharedMemory::OpenSharedMemory(size_t sz){ - // open - fd = shm_open(name.c_str(), O_RDWR, 0); - if (fd < 0) { - FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno); - throw SharedMemoryException(); - } +// template +// void SharedMemory::OpenSharedMemory(size_t sz){ +// // open +// fd = shm_open(name.c_str(), O_RDWR, 0); +// if (fd < 0) { +// FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno); +// throw SharedMemoryException(); +// } - shared_struct = MapSharedMemory(sz); - // return MapSharedMemory(sz); -} +// shared_struct = MapSharedMemory(sz); +// // return MapSharedMemory(sz); +// } -template -void SharedMemory::UnmapSharedMemory() { - if (munmap(shared_struct, shmSize) < 0) { - FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno); - close(fd); - throw SharedMemoryException(); - } -} +// template +// void SharedMemory::UnmapSharedMemory() { +// if (munmap(shared_struct, shmSize) < 0) { +// FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno); +// close(fd); +// throw SharedMemoryException(); +// } +// } -template -void SharedMemory::RemoveSharedMemory() { - if (shm_unlink(name.c_str()) < 0) { - // silent exit if shm did not exist anyway - if (errno == ENOENT) - return; - FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno); - throw SharedMemoryException(); - } - FILE_LOG(logINFO) << "Shared memory deleted " << name; -} +// template +// void SharedMemory::RemoveSharedMemory() { +// if (shm_unlink(name.c_str()) < 0) { +// // silent exit if shm did not exist anyway +// if (errno == ENOENT) +// return; +// FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno); +// throw SharedMemoryException(); +// } +// FILE_LOG(logINFO) << "Shared memory deleted " << name; +// } -template -T* SharedMemory::MapSharedMemory(size_t sz) { - void* addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (addr == MAP_FAILED) { - FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno); - close(fd); - throw SharedMemoryException(); - } - shmSize = sz; - close(fd); - return (T*)addr; -} +// template +// T* SharedMemory::MapSharedMemory(size_t sz) { +// void* addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); +// if (addr == MAP_FAILED) { +// FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno); +// close(fd); +// throw SharedMemoryException(); +// } +// shmSize = sz; +// close(fd); +// return (T*)addr; +// } -template -std::string SharedMemory::ConstructSharedMemoryName(int multiId, int slsId) { +// template +// std::string SharedMemory::ConstructSharedMemoryName(int multiId, int slsId) { - // using environment path - std::string sEnvPath = ""; - char* envpath = getenv(SHM_ENV_NAME); - if (envpath != nullptr) { - sEnvPath.assign(envpath); - sEnvPath.insert(0,"_"); - } +// // using environment path +// std::string sEnvPath = ""; +// char* envpath = getenv(SHM_ENV_NAME); +// if (envpath != nullptr) { +// sEnvPath.assign(envpath); +// sEnvPath.insert(0,"_"); +// } - std::stringstream ss; - if (slsId < 0) - ss << SHM_MULTI_PREFIX << multiId << sEnvPath; - else - ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath; +// std::stringstream ss; +// if (slsId < 0) +// ss << SHM_MULTI_PREFIX << multiId << sEnvPath; +// else +// ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath; - std::string temp = ss.str(); - if (temp.length() > NAME_MAX) { - FILE_LOG(logERROR) << "Shared memory initialization failed. " << - temp << " has " << temp.length() << " characters. \n" - "Maximum is " << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME; - throw SharedMemoryException(); - } - return temp; -} +// std::string temp = ss.str(); +// if (temp.length() > NAME_MAX) { +// FILE_LOG(logERROR) << "Shared memory initialization failed. " << +// temp << " has " << temp.length() << " characters. \n" +// "Maximum is " << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME; +// throw SharedMemoryException(); +// } +// return temp; +// // } -template -int SharedMemory::VerifySizeMatch(size_t expectedSize) { - struct stat sb; - // could not fstat - if (fstat(fd, &sb) < 0) { - FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match " - "(could not fstat): " << strerror(errno); - close(fd); - throw SharedMemoryException(); - } +// template +// int SharedMemory::VerifySizeMatch(size_t expectedSize) { +// struct stat sb; +// // could not fstat +// if (fstat(fd, &sb) < 0) { +// FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match " +// "(could not fstat): " << strerror(errno); +// close(fd); +// throw SharedMemoryException(); +// } - //size does not match - long unsigned int sz = (long unsigned int)sb.st_size; - if (sz != expectedSize) { - FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match"; - FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz; - throw SharedMemoryException(); - return 1; - } - return 0; -} +// //size does not match +// long unsigned int sz = (long unsigned int)sb.st_size; +// if (sz != expectedSize) { +// FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match"; +// FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz; +// throw SharedMemoryException(); +// return 1; +// } +// return 0; +// } -template class SharedMemory; -template class SharedMemory; \ No newline at end of file +// template class SharedMemory; +// template class SharedMemory; \ No newline at end of file diff --git a/slsDetectorSoftware/sharedMemory/SharedMemory.h b/slsDetectorSoftware/sharedMemory/SharedMemory.h index 6b15e61d4..19782263e 100644 --- a/slsDetectorSoftware/sharedMemory/SharedMemory.h +++ b/slsDetectorSoftware/sharedMemory/SharedMemory.h @@ -8,81 +8,168 @@ *@short functions basic implemenation of shared memory */ +#include "ansi.h" +#include "logger.h" +#include "sls_detector_exceptions.h" + +#include "stdlib.h" +#include // errno +#include // strerror +#include // O_CREAT, O_TRUNC.. +#include +#include +#include // printf +#include // shared memory +#include // fstat +#include + +#define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_" +#define SHM_SLS_PREFIX "_sls_" +#define SHM_ENV_NAME "SLSDETNAME" + #include #include template -class SharedMemory{ -public: - /** +class SharedMemory { + public: + /** * Constructor * creates the single/multi detector shared memory name * @param multiId multi detector id * @param slsId sls detector id, -1 if a multi detector shared memory */ - SharedMemory(int multiId, int slsId); + SharedMemory(int multiId, int slsId) : fd(-1), + shmSize(0) { + name = ConstructSharedMemoryName(multiId, slsId); + } - /** + /** * Destructor */ - ~SharedMemory(); + ~SharedMemory() { + if (fd >= 0) + close(fd); + } /** * Verify if it exists * @param name of shared memory * @return true if exists, else false */ - bool IsExisting(); + bool IsExisting() { + bool ret = true; + int tempfd = shm_open(name.c_str(), O_RDWR, 0); + if ((tempfd < 0) && (errno == ENOENT)) { + ret = false; + } + close(tempfd); + return ret; + } - /** + /** * Get shared memory name */ - std::string GetName(); + std::string GetName() { + return name; + } /** * Create Shared memory and call MapSharedMemory to map it to an address * throws a SharedMemoryException exception on failure to create, ftruncate or map * @param sz of shared memory */ - void CreateSharedMemory(size_t sz); + void CreateSharedMemory(size_t sz = 0) { + // create + if (sz == 0){ + sz = sizeof(T); + } + + fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); + if (fd < 0) { + FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno); + throw SharedMemoryException(); + } + + // resize + if (ftruncate(fd, sz) < 0) { + FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno); + close(fd); + RemoveSharedMemory(); + throw SharedMemoryException(); + } + + // map + // void* addr = MapSharedMemory(sz); + shared_struct = MapSharedMemory(sz); + FILE_LOG(logINFO) << "Shared memory created " << name; + + // return addr; + } /** * Open existing Shared memory and call MapSharedMemory to map it to an address * throws a SharedMemoryException exception on failure to open or map * @param sz of shared memory */ - void OpenSharedMemory(size_t sz); + void OpenSharedMemory() { + // open + size_t sz = sizeof(T); + fd = shm_open(name.c_str(), O_RDWR, 0); + if (fd < 0) { + FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno); + throw SharedMemoryException(); + } + + shared_struct = MapSharedMemory(sz); + // return MapSharedMemory(sz); + } /** * Unmap shared memory from an address * throws a SharedMemoryException exception on failure */ - void UnmapSharedMemory(); + void UnmapSharedMemory() { + if (munmap(shared_struct, shmSize) < 0) { + FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno); + close(fd); + throw SharedMemoryException(); + } + } - /** + /** * Remove existing Shared memory */ - void RemoveSharedMemory(); + void RemoveSharedMemory() { + if (shm_unlink(name.c_str()) < 0) { + // silent exit if shm did not exist anyway + if (errno == ENOENT) + return; + FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno); + throw SharedMemoryException(); + } + FILE_LOG(logINFO) << "Shared memory deleted " << name; + } /** * Maximum length of name as from man pages */ static const int NAME_MAX = 255; - /* Using the call operator to access the pointer */ - T* operator()(){ + + T *operator()() { return shared_struct; } - const T* operator()() const{ + const T *operator()() const { return shared_struct; } -private: + private: /** * Create Shared memory name * throws exception if name created is longer than required 255(manpages) @@ -90,31 +177,85 @@ private: * @param slsId sls detector id, -1 if a multi detector shared memory * @returns shared memory name */ - std::string ConstructSharedMemoryName(int multiId, int slsId); + std::string ConstructSharedMemoryName(int multiId, int slsId) { + + // using environment path + std::string sEnvPath = ""; + char *envpath = getenv(SHM_ENV_NAME); + if (envpath != nullptr) { + sEnvPath.assign(envpath); + sEnvPath.insert(0, "_"); + } + + std::stringstream ss; + if (slsId < 0) + ss << SHM_MULTI_PREFIX << multiId << sEnvPath; + else + ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath; + + std::string temp = ss.str(); + if (temp.length() > NAME_MAX) { + FILE_LOG(logERROR) << "Shared memory initialization failed. " << temp << " has " << temp.length() << " characters. \n" + "Maximum is " + << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME; + throw SharedMemoryException(); + } + return temp; + } /** * Map shared memory to an address * throws a SharedMemoryException exception on failure * @param sz of shared memory */ - T* MapSharedMemory(size_t sz); + + T *MapSharedMemory(size_t sz) { + void *addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno); + close(fd); + throw SharedMemoryException(); + } + shmSize = sz; + close(fd); + return (T *)addr; + } /** * Verify if existing shared memory size matches expected size * @param expectedSize expected size of shared memory, replaced with smaller size if size does not match * @return 0 for success, 1 for fail */ - int VerifySizeMatch(size_t expectedSize); + int VerifySizeMatch(size_t expectedSize) { + struct stat sb; + // could not fstat + if (fstat(fd, &sb) < 0) { + FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match " + "(could not fstat): " + << strerror(errno); + close(fd); + throw SharedMemoryException(); + } - /** Shared memory name */ - std::string name; + //size does not match + long unsigned int sz = (long unsigned int)sb.st_size; + if (sz != expectedSize) { + FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match"; + FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz; + throw SharedMemoryException(); + return 1; + } + return 0; + } - /** File descriptor */ - int fd; + /** Shared memory name */ + std::string name; - /** shm size */ - size_t shmSize; + /** File descriptor */ + int fd; - T* shared_struct; + /** shm size */ + size_t shmSize; + T *shared_struct; }; diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 502c391e2..d0c039c87 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -59,7 +59,7 @@ slsDetector::slsDetector(int multiId, int id, bool verify) slsDetector::~slsDetector() { if (sharedMemory) { - sharedMemory->UnmapSharedMemory(thisDetector); + sharedMemory->UnmapSharedMemory(); delete sharedMemory; } } @@ -208,7 +208,7 @@ void slsDetector::freeSharedMemory(int multiId, int slsId) { void slsDetector::freeSharedMemory() { if (sharedMemory) { - sharedMemory->UnmapSharedMemory(thisDetector); + sharedMemory->UnmapSharedMemory(); sharedMemory->RemoveSharedMemory(); delete sharedMemory; sharedMemory = nullptr; @@ -240,11 +240,15 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId, // create if (created) { - thisDetector = (sharedSlsDetector *)sharedMemory->CreateSharedMemory(sz); + // thisDetector = (sharedSlsDetector *)sharedMemory->CreateSharedMemory(sz); + sharedMemory->CreateSharedMemory(sz); + thisDetector = (*sharedMemory)(); } // open and verify version else { - thisDetector = (sharedSlsDetector *)sharedMemory->OpenSharedMemory(sz); + // thisDetector = (sharedSlsDetector *)sharedMemory->OpenSharedMemory(sz); + sharedMemory->OpenSharedMemory(); + thisDetector = (*sharedMemory)(); if (verify && thisDetector->shmversion != SLS_SHMVERSION) { FILE_LOG(logERROR) << "Single shared memory " "(" @@ -259,7 +263,7 @@ void slsDetector::initSharedMemory(bool created, detectorType type, int multiId, if (sharedMemory) { // unmap if (thisDetector) { - sharedMemory->UnmapSharedMemory(thisDetector); + sharedMemory->UnmapSharedMemory(); thisDetector = nullptr; } // delete @@ -643,10 +647,10 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b throw SharedMemoryException(); } - size_t sz = sizeof(sharedSlsDetector); - // open, map, verify version - auto sdet = (sharedSlsDetector *)shm.OpenSharedMemory(sz); + // auto sdet = (sharedSlsDetector *)shm.OpenSharedMemory(sz); + shm.OpenSharedMemory(); + auto sdet = shm(); if (verify && sdet->shmversion != SLS_SHMVERSION) { FILE_LOG(logERROR) << "Single shared memory " "(" @@ -654,13 +658,13 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b "(expected 0x" << std::hex << SLS_SHMVERSION << " but got 0x" << sdet->shmversion << ")" << std::dec; // unmap and throw - sharedMemory->UnmapSharedMemory(thisDetector); + sharedMemory->UnmapSharedMemory(); throw SharedMemoryException(); } // get type, unmap auto type = sdet->myDetectorType; - shm.UnmapSharedMemory(sdet); + shm.UnmapSharedMemory(); return type; } diff --git a/slsDetectorSoftware/tests/CMakeLists.txt b/slsDetectorSoftware/tests/CMakeLists.txt new file mode 100644 index 000000000..bbe9c32aa --- /dev/null +++ b/slsDetectorSoftware/tests/CMakeLists.txt @@ -0,0 +1,23 @@ + + +include_directories( + ${PROJECT_SOURCE_DIR}/catch +) + +set(SOURCES + test.cpp + test-SharedMemory.cpp +) + +add_executable(testSlsDetector ${SOURCES}) +target_link_libraries(testSlsDetector + slsSupportLib + slsDetectorShared + pthread + rt +) +set_target_properties(testSlsDetector PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin + ) +#TODO! Move to automatic test discovery +add_test(test-testSlsDetector ${CMAKE_BINARY_DIR}/bin/testSlsDetector) \ No newline at end of file diff --git a/slsDetectorSoftware/tests/test-SharedMemory.cpp b/slsDetectorSoftware/tests/test-SharedMemory.cpp new file mode 100644 index 000000000..e541ad3e2 --- /dev/null +++ b/slsDetectorSoftware/tests/test-SharedMemory.cpp @@ -0,0 +1,30 @@ + +#include "catch.hpp" +#include "SharedMemory.h" +#include "string_utils.h" + + +TEST_CASE("SharedMemory") { + struct Data{ + int x; + double y; + char mess[50]; + }; + + + SharedMemory shm(0,-1); + shm.CreateSharedMemory(); + CHECK(shm.GetName() == "/slsDetectorPackage_multi_0"); + + shm()->x = 3; + shm()->y = 5.7; + sls::strcpy_safe(shm()->mess, "Some string"); + + CHECK(shm()->x == 3); + CHECK(shm()->y == 5.7); + CHECK(std::string(shm()->mess) == "Some string"); + + shm.UnmapSharedMemory(); + shm.RemoveSharedMemory(); + +} \ No newline at end of file diff --git a/slsDetectorSoftware/tests/test.cpp b/slsDetectorSoftware/tests/test.cpp new file mode 100644 index 000000000..8daed99c4 --- /dev/null +++ b/slsDetectorSoftware/tests/test.cpp @@ -0,0 +1,3 @@ +// tests-main.cpp +#define CATCH_CONFIG_MAIN +#include "catch.hpp" \ No newline at end of file